Skip to content

Conversation

@cloud-fan
Copy link
Contributor

What changes were proposed in this pull request?

I noticed an outdated comment in the rule InlineCTE

      // CTEs in SQL Commands have been inlined by `CTESubstitution` already, so it is safe to add
      // WithCTE as top node here.

This is not true anymore after #42036 . It's not a big deal as we replace not-inlined CTE relations with Repartition during optimization, so it doesn't matter where we put the WithCTE node with not-inlined CTE relations, as it will disappear eventually. But it's still better to keep it at its original place, as third-party rules may be sensitive about the plan shape.

Why are the changes needed?

to keep the plan shape as much as can after inlining CTE relations.

Does this PR introduce any user-facing change?

no

How was this patch tested?

new test

Was this patch authored or co-authored using generative AI tooling?

no

@github-actions github-actions bot added the SQL label May 16, 2024
@cloud-fan
Copy link
Contributor Author

cc @viirya @amaliujia @MaxGekk

outerCTEId.foreach { cteId =>
val (_, _, outerRefMap) = cteMap(cteId)
outerRefMap(ref.cteId) += 1
cteMap(cteId).recordOutgoingReference(ref.cteId)
Copy link
Contributor

Choose a reason for hiding this comment

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

I personally like to not hide the +1 implementation details here as I had troubles to understand it.

Or the function name can be IncreaseOutgoingReferenceByOne?

Copy link
Contributor

@amaliujia amaliujia left a comment

Choose a reason for hiding this comment

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

LGTM

@viirya
Copy link
Member

viirya commented May 20, 2024

This is not true anymore after #42036 . It's not a big deal as we replace not-inlined CTE relations with Repartition during

Repartition? Do you mean Relation?

…mizer/InlineCTESuite.scala

Co-authored-by: Liang-Chi Hsieh <[email protected]>
@cloud-fan
Copy link
Contributor Author

Repartition? Do you mean Relation?

It's Reparition, because we rely on shuffle reuse to reuse CTE relations.

@cloud-fan
Copy link
Contributor Author

thanks for the review, merging to master!

@cloud-fan cloud-fan closed this in 8a0927c Jun 4, 2024
cloud-fan added a commit that referenced this pull request Jun 5, 2024
…CTE relations

### What changes were proposed in this pull request?

This is a followup of #46617 .  Subquery expression has a bunch of correlation checks which need to match certain plan shapes. We broke this by leaving `WithCTE` in the plan for un-referenced CTE relations. This PR fixes the issue by skipping CTE plan nodes in correlated subquery expression checks.

### Why are the changes needed?

bug fix
### Does this PR introduce _any_ user-facing change?

no bug is not released yet.

### How was this patch tested?

new tests

### Was this patch authored or co-authored using generative AI tooling?

no

Closes #46869 from cloud-fan/check.

Authored-by: Wenchen Fan <[email protected]>
Signed-off-by: Wenchen Fan <[email protected]>
cloud-fan added a commit that referenced this pull request Jun 28, 2024
…d not fail

### What changes were proposed in this pull request?

This is a follow-up of #46617 to fix a bug. When we re-construct the `WithCTE` node, we should use the new CTE definitions that have been applied `inlineCTE`.

### Why are the changes needed?

bug fix, otherwise we may hit errors such as
```
java.util.NoSuchElementException: key not found: 0
	at scala.collection.MapOps.default(Map.scala:289)
	at scala.collection.MapOps.default$(Map.scala:288)
	at scala.collection.AbstractMap.default(Map.scala:420)
	at scala.collection.mutable.HashMap.apply(HashMap.scala:440)
	at org.apache.spark.sql.catalyst.optimizer.PushdownPredicatesAndPruneColumnsForCTEDef$.gatherPredicatesAndAttributes(PushdownPredicatesAndPruneColumnsForCTEDef.scala:74)
	at org.apache.spark.sql.catalyst.optimizer.PushdownPredicatesAndPruneColumnsForCTEDef$.$anonfun$gatherPredicatesAndAttributes$1(PushdownPredicatesAndPruneColumnsForCTEDef.scala:68)
	at scala.collection.immutable.Vector.foreach(Vector.scala:2124)
	at org.apache.spark.sql.catalyst.optimizer.PushdownPredicatesAndPruneColumnsForCTEDef$.gatherPredicatesAndAttributes(PushdownPredicatesAndPruneColumnsForCTEDef.scala:67)
```

### Does this PR introduce _any_ user-facing change?

no, the bug is not released yet

### How was this patch tested?

new test

### Was this patch authored or co-authored using generative AI tooling?

no

Closes #47141 from cloud-fan/fix.

Lead-authored-by: Wenchen Fan <[email protected]>
Co-authored-by: Wenchen Fan <[email protected]>
Signed-off-by: Wenchen Fan <[email protected]>
@LuciferYang
Copy link
Contributor

late LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants