query_precision.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. """Exact text-number comparison for query filtering and indexed ordering."""
  2. _parse_number = None
  3. def number(value):
  4. # Lazy import avoids objects -> index helpers -> customs -> storage cycles.
  5. global _parse_number
  6. if _parse_number is None:
  7. from .customs.engine.numbers import number as parser
  8. _parse_number = parser
  9. return _parse_number(value)
  10. CALENDAR = {'YEAR','WEEK','年度','月度','ISO年','ISO周','归属年','归属月','选样年度','排名','记录数'}
  11. def decimal_compare(a,b):
  12. left,right=number(a),number(b)
  13. if left is None or right is None:return None
  14. return (left>right)-(left<right)
  15. def decimal_collation(a,b):
  16. left,right=number(a),number(b)
  17. if left is None or right is None:
  18. if left is None and right is None:return (a>b)-(a<b)
  19. return -1 if left is None else 1
  20. return (left>right)-(left<right)
  21. def register_precision(db):
  22. db.create_function('decimal_cmp',2,decimal_compare,deterministic=True)
  23. db.create_collation('DECIMAL_VALUE',decimal_collation)
  24. def numeric_field(column):
  25. return column in {'MONORSUM','MONORSUM_USD','COUNT','amount','quantity'} or any(k in column for k in ('金额','数量','单价','价格','指数','占比','环比','权重'))