Skip to main content

KernelPredicateEvaluator

Trait KernelPredicateEvaluator 

Source
pub trait KernelPredicateEvaluator {
    type Output;

Show 23 methods // Required methods fn eval_pred_scalar( &self, val: &Scalar, inverted: bool, ) -> Option<Self::Output>; fn eval_pred_scalar_is_null( &self, val: &Scalar, inverted: bool, ) -> Option<Self::Output>; fn eval_pred_is_null( &self, col: &ColumnName, inverted: bool, ) -> Option<Self::Output>; fn eval_pred_lt( &self, col: &ColumnName, val: &Scalar, inverted: bool, ) -> Option<Self::Output>; fn eval_pred_gt( &self, col: &ColumnName, val: &Scalar, inverted: bool, ) -> Option<Self::Output>; fn eval_pred_eq( &self, col: &ColumnName, val: &Scalar, inverted: bool, ) -> Option<Self::Output>; fn eval_pred_binary_scalars( &self, op: BinaryPredicateOp, left: &Scalar, right: &Scalar, inverted: bool, ) -> Option<Self::Output>; fn eval_pred_binary_columns( &self, op: BinaryPredicateOp, a: &ColumnName, b: &ColumnName, inverted: bool, ) -> Option<Self::Output>; fn eval_pred_opaque( &self, op: &OpaquePredicateOpRef, exprs: &[Expr], inverted: bool, ) -> Option<Self::Output>; fn eval_pred_expr_opaque( &self, op: &OpaqueExpressionOpRef, exprs: &[Expr], inverted: bool, ) -> Option<Self::Output>; fn finish_eval_pred_junction( &self, op: JunctionPredicateOp, preds: &mut dyn Iterator<Item = Option<Self::Output>>, inverted: bool, ) -> Option<Self::Output>; // Provided methods fn eval_pred_column( &self, col: &ColumnName, inverted: bool, ) -> Option<Self::Output> { ... } fn eval_pred_not(&self, pred: &Pred, inverted: bool) -> Option<Self::Output> { ... } fn eval_pred_expr( &self, expr: &Expr, inverted: bool, ) -> Option<Self::Output> { ... } fn eval_pred_unary( &self, op: UnaryPredicateOp, expr: &Expr, inverted: bool, ) -> Option<Self::Output> { ... } fn eval_pred_distinct( &self, col: &ColumnName, val: &Scalar, inverted: bool, ) -> Option<Self::Output> { ... } fn eval_pred_in( &self, _col: &ColumnName, _val: &Scalar, _inverted: bool, ) -> Option<Self::Output> { ... } fn eval_pred_binary( &self, op: BinaryPredicateOp, left: &Expr, right: &Expr, inverted: bool, ) -> Option<Self::Output> { ... } fn eval_pred_junction( &self, op: JunctionPredicateOp, preds: &[Pred], inverted: bool, ) -> Option<Self::Output> { ... } fn eval_pred(&self, pred: &Pred, inverted: bool) -> Option<Self::Output> { ... } fn eval_pred_sql_where( &self, pred: &Pred, inverted: bool, ) -> Option<Self::Output> { ... } fn eval(&self, pred: &Pred) -> Option<Self::Output> { ... } fn eval_sql_where(&self, pred: &Pred) -> Option<Self::Output> { ... }
}
Expand description

Uses kernel (not engine) logic to evaluate a predicate tree against column names that resolve as scalars. Useful for testing/debugging but also serves as a reference implementation that documents the expression semantics that kernel relies on for data skipping.

§Inverted expression semantics

Because inversion (NOT operator) has special semantics and can often be optimized away by pushing it down, most methods take an inverted flag. That allows operations like Pred::Not to simply evaluate their operand with a flipped inverted flag, and greatly simplifies the implementations of most operators (other than those which have to directly implement NOT semantics, which are unavoidably complex in that regard).

§Parameterized output type

The types involved in predicate evaluation are parameterized and implementation-specific. For example, a DirectDataSkippingPredicateEvaluator directly evaluates the predicate (e.g. using parquet footer stats) and returns boolean results, while IndirectDataSkippingPredicateEvaluator instead transforms the input predicate to a data skipping predicate that the engine can evaluated directly against Delta data skipping stats during log replay. Although this approach is harder to read and reason about at first, the majority of predicates can be implemented generically, which greatly reduces redundancy and ensures that all flavors of predicate evaluation have the same semantics.

§NULL and error semantics

Literal NULL values almost always produce cascading changes in the predicate’s structure, so we represent them by Option::None rather than Scalar::Null. This allows e.g. A < NULL to be rewritten as NULL, or AND(NULL, FALSE) to be rewritten as FALSE.

Almost all operations produce NULL output if any input is NULL. Any resolution failures also produce NULL (such as missing columns or type mismatch between a column and the scalar it is compared against). NULL-checking operations like IS [NOT] NULL and DISTINCT are special, and rely on nullcount stats for their work (NULL/missing nullcount stats makes them output NULL).

For safety reasons, NULL-checking operations only accept literal and column inputs where stats-based skipping is well-defined. If an arbitrary data skipping predicate evaluates to NULL, there is no way to tell whether the original predicate really evaluated to NULL (safe to use), or the data skipping version evaluated to NULL due to missing stats (very unsafe to use).

NOTE: The error-handling semantics of this trait’s scalar-based predicate evaluation may differ from those of the engine’s predicate evaluation, because kernel predicates don’t include the necessary type information to reliably detect all type errors.

Required Associated Types§

Required Methods§

Source

fn eval_pred_scalar(&self, val: &Scalar, inverted: bool) -> Option<Self::Output>

A (possibly inverted) boolean scalar value, e.g. [NOT] <value>.

Source

fn eval_pred_scalar_is_null( &self, val: &Scalar, inverted: bool, ) -> Option<Self::Output>

A (possibly inverted) scalar NULL test, e.g. <value> IS [NOT] NULL.

Source

fn eval_pred_is_null( &self, col: &ColumnName, inverted: bool, ) -> Option<Self::Output>

A (possibly inverted) NULL check, e.g. <expr> IS [NOT] NULL.

Source

fn eval_pred_lt( &self, col: &ColumnName, val: &Scalar, inverted: bool, ) -> Option<Self::Output>

A (possibly inverted) less-than comparison, e.g. <col> < <value>.

Source

fn eval_pred_gt( &self, col: &ColumnName, val: &Scalar, inverted: bool, ) -> Option<Self::Output>

A (possibly inverted) greater-than comparison, e.g. <col> > <value>

Source

fn eval_pred_eq( &self, col: &ColumnName, val: &Scalar, inverted: bool, ) -> Option<Self::Output>

A (possibly inverted) equality comparison, e.g. <col> = <value> or <col> != <value>.

NOTE: Caller is responsible to commute the operation if needed, e.g. <value> != <col> becomes <col> != <value>.

Source

fn eval_pred_binary_scalars( &self, op: BinaryPredicateOp, left: &Scalar, right: &Scalar, inverted: bool, ) -> Option<Self::Output>

A (possibly inverted) comparison between two scalars, e.g. <valueA> != <valueB>.

Source

fn eval_pred_binary_columns( &self, op: BinaryPredicateOp, a: &ColumnName, b: &ColumnName, inverted: bool, ) -> Option<Self::Output>

A (possibly inverted) comparison between two columns, e.g. <colA> != <colB>.

Source

fn eval_pred_opaque( &self, op: &OpaquePredicateOpRef, exprs: &[Expr], inverted: bool, ) -> Option<Self::Output>

Dispatches an opaque predicate.

Source

fn eval_pred_expr_opaque( &self, op: &OpaqueExpressionOpRef, exprs: &[Expr], inverted: bool, ) -> Option<Self::Output>

Dispatches an opaque expression used as a predicate

Source

fn finish_eval_pred_junction( &self, op: JunctionPredicateOp, preds: &mut dyn Iterator<Item = Option<Self::Output>>, inverted: bool, ) -> Option<Self::Output>

Completes evaluation of a (possibly inverted) junction predicate.

AND and OR are implemented by first evaluating its (possibly inverted) inputs. This part is always the same, provided by Self::eval_pred_junction). The results are then combined to become the predicate’s output in some implementation-defined way (this method).

Provided Methods§

Source

fn eval_pred_column( &self, col: &ColumnName, inverted: bool, ) -> Option<Self::Output>

A (possibly inverted) boolean column access, e.g. [NOT] <col>.

Source

fn eval_pred_not(&self, pred: &Pred, inverted: bool) -> Option<Self::Output>

Dispatches a (possibly inverted) NOT predicate

Source

fn eval_pred_expr(&self, expr: &Expr, inverted: bool) -> Option<Self::Output>

Dispatches a (possibly inverted) boolean expression used as a predicate

Source

fn eval_pred_unary( &self, op: UnaryPredicateOp, expr: &Expr, inverted: bool, ) -> Option<Self::Output>

Dispatches a (possibly inverted) unary expression to each operator’s specific implementation.

Source

fn eval_pred_distinct( &self, col: &ColumnName, val: &Scalar, inverted: bool, ) -> Option<Self::Output>

A (possibly inverted) DISTINCT test, e.g. [NOT] DISTINCT(<col>, false). DISTINCT can be seen as one of two operations, depending on the input:

  1. DISTINCT(<col>, NULL) is equivalent to <col> IS NOT NULL
  2. DISTINCT(<col>, <value>) is equivalent to OR(<col> IS NULL, <col> != <value>)
Source

fn eval_pred_in( &self, _col: &ColumnName, _val: &Scalar, _inverted: bool, ) -> Option<Self::Output>

A (possibly inverted) IN-list check, e.g. <col> [NOT] IN <array-value>.

Unsupported by default, but implementations can override it if they wish.

Source

fn eval_pred_binary( &self, op: BinaryPredicateOp, left: &Expr, right: &Expr, inverted: bool, ) -> Option<Self::Output>

Dispatches a (possibly inverted) binary expression to each operator’s specific implementation.

NOTE: Only binary operators that produce boolean outputs are supported.

Source

fn eval_pred_junction( &self, op: JunctionPredicateOp, preds: &[Pred], inverted: bool, ) -> Option<Self::Output>

Dispatches a predicate junction operation (AND or OR), leveraging each implementation’s Self::finish_eval_pred_junction.

Source

fn eval_pred(&self, pred: &Pred, inverted: bool) -> Option<Self::Output>

Dispatches a predicate to the specific implementation for each predicate variant.

Source

fn eval_pred_sql_where( &self, pred: &Pred, inverted: bool, ) -> Option<Self::Output>

Evaluates a (possibly inverted) predicate with SQL WHERE semantics.

NOTE: A NULL literal in a boolean position is treated as unknown (not false), because callers like build_actions_meta_predicate use NULL as a sentinel for unsupported arms. Treating it as false would let AND(supported, NULL) incorrectly prune files.

By default, Self::eval_pred behaves badly for comparisons involving NULL columns (e.g. a < 10 when a is NULL), because the comparison correctly evaluates to NULL, but NULL values are interpreted as “stats missing” (= cannot skip). This ambiguity can “poison” the entire predicate, causing it to return NULL instead of FALSE that would allow skipping:

WHERE a < 10 -- NULL (can't skip file)
WHERE a < 10 AND TRUE -- NULL (can't skip file)
WHERE a < 10 OR FALSE -- NULL (can't skip file)

Meanwhile, SQL WHERE semantics only keeps rows for which the filter evaluates to TRUE (discarding rows that evaluate to FALSE or NULL):

WHERE a < 10 -- NULL (discard row)
WHERE a < 10 AND TRUE -- NULL (discard row)
WHERE a < 10 OR FALSE -- NULL (discard row)

Conceptually, the behavior difference between data skipping and SQL WHERE semantics can be addressed by evaluating with null-safe semantics, as if by <expr> IS NOT NULL AND <expr>:

WHERE (a < 10) IS NOT NULL AND (a < 10) -- FALSE (skip file)
WHERE (a < 10 AND TRUE) IS NOT NULL AND (a < 10 AND TRUE) -- FALSE (skip file)
WHERE (a < 10 OR FALSE) IS NOT NULL AND (a < 10 OR FALSE) -- FALSE (skip file)

HOWEVER, we cannot safely NULL-check the result of an arbitrary data skipping predicate because a predicate will also produce NULL if the value is just plain missing (e.g. data skipping over a column that lacks stats), and if that NULL should propagate all the way to top-level, it would be wrongly interpreted as FALSE (= skippable).

To prevent wrong data skipping, the predicate evaluator always returns NULL for a NULL check over anything except for literals and columns with known values. So we must push the NULL check down through supported operations (AND as well as null-intolerant comparisons like <, !=, etc) until it reaches columns and literals where it can do some good, e.g.:

WHERE a < 10 AND (b < 20 OR c < 30)

would conceptually be interpreted as

WHERE
  (a < 10 AND (b < 20 OR c < 30)) IS NOT NULL AND
  (a < 10 AND (b < 20 OR c < 30))

We then push the NULL check down through the top-level AND:

WHERE
  (a < 10 IS NOT NULL AND a < 10) AND
  ((b < 20 OR c < 30) IS NOT NULL AND (b < 20 OR c < 30))

and attempt to push it further into the a < 10 and OR clauses:

WHERE
  (a IS NOT NULL AND 10 IS NOT NULL AND a < 10) AND
  (b < 20 OR c < 30)

Any time the push-down reaches an operator that does not support push-down (such as OR), we simply drop the NULL check. This way, the top-level NULL check only applies to sub-predicates that can safely implement it, while ignoring other sub-predicates. The unsupported sub-predicates could produce nulls at runtime that prevent skipping, but false positives are OK – the query will still correctly filter out the unwanted rows that result.

At predicate evaluation time, a NULL value of a (from our example) would evaluate as:

AND(..., AND(a IS NOT NULL, 10 IS NOT NULL, a < 10), ...)
AND(..., AND(FALSE, TRUE, NULL), ...)
AND(..., FALSE, ...)
FALSE

While a non-NULL value of a would instead evaluate as:

AND(..., AND(a IS NOT NULL, 10 IS NOT NULL, a < 10), ...)
AND(..., AND(TRUE, TRUE, <result>), ...)
AND(..., <result>, ...)

And a missing value for a would safely disable the clause:

AND(..., AND(a IS NOT NULL, 10 IS NOT NULL, a < 10), ...)
AND(..., AND(NULL, TRUE, NULL), ...)
AND(..., NULL, ...)

WARNING: Not an idempotent transform. If data skipping eval produces a sql predicate, evaluating the result with sql semantics has undefined behavior.

Source

fn eval(&self, pred: &Pred) -> Option<Self::Output>

A convenient non-inverted wrapper for Self::eval_pred

Source

fn eval_sql_where(&self, pred: &Pred) -> Option<Self::Output>

A convenient non-inverted wrapper for Self::eval_pred_sql_where.

Implementors§