-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-51820][SQL][CONNECT] Address remaining issues for new group/order by ordinal approach
#50699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
mihailotim-db
wants to merge
1
commit into
apache:master
from
mihailotim-db:mihailotim-db/fix_ordinal_followup
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...rc/test/scala/org/apache/spark/sql/ReplaceIntegerLiteralsWithOrdinalsDataframeSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* | ||
| * 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.spark.sql | ||
|
|
||
| import org.apache.spark.sql.catalyst.analysis.UnresolvedOrdinal | ||
| import org.apache.spark.sql.catalyst.expressions.SortOrder | ||
| import org.apache.spark.sql.functions.lit | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.test.SharedSparkSession | ||
|
|
||
| class ReplaceIntegerLiteralsWithOrdinalsDataframeSuite extends QueryTest with SharedSparkSession { | ||
mihailotim-db marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| test("Group by ordinal - Dataframe") { | ||
| val query = "SELECT * FROM VALUES(1,2),(1,3),(2,4)" | ||
|
|
||
| withSQLConf(SQLConf.GROUP_BY_ORDINAL.key -> "true") { | ||
| val groupedDataset = sql(query).groupBy(lit(1)) | ||
|
|
||
| assert(groupedDataset.groupingExprs.collect { | ||
| case ordinal @ UnresolvedOrdinal(1) => ordinal | ||
| }.nonEmpty) | ||
|
|
||
| checkError( | ||
| exception = intercept[AnalysisException](sql(query).groupBy(lit(-1)).count()), | ||
| condition = "GROUP_BY_POS_OUT_OF_RANGE", | ||
| parameters = Map("index" -> "-1", "size" -> "2"), | ||
| context = ExpectedContext(fragment = "lit", getCurrentClassCallSitePattern) | ||
| ) | ||
| } | ||
|
|
||
| withSQLConf(SQLConf.GROUP_BY_ORDINAL.key -> "false") { | ||
| val groupedDataset = sql(query).groupBy(lit(1)) | ||
|
|
||
| assert(groupedDataset.groupingExprs.collect { | ||
| case ordinal @ UnresolvedOrdinal(1) => ordinal | ||
| }.isEmpty) | ||
| } | ||
| } | ||
|
|
||
| test("Order/Sort by ordinal - Dataframe") { | ||
| val sqlText = "SELECT * FROM VALUES(2,1),(1,2)" | ||
|
|
||
| withSQLConf(SQLConf.ORDER_BY_ORDINAL.key -> "true") { | ||
| val queries = Seq( | ||
| sql(sqlText).orderBy(lit(1)), | ||
| sql(sqlText).sort(lit(1)) | ||
| ) | ||
|
|
||
| for (query <- queries) { | ||
| val unresolvedPlan = query.queryExecution.logical | ||
| val resolvedPlan = query.queryExecution.analyzed | ||
|
|
||
| assert(unresolvedPlan.expressions.collect { | ||
| case ordinal @ SortOrder(UnresolvedOrdinal(1), _, _, _) => ordinal | ||
| }.nonEmpty) | ||
|
|
||
| assert(resolvedPlan.expressions.collect { | ||
| case ordinal @ SortOrder(UnresolvedOrdinal(1), _, _, _) => ordinal | ||
| }.isEmpty) | ||
|
|
||
| checkAnswer(query, Row(1, 2) :: Row(2, 1) :: Nil) | ||
| } | ||
|
|
||
| checkError( | ||
| exception = intercept[AnalysisException](sql(sqlText).orderBy(lit(-1))), | ||
| condition = "ORDER_BY_POS_OUT_OF_RANGE", | ||
| parameters = Map("index" -> "-1", "size" -> "2") | ||
| ) | ||
|
|
||
| checkError( | ||
| exception = intercept[AnalysisException](sql(sqlText).sort(lit(-1))), | ||
| condition = "ORDER_BY_POS_OUT_OF_RANGE", | ||
| parameters = Map("index" -> "-1", "size" -> "2") | ||
| ) | ||
| } | ||
|
|
||
| withSQLConf(SQLConf.ORDER_BY_ORDINAL.key -> "false") { | ||
| val queries = Seq( | ||
| sql(sqlText).orderBy(lit(1)), | ||
| sql(sqlText).sort(lit(1)) | ||
| ) | ||
|
|
||
| for (query <- queries) { | ||
| val unresolvedPlan = query.queryExecution.logical | ||
| val resolvedPlan = query.queryExecution.analyzed | ||
|
|
||
| assert(unresolvedPlan.expressions.collect { | ||
| case ordinal @ SortOrder(UnresolvedOrdinal(1), _, _, _) => ordinal | ||
| }.isEmpty) | ||
|
|
||
| assert(resolvedPlan.expressions.collect { | ||
| case ordinal @ SortOrder(UnresolvedOrdinal(1), _, _, _) => ordinal | ||
| }.isEmpty) | ||
|
|
||
| checkAnswer(query, Row(2, 1) :: Row(1, 2) :: Nil) | ||
| } | ||
|
|
||
| checkAnswer(sql(sqlText).orderBy(lit(-1)), Row(2, 1) :: Row(1, 2) :: Nil) | ||
|
|
||
| checkAnswer(sql(sqlText).sort(lit(-1)), Row(2, 1) :: Row(1, 2) :: Nil) | ||
| } | ||
| } | ||
| } | ||
115 changes: 115 additions & 0 deletions
115
...core/src/test/scala/org/apache/spark/sql/ReplaceIntegerLiteralsWithOrdinalsSqlSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| * 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.spark.sql | ||
|
|
||
| import org.apache.spark.sql.catalyst.analysis.UnresolvedOrdinal | ||
| import org.apache.spark.sql.catalyst.expressions.SortOrder | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.test.SharedSparkSession | ||
|
|
||
| class ReplaceIntegerLiteralsWithOrdinalsSqlSuite extends QueryTest with SharedSparkSession { | ||
|
|
||
| test("Group by ordinal - SQL") { | ||
| val correctSqlText = "SELECT col1, max(col2) FROM VALUES(1,2),(1,3),(2,4) GROUP BY 1" | ||
| val groupByPosOutOfRangeSqlText = | ||
| "SELECT col1, max(col2) FROM VALUES(1,2),(1,3),(2,4) GROUP BY -1" | ||
|
|
||
| withSQLConf(SQLConf.GROUP_BY_ORDINAL.key -> "true") { | ||
| val query = sql(correctSqlText) | ||
| val parsedPlan = query.queryExecution.logical | ||
| val analyzedPlan = query.queryExecution.analyzed | ||
|
|
||
| assert(parsedPlan.expressions.collect { | ||
| case ordinal @ UnresolvedOrdinal(1) => ordinal | ||
| }.nonEmpty) | ||
|
|
||
| assert(analyzedPlan.expressions.collect { | ||
| case ordinal @ UnresolvedOrdinal(1) => ordinal | ||
| }.isEmpty) | ||
|
|
||
| checkAnswer(query, Row(1, 3) :: Row(2, 4) :: Nil) | ||
|
|
||
| checkError( | ||
| exception = intercept[AnalysisException](sql(groupByPosOutOfRangeSqlText)), | ||
| condition = "GROUP_BY_POS_OUT_OF_RANGE", | ||
| parameters = Map("index" -> "-1", "size" -> "2"), | ||
| queryContext = Array(ExpectedContext(fragment = "-1", start = 61, stop = 62)) | ||
| ) | ||
| } | ||
|
|
||
| withSQLConf(SQLConf.GROUP_BY_ORDINAL.key -> "false") { | ||
| val parsedPlan = spark.sessionState.sqlParser.parsePlan(correctSqlText) | ||
|
|
||
| assert(parsedPlan.expressions.collect { | ||
| case ordinal @ UnresolvedOrdinal(1) => ordinal | ||
| }.isEmpty) | ||
|
|
||
| checkError( | ||
| exception = intercept[AnalysisException](sql(groupByPosOutOfRangeSqlText)), | ||
| condition = "MISSING_AGGREGATION", | ||
| parameters = Map("expression" -> "\"col1\"", "expressionAnyValue" -> "\"any_value(col1)\"") | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| test("Order by ordinal - SQL") { | ||
| val correctSqlText = "SELECT col1 FROM VALUES(2,1),(1,2) ORDER BY 1" | ||
| val orderByPosOutOfRangeSqlText = "SELECT col1 FROM VALUES(2,1),(1,2) ORDER BY -1" | ||
|
|
||
| withSQLConf(SQLConf.ORDER_BY_ORDINAL.key -> "true") { | ||
| val query = sql(correctSqlText) | ||
| val parsedPlan = query.queryExecution.logical | ||
| val analyzedPlan = query.queryExecution.analyzed | ||
|
|
||
| assert(parsedPlan.expressions.collect { | ||
| case ordinal @ SortOrder(UnresolvedOrdinal(1), _, _, _) => ordinal | ||
| }.nonEmpty) | ||
|
|
||
| assert(analyzedPlan.expressions.collect { | ||
| case ordinal @ SortOrder(UnresolvedOrdinal(1), _, _, _) => ordinal | ||
| }.isEmpty) | ||
|
|
||
| checkAnswer(query, Row(1) :: Row(2) :: Nil) | ||
|
|
||
| checkError( | ||
| exception = intercept[AnalysisException](sql(orderByPosOutOfRangeSqlText)), | ||
| condition = "ORDER_BY_POS_OUT_OF_RANGE", | ||
| parameters = Map("index" -> "-1", "size" -> "1"), | ||
| queryContext = Array(ExpectedContext(fragment = "-1", start = 44, stop = 45)) | ||
| ) | ||
| } | ||
|
|
||
| withSQLConf(SQLConf.ORDER_BY_ORDINAL.key -> "false") { | ||
| val query = sql(correctSqlText) | ||
| val parsedPlan = query.queryExecution.logical | ||
| val analyzedPlan = query.queryExecution.analyzed | ||
|
|
||
| assert(parsedPlan.expressions.collect { | ||
| case ordinal @ SortOrder(UnresolvedOrdinal(1), _, _, _) => ordinal | ||
| }.isEmpty) | ||
|
|
||
| assert(analyzedPlan.expressions.collect { | ||
| case ordinal @ SortOrder(UnresolvedOrdinal(1), _, _, _) => ordinal | ||
| }.isEmpty) | ||
|
|
||
| checkAnswer(query, Row(2) :: Row(1) :: Nil) | ||
|
|
||
| checkAnswer(sql(orderByPosOutOfRangeSqlText), Row(2) :: Row(1) :: Nil) | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.