Skip to content

Commit 75522ba

Browse files
author
Terran
committed
add more test case
1 parent f78c562 commit 75522ba

2 files changed

Lines changed: 161 additions & 0 deletions

File tree

core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12321,4 +12321,80 @@ private void checkLoptOptimizeJoinRule(LoptOptimizeJoinRule rule) {
1232112321
SqlValidatorTest.operatorTableFor(SqlLibrary.SPARK)))
1232212322
.withRule(CoreRules.PROJECT_REDUCE_EXPRESSIONS).check();
1232312323
}
12324+
12325+
/** Test case for
12326+
* <a href="https://issues.apache.org/jira/browse/CALCITE-7497">[CALCITE-7497]
12327+
* Enable Lambda supports constant folding</a>.
12328+
*
12329+
* <p>Boolean constant expression {@code 1 < 2} in lambda body should be
12330+
* folded to {@code true}. */
12331+
@Test void testReduceLambdaBodyBooleanConstantFolding() {
12332+
final String sql = "select \"EXISTS\"(ARRAY[1, 2, 3], x -> x > 1 AND 1 < 2)";
12333+
sql(sql)
12334+
.withFactory(f ->
12335+
f.withOperatorTable(opTab ->
12336+
SqlValidatorTest.operatorTableFor(SqlLibrary.SPARK)))
12337+
.withRule(CoreRules.PROJECT_REDUCE_EXPRESSIONS).check();
12338+
}
12339+
12340+
/** Test case for
12341+
* <a href="https://issues.apache.org/jira/browse/CALCITE-7497">[CALCITE-7497]
12342+
* Enable Lambda supports constant folding</a>.
12343+
*
12344+
* <p>Negative constant arithmetic {@code -1 * -2} in lambda body should be
12345+
* folded to {@code 2}. */
12346+
@Test void testReduceLambdaBodyNegativeConstantFolding() {
12347+
final String sql = "select \"EXISTS\"(ARRAY[1, 2, 3], x -> x > -1 * -2)";
12348+
sql(sql)
12349+
.withFactory(f ->
12350+
f.withOperatorTable(opTab ->
12351+
SqlValidatorTest.operatorTableFor(SqlLibrary.SPARK)))
12352+
.withRule(CoreRules.PROJECT_REDUCE_EXPRESSIONS).check();
12353+
}
12354+
12355+
/** Test case for
12356+
* <a href="https://issues.apache.org/jira/browse/CALCITE-7497">[CALCITE-7497]
12357+
* Enable Lambda supports constant folding</a>.
12358+
*
12359+
* <p>MOD function with constant arguments {@code MOD(10, 3)} in lambda body
12360+
* should be folded to {@code 1}. */
12361+
@Test void testReduceLambdaBodyModConstantFolding() {
12362+
final String sql = "select \"EXISTS\"(ARRAY[1, 2, 3], x -> x > MOD(10, 3))";
12363+
sql(sql)
12364+
.withFactory(f ->
12365+
f.withOperatorTable(opTab ->
12366+
SqlValidatorTest.operatorTableFor(SqlLibrary.SPARK)))
12367+
.withRule(CoreRules.PROJECT_REDUCE_EXPRESSIONS).check();
12368+
}
12369+
12370+
/** Test case for
12371+
* <a href="https://issues.apache.org/jira/browse/CALCITE-7497">[CALCITE-7497]
12372+
* Enable Lambda supports constant folding</a>.
12373+
*
12374+
* <p>OR-connected constant expressions {@code 1 + 2} and {@code 10 - 3}
12375+
* in lambda body should each be folded independently. */
12376+
@Test void testReduceLambdaBodyOrConstantFolding() {
12377+
final String sql = "select \"EXISTS\"(ARRAY[1, 2, 3], x -> x > 1 + 2 OR x < 10 - 3)";
12378+
sql(sql)
12379+
.withFactory(f ->
12380+
f.withOperatorTable(opTab ->
12381+
SqlValidatorTest.operatorTableFor(SqlLibrary.SPARK)))
12382+
.withRule(CoreRules.PROJECT_REDUCE_EXPRESSIONS).check();
12383+
}
12384+
12385+
/** Test case for
12386+
* <a href="https://issues.apache.org/jira/browse/CALCITE-7497">[CALCITE-7497]
12387+
* Enable Lambda supports constant folding</a>.
12388+
*
12389+
* <p>Deeply nested constant expression {@code (1 + 2) * (3 + 4)} in lambda
12390+
* body should be fully folded to {@code 21}. */
12391+
@Test void testReduceLambdaBodyDeepNestedConstantFolding() {
12392+
final String sql = "select \"EXISTS\"(ARRAY[1, 2, 3], x -> x > (1 + 2) * (3 + 4))";
12393+
sql(sql)
12394+
.withFactory(f ->
12395+
f.withOperatorTable(opTab ->
12396+
SqlValidatorTest.operatorTableFor(SqlLibrary.SPARK)))
12397+
.withRule(CoreRules.PROJECT_REDUCE_EXPRESSIONS).check();
12398+
}
12399+
1232412400
}

core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16913,6 +16913,23 @@ LogicalProject(DEPTNO=[$7], SAL=[$5])
1691316913
<![CDATA[
1691416914
LogicalProject(EXPR$0=[EXISTS(ARRAY(1), (X) -> true)])
1691516915
LogicalValues(tuples=[[{ 0 }]])
16916+
]]>
16917+
</Resource>
16918+
</TestCase>
16919+
<TestCase name="testReduceLambdaBodyBooleanConstantFolding">
16920+
<Resource name="sql">
16921+
<![CDATA[select "EXISTS"(ARRAY[1, 2, 3], x -> x > 1 AND 1 < 2)]]>
16922+
</Resource>
16923+
<Resource name="planBefore">
16924+
<![CDATA[
16925+
LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> AND(>(X, 1), <(1, 2)))])
16926+
LogicalValues(tuples=[[{ 0 }]])
16927+
]]>
16928+
</Resource>
16929+
<Resource name="planAfter">
16930+
<![CDATA[
16931+
LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> AND(>(X, 1), true))])
16932+
LogicalValues(tuples=[[{ 0 }]])
1691616933
]]>
1691716934
</Resource>
1691816935
</TestCase>
@@ -16930,6 +16947,74 @@ LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> >(X, +(1, 2)))])
1693016947
<![CDATA[
1693116948
LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> >(X, 3))])
1693216949
LogicalValues(tuples=[[{ 0 }]])
16950+
]]>
16951+
</Resource>
16952+
</TestCase>
16953+
<TestCase name="testReduceLambdaBodyDeepNestedConstantFolding">
16954+
<Resource name="sql">
16955+
<![CDATA[select "EXISTS"(ARRAY[1, 2, 3], x -> x > (1 + 2) * (3 + 4))]]>
16956+
</Resource>
16957+
<Resource name="planBefore">
16958+
<![CDATA[
16959+
LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> >(X, *(+(1, 2), +(3, 4))))])
16960+
LogicalValues(tuples=[[{ 0 }]])
16961+
]]>
16962+
</Resource>
16963+
<Resource name="planAfter">
16964+
<![CDATA[
16965+
LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> >(X, 21))])
16966+
LogicalValues(tuples=[[{ 0 }]])
16967+
]]>
16968+
</Resource>
16969+
</TestCase>
16970+
<TestCase name="testReduceLambdaBodyModConstantFolding">
16971+
<Resource name="sql">
16972+
<![CDATA[select "EXISTS"(ARRAY[1, 2, 3], x -> x > MOD(10, 3))]]>
16973+
</Resource>
16974+
<Resource name="planBefore">
16975+
<![CDATA[
16976+
LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> >(X, MOD(10, 3)))])
16977+
LogicalValues(tuples=[[{ 0 }]])
16978+
]]>
16979+
</Resource>
16980+
<Resource name="planAfter">
16981+
<![CDATA[
16982+
LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> >(X, 1))])
16983+
LogicalValues(tuples=[[{ 0 }]])
16984+
]]>
16985+
</Resource>
16986+
</TestCase>
16987+
<TestCase name="testReduceLambdaBodyNegativeConstantFolding">
16988+
<Resource name="sql">
16989+
<![CDATA[select "EXISTS"(ARRAY[1, 2, 3], x -> x > -1 * -2)]]>
16990+
</Resource>
16991+
<Resource name="planBefore">
16992+
<![CDATA[
16993+
LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> >(X, *(-1, -2)))])
16994+
LogicalValues(tuples=[[{ 0 }]])
16995+
]]>
16996+
</Resource>
16997+
<Resource name="planAfter">
16998+
<![CDATA[
16999+
LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> >(X, 2))])
17000+
LogicalValues(tuples=[[{ 0 }]])
17001+
]]>
17002+
</Resource>
17003+
</TestCase>
17004+
<TestCase name="testReduceLambdaBodyOrConstantFolding">
17005+
<Resource name="sql">
17006+
<![CDATA[select "EXISTS"(ARRAY[1, 2, 3], x -> x > 1 + 2 OR x < 10 - 3)]]>
17007+
</Resource>
17008+
<Resource name="planBefore">
17009+
<![CDATA[
17010+
LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> OR(>(X, +(1, 2)), <(X, -(10, 3))))])
17011+
LogicalValues(tuples=[[{ 0 }]])
17012+
]]>
17013+
</Resource>
17014+
<Resource name="planAfter">
17015+
<![CDATA[
17016+
LogicalProject(EXPR$0=[EXISTS(ARRAY(1, 2, 3), (X) -> OR(>(X, 3), <(X, 7)))])
17017+
LogicalValues(tuples=[[{ 0 }]])
1693317018
]]>
1693417019
</Resource>
1693517020
</TestCase>

0 commit comments

Comments
 (0)