PHOENIX-6791 WHERE optimizer redesign#2428
Draft
kadirozde wants to merge 1 commit intoapache:masterfrom
Draft
Conversation
022c608 to
2e7a7a6
Compare
Replaces the legacy WhereOptimizer's range-enumeration algorithm with an N-dimensional key-space model. The new implementation in phoenix-core-client/src/main/java/org/apache/phoenix/compile/keyspace/ represents each WHERE clause as a KeySpaceList (a disjunction of axis-aligned KeySpace boxes, one dimension per PK column), composes it via a mathematical algebra (AND = per-dim intersect + fixpoint merge; OR = concat + fixpoint merge with containment / N-1 agreement rules), and emits ScanRanges-shaped output at the boundary layer. Algorithm is O(N^2) bounded by cartesian- widening before byte expansion, eliminating the O(product) explosions that caused production OOMs (PHOENIX-7770, PHOENIX-5833). Gated by QueryServices.WHERE_OPTIMIZER_V2_ENABLED (default true on this branch). When off, the legacy optimizer runs unchanged. New package compile.keyspace/ contents: - KeySpace, KeySpaceList: N-dim algebra with merge rules. - ExpressionNormalizer: lex-expand RVC inequalities; handle scalar IN. - KeySpaceExpressionVisitor: Expression tree -> KeySpaceList; handles scalar functions including wrapped PK children in RVC-IN via resolveScalarFunctionChain. - KeyRangeExtractor: KeySpaceList -> ScanRanges-shaped output. Default emits the V1 projection (per-PK-column disjunctions); compound emission is an optional optimization with routing gates that fall back to V1 projection when downstream ScanUtil/ScanRanges/SkipScanFilter would misbehave on compound input. Emission splits trailing-pinned dims into individual slots when the compound has an unbounded side, matching V1's bound-count semantics so the optimizer's INDEX-vs-DATA-TABLE tiebreak stays correct. - WhereOptimizerV2: driver that orchestrates normalize -> visit -> extract. - oracle/: 1000-line pure-Java reference implementation for differential testing; no Phoenix types. Byte-level corrections in the extractor: - stripTrailingSeparator fixes the double-separator bug when compound bytes flow through ScanUtil.setKey for variable-length trailing PK fields. - collapseToSingleBoundingRange widens coalesced mixed-width non-point ranges into a single bounding range so SkipScanFilter doesn't mis- navigate, while keeping scan start/stop as tight as V1. - computeCompoundSpan measures actual byte span of post-coalesce compounds so ScanRanges.getBoundPkColumnCount doesn't over-count when multiple per-space compounds collapse. Tests: - 49 keyspace-level unit tests (algebra, normalization, visitor, extractor). - 138 WhereOptimizerTest methods run under both V1 (WhereOptimizerTest) and V2 (WhereOptimizerV2Test), 284/284 green. - testRvcInListLeadingScalarFunction / testRvcInListMiddleScalarFunction added as characterization tests for RVC-IN with scalar-function children. - JMH compile-time benchmark (WhereOptimizerBenchmark) shows V2 is within ~10% of V1 on normal shapes and 4-10x faster on cartesian-explosion shapes (the pathological pattern V1 times out on). docs/where-optimizer-v2.md (new): design and implementation walkthrough, V1-vs-V2 performance comparison (CPU/IO/memory), known limitations with characterization tests, and rollout notes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduces a v2 WHERE optimizer in compile/keyspace/ that models each expression as a list of per-PK-column KeyRange tuples (KeySpace), with AND as per-dim intersection and OR governed by containment + equal-on-N-1 merge rules. Key-range conversion is deferred until trailing dims are dropped against a cartesian bound, making the algorithm O(N^2) in PK arity. RVC inequalities and IN lists are lowered to lexicographic OR-of-AND form in a single ExpressionNormalizer pass so per-dim intersection composes uniformly with every other predicate.
Feature flag phoenix.where.optimizer.v2.enabled routes WhereOptimizer .pushKeyExpressionsToScan to the v2 driver; default is now true. The legacy implementation is untouched and remains the fallback when the flag is disabled. All 138 WhereOptimizerTest methods pass under both flag values; 37 byte-shape divergences are captured in isV2Optimizer() branches with verbose comments explaining why v2 differs from v1 (DESC encoding, RVC clip, tautology simplification, trim-trailing, scalar-function composition limits, filter shape). 37 new keyspace- level unit tests cover the algebra, normalization, visitor, and extractor in isolation.