Skip to content

perf(sql): optimize expression evaluation - #4075

Merged
ngjaying merged 3 commits into
lf-edge:masterfrom
ngjaying:agent/optimize-expression-evaluation
Jul 17, 2026
Merged

perf(sql): optimize expression evaluation#4075
ngjaying merged 3 commits into
lf-edge:masterfrom
ngjaying:agent/optimize-expression-evaluation

Conversation

@ngjaying

Copy link
Copy Markdown
Collaborator

Summary

  • Reuse the project evaluator and its valuer chain across input rows.
  • Normalize numeric operands with one type switch instead of repeated type checks and cast dispatch.
  • Compare common changed_cols value types directly before falling back to reflect.DeepEqual.

Why

  • SQL expression evaluation runs for every input row, so small setup and dispatch costs accumulate in projection, arithmetic, filtering, sorting, and change detection.
  • The project operator rebuilt the same evaluator scaffolding for every row even though only the current tuple, window, and aggregate data changed.
  • Numeric normalization inspected each operand type multiple times, while changed_cols used reflection even for primitive values.

Changes In This PR

  • Expose reusable internal multi-valuer implementations and reset their per-row data from ProjectOp without changing expression indices or SliceTuple output semantics.
  • Replace convertNum helper dispatch with a single type switch that preserves the existing integer and floating-point conversions.
  • Add primitive fast paths for string, int64, float64, bool, and int comparisons in changed_cols; complex values retain reflect.DeepEqual behavior.
  • Add focused regression tests and benchmarks for all three paths.
  • Organize the changes as three independent commits for review.

Notes

  • Benchmark medians on linux/amd64, Intel i9-14900HX, seven runs:
    • int64 SimpleDataEval: 10.82 ns/op to 8.08 ns/op (about 25% faster), 0 allocations before and after.
    • SliceTuple projection: 158.6 ns/op to 54.9 ns/op (about 65% faster), from 112 B / 4 allocations to 0 B / 0 allocations.
    • stable changed_cols: 337.0 ns/op to 304.8 ns/op (about 10% faster), with allocations unchanged.
  • Passed go test -race ./internal/xsql ./internal/topo/operator -count=1 and focused race tests for the changed column functions.
  • git diff --check passes.
  • A full local binder/function run still hits the pre-existing TestIncAggFunctionErr failure, reproduced unchanged on upstream/master. go vet likewise reports the existing lock-copy warning in untouched internal/xsql/row.go:187.

@codecov

codecov Bot commented Jul 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.19048% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.78%. Comparing base (a1c06e8) to head (175ccd8).

Files with missing lines Patch % Lines
internal/xsql/valuer.go 91.84% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4075      +/-   ##
==========================================
+ Coverage   70.74%   70.78%   +0.03%     
==========================================
  Files         463      463              
  Lines       54283    54347      +64     
==========================================
+ Hits        38402    38466      +64     
- Misses      12888    12889       +1     
+ Partials     2993     2992       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ngjaying
ngjaying marked this pull request as ready for review July 17, 2026 03:03
@ngjaying
ngjaying requested a review from Yisaer July 17, 2026 03:03
ngjaying added 3 commits July 17, 2026 14:12
Signed-off-by: Jiayin Ng <ngjaying@gmail.com>
Signed-off-by: Jiayin Ng <ngjaying@gmail.com>
Signed-off-by: Jiayin Ng <ngjaying@gmail.com>
@ngjaying
ngjaying force-pushed the agent/optimize-expression-evaluation branch from 843316b to 175ccd8 Compare July 17, 2026 06:15
@ngjaying
ngjaying merged commit b98a846 into lf-edge:master Jul 17, 2026
62 checks passed
@ngjaying
ngjaying deleted the agent/optimize-expression-evaluation branch July 17, 2026 06:37
ngjaying added a commit that referenced this pull request Aug 5, 2026
## Summary
- Reuse the project evaluator and its valuer chain across input rows.
- Normalize numeric operands with one type switch instead of repeated
type checks and cast dispatch.
- Compare common `changed_cols` value types directly before falling back
to `reflect.DeepEqual`.

## Why
- SQL expression evaluation runs for every input row, so small setup and
dispatch costs accumulate in projection, arithmetic, filtering, sorting,
and change detection.
- The project operator rebuilt the same evaluator scaffolding for every
row even though only the current tuple, window, and aggregate data
changed.
- Numeric normalization inspected each operand type multiple times,
while `changed_cols` used reflection even for primitive values.

## Changes In This PR
- Expose reusable internal multi-valuer implementations and reset their
per-row data from `ProjectOp` without changing expression indices or
SliceTuple output semantics.
- Replace `convertNum` helper dispatch with a single type switch that
preserves the existing integer and floating-point conversions.
- Add primitive fast paths for string, int64, float64, bool, and int
comparisons in `changed_cols`; complex values retain `reflect.DeepEqual`
behavior.
- Add focused regression tests and benchmarks for all three paths.
- Organize the changes as three independent commits for review.

## Notes
- Benchmark medians on linux/amd64, Intel i9-14900HX, seven runs:
- int64 `SimpleDataEval`: 10.82 ns/op to 8.08 ns/op (about 25% faster),
0 allocations before and after.
- SliceTuple projection: 158.6 ns/op to 54.9 ns/op (about 65% faster),
from 112 B / 4 allocations to 0 B / 0 allocations.
- stable `changed_cols`: 337.0 ns/op to 304.8 ns/op (about 10% faster),
with allocations unchanged.
- Passed `go test -race ./internal/xsql ./internal/topo/operator
-count=1` and focused race tests for the changed column functions.
- `git diff --check` passes.
- A full local binder/function run still hits the pre-existing
`TestIncAggFunctionErr` failure, reproduced unchanged on
`upstream/master`. `go vet` likewise reports the existing lock-copy
warning in untouched `internal/xsql/row.go:187`.

---------

Signed-off-by: Jiayin Ng <ngjaying@gmail.com>
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.

2 participants