You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "cloud-fan (via GitHub)" <gi...@apache.org> on 2023/11/14 07:45:59 UTC
[PR] [SPARK-45920][SQL] group by ordinal should be idempotent [spark]
cloud-fan opened a new pull request, #43797:
URL: https://github.com/apache/spark/pull/43797
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'core/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
GROUP BY ordinal is not idempotent today. If the ordinal points to another integer literal and the plan get analyzed again, we will re-do the ordinal resolution which can lead to wrong result or index out-of-bound error. This PR fixes it by using a hack: if the ordinal points to another integer literal, don't replace the ordinal.
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
2. If you fix a bug, you can clarify why it is a bug.
-->
For advanced users or Spark plugins, they may manipulate the logical plans directly. We need to make the framework more reliable.
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as the documentation fix.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
No
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
new test
### Was this patch authored or co-authored using generative AI tooling?
<!--
If generative AI tooling has been used in the process of authoring this patch, please include the
phrase: 'Generated-by: ' followed by the name of the tool and its version.
If no, write 'No'.
Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
-->
no
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-45920][SQL] group by ordinal should be idempotent [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #43797:
URL: https://github.com/apache/spark/pull/43797#issuecomment-1809690606
cc @gengliangwang @viirya @beliefer
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-45920][SQL] group by ordinal should be idempotent [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #43797:
URL: https://github.com/apache/spark/pull/43797#discussion_r1392248485
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -1999,7 +1999,18 @@ class Analyzer(override val catalogManager: CatalogManager) extends RuleExecutor
throw QueryCompilationErrors.groupByPositionRefersToAggregateFunctionError(
index, ordinalExpr)
} else {
- ordinalExpr
+ trimAliases(ordinalExpr) match {
+ // HACK ALERT: If the ordinal expression is also an integer literal, don't use it
+ // but still keep the ordinal literal. The reason is we may repeatedly
+ // analyze the plan. Using a different integer literal may lead to
+ // a repeat GROUP BY ordinal resolution which is wrong. GROUP BY
+ // constant is meaningless so whatever value does not matter here.
+ // TODO: GROUP BY ordinal should pull out grouping expressions to a Project, then
Review Comment:
I'd like to backport this surgical fix, and only do the refactoring in the master branch. So yes, this is a temporary fix for the master branch.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-45920][SQL] group by ordinal should be idempotent [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #43797:
URL: https://github.com/apache/spark/pull/43797#discussion_r1392868055
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -1999,7 +1999,18 @@ class Analyzer(override val catalogManager: CatalogManager) extends RuleExecutor
throw QueryCompilationErrors.groupByPositionRefersToAggregateFunctionError(
index, ordinalExpr)
} else {
- ordinalExpr
+ trimAliases(ordinalExpr) match {
+ // HACK ALERT: If the ordinal expression is also an integer literal, don't use it
+ // but still keep the ordinal literal. The reason is we may repeatedly
+ // analyze the plan. Using a different integer literal may lead to
+ // a repeat GROUP BY ordinal resolution which is wrong. GROUP BY
+ // constant is meaningless so whatever value does not matter here.
+ // TODO: GROUP BY ordinal should pull out grouping expressions to a Project, then
Review Comment:
Could you file an official JIRA issue and make this as the IDed TODO, please?
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-45920][SQL] group by ordinal should be idempotent [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #43797:
URL: https://github.com/apache/spark/pull/43797#issuecomment-1814017740
In you don't mind, could you make a backporting PR to branch-3.5/3.4/3.3 separately? For this PR, I believe we should be more careful due to the different golden files per branches, @cloud-fan .
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-45920][SQL] group by ordinal should be idempotent [spark]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #43797:
URL: https://github.com/apache/spark/pull/43797#discussion_r1392232962
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -1999,7 +1999,18 @@ class Analyzer(override val catalogManager: CatalogManager) extends RuleExecutor
throw QueryCompilationErrors.groupByPositionRefersToAggregateFunctionError(
index, ordinalExpr)
} else {
- ordinalExpr
+ trimAliases(ordinalExpr) match {
+ // HACK ALERT: If the ordinal expression is also an integer literal, don't use it
+ // but still keep the ordinal literal. The reason is we may repeatedly
+ // analyze the plan. Using a different integer literal may lead to
+ // a repeat GROUP BY ordinal resolution which is wrong. GROUP BY
+ // constant is meaningless so whatever value does not matter here.
+ // TODO: GROUP BY ordinal should pull out grouping expressions to a Project, then
Review Comment:
So this is a temporary fix only?
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-45920][SQL] group by ordinal should be idempotent [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #43797:
URL: https://github.com/apache/spark/pull/43797#issuecomment-1811735675
> Given the following warning, this sounds like this could cause a correctness issue. Did I understand correctly?
Yes, but only when you manipulate logical plans directly. SQL/DataFrame API can't trigger it.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-45920][SQL] group by ordinal should be idempotent [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #43797:
URL: https://github.com/apache/spark/pull/43797#discussion_r1392233357
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -1999,7 +1999,18 @@ class Analyzer(override val catalogManager: CatalogManager) extends RuleExecutor
throw QueryCompilationErrors.groupByPositionRefersToAggregateFunctionError(
index, ordinalExpr)
} else {
- ordinalExpr
+ trimAliases(ordinalExpr) match {
+ // HACK ALERT: If the ordinal expression is also an integer literal, don't use it
+ // but still keep the ordinal literal. The reason is we may repeatedly
+ // analyze the plan. Using a different integer literal may lead to
+ // a repeat GROUP BY ordinal resolution which is wrong. GROUP BY
+ // constant is meaningless so whatever value does not matter here.
+ // TODO: GROUP BY ordinal should pull out grouping expressions to a Project, then
+ // the resolved ordinal expression is always `AttributeReference`.
+ case Literal(_: Int, IntegerType) =>
+ Literal(index)
Review Comment:
I'm only fixing repeated analysis (analyzing an analyzed plan). It's a much bigger topic to support analyzing an optimized plan, so constant folding is not considered here.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-45920][SQL] group by ordinal should be idempotent [spark]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #43797:
URL: https://github.com/apache/spark/pull/43797#discussion_r1393547242
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/SubstituteUnresolvedOrdinalsSuite.scala:
##########
@@ -67,4 +68,22 @@ class SubstituteUnresolvedOrdinalsSuite extends AnalysisTest {
testRelation2.groupBy(Literal(1), Literal(2))($"a", $"b"))
}
}
+
+ test("group by ordinal repeated analysis") {
Review Comment:
+1
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -1999,7 +1999,18 @@ class Analyzer(override val catalogManager: CatalogManager) extends RuleExecutor
throw QueryCompilationErrors.groupByPositionRefersToAggregateFunctionError(
index, ordinalExpr)
} else {
- ordinalExpr
+ trimAliases(ordinalExpr) match {
+ // HACK ALERT: If the ordinal expression is also an integer literal, don't use it
+ // but still keep the ordinal literal. The reason is we may repeatedly
+ // analyze the plan. Using a different integer literal may lead to
+ // a repeat GROUP BY ordinal resolution which is wrong. GROUP BY
+ // constant is meaningless so whatever value does not matter here.
+ // TODO: GROUP BY ordinal should pull out grouping expressions to a Project, then
Review Comment:
+1
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-45920][SQL] group by ordinal should be idempotent [spark]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #43797:
URL: https://github.com/apache/spark/pull/43797#discussion_r1392230893
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -1999,7 +1999,18 @@ class Analyzer(override val catalogManager: CatalogManager) extends RuleExecutor
throw QueryCompilationErrors.groupByPositionRefersToAggregateFunctionError(
index, ordinalExpr)
} else {
- ordinalExpr
+ trimAliases(ordinalExpr) match {
+ // HACK ALERT: If the ordinal expression is also an integer literal, don't use it
+ // but still keep the ordinal literal. The reason is we may repeatedly
+ // analyze the plan. Using a different integer literal may lead to
+ // a repeat GROUP BY ordinal resolution which is wrong. GROUP BY
+ // constant is meaningless so whatever value does not matter here.
+ // TODO: GROUP BY ordinal should pull out grouping expressions to a Project, then
+ // the resolved ordinal expression is always `AttributeReference`.
+ case Literal(_: Int, IntegerType) =>
+ Literal(index)
Review Comment:
Is this only for IntegerType? If `ordinalExpr` is something like cast(long as int) that is foldable?
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-45920][SQL] group by ordinal should be idempotent [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun closed pull request #43797: [SPARK-45920][SQL] group by ordinal should be idempotent
URL: https://github.com/apache/spark/pull/43797
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-45920][SQL] group by ordinal should be idempotent [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #43797:
URL: https://github.com/apache/spark/pull/43797#discussion_r1392866749
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/SubstituteUnresolvedOrdinalsSuite.scala:
##########
@@ -67,4 +68,22 @@ class SubstituteUnresolvedOrdinalsSuite extends AnalysisTest {
testRelation2.groupBy(Literal(1), Literal(2))($"a", $"b"))
}
}
+
+ test("group by ordinal repeated analysis") {
Review Comment:
Shall we add a test prefix `SPARK-45920:`?
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-45920][SQL] group by ordinal should be idempotent [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #43797:
URL: https://github.com/apache/spark/pull/43797#issuecomment-1811877168
BTW, the first commit failed with the following relevant failure. Could you double-check that because the last commit seems irrelevant with that failure, @cloud-fan ?
<img width="467" alt="Screenshot 2023-11-14 at 10 25 16 PM" src="https://github.com/apache/spark/assets/9700541/833c05c1-7a97-45d1-913e-c44bd07c8990">
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-45920][SQL] group by ordinal should be idempotent [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #43797:
URL: https://github.com/apache/spark/pull/43797#issuecomment-1814016497
Merged to master first.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org