You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by "iwanttobepowerful (via GitHub)" <gi...@apache.org> on 2025/10/24 07:18:43 UTC

[PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

iwanttobepowerful opened a new pull request, #4598:
URL: https://github.com/apache/calcite/pull/4598

   CALCITE-7242


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #4598:
URL: https://github.com/apache/calcite/pull/4598#issuecomment-3448275921

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4598) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [1 New issue](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4598&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4598&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=4598&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [87.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4598&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4598&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4598)
   
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #4598:
URL: https://github.com/apache/calcite/pull/4598#issuecomment-3590958490

   This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 90 days if no further activity occurs. If you think that’s incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@calcite.apache.org list. Thank you for your contributions.


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "iwanttobepowerful (via GitHub)" <gi...@apache.org>.
iwanttobepowerful commented on PR #4598:
URL: https://github.com/apache/calcite/pull/4598#issuecomment-3635049954

   > What is the status of this PR?
   @mihaibudiu 
   I’ll work on this PR once I’ve finished processing the other several PRs.


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #4598:
URL: https://github.com/apache/calcite/pull/4598#issuecomment-3692600196

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4598) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [1 New issue](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4598&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4598&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=4598&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [87.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4598&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4598&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4598)
   
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "xiedeyantu (via GitHub)" <gi...@apache.org>.
xiedeyantu commented on PR #4598:
URL: https://github.com/apache/calcite/pull/4598#issuecomment-3442748950

   > For example:
   > 
   > ```sql
   > select e.deptno, e.deptno < some (select deptno from emp where emp.ename = e.ename) as v from emp as e;
   > ```
   > 
   > ```
   > LogicalProject(DEPTNO=[$1], V=[OR(AND(IS TRUE(<($1, $3)), IS NOT TRUE(OR(IS NULL($6), =($4, 0)))), AND(IS TRUE(>($4, $5)), null, IS NOT TRUE(OR(IS NULL($6), =($4, 0))), IS NOT TRUE(<($1, $3))), AND(<($1, $3), IS NOT TRUE(OR(IS NULL($6), =($4, 0))), IS NOT TRUE(<($1, $3)), IS NOT TRUE(>($4, $5))))]), id = 7108
   >   LogicalJoin(condition=[IS NOT DISTINCT FROM($0, $2)], joinType=[left]), id = 7106
   >     LogicalProject(ename=[$1], deptno=[$8]), id = 7088
   >       LogicalTableScan(table=[[scott, emp]]), id = 7017
   >     LogicalProject(ename=[$0], m=[$2], c=[CASE(IS NOT NULL($3), $3, 0)], d=[CASE(IS NOT NULL($4), $4, 0)], trueLiteral=[$5]), id = 7104
   >       LogicalJoin(condition=[IS NOT DISTINCT FROM($0, $1)], joinType=[left]), id = 7102
   >         LogicalAggregate(group=[{0}]), id = 7093
   >           LogicalProject(ename=[$1]), id = 7091
   >             LogicalTableScan(table=[[scott, emp]]), id = 7017
   >         LogicalAggregate(group=[{0}], m=[MAX($1)], c=[COUNT()], d=[COUNT($1)], trueLiteral=[LITERAL_AGG(true)]), id = 7100
   >           LogicalProject(ename=[$1], DEPTNO=[$8]), id = 7098
   >             LogicalFilter(condition=[IS NOT NULL($1)]), id = 7096
   >               LogicalTableScan(table=[[scott, emp]]), id = 7017
   > I20251024 08:00:51.245133 767227 RuleEventLogger.java:45] 7b4b20a3988a9436:cf9ea6a600000000] call#1505: Apply rule [ExtractLiteralAgg] to [rel#7116:LogicalAggregate]
   > I20251024 08:00:51.245357 767227 RuleEventLogger.java:45] 7b4b20a3988a9436:cf9ea6a600000000] call#1506: Apply rule [ExtractLiteralAgg] to [rel#7123:LogicalAggregate]
   > I20251024 08:00:51.245544 767227 RuleEventLogger.java:54] 7b4b20a3988a9436:cf9ea6a600000000] call#1506: Full plan for rule input [rel#7123:LogicalAggregate]: 
   > LogicalAggregate(group=[{0}], m=[MAX($1)], c=[COUNT()], d=[COUNT($1)], trueLiteral=[LITERAL_AGG(true)])
   >   LogicalProject(ename=[$1], DEPTNO=[$8])
   >     LogicalFilter(condition=[IS NOT NULL($1)])
   >       LogicalTableScan(table=[[scott, emp]])
   > I20251024 08:00:51.245569 767227 RuleEventLogger.java:60] 7b4b20a3988a9436:cf9ea6a600000000] call#1506: Rule [ExtractLiteralAgg] produced [rel#7134:LogicalProject]
   > I20251024 08:00:51.245609 767227 RuleEventLogger.java:62] 7b4b20a3988a9436:cf9ea6a600000000] call#1506: Full plan for [rel#7134:LogicalProject]:
   > LogicalProject(ename=[$0], m=[$1], c=[$2], d=[$3], trueLiteral=[true])
   >   LogicalAggregate(group=[{0}], m=[MAX($1)], c=[COUNT()], d=[COUNT($1)])
   >     LogicalProject(ename=[$1], DEPTNO=[$8])
   >       LogicalFilter(condition=[IS NOT NULL($1)])
   >         LogicalTableScan(table=[[scott, emp]])
   > ```
   
   Can this log be converted into a test case? Typically, a rule should have one or more test cases in RelOptRulesTest.java for verification. To go a step further, a corresponding end-to-end test can be added in planner.iq to validate the correctness of the result.


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #4598:
URL: https://github.com/apache/calcite/pull/4598#issuecomment-3443209387

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4598) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [1 New issue](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4598&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4598&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=4598&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [12.9% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4598&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4598&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4598)
   
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #4598:
URL: https://github.com/apache/calcite/pull/4598#issuecomment-3460042986

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4598) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [1 New issue](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4598&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4598&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=4598&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [87.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4598&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4598&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4598)
   
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #4598:
URL: https://github.com/apache/calcite/pull/4598#issuecomment-3460017436

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4598) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [1 New issue](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4598&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4598&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=4598&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [87.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4598&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4598&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4598)
   
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "xiedeyantu (via GitHub)" <gi...@apache.org>.
xiedeyantu commented on code in PR #4598:
URL: https://github.com/apache/calcite/pull/4598#discussion_r2463164298


##########
core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml:
##########
@@ -216,6 +216,189 @@ LogicalProject(HIREDATE=[$1])
         LogicalProject(MGR=[$3])
           LogicalFilter(condition=[AND(IS NULL($3), =($4, CURRENT_TIMESTAMP))])
             LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testAggregateExtractLiteralAggRule1">
+    <Resource name="sql">
+      <![CDATA[select e.deptno,
+e.deptno < some (select deptno from emp where emp.ename = e.ename) as v
+from emp as e]]>
+    </Resource>
+    <Resource name="planBefore">
+      <![CDATA[
+LogicalProject(variablesSet=[[$cor0]], DEPTNO=[$7], V=[< SOME($7, {
+LogicalProject(DEPTNO=[$7])
+  LogicalFilter(condition=[=($1, $cor0.ENAME)])
+    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+})])
+  LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+    </Resource>
+    <Resource name="planMid">
+      <![CDATA[
+LogicalProject(DEPTNO=[$7], V=[CAST(OR(AND(IS TRUE(<($7, $9)), IS NOT TRUE(OR(IS NULL($12), =($10, 0)))), AND(IS TRUE(>($10, $11)), null, IS NOT TRUE(OR(IS NULL($12), =($10, 0))), IS NOT TRUE(<($7, $9))), AND(<($7, $9), IS NOT TRUE(OR(IS NULL($12), =($10, 0))), IS NOT TRUE(<($7, $9)), IS NOT TRUE(>($10, $11))))):BOOLEAN NOT NULL])
+  LogicalCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{1}])
+    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+    LogicalProject(m=[$0], c=[$1], d=[$1], trueLiteral=[$2])
+      LogicalAggregate(group=[{}], m=[MAX($0)], c=[COUNT()], trueLiteral=[LITERAL_AGG(true)])
+        LogicalProject(DEPTNO=[$7])
+          LogicalFilter(condition=[=($1, $cor0.ENAME)])
+            LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+    </Resource>
+    <Resource name="planAfter">
+      <![CDATA[
+LogicalProject(DEPTNO=[$7], V=[CAST(OR(AND(IS TRUE(<($7, $9)), IS NOT TRUE(OR(IS NULL($12), =($10, 0)))), AND(IS TRUE(>($10, $11)), null, IS NOT TRUE(OR(IS NULL($12), =($10, 0))), IS NOT TRUE(<($7, $9))), AND(<($7, $9), IS NOT TRUE(OR(IS NULL($12), =($10, 0))), IS NOT TRUE(<($7, $9)), IS NOT TRUE(>($10, $11))))):BOOLEAN NOT NULL])
+  LogicalJoin(condition=[=($1, $13)], joinType=[left])
+    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+    LogicalProject(m=[$2], c=[CASE(IS NOT NULL($3), $3, 0)], d=[CASE(IS NOT NULL($3), $3, 0)], trueLiteral=[$4], ENAME=[$0])
+      LogicalJoin(condition=[IS NOT DISTINCT FROM($0, $1)], joinType=[left])
+        LogicalAggregate(group=[{1}])
+          LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+        LogicalProject(ENAME=[$0], m=[$1], c=[$2], $f3=[true])
+          LogicalAggregate(group=[{0}], m=[MAX($1)], c=[COUNT()])
+            LogicalProject(ENAME=[$1], DEPTNO=[$7])
+              LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testAggregateExtractLiteralAggRule2">
+    <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($1, {
+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=[=($1, $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=[=($1, $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], $f1=[true])
+      LogicalAggregate(group=[{0}])
+        LogicalProject(MGR=[$3])
+          LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testAggregateExtractLiteralAggRule3">
+    <Resource name="sql">
+      <![CDATA[select * from emp where empno > ANY (
+  select deptno from dept where emp.job = dept.name)
+]]>
+    </Resource>
+    <Resource name="planBefore">
+      <![CDATA[
+LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+  LogicalFilter(condition=[> SOME($0, {
+LogicalProject(DEPTNO=[$0])
+  LogicalFilter(condition=[=($cor0.JOB, $1)])
+    LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+})], variablesSet=[[$cor0]])
+    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+    </Resource>
+    <Resource name="planMid">
+      <![CDATA[
+LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+  LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+    LogicalFilter(condition=[OR(AND(IS TRUE(>($0, $9)), IS NOT TRUE(OR(IS NULL($12), =($10, 0)))), AND(IS TRUE(>($10, $11)), null, IS NOT TRUE(OR(IS NULL($12), =($10, 0))), IS NOT TRUE(>($0, $9))), AND(>($0, $9), IS NOT TRUE(OR(IS NULL($12), =($10, 0))), IS NOT TRUE(>($0, $9)), IS NOT TRUE(>($10, $11))))])
+      LogicalCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{2}])
+        LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+        LogicalProject(m=[$0], c=[$1], d=[$1], trueLiteral=[$2])
+          LogicalAggregate(group=[{}], m=[MIN($0)], c=[COUNT()], trueLiteral=[LITERAL_AGG(true)])

Review Comment:
   I noticed that all cases use LITERAL_AGG(true). Would there ever be cases with LITERAL_AGG(false)?
   By the way, it would be ideal if we could add an end-to-end test in planner.iqto verify the correctness of the results.



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "iwanttobepowerful (via GitHub)" <gi...@apache.org>.
iwanttobepowerful commented on code in PR #4598:
URL: https://github.com/apache/calcite/pull/4598#discussion_r2463190666


##########
core/src/main/java/org/apache/calcite/rel/rules/AggregateExtractLiteralAggRule.java:
##########
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.tools.RelBuilder;
+
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Rule transforms an {@link org.apache.calcite.rel.core.Aggregate} containing
+ * {@code LITERAL_AGG} aggregate function into an {@code Aggregate} that still
+ * performs "group by" on the relevant groups, while placing a {@code Project}
+ * RelNode on top that returns the literal value.
+ */
+@Value.Enclosing
+public class AggregateExtractLiteralAggRule
+    extends RelRule<AggregateExtractLiteralAggRule.Config>
+    implements TransformationRule {
+
+  protected AggregateExtractLiteralAggRule(Config config) {
+    super(config);
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    final RelBuilder relBuilder = call.builder();
+    final Aggregate aggregate = call.rel(0);
+    final List<AggregateCall> aggCalls = aggregate.getAggCallList();
+    if (aggCalls.isEmpty()) {
+      return;
+    }
+
+    // Collect indices of LITERAL_AGG calls.
+    final List<Integer> literalAggIndices = new ArrayList<>();
+    for (int i = 0; i < aggCalls.size(); i++) {
+      final AggregateCall ac = aggCalls.get(i);
+      if (ac.getAggregation().getKind() == SqlKind.LITERAL_AGG) {
+        literalAggIndices.add(i);
+      }
+    }
+
+    if (literalAggIndices.isEmpty()) {
+      // nothing to do
+      return;
+    }
+
+    // Build new AggregateCall list without LITERAL_AGG entries.
+    final List<AggregateCall> newAggCalls = new ArrayList<>();
+    final Map<Integer, Integer> oldAggIndexToNewAggIndex = new HashMap<>();
+    int newAggPos = 0;
+    for (int i = 0; i < aggCalls.size(); i++) {
+      if (!literalAggIndices.contains(i)) {
+        newAggCalls.add(aggCalls.get(i));
+        oldAggIndexToNewAggIndex.put(i, newAggPos++);
+      }
+    }
+
+    relBuilder.push(aggregate.getInput());
+    final RelBuilder.GroupKey groupKey =
+        relBuilder.groupKey(aggregate.getGroupSet(), aggregate.getGroupSets());
+    final RelNode newAggregate = relBuilder.aggregate(groupKey, newAggCalls).build();
+
+    final RexBuilder rexBuilder = aggregate.getCluster().getRexBuilder();
+
+    // Number of group columns in output (group keys appear first).
+    final int groupCount = aggregate.getGroupSet().cardinality();
+    final int origAggCount = aggCalls.size();
+    final int origOutputCount = groupCount + origAggCount;
+
+    // Build projection expressions to restore original output layout.
+    final List<RexNode> projects = new ArrayList<>(origOutputCount);
+    for (int outPos = 0; outPos < origOutputCount; outPos++) {
+      if (outPos < groupCount) {
+        // Group key columns remain in the same positions.
+        projects.add(rexBuilder.makeInputRef(newAggregate, outPos));
+      } else {
+        // Aggregate output: determine original aggregate index.
+        final int origAggIndex = outPos - groupCount;
+        if (literalAggIndices.contains(origAggIndex)) {
+          // Replacement for LITERAL_AGG: try to extract literal from the original AggregateCall.
+          projects.add(aggCalls.get(origAggIndex).rexList.get(0));
+        } else {
+          // Non-literal aggregate: compute its new output index in newAggregate.
+          final Integer newAggIndex = oldAggIndexToNewAggIndex.get(origAggIndex);
+          if (newAggIndex != null) {
+            projects.add(rexBuilder.makeInputRef(newAggregate, groupCount + newAggIndex));
+          }
+        }
+      }
+    }
+
+    relBuilder.push(newAggregate);

Review Comment:
   maybe not.
   ```java
   /** Creates an {@link Aggregate} with an array of
      * {@link AggregateCall}s. */
     public RelBuilder aggregate(GroupKey groupKey,
         List<AggregateCall> aggregateCalls) {}
   ```



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #4598:
URL: https://github.com/apache/calcite/pull/4598#issuecomment-3442189307

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4598) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [1 New issue](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4598&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4598&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=4598&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [12.7% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4598&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4598&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4598)
   
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "xiedeyantu (via GitHub)" <gi...@apache.org>.
xiedeyantu commented on code in PR #4598:
URL: https://github.com/apache/calcite/pull/4598#discussion_r2660057186


##########
core/src/main/java/org/apache/calcite/rel/rules/AggregateExtractLiteralAggRule.java:
##########
@@ -34,7 +34,10 @@
 import java.util.Map;
 
 /**
- * AggregateExtractLiteralAggRule gets rid of the LITERAL_AGG into most databases can handle.
+ * Rule transforms an {@link org.apache.calcite.rel.core.Aggregate} containing

Review Comment:
   An example of SQL or plan is required here. Please refer to other rules.



##########
core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java:
##########
@@ -11413,4 +11413,88 @@ private void checkLoptOptimizeJoinRule(LoptOptimizeJoinRule rule) {
         })
         .check();
   }
+
+  /** Test case of
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-7242">[CALCITE-7242]
+   * Implement a rule to eliminate LITERAL_AGG so that other databases can handle it</a>. */
+  @Test void testAggregateExtractLiteralAggRule1() {

Review Comment:
   This doesn't seem to have been modified.



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #4598: [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it
URL: https://github.com/apache/calcite/pull/4598


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #4598:
URL: https://github.com/apache/calcite/pull/4598#issuecomment-3882036208

   This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 90 days if no further activity occurs. If you think that’s incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@calcite.apache.org list. Thank you for your contributions.


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "xiedeyantu (via GitHub)" <gi...@apache.org>.
xiedeyantu commented on code in PR #4598:
URL: https://github.com/apache/calcite/pull/4598#discussion_r2463148386


##########
core/src/main/java/org/apache/calcite/rel/rules/AggregateExtractLiteralAggRule.java:
##########
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.tools.RelBuilder;
+
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Rule transforms an {@link org.apache.calcite.rel.core.Aggregate} containing
+ * {@code LITERAL_AGG} aggregate function into an {@code Aggregate} that still
+ * performs "group by" on the relevant groups, while placing a {@code Project}
+ * RelNode on top that returns the literal value.
+ */
+@Value.Enclosing
+public class AggregateExtractLiteralAggRule
+    extends RelRule<AggregateExtractLiteralAggRule.Config>
+    implements TransformationRule {
+
+  protected AggregateExtractLiteralAggRule(Config config) {
+    super(config);
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    final RelBuilder relBuilder = call.builder();
+    final Aggregate aggregate = call.rel(0);
+    final List<AggregateCall> aggCalls = aggregate.getAggCallList();
+    if (aggCalls.isEmpty()) {
+      return;
+    }
+
+    // Collect indices of LITERAL_AGG calls.
+    final List<Integer> literalAggIndices = new ArrayList<>();
+    for (int i = 0; i < aggCalls.size(); i++) {
+      final AggregateCall ac = aggCalls.get(i);
+      if (ac.getAggregation().getKind() == SqlKind.LITERAL_AGG) {
+        literalAggIndices.add(i);
+      }
+    }
+
+    if (literalAggIndices.isEmpty()) {
+      // nothing to do
+      return;
+    }
+
+    // Build new AggregateCall list without LITERAL_AGG entries.
+    final List<AggregateCall> newAggCalls = new ArrayList<>();
+    final Map<Integer, Integer> oldAggIndexToNewAggIndex = new HashMap<>();
+    int newAggPos = 0;
+    for (int i = 0; i < aggCalls.size(); i++) {
+      if (!literalAggIndices.contains(i)) {
+        newAggCalls.add(aggCalls.get(i));
+        oldAggIndexToNewAggIndex.put(i, newAggPos++);
+      }
+    }
+
+    relBuilder.push(aggregate.getInput());
+    final RelBuilder.GroupKey groupKey =
+        relBuilder.groupKey(aggregate.getGroupSet(), aggregate.getGroupSets());
+    final RelNode newAggregate = relBuilder.aggregate(groupKey, newAggCalls).build();
+
+    final RexBuilder rexBuilder = aggregate.getCluster().getRexBuilder();
+
+    // Number of group columns in output (group keys appear first).
+    final int groupCount = aggregate.getGroupSet().cardinality();
+    final int origAggCount = aggCalls.size();
+    final int origOutputCount = groupCount + origAggCount;
+
+    // Build projection expressions to restore original output layout.
+    final List<RexNode> projects = new ArrayList<>(origOutputCount);
+    for (int outPos = 0; outPos < origOutputCount; outPos++) {
+      if (outPos < groupCount) {
+        // Group key columns remain in the same positions.
+        projects.add(rexBuilder.makeInputRef(newAggregate, outPos));
+      } else {
+        // Aggregate output: determine original aggregate index.
+        final int origAggIndex = outPos - groupCount;
+        if (literalAggIndices.contains(origAggIndex)) {
+          // Replacement for LITERAL_AGG: try to extract literal from the original AggregateCall.
+          projects.add(aggCalls.get(origAggIndex).rexList.get(0));
+        } else {
+          // Non-literal aggregate: compute its new output index in newAggregate.
+          final Integer newAggIndex = oldAggIndexToNewAggIndex.get(origAggIndex);
+          if (newAggIndex != null) {
+            projects.add(rexBuilder.makeInputRef(newAggregate, groupCount + newAggIndex));
+          }
+        }
+      }
+    }
+
+    relBuilder.push(newAggregate);

Review Comment:
   if `final RelNode newAggregate = relBuilder.aggregate(groupKey, newAggCalls).build();` change to `final RelNode newAggregate = relBuilder.aggregate(groupKey, newAggCalls);`, This push should be omitted. This is a minor, non-essential suggestion.



##########
core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java:
##########
@@ -945,4 +945,8 @@ private CoreRules() {}
    *  into equivalent {@link Union} ALL of GROUP BY operations. */
   public static final AggregateGroupingSetsToUnionRule AGGREGATE_GROUPING_SETS_TO_UNION =
       AggregateGroupingSetsToUnionRule.Config.DEFAULT.toRule();
+
+  /** Rule that gets rid of the LITERAL_AGG into most databases can handle. */

Review Comment:
   I think a brief description of how the plan is transformed from one form to another is needed here, rather than describing where the rule will be used.



##########
core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java:
##########
@@ -11413,4 +11413,88 @@ private void checkLoptOptimizeJoinRule(LoptOptimizeJoinRule rule) {
         })
         .check();
   }
+
+  /** Test case of
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-7242">[CALCITE-7242]
+   * Implement a rule to eliminate LITERAL_AGG so that other databases can handle it</a>. */
+  @Test void testAggregateExtractLiteralAggRule1() {

Review Comment:
   If a rule requires multiple test cases, it's best to avoid naming them with a simple incrementing number suffix.. This makes it difficult to quickly understand the purpose of each test. For example, this name could be changed to something like `testAggregateExtractLiteralAggRuleSomeLessThan`.



##########
core/src/main/java/org/apache/calcite/rel/rules/AggregateExtractLiteralAggRule.java:
##########
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.tools.RelBuilder;
+
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Rule transforms an {@link org.apache.calcite.rel.core.Aggregate} containing
+ * {@code LITERAL_AGG} aggregate function into an {@code Aggregate} that still
+ * performs "group by" on the relevant groups, while placing a {@code Project}
+ * RelNode on top that returns the literal value.
+ */
+@Value.Enclosing
+public class AggregateExtractLiteralAggRule
+    extends RelRule<AggregateExtractLiteralAggRule.Config>
+    implements TransformationRule {
+
+  protected AggregateExtractLiteralAggRule(Config config) {
+    super(config);
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    final RelBuilder relBuilder = call.builder();
+    final Aggregate aggregate = call.rel(0);
+    final List<AggregateCall> aggCalls = aggregate.getAggCallList();
+    if (aggCalls.isEmpty()) {
+      return;
+    }
+
+    // Collect indices of LITERAL_AGG calls.
+    final List<Integer> literalAggIndices = new ArrayList<>();
+    for (int i = 0; i < aggCalls.size(); i++) {
+      final AggregateCall ac = aggCalls.get(i);
+      if (ac.getAggregation().getKind() == SqlKind.LITERAL_AGG) {
+        literalAggIndices.add(i);
+      }
+    }
+
+    if (literalAggIndices.isEmpty()) {
+      // nothing to do
+      return;
+    }
+
+    // Build new AggregateCall list without LITERAL_AGG entries.
+    final List<AggregateCall> newAggCalls = new ArrayList<>();
+    final Map<Integer, Integer> oldAggIndexToNewAggIndex = new HashMap<>();
+    int newAggPos = 0;
+    for (int i = 0; i < aggCalls.size(); i++) {
+      if (!literalAggIndices.contains(i)) {
+        newAggCalls.add(aggCalls.get(i));
+        oldAggIndexToNewAggIndex.put(i, newAggPos++);
+      }
+    }
+
+    relBuilder.push(aggregate.getInput());
+    final RelBuilder.GroupKey groupKey =
+        relBuilder.groupKey(aggregate.getGroupSet(), aggregate.getGroupSets());
+    final RelNode newAggregate = relBuilder.aggregate(groupKey, newAggCalls).build();
+
+    final RexBuilder rexBuilder = aggregate.getCluster().getRexBuilder();
+
+    // Number of group columns in output (group keys appear first).
+    final int groupCount = aggregate.getGroupSet().cardinality();
+    final int origAggCount = aggCalls.size();
+    final int origOutputCount = groupCount + origAggCount;
+
+    // Build projection expressions to restore original output layout.
+    final List<RexNode> projects = new ArrayList<>(origOutputCount);
+    for (int outPos = 0; outPos < origOutputCount; outPos++) {
+      if (outPos < groupCount) {
+        // Group key columns remain in the same positions.
+        projects.add(rexBuilder.makeInputRef(newAggregate, outPos));
+      } else {
+        // Aggregate output: determine original aggregate index.
+        final int origAggIndex = outPos - groupCount;
+        if (literalAggIndices.contains(origAggIndex)) {
+          // Replacement for LITERAL_AGG: try to extract literal from the original AggregateCall.
+          projects.add(aggCalls.get(origAggIndex).rexList.get(0));
+        } else {
+          // Non-literal aggregate: compute its new output index in newAggregate.
+          final Integer newAggIndex = oldAggIndexToNewAggIndex.get(origAggIndex);
+          if (newAggIndex != null) {

Review Comment:
   `assert newAggIndex != null` ?Could the index here be null?



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "xiedeyantu (via GitHub)" <gi...@apache.org>.
xiedeyantu commented on code in PR #4598:
URL: https://github.com/apache/calcite/pull/4598#discussion_r2460055473


##########
core/src/main/java/org/apache/calcite/rel/rules/AggregateExtractLiteralAggRule.java:
##########
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rel.logical.LogicalAggregate;
+import org.apache.calcite.rel.logical.LogicalProject;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.tools.RelBuilder;
+
+import com.google.common.collect.ImmutableSet;
+
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * AggregateExtractLiteralAggRule gets rid of the LITERAL_AGG into most databases can handle.

Review Comment:
   Javadoc should typically describe what this rule does, specifically the transformation from which type of SQL to which other type of SQL, or from which type of Plan to which other type of Plan.



##########
core/src/main/java/org/apache/calcite/rel/rules/AggregateExtractLiteralAggRule.java:
##########
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rel.logical.LogicalAggregate;
+import org.apache.calcite.rel.logical.LogicalProject;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.tools.RelBuilder;
+
+import com.google.common.collect.ImmutableSet;
+
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * AggregateExtractLiteralAggRule gets rid of the LITERAL_AGG into most databases can handle.
+ */
+@Value.Enclosing
+public class AggregateExtractLiteralAggRule
+    extends RelRule<AggregateExtractLiteralAggRule.Config>
+    implements TransformationRule {
+
+  protected AggregateExtractLiteralAggRule(Config config) {
+    super(config);
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    final LogicalAggregate aggregate = call.rel(0);
+
+    final List<AggregateCall> aggCalls = aggregate.getAggCallList();
+    if (aggCalls.isEmpty()) {
+      return;
+    }
+
+    // Collect indices of LITERAL_AGG calls.
+    final List<Integer> literalAggIndices = new ArrayList<>();
+    for (int i = 0; i < aggCalls.size(); i++) {
+      final AggregateCall ac = aggCalls.get(i);
+      if (ac.getAggregation().getName().equals("LITERAL_AGG")) {

Review Comment:
   Would it be better to use `ac.getAggregation().getKind() == SqlKind.LITERAL_AGG`?



##########
core/src/main/java/org/apache/calcite/rel/rules/AggregateExtractLiteralAggRule.java:
##########
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rel.logical.LogicalAggregate;
+import org.apache.calcite.rel.logical.LogicalProject;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.tools.RelBuilder;
+
+import com.google.common.collect.ImmutableSet;
+
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * AggregateExtractLiteralAggRule gets rid of the LITERAL_AGG into most databases can handle.
+ */
+@Value.Enclosing
+public class AggregateExtractLiteralAggRule
+    extends RelRule<AggregateExtractLiteralAggRule.Config>
+    implements TransformationRule {
+
+  protected AggregateExtractLiteralAggRule(Config config) {
+    super(config);
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    final LogicalAggregate aggregate = call.rel(0);

Review Comment:
   Is better to use Aggregate?



##########
core/src/main/java/org/apache/calcite/rel/rules/AggregateExtractLiteralAggRule.java:
##########
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rel.logical.LogicalAggregate;
+import org.apache.calcite.rel.logical.LogicalProject;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.tools.RelBuilder;
+
+import com.google.common.collect.ImmutableSet;
+
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * AggregateExtractLiteralAggRule gets rid of the LITERAL_AGG into most databases can handle.
+ */
+@Value.Enclosing
+public class AggregateExtractLiteralAggRule
+    extends RelRule<AggregateExtractLiteralAggRule.Config>
+    implements TransformationRule {
+
+  protected AggregateExtractLiteralAggRule(Config config) {
+    super(config);
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    final LogicalAggregate aggregate = call.rel(0);
+
+    final List<AggregateCall> aggCalls = aggregate.getAggCallList();
+    if (aggCalls.isEmpty()) {
+      return;
+    }
+
+    // Collect indices of LITERAL_AGG calls.
+    final List<Integer> literalAggIndices = new ArrayList<>();
+    for (int i = 0; i < aggCalls.size(); i++) {
+      final AggregateCall ac = aggCalls.get(i);
+      if (ac.getAggregation().getName().equals("LITERAL_AGG")) {
+        literalAggIndices.add(i);
+      }
+    }
+
+    if (literalAggIndices.isEmpty()) {
+      // nothing to do
+      return;
+    }
+
+    // Build new AggregateCall list without LITERAL_AGG entries.
+    final List<AggregateCall> newAggCalls = new ArrayList<>();
+    final Map<Integer, Integer> oldAggIndexToNewAggIndex = new HashMap<>();
+    int newAggPos = 0;
+    for (int i = 0; i < aggCalls.size(); i++) {
+      if (!literalAggIndices.contains(i)) {
+        newAggCalls.add(aggCalls.get(i));
+        oldAggIndexToNewAggIndex.put(i, newAggPos++);
+      }
+    }
+
+    // Create new Aggregate preserving groupSet/groupSets/hints.
+    final LogicalAggregate newAggregate =

Review Comment:
   I noticed that the operators in this rule are all created using the create method(Aggregate and Project). Could they be changed to use RelBuilder?



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #4598:
URL: https://github.com/apache/calcite/pull/4598#issuecomment-4427410138

   This pull request has been closed due to lack of activity. If you think that is incorrect, or the pull request requires review, you can revive the PR at any time.


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #4598:
URL: https://github.com/apache/calcite/pull/4598#issuecomment-3442591228

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4598) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [1 New issue](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4598&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4598&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=4598&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [12.7% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4598&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4598&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4598)
   
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "iwanttobepowerful (via GitHub)" <gi...@apache.org>.
iwanttobepowerful commented on PR #4598:
URL: https://github.com/apache/calcite/pull/4598#issuecomment-3441706686

   For example:
   ```sql
   select e.deptno, e.deptno < some (select deptno from emp where emp.ename = e.ename) as v from emp as e;
   ```
   
   ```
   LogicalProject(DEPTNO=[$1], V=[OR(AND(IS TRUE(<($1, $3)), IS NOT TRUE(OR(IS NULL($6), =($4, 0)))), AND(IS TRUE(>($4, $5)), null, IS NOT TRUE(OR(IS NULL($6), =($4, 0))), IS NOT TRUE(<($1, $3))), AND(<($1, $3), IS NOT TRUE(OR(IS NULL($6), =($4, 0))), IS NOT TRUE(<($1, $3)), IS NOT TRUE(>($4, $5))))]), id = 7108
     LogicalJoin(condition=[IS NOT DISTINCT FROM($0, $2)], joinType=[left]), id = 7106
       LogicalProject(ename=[$1], deptno=[$8]), id = 7088
         LogicalTableScan(table=[[scott, emp]]), id = 7017
       LogicalProject(ename=[$0], m=[$2], c=[CASE(IS NOT NULL($3), $3, 0)], d=[CASE(IS NOT NULL($4), $4, 0)], trueLiteral=[$5]), id = 7104
         LogicalJoin(condition=[IS NOT DISTINCT FROM($0, $1)], joinType=[left]), id = 7102
           LogicalAggregate(group=[{0}]), id = 7093
             LogicalProject(ename=[$1]), id = 7091
               LogicalTableScan(table=[[scott, emp]]), id = 7017
           LogicalAggregate(group=[{0}], m=[MAX($1)], c=[COUNT()], d=[COUNT($1)], trueLiteral=[LITERAL_AGG(true)]), id = 7100
             LogicalProject(ename=[$1], DEPTNO=[$8]), id = 7098
               LogicalFilter(condition=[IS NOT NULL($1)]), id = 7096
                 LogicalTableScan(table=[[scott, emp]]), id = 7017
   I20251024 08:00:51.245133 767227 RuleEventLogger.java:45] 7b4b20a3988a9436:cf9ea6a600000000] call#1505: Apply rule [ExtractLiteralAgg] to [rel#7116:LogicalAggregate]
   I20251024 08:00:51.245357 767227 RuleEventLogger.java:45] 7b4b20a3988a9436:cf9ea6a600000000] call#1506: Apply rule [ExtractLiteralAgg] to [rel#7123:LogicalAggregate]
   I20251024 08:00:51.245544 767227 RuleEventLogger.java:54] 7b4b20a3988a9436:cf9ea6a600000000] call#1506: Full plan for rule input [rel#7123:LogicalAggregate]: 
   LogicalAggregate(group=[{0}], m=[MAX($1)], c=[COUNT()], d=[COUNT($1)], trueLiteral=[LITERAL_AGG(true)])
     LogicalProject(ename=[$1], DEPTNO=[$8])
       LogicalFilter(condition=[IS NOT NULL($1)])
         LogicalTableScan(table=[[scott, emp]])
   I20251024 08:00:51.245569 767227 RuleEventLogger.java:60] 7b4b20a3988a9436:cf9ea6a600000000] call#1506: Rule [ExtractLiteralAgg] produced [rel#7134:LogicalProject]
   I20251024 08:00:51.245609 767227 RuleEventLogger.java:62] 7b4b20a3988a9436:cf9ea6a600000000] call#1506: Full plan for [rel#7134:LogicalProject]:
   LogicalProject(ename=[$0], m=[$1], c=[$2], d=[$3], trueLiteral=[true])
     LogicalAggregate(group=[{0}], m=[MAX($1)], c=[COUNT()], d=[COUNT($1)])
       LogicalProject(ename=[$1], DEPTNO=[$8])
         LogicalFilter(condition=[IS NOT NULL($1)])
           LogicalTableScan(table=[[scott, emp]])
   ```


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "xiedeyantu (via GitHub)" <gi...@apache.org>.
xiedeyantu commented on PR #4598:
URL: https://github.com/apache/calcite/pull/4598#issuecomment-3447953424

   CI failed, message is:
   ```
   FAILURE   0.0sec, org.apache.calcite.test.LintTest > testLintLog()
       java.lang.AssertionError: 
       Expected: an empty collection
            but: <[invalid git log message 'address comments and add tests'; Message must start with upper-case letter]>
           at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18)
           at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:6)
           at org.apache.calcite.test.LintTest.testLintLog(LintTest.java:308)
   ```
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "iwanttobepowerful (via GitHub)" <gi...@apache.org>.
iwanttobepowerful commented on code in PR #4598:
URL: https://github.com/apache/calcite/pull/4598#discussion_r2463191080


##########
core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml:
##########
@@ -216,6 +216,189 @@ LogicalProject(HIREDATE=[$1])
         LogicalProject(MGR=[$3])
           LogicalFilter(condition=[AND(IS NULL($3), =($4, CURRENT_TIMESTAMP))])
             LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testAggregateExtractLiteralAggRule1">
+    <Resource name="sql">
+      <![CDATA[select e.deptno,
+e.deptno < some (select deptno from emp where emp.ename = e.ename) as v
+from emp as e]]>
+    </Resource>
+    <Resource name="planBefore">
+      <![CDATA[
+LogicalProject(variablesSet=[[$cor0]], DEPTNO=[$7], V=[< SOME($7, {
+LogicalProject(DEPTNO=[$7])
+  LogicalFilter(condition=[=($1, $cor0.ENAME)])
+    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+})])
+  LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+    </Resource>
+    <Resource name="planMid">
+      <![CDATA[
+LogicalProject(DEPTNO=[$7], V=[CAST(OR(AND(IS TRUE(<($7, $9)), IS NOT TRUE(OR(IS NULL($12), =($10, 0)))), AND(IS TRUE(>($10, $11)), null, IS NOT TRUE(OR(IS NULL($12), =($10, 0))), IS NOT TRUE(<($7, $9))), AND(<($7, $9), IS NOT TRUE(OR(IS NULL($12), =($10, 0))), IS NOT TRUE(<($7, $9)), IS NOT TRUE(>($10, $11))))):BOOLEAN NOT NULL])
+  LogicalCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{1}])
+    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+    LogicalProject(m=[$0], c=[$1], d=[$1], trueLiteral=[$2])
+      LogicalAggregate(group=[{}], m=[MAX($0)], c=[COUNT()], trueLiteral=[LITERAL_AGG(true)])
+        LogicalProject(DEPTNO=[$7])
+          LogicalFilter(condition=[=($1, $cor0.ENAME)])
+            LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+    </Resource>
+    <Resource name="planAfter">
+      <![CDATA[
+LogicalProject(DEPTNO=[$7], V=[CAST(OR(AND(IS TRUE(<($7, $9)), IS NOT TRUE(OR(IS NULL($12), =($10, 0)))), AND(IS TRUE(>($10, $11)), null, IS NOT TRUE(OR(IS NULL($12), =($10, 0))), IS NOT TRUE(<($7, $9))), AND(<($7, $9), IS NOT TRUE(OR(IS NULL($12), =($10, 0))), IS NOT TRUE(<($7, $9)), IS NOT TRUE(>($10, $11))))):BOOLEAN NOT NULL])
+  LogicalJoin(condition=[=($1, $13)], joinType=[left])
+    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+    LogicalProject(m=[$2], c=[CASE(IS NOT NULL($3), $3, 0)], d=[CASE(IS NOT NULL($3), $3, 0)], trueLiteral=[$4], ENAME=[$0])
+      LogicalJoin(condition=[IS NOT DISTINCT FROM($0, $1)], joinType=[left])
+        LogicalAggregate(group=[{1}])
+          LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+        LogicalProject(ENAME=[$0], m=[$1], c=[$2], $f3=[true])
+          LogicalAggregate(group=[{0}], m=[MAX($1)], c=[COUNT()])
+            LogicalProject(ENAME=[$1], DEPTNO=[$7])
+              LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testAggregateExtractLiteralAggRule2">
+    <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($1, {
+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=[=($1, $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=[=($1, $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], $f1=[true])
+      LogicalAggregate(group=[{0}])
+        LogicalProject(MGR=[$3])
+          LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testAggregateExtractLiteralAggRule3">
+    <Resource name="sql">
+      <![CDATA[select * from emp where empno > ANY (
+  select deptno from dept where emp.job = dept.name)
+]]>
+    </Resource>
+    <Resource name="planBefore">
+      <![CDATA[
+LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+  LogicalFilter(condition=[> SOME($0, {
+LogicalProject(DEPTNO=[$0])
+  LogicalFilter(condition=[=($cor0.JOB, $1)])
+    LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+})], variablesSet=[[$cor0]])
+    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+    </Resource>
+    <Resource name="planMid">
+      <![CDATA[
+LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+  LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+    LogicalFilter(condition=[OR(AND(IS TRUE(>($0, $9)), IS NOT TRUE(OR(IS NULL($12), =($10, 0)))), AND(IS TRUE(>($10, $11)), null, IS NOT TRUE(OR(IS NULL($12), =($10, 0))), IS NOT TRUE(>($0, $9))), AND(>($0, $9), IS NOT TRUE(OR(IS NULL($12), =($10, 0))), IS NOT TRUE(>($0, $9)), IS NOT TRUE(>($10, $11))))])
+      LogicalCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{2}])
+        LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+        LogicalProject(m=[$0], c=[$1], d=[$1], trueLiteral=[$2])
+          LogicalAggregate(group=[{}], m=[MIN($0)], c=[COUNT()], trueLiteral=[LITERAL_AGG(true)])

Review Comment:
   add an end-to-end test in planner.iq is needed.
   but we can not see logical plan that affected by this rule.



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7242] Implement a rule to eliminate LITERAL_AGG so that other databases can handle it [calcite]

Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on PR #4598:
URL: https://github.com/apache/calcite/pull/4598#issuecomment-3633698838

   What is the status of this PR?


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org