Skip to content

fix(cpp): own optimized expression trees exactly once - #1131

Open
ColinLeeo wants to merge 2 commits into
apache:developfrom
ColinLeeo:colin/cpp-global-time-filter
Open

ColinLeeo wants to merge 2 commits into
apache:developfrom
ColinLeeo:colin/cpp-global-time-filter

Conversation

@ColinLeeo

Copy link
Copy Markdown
Contributor

Summary

Queries that combine a global time filter with series expressions (AND/OR around a GLOBALTIME_EXPR) had 0% test coverage and were in fact broken: QueryExpression::optimize() created filters/nodes, registered them in my_exprs_/my_filters_, and handed the same objects back as expression_, so closing the result set freed them two or three times. When a global OR was pushed down, one Filter instance was also shared by every series leaf. A standalone probe (AND/OR with a global filter, two global filters) segfaulted in all three shapes.

The optimization now consumes the input tree and leaves exactly one owner for the result:

  • combining two global filters reuses the parent AND/OR node and transfers both filters into the combined one
  • pushing a filter into several leaves copies it via a new Filter::clone() (implemented by all time operators and by AndFilter/OrFilter); a filter that cannot be cloned fails the optimization instead of being shared
  • detached nodes are released on every failure path, and TsFileExecutor clears expression_ when optimization fails so a later close cannot delete the consumed tree

New tests:

  • cpp/test/reader/global_time_filter_test.cc: OR/AND of a global filter with series filters, two global filters, multi-series OR pushdown, AND over an OR of series, and empty branches
  • cpp/test/reader/filter/logical_filter_test.cc: direct AndFilter/OrFilter predicate, pruning, range-union/intersection and batch semantics
  • cpp/test/reader/expression_test.cc: clone-failure paths and the documented consume-on-failure contract

Coverage: expression.cc 34.5% -> 73.6%, or_filter.h 0% -> 76.0%, and_filter.h 40.0% -> 72.3%.

Test plan

  • ./mvnw -B clean verify -P with-java,with-python -Denable.lzma2=ON (Java, C++, Python; 1862 C++ tests passed)
  • ./mvnw -B clean verify -P with-cpp -Denable.asan=ON -Dbuild.type=RelWithDebInfo -Denable.lzma2=ON (1863 tests, 0 failed; macOS run used the Xcode 26.6 toolchain with ASAN_OPTIONS=detect_container_overflow=0 for the uninstrumented ANTLR runtime)
  • ./mvnw spotless:check -P with-java,with-python

Rewriting a query's global time filter combination allocated new filters
and expressions that were registered in QueryExpression's ownership lists
while also becoming the query's expression_ tree, so closing the result
set freed the same objects two or three times. A single filter instance
was also shared across every series leaf when a global OR was pushed down.

Optimization now consumes the input tree and leaves exactly one owner for
the result: combined filters reuse the parent node, per-leaf copies come
from a new Filter::clone(), and detached nodes are released on failure.
The executor clears expression_ when optimization fails so a later close
cannot delete the already-consumed tree.

Add end-to-end coverage for AND/OR with a global time filter, two global
filters, multi-series OR pushdown, empty branches, clone failures, and
direct AndFilter/OrFilter semantics.

expression.cc: 34.5% -> 73.6%, or_filter.h: 0% -> 76.0%,
and_filter.h: 40.0% -> 72.3%.
BinaryFilter never released its operands, so every AndFilter/OrFilter
created while optimizing a global time filter leaked the filters it
wrapped, and their get_time_ranges() results leaked the child TimeRange
objects. LeakSanitizer flagged both in the new expression tests.

Own the operands in BinaryFilter, free the temporary child ranges in
AndFilter/OrFilter::get_time_ranges(), and heap-allocate the operands in
the logical-filter unit test now that the wrapper owns them. Also guard
OrFilter::get_time_ranges() against both children being empty.
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