mihaibudiu commented on code in PR #5058:
URL: https://github.com/apache/calcite/pull/5058#discussion_r3501933725
##########
core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml:
##########
@@ -229,6 +229,140 @@ LogicalProject(HIREDATE=[$1])
LogicalProject(MGR=[$3])
LogicalFilter(condition=[AND(IS NULL($3), =($4, CURRENT_TIMESTAMP))])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testAggregateExtractLiteralAggRuleWithAnySubQuery">
+ <Resource name="sql">
+ <![CDATA[select deptno, name = ANY (
+ select mgr from emp)
+from dept]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalProject(DEPTNO=[$0], EXPR$1=[IN(CAST($1):INTEGER NOT NULL, {
+LogicalProject(MGR=[$3])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+})])
+ LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+]]>
+ </Resource>
+ <Resource name="planMid">
+ <![CDATA[
+LogicalProject(DEPTNO=[$0], EXPR$1=[OR(AND(IS NOT NULL($5), <>($2, 0)),
AND(<($3, $2), null, <>($2, 0), IS NULL($5)))])
+ LogicalJoin(condition=[=(CAST($1):INTEGER NOT NULL, $4)], joinType=[left])
+ LogicalJoin(condition=[true], joinType=[inner])
+ LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+ LogicalAggregate(group=[{}], c=[COUNT()], ck=[COUNT($0)])
+ LogicalProject(MGR=[$3])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ LogicalAggregate(group=[{0}], i=[LITERAL_AGG(true)])
+ LogicalProject(MGR=[$3])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(DEPTNO=[$0], EXPR$1=[OR(AND(IS NOT NULL($5), <>($2, 0)),
AND(<($3, $2), null, <>($2, 0), IS NULL($5)))])
+ LogicalJoin(condition=[=(CAST($1):INTEGER NOT NULL, $4)], joinType=[left])
+ LogicalJoin(condition=[true], joinType=[inner])
+ LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+ LogicalAggregate(group=[{}], c=[COUNT()], ck=[COUNT($0)])
+ LogicalProject(MGR=[$3])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ LogicalProject(MGR=[$0], i=[true])
+ LogicalAggregate(group=[{0}])
+ LogicalProject(MGR=[$3])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testAggregateExtractLiteralAggRuleWithEmptyInput">
Review Comment:
why is there no sql resource for this plan?
##########
core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml:
##########
@@ -229,6 +229,140 @@ LogicalProject(HIREDATE=[$1])
LogicalProject(MGR=[$3])
LogicalFilter(condition=[AND(IS NULL($3), =($4, CURRENT_TIMESTAMP))])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testAggregateExtractLiteralAggRuleWithAnySubQuery">
+ <Resource name="sql">
+ <![CDATA[select deptno, name = ANY (
+ select mgr from emp)
+from dept]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalProject(DEPTNO=[$0], EXPR$1=[IN(CAST($1):INTEGER NOT NULL, {
+LogicalProject(MGR=[$3])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+})])
+ LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+]]>
+ </Resource>
+ <Resource name="planMid">
+ <![CDATA[
+LogicalProject(DEPTNO=[$0], EXPR$1=[OR(AND(IS NOT NULL($5), <>($2, 0)),
AND(<($3, $2), null, <>($2, 0), IS NULL($5)))])
+ LogicalJoin(condition=[=(CAST($1):INTEGER NOT NULL, $4)], joinType=[left])
+ LogicalJoin(condition=[true], joinType=[inner])
+ LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+ LogicalAggregate(group=[{}], c=[COUNT()], ck=[COUNT($0)])
+ LogicalProject(MGR=[$3])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ LogicalAggregate(group=[{0}], i=[LITERAL_AGG(true)])
+ LogicalProject(MGR=[$3])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(DEPTNO=[$0], EXPR$1=[OR(AND(IS NOT NULL($5), <>($2, 0)),
AND(<($3, $2), null, <>($2, 0), IS NULL($5)))])
+ LogicalJoin(condition=[=(CAST($1):INTEGER NOT NULL, $4)], joinType=[left])
+ LogicalJoin(condition=[true], joinType=[inner])
+ LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+ LogicalAggregate(group=[{}], c=[COUNT()], ck=[COUNT($0)])
+ LogicalProject(MGR=[$3])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ LogicalProject(MGR=[$0], i=[true])
+ LogicalAggregate(group=[{0}])
+ LogicalProject(MGR=[$3])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testAggregateExtractLiteralAggRuleWithEmptyInput">
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalAggregate(group=[{7}], i=[LITERAL_AGG(true)])
+ LogicalValues(tuples=[[]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(DEPTNO=[$0], i=[true])
+ LogicalAggregate(group=[{7}])
+ LogicalValues(tuples=[[]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testAggregateExtractLiteralAggRuleWithInSubQuery">
+ <Resource name="sql">
+ <![CDATA[select empno
+from sales.emp
+where deptno in (select deptno from sales.emp where empno < 20)
+or emp.sal < 100]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalProject(EMPNO=[$0])
+ LogicalFilter(condition=[OR(IN($7, {
+LogicalProject(DEPTNO=[$7])
+ LogicalFilter(condition=[<($0, 20)])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+}), <($5, 100))])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ <Resource name="planMid">
+ <![CDATA[
+LogicalProject(EMPNO=[$0])
+ LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4],
SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+ LogicalFilter(condition=[OR(IS NOT NULL($10), <($5, 100))])
+ LogicalJoin(condition=[=($7, $9)], joinType=[left])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ LogicalAggregate(group=[{0}], i=[LITERAL_AGG(true)])
+ LogicalProject(DEPTNO=[$7])
+ LogicalFilter(condition=[<($0, 20)])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(EMPNO=[$0])
+ LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4],
SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+ LogicalFilter(condition=[OR(IS NOT NULL($10), <($5, 100))])
+ LogicalJoin(condition=[=($7, $9)], joinType=[left])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ LogicalProject(DEPTNO=[$0], i=[true])
+ LogicalAggregate(group=[{0}])
+ LogicalProject(DEPTNO=[$7])
+ LogicalFilter(condition=[<($0, 20)])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testAggregateExtractLiteralAggRuleWithOnlyLiteralAgg">
Review Comment:
or these two?
##########
core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java:
##########
@@ -990,6 +990,11 @@ private CoreRules() {}
public static final AggregateGroupingSetsToUnionRule
AGGREGATE_GROUPING_SETS_TO_UNION =
AggregateGroupingSetsToUnionRule.Config.DEFAULT.toRule();
+ /** Rule that removes {@code LITERAL_AGG} aggregate calls by replacing them
+ * with literal expressions in a {@link Project}. */
+ public static final AggregateExtractLiteralAggRule
AGGREGATE_EXTRACT_LITERAL_AGG =
Review Comment:
I would not use "EXTRACT", that implies you extract and keep them, but you
plainly remove them.
"REMOVE" would work just fine I think, or "ELIMINATE"
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]