Skip to content

perf(expression): Support split and parallel scans - Part-3.5: compile Evaluator expressions and compare raw values - #1166

Open
kamcheungting-db wants to merge 3 commits into
apache:mainfrom
kamcheungting-db:perf/evaluator-raw-scalar-compare
Open

kamcheungting-db wants to merge 3 commits into
apache:mainfrom
kamcheungting-db:perf/evaluator-raw-scalar-compare

Conversation

@kamcheungting-db

@kamcheungting-db kamcheungting-db commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does

Makes Evaluator (used for residual filters by FileScanTaskReader) evaluate rows without building a Literal per value, and without visiting the expression tree per row.

Evaluator::Make() now compiles the bound expression once into a small tree of evaluation nodes. Evaluate() runs those nodes.

Why

Per row, the old path:

  • visited the bound expression, doing a dynamic_pointer_cast (a shared_ptr copy) for each predicate, and
  • built a Literal for every compared value, which copies the type's shared_ptr singleton (for example int64()) and, for strings and bytes, the value itself.

With several threads evaluating residuals, every thread updates the same atomic reference count of the shared type object, so the cache line bounces between cores. In the Part-5 parallel scan benchmark (#1168) a per-row residual made 8 workers slower than one thread. A perf profile put about two-thirds of the samples on the lock inc/lock xadd in Literal::Long and EvalVisitor::GtEq, and on the virtual call through the shared type object in Literal::operator<=>.

How it works

BEFORE: every row walks the expression tree and builds Literals

  Evaluate(row) ──> Visit(bound expr)
                      │  per predicate: dynamic_pointer_cast<BoundPredicate>   (shared_ptr copy, atomic ++/--)
                      v
                    EvalVisitor::GtEq(term, lit)
                      │  term->Evaluate(row) ──> accessor.GetLiteral(row)
                      │                            └─ Literal::Long(v) copies int64() singleton
                      │                               (atomic ++ on one shared refcount;
                      │                                strings/bytes also copy the value)
                      v
                    value >= lit   (virtual call through the shared type object)
                      └─ ~Literal  (atomic -- on the same refcount)

  N threads ──> all write the same cache line per row ──> slower with more threads


AFTER: compile once in Evaluator::Make(), then run nodes per row

  Evaluator::Make(schema, expr)
      │ Bind
      v
  bound expr ──Compile()──> node tree (owned by the Evaluator, immutable)

      true / false          ──> ConstantNode
      not / and / or        ──> NotNode / AndNode / OrNode   (both sides evaluated, left first)
      pred(ref, literal)    ──> ReferencePredicateNode        (primitive field, same literal type)
      pred(ref) unary       ──> ReferencePredicateNode        (is_null, not_null, is_nan, not_nan)
      in / not_in, transforms,
      unbound, aggregates   ──> VisitorNode (the old literal-based path)

  Evaluate(row) ──> root->Evaluate(row)

  ReferencePredicateNode::Evaluate(row)
      │ accessor.Get(row) ──> Scalar (int32_t, int64_t, double, string_view, Decimal, ...)
      v
      compare Scalar with the bound Literal's value, no Literal built:
        null value or null/out-of-range literal ──> unordered
        float / double                          ──> std::strong_order (total order)
        decimal                                 ──> Decimal::Compare(lhs, rhs, scales)
        string / binary / fixed / uuid          ──> unsigned byte compare
      │
      ├─ ordered or unordered ──> apply <, <=, >, >=, ==, !=
      └─ scalar not in the field type's representation ──> VisitorNode fallback for this row
                                                          (same result or error as before)

The node tree is immutable after Make(), so one Evaluator can still be used from several threads.

Node mapping

Expression Node
true / false constant
not, and, or combinators; and/or still evaluate both sides, left first, so a failing row still reports its error
literal or unary predicate on a field reference of a primitive type reads the field's raw Scalar and compares it with the bound literal
everything else (set predicates, transforms, unbound expressions, aggregates) the existing literal-based visitor

The raw comparison matches Literal ordering: null and out-of-range literals are unordered, float/double use the total order (std::strong_order), decimals compare across scales, and strings, binary, fixed, and UUIDs compare as unsigned bytes. When a raw value does not have the field type's representation, the node falls back to the visitor for that row, so results and errors stay the same.

Tests

eval_expr_test (evaluator_test.cc):

  • MatchesLiteralSemantics: more than 1,000 checks comparing the evaluator with BoundPredicate::Test() on the literal-based value, across boolean, int, date, long, time, timestamp, timestamptz, float, double, decimal, string, binary, fixed, and UUID; all literal and unary operations; and nulls, NaNs, signed zeros, infinities, prefixes, and high bytes. Changing one comparison operator in the new code makes it fail.
  • Compound expressions still report errors from either side; IN and transform predicates still evaluate correctly.

The existing expression_test and eval_expr_test suites pass, and the full ctest suite passes. pre-commit is clean.

Performance

In the Part-5 benchmark, 8 workers versus a serial scan with an all-pass residual on 4 large Parquet files went from 0.80x to 6.72x, and the serial scan itself got about 1.9x faster (0.834s to 0.432s). Details are in the Part-5 PR.

Related

Part 3.5 of the split and parallel scans series: Part 1 #976 · Part 2a #1002 · Part 2b #1062 · Part 2c #1065 · Part 3 #1113 · Part 4 #1167 · Part 5 #1168.

This pull request and its description were written by Isaac.

Evaluator::Evaluate() visited the bound expression for every row, copying
shared_ptrs while dispatching on each predicate, and built a Literal for
every compared value. Building a Literal copies the type's shared_ptr
singleton and, for strings and bytes, the value itself. With several
threads evaluating residual filters, the atomic reference count of the
shared type object became the bottleneck: a parallel scan with a per-row
residual ran slower than a serial one.

Evaluator::Make() now compiles the bound expression once into evaluation
nodes. Literal and unary predicates on a field reference of a primitive
type read the field's raw scalar and compare it with the bound literal,
matching Literal ordering: null and out-of-range literals are unordered,
floating-point values use the total order, decimals compare across
scales, and strings and bytes compare as unsigned bytes. Set predicates,
transforms, and any value without a raw representation still go through
the literal-based visitor, so results and errors are unchanged. And/Or
still evaluate both sides, so a failing row still reports its error.

Co-authored-by: Isaac <no-reply@databricks.com>
@kamcheungting-db kamcheungting-db changed the title perf(expression): compile Evaluator expressions and compare raw values perf(expression): Support split and parallel scans - Part-3.5: compile Evaluator expressions and compare raw values Nov 23, 2026
kamcheungting-db and others added 2 commits November 23, 2026 19:12
Co-authored-by: Isaac <no-reply@databricks.com>
… test

Use std::array instead of C-style arrays and designated initializers for
ColumnCase.

Co-authored-by: Isaac <no-reply@databricks.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant