Skip to content

Conversation

@NobiGo
Copy link
Contributor

@NobiGo NobiGo commented Jul 22, 2025

…When sub-query is aggregate qurey

@sonarqubecloud
Copy link

// lateral (select count(cast(deptno as integer)) from "scott".emp
// where emp.deptno = dept.deptno) on true;
SqlJoin sqlJoin = (SqlJoin) node;
if (sqlJoin.getJoinType() == JoinType.COMMA && sqlJoin.getRight() instanceof SqlCall) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain where the discussion in the JIRA is reflected in this code?
I think the consensus there was that a left join is not always right

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, @mihaibudiu. The jira cases discusses how converting an inner lateral join to a left join is WRONG. Do not merge this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will recheck this. This repair method does have some issues. I will add more test cases to verify it.

@suibianwanwank
Copy link
Contributor

I believe this is not the root cause of the incorrect query results. It's related to #4375.

The issue occurs in the Decorrelator. PR-4375 appears to have missed some cases where parentPropagatesNullValues should be set to false when Correlate's joinType is not Left.

I'm confident the following modification can fix all tests in this PR.

diff --git a/core/src/main/java/org/apache/calcite/sql2rel/RelDecorrelator.java b/core/src/main/java/org/apache/calcite/sql2rel/RelDecorrelator.java
index b5500c871..e150079c7 100644
--- a/core/src/main/java/org/apache/calcite/sql2rel/RelDecorrelator.java
+++ b/core/src/main/java/org/apache/calcite/sql2rel/RelDecorrelator.java
@@ -1629,7 +1629,8 @@ private static boolean isWidening(RelDataType type, RelDataType type1) {
     }
 
     frameStack.push(Pair.of(rel.getCorrelationId(), leftFrame));
-    final Frame rightFrame = getInvoke(oldRight, true, rel, parentPropagatesNullValues);
+    final Frame rightFrame = getInvoke(oldRight, true, rel,
+        parentPropagatesNullValues && rel.getJoinType() == JoinRelType.LEFT);
     frameStack.pop();

@NobiGo
Copy link
Contributor Author

NobiGo commented Jul 25, 2025

I believe this is not the root cause of the incorrect query results. It's related to #4375.

The issue occurs in the Decorrelator. PR-4375 appears to have missed some cases where parentPropagatesNullValues should be set to false when Correlate's joinType is not Left.

I'm confident the following modification can fix all tests in this PR.

diff --git a/core/src/main/java/org/apache/calcite/sql2rel/RelDecorrelator.java b/core/src/main/java/org/apache/calcite/sql2rel/RelDecorrelator.java
index b5500c871..e150079c7 100644
--- a/core/src/main/java/org/apache/calcite/sql2rel/RelDecorrelator.java
+++ b/core/src/main/java/org/apache/calcite/sql2rel/RelDecorrelator.java
@@ -1629,7 +1629,8 @@ private static boolean isWidening(RelDataType type, RelDataType type1) {
     }
 
     frameStack.push(Pair.of(rel.getCorrelationId(), leftFrame));
-    final Frame rightFrame = getInvoke(oldRight, true, rel, parentPropagatesNullValues);
+    final Frame rightFrame = getInvoke(oldRight, true, rel,
+        parentPropagatesNullValues && rel.getJoinType() == JoinRelType.LEFT);
     frameStack.pop();

I will recheck this.

@NobiGo NobiGo marked this pull request as draft July 25, 2025 01:52
@github-actions
Copy link

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 [email protected] list. Thank you for your contributions.

@github-actions github-actions bot added the stale label Aug 25, 2025
@github-actions github-actions bot removed the stale label Sep 7, 2025
@github-actions
Copy link

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 [email protected] list. Thank you for your contributions.

@github-actions github-actions bot added the stale label Oct 25, 2025
@github-actions github-actions bot removed the stale label Jan 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants