Skip to content

PHOENIX-6791 WHERE optimizer redesign#2428

Draft
kadirozde wants to merge 1 commit intoapache:masterfrom
kadirozde:6791
Draft

PHOENIX-6791 WHERE optimizer redesign#2428
kadirozde wants to merge 1 commit intoapache:masterfrom
kadirozde:6791

Conversation

@kadirozde
Copy link
Copy Markdown
Contributor

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.

@kadirozde kadirozde marked this pull request as draft April 28, 2026 07:58
@kadirozde kadirozde changed the title PHOENIX-6791 Replace WHERE optimizer with N-dimensional key-space model PHOENIX-6791 WHERE optimizer redesign Apr 29, 2026
@kadirozde kadirozde force-pushed the 6791 branch 3 times, most recently from 022c608 to 2e7a7a6 Compare May 1, 2026 01:21
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.
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