[ 
https://issues.apache.org/jira/browse/CALCITE-7977?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

ASF GitHub Bot updated CALCITE-7977:
------------------------------------
    Labels: pull-request-available  (was: )

> Avoid repeated TreeRangeSet splitting and allocations in RexSimplify.residue
> ----------------------------------------------------------------------------
>
>                 Key: CALCITE-7977
>                 URL: https://issues.apache.org/jira/browse/CALCITE-7977
>             Project: Calcite
>          Issue Type: Improvement
>          Components: core
>            Reporter: ShuMing Li
>            Priority: Major
>              Labels: pull-request-available
>
> Follow-up to CALCITE-7867.
> Even after switching {{RexSimplify.residue}} to narrow a single mutable 
> {{TreeRangeSet}} in place, simplifying large disjunctions of equalities on 
> NOT NULL expressions or large conjunctions of inequalities in filters still 
> performs {{O(N^2 log N)}} {{TreeRangeSet}} interval splits and {{O(N^2)}} 
> allocations:
> {code:sql}
> -- Pattern 1: large OR of equalities on a NOT NULL expression (e.g., expanded 
> IN list)
> x = c1 OR x = c2 OR ... OR x = cN
> -- Pattern 2: large AND of inequalities in a filter (e.g., expanded NOT IN 
> list, even on nullable x)
> x <> c1 AND x <> c2 AND ... AND x <> cN
> {code}
> Specifically:
> # In {{simplifyOrTerms}} (when simplifying {{IS_NOT_TRUE(x = c_i) -> x <> 
> c_i}}) and {{simplifyAndTerms}} (when simplifying {{x <> c_i}}), each 
> iteration calls {{residue}} with {{i - 1}} preceding {{x <> c_j}} predicates 
> against the non-point range set {{(-inf, c_i) U (c_i, +inf)}}.
> # For each {{<>}} predicate, {{residue}} allocates 
> {{ImmutableRangeSet.of(pointRange).complement()}} only for {{enclosesAll}}, 
> calls {{TreeRangeSet.remove(pointRange)}} to split continuous ranges 
> repeatedly, and at the end copies all {{O(i)}} ranges via 
> {{ImmutableRangeSet.copyOf(result)}}. Back in {{simplifyUsingPredicates}}, 
> the resulting multi-hole {{RangeSet}} always falls into the {{else}} branch 
> and is discarded, returning the original term {{e}}.
> # For non-{{<>}} predicates, {{residue}} allocates 
> {{ImmutableRangeSet.of(r1).complement()}} and constructs a {{Comparison}} 
> wrapper on every predicate in {{pulledUpPredicates}}.
> # {{RangeSets.minus(rangeSet, range)}} unconditionally copies {{rangeSet}} 
> into a {{TreeRangeSet}} even when {{range}} is empty or disjoint from 
> {{rangeSet}}.
> h3. Proposed Optimization
> # *Deferred hole buffer for consecutive {{<>}} predicates*: When narrowing a 
> non-point continuous range set, once a first confirmed missing point {{c1}} 
> is encountered ({{!enclosesNotEquals(current, c1) && current.contains(c1)}}), 
> {{current}} already lacks some point {{p != c1}}. Removing {{c1}} leaves at 
> least two missing points {{{p, c1}}}, so no subsequent {{<>}} predicate on 
> the same {{ref}} can satisfy {{enclosesNotEquals}}, and removing finitely 
> many points from a continuous non-point range set can never yield empty, 
> {{ALL}}, {{r0}}, or a single point. We record the start index in 
> {{predicates}} and defer subsequent {{<>}} removals with zero allocation, 
> flushing them in original order only if a non-{{<>}} predicate on the same 
> {{ref}} appears later. If none appears, removing just the first confirmed 
> hole preserves the exact outcome in {{simplifyUsingPredicates}}.
> # *Zero-allocation checks & lazy {{TreeRangeSet}}*: Lazily allocate 
> {{TreeRangeSet}} only on the first mutation, match predicates directly 
> against the known {{ref}} without constructing {{Comparison}}, replace 
> {{ImmutableRangeSet.of(pointRange).complement()}} with {{encloses(lessThan) 
> && encloses(greaterThan)}}, and narrow non-{{<>}} ranges directly via 
> {{TreeRangeSet.remove}} without constructing a complement 
> {{ImmutableRangeSet}}.
> # *Zero-copy short-circuit in {{RangeSets.minus}}*: Return {{rangeSet}} 
> directly when {{range.isEmpty() || rangeSet.subRangeSet(range).isEmpty()}}.
> h3. Benchmark Results
> Measured on {{RexProgramTest}} synthetic patterns ({{x = 0 OR ... OR x = 
> N-1}} on NOT NULL int; {{x <> 0 AND ... AND x <> N-1}} as filter):
> ||N||OR of = (before -> after)||AND of <> (before -> after)||
> |2,000|1,016.6 ms -> 235.1 ms (4.3x)|864.3 ms -> 111.4 ms (7.8x)|
> |5,000|6,677.5 ms -> 692.6 ms (9.6x)|5,908.9 ms -> 526.6 ms (11.2x)|



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to