Sean Broeder created CALCITE-7747:
-------------------------------------

             Summary: RexSimplify.simplifySearch does not simplify 
SEARCH(literal, Sarg) when pointCount >= 2
                 Key: CALCITE-7747
                 URL: https://issues.apache.org/jira/browse/CALCITE-7747
             Project: Calcite
          Issue Type: Bug
          Components: core
    Affects Versions: 1.42.0
            Reporter: Sean Broeder
            Assignee: Sean Broeder


RexSimplify.simplifySearch only evaluates a SEARCH(subject, Sarg) call against 
its Sarg when sarg.isPoints() && sarg.pointCount <= 1 (via 
RexUtil.expandSearch). It never checks whether subject is itself a constant 
(RexLiteral), so a SEARCH call whose subject is a literal — but whose Sarg has 
two or more points — is returned unevaluated instead of folding to a boolean 
constant.

Minimal reproduction (added to RexProgramTest.java, mirrors the existing 
testSimplifySearchWithSinglePointSargToEquals test):


{code:java}
@Test void testSimplifySearchWithLiteralOperandAndMultiPointSargNotFolded() {
  final RangeSet<BigDecimal> rangeSet =
      ImmutableRangeSet.<BigDecimal>builder()
            .add(Range.singleton(BigDecimal.valueOf(1)))
            .add(Range.singleton(BigDecimal.valueOf(2)))
            .build();
  final Sarg<BigDecimal> sarg = Sarg.of(RexUnknownAs.UNKNOWN, rangeSet);
  final RexLiteral searchLiteral = rexBuilder.makeSearchArgumentLiteral(sarg, 
tInt());
  final RexNode literalOperand = literal(5);
  final RexNode searchCall =
      rexBuilder.makeCall(SqlStdOperatorTable.SEARCH, literalOperand, 
searchLiteral);
  checkSimplify(searchCall, "false");  // actual: unchanged, "SEARCH(5, Sarg[1, 
2])"
}

{code}

Suggested fix: in simplifySearch, when the (possibly-just-simplified) search 
operand is a RexLiteral, expand via the existing RexUtil.expandSearch and 
recursively simplify() the result — regardless of point count — rather than 
gating that path on pointCount <= 1:

{code:java}
} else if (searchOperand instanceof RexLiteral || (sarg.isPoints() && 
sarg.pointCount <= 1)) {
    final RexNode expanded = RexUtil.expandSearch(rexBuilder, null, call);
    return searchOperand instanceof RexLiteral ? simplify(expanded, unknownAs) 
: expanded;
}
{code}




--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to