@@ -61,8 +61,8 @@ def may_match(self, statistics: Iterable[ColumnStatistics]) -> bool:
6161 returning branches. Columns or predicates this helper cannot reason
6262 about count as "may match", so the answer errs towards keeping data.
6363 """
64- filters = self . decode ()
65- return all (_column_may_match ( filters , stat ) for stat in statistics )
64+ by_name = { stat . column_name : stat for stat in statistics }
65+ return all (_filter_may_match ( f , by_name ) for f in self . decode (). filters )
6666
6767
6868def _as_py (value : pa .Scalar [Any ] | None ) -> Any :
@@ -71,43 +71,54 @@ def _as_py(value: pa.Scalar[Any] | None) -> Any:
7171 return value .as_py ()
7272
7373
74- def _column_may_match (filters : PushdownFilters , stat : ColumnStatistics ) -> bool :
75- lo , hi = _as_py (stat .min ), _as_py (stat .max )
76- if not stat .has_not_null :
77- # Entirely NULL: comparison, equality and IN predicates never match.
78- return not _has_value_predicate (filters , stat .column_name )
79- if lo is None and hi is None :
74+ def _filter_may_match (f : Any , stats : dict [str , ColumnStatistics ]) -> bool :
75+ """Whether filter ``f`` can be true for some row described by ``stats``; unknown shapes return True."""
76+ from vgi .table_filter_pushdown import (
77+ AndFilter ,
78+ ComparisonOp ,
79+ ConstantFilter ,
80+ InFilter ,
81+ IsNotNullFilter ,
82+ IsNullFilter ,
83+ OrFilter ,
84+ )
85+
86+ if isinstance (f , AndFilter ):
87+ return all (_filter_may_match (child , stats ) for child in f .children )
88+ if isinstance (f , OrFilter ):
89+ return any (_filter_may_match (child , stats ) for child in f .children )
90+ stat = stats .get (f .column_name )
91+ if stat is None :
8092 return True
93+ if isinstance (f , IsNullFilter ):
94+ return stat .has_null
95+ if isinstance (f , IsNotNullFilter ):
96+ return stat .has_not_null
97+ if not stat .has_not_null :
98+ # Entirely NULL: comparisons and IN never match.
99+ return not isinstance (f , ConstantFilter | InFilter )
100+ lo , hi = _as_py (stat .min ), _as_py (stat .max )
81101 try :
82- constant = filters .get_column_constant (stat .column_name )
83- if constant is not None :
84- c = _as_py (constant )
85- if c is not None and ((lo is not None and c < lo ) or (hi is not None and c > hi )):
86- return False
87- in_values = filters .get_column_in_values (stat .column_name )
88- if in_values is not None :
89- values = [v for v in in_values .to_pylist () if v is not None ]
90- if values and all ((lo is not None and v < lo ) or (hi is not None and v > hi ) for v in values ):
91- return False
92- bounds = filters .get_column_bounds (stat .column_name )
93- if bounds is not None :
94- bmin , bmax = _as_py (bounds .min_value ), _as_py (bounds .max_value )
95- if bmin is not None and hi is not None and (hi < bmin or (hi == bmin and not bounds .min_inclusive )):
96- return False
97- if bmax is not None and lo is not None and (lo > bmax or (lo == bmax and not bounds .max_inclusive )):
98- return False
102+ if isinstance (f , InFilter ):
103+ values = [v for v in f .values .to_pylist () if v is not None ]
104+ return any ((lo is None or v >= lo ) and (hi is None or v <= hi ) for v in values )
105+ if isinstance (f , ConstantFilter ):
106+ c = _as_py (f .value )
107+ if c is None :
108+ return True
109+ if f .op == ComparisonOp .EQ :
110+ return (lo is None or c >= lo ) and (hi is None or c <= hi )
111+ if f .op == ComparisonOp .NE :
112+ return not (lo is not None and hi is not None and lo == hi == c )
113+ if f .op == ComparisonOp .GT :
114+ return hi is None or hi > c
115+ if f .op == ComparisonOp .GE :
116+ return hi is None or hi >= c
117+ if f .op == ComparisonOp .LT :
118+ return lo is None or lo < c
119+ if f .op == ComparisonOp .LE :
120+ return lo is None or lo <= c
99121 except TypeError :
100122 # Incomparable types (e.g. a predicate literal of another type): keep the candidate.
101123 return True
102124 return True
103-
104-
105- def _has_value_predicate (filters : PushdownFilters , column_name : str ) -> bool :
106- try :
107- return (
108- filters .get_column_constant (column_name ) is not None
109- or filters .get_column_in_values (column_name ) is not None
110- or filters .get_column_bounds (column_name ) is not None
111- )
112- except TypeError :
113- return False
0 commit comments