forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-39876][FOLLOW-UP][SQL] Add parser and Dataset tests for SQL UN…
…PIVOT ### What changes were proposed in this pull request? Adds more tests for the SQL `UNPIVOT` clause. apache#37407 (comment) ### Why are the changes needed? Better test coverage. ### Does this PR introduce _any_ user-facing change? No, only more tests and fixing one issue. SQL `UNPIVOT` has not been released yet. ### How was this patch tested? In `UnpivotParserSuite` and `DatasetUnpivotSuite`. Closes apache#38153 from EnricoMi/branch-sql-unpivot-tests. Authored-by: Enrico Minack <github@enrico.minack.dev> Signed-off-by: Wenchen Fan <wenchen@databricks.com>
- Loading branch information
1 parent
6c6dd01
commit ca85f86
Showing
4 changed files
with
327 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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
195 changes: 195 additions & 0 deletions
195
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/UnpivotParserSuite.scala
This file contains 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,195 @@ | ||
/* | ||
* 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.catalyst.parser | ||
|
||
import org.apache.spark.sql.catalyst.analysis._ | ||
import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, Unpivot} | ||
|
||
class UnpivotParserSuite extends AnalysisTest { | ||
|
||
import CatalystSqlParser._ | ||
import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
import org.apache.spark.sql.catalyst.dsl.plans._ | ||
|
||
private def assertEqual(sqlCommand: String, plan: LogicalPlan): Unit = { | ||
comparePlans(parsePlan(sqlCommand), plan, checkAnalysis = false) | ||
} | ||
|
||
private def intercept(sqlCommand: String, errorClass: Option[String], messages: String*): Unit = | ||
interceptParseException(parsePlan)(sqlCommand, messages: _*)(errorClass) | ||
|
||
test("unpivot - single value") { | ||
assertEqual( | ||
"SELECT * FROM t UNPIVOT (val FOR col in (a, b))", | ||
Unpivot( | ||
None, | ||
Some(Seq(Seq(UnresolvedAlias($"a")), Seq(UnresolvedAlias($"b")))), | ||
None, | ||
"col", | ||
Seq("val"), | ||
table("t")) | ||
.where(coalesce($"val").isNotNull) | ||
.select(star()) | ||
) | ||
} | ||
|
||
test("unpivot - single value with alias") { | ||
Seq( | ||
"SELECT * FROM t UNPIVOT (val FOR col in (a A, b))", | ||
"SELECT * FROM t UNPIVOT (val FOR col in (a AS A, b))" | ||
).foreach { sql => | ||
withClue(sql) { | ||
assertEqual( | ||
sql, | ||
Unpivot( | ||
None, | ||
Some(Seq(Seq(UnresolvedAlias($"a")), Seq(UnresolvedAlias($"b")))), | ||
Some(Seq(Some("A"), None)), | ||
"col", | ||
Seq("val"), | ||
table("t")) | ||
.where(coalesce($"val").isNotNull) | ||
.select(star()) | ||
) | ||
} | ||
} | ||
} | ||
|
||
test("unpivot - multiple values") { | ||
assertEqual( | ||
"SELECT * FROM t UNPIVOT ((val1, val2) FOR col in ((a, b), (c, d)))", | ||
Unpivot( | ||
None, | ||
Some(Seq(Seq($"a", $"b").map(UnresolvedAlias(_)), Seq($"c", $"d").map(UnresolvedAlias(_)))), | ||
None, | ||
"col", | ||
Seq("val1", "val2"), | ||
table("t")) | ||
.where(coalesce($"val1", $"val2").isNotNull) | ||
.select(star()) | ||
) | ||
} | ||
|
||
test("unpivot - multiple values with alias") { | ||
Seq( | ||
"SELECT * FROM t UNPIVOT ((val1, val2) FOR col in ((a, b) first, (c, d)))", | ||
"SELECT * FROM t UNPIVOT ((val1, val2) FOR col in ((a, b) AS first, (c, d)))" | ||
).foreach { sql => | ||
withClue(sql) { | ||
assertEqual( | ||
sql, | ||
Unpivot( | ||
None, | ||
Some(Seq( | ||
Seq($"a", $"b").map(UnresolvedAlias(_)), | ||
Seq($"c", $"d").map(UnresolvedAlias(_)) | ||
)), | ||
Some(Seq(Some("first"), None)), | ||
"col", | ||
Seq("val1", "val2"), | ||
table("t")) | ||
.where(coalesce($"val1", $"val2").isNotNull) | ||
.select(star()) | ||
) | ||
} | ||
} | ||
} | ||
|
||
test("unpivot - multiple values with inner alias") { | ||
Seq( | ||
"SELECT * FROM t UNPIVOT ((val1, val2) FOR col in ((a A, b), (c, d)))", | ||
"SELECT * FROM t UNPIVOT ((val1, val2) FOR col in ((a AS A, b), (c, d)))" | ||
).foreach { sql => | ||
withClue(sql) { | ||
intercept(sql, Some("PARSE_SYNTAX_ERROR"), "Syntax error at or near ") | ||
} | ||
} | ||
} | ||
|
||
test("unpivot - alias") { | ||
Seq( | ||
"SELECT up.* FROM t UNPIVOT (val FOR col in (a, b)) up", | ||
"SELECT up.* FROM t UNPIVOT (val FOR col in (a, b)) AS up" | ||
).foreach { sql => | ||
withClue(sql) { | ||
assertEqual( | ||
sql, | ||
Unpivot( | ||
None, | ||
Some(Seq(Seq(UnresolvedAlias($"a")), Seq(UnresolvedAlias($"b")))), | ||
None, | ||
"col", | ||
Seq("val"), | ||
table("t")) | ||
.where(coalesce($"val").isNotNull) | ||
.subquery("up") | ||
.select(star("up")) | ||
) | ||
} | ||
} | ||
} | ||
|
||
test("unpivot - no unpivot value names") { | ||
intercept( | ||
"SELECT * FROM t UNPIVOT (() FOR col in ((a, b), (c, d)))", | ||
Some("PARSE_SYNTAX_ERROR"), "Syntax error at or near " | ||
) | ||
} | ||
|
||
test("unpivot - no unpivot columns") { | ||
Seq( | ||
"SELECT * FROM t UNPIVOT (val FOR col in ())", | ||
"SELECT * FROM t UNPIVOT ((val1, val2) FOR col in ())", | ||
"SELECT * FROM t UNPIVOT ((val1, val2) FOR col in (()))" | ||
).foreach { sql => | ||
withClue(sql) { | ||
intercept(sql, Some("PARSE_SYNTAX_ERROR"), "Syntax error at or near ") | ||
} | ||
} | ||
} | ||
|
||
test("unpivot - exclude nulls") { | ||
assertEqual( | ||
"SELECT * FROM t UNPIVOT EXCLUDE NULLS (val FOR col in (a, b))", | ||
Unpivot( | ||
None, | ||
Some(Seq(Seq(UnresolvedAlias($"a")), Seq(UnresolvedAlias($"b")))), | ||
None, | ||
"col", | ||
Seq("val"), | ||
table("t")) | ||
.where(coalesce($"val").isNotNull) | ||
.select(star()) | ||
) | ||
} | ||
|
||
test("unpivot - include nulls") { | ||
assertEqual( | ||
"SELECT * FROM t UNPIVOT INCLUDE NULLS (val FOR col in (a, b))", | ||
Unpivot( | ||
None, | ||
Some(Seq(Seq(UnresolvedAlias($"a")), Seq(UnresolvedAlias($"b")))), | ||
None, | ||
"col", | ||
Seq("val"), | ||
table("t")) | ||
.select(star()) | ||
) | ||
} | ||
|
||
} |
This file contains 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