-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-48990][SQL] Unified variable related SQL syntax keywords #47469
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
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
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
188 changes: 188 additions & 0 deletions
188
...re/src/test/scala/org/apache/spark/sql/execution/command/DeclareVariableParserSuite.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,188 @@ | ||
| /* | ||
| * 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.execution.command | ||
|
|
||
| import org.apache.spark.sql.catalyst.analysis.{AnalysisTest, UnresolvedAttribute, UnresolvedFunction, UnresolvedIdentifier, UnresolvedInlineTable} | ||
| import org.apache.spark.sql.catalyst.expressions.{Add, Cast, Divide, Literal, ScalarSubquery} | ||
| import org.apache.spark.sql.catalyst.parser.CatalystSqlParser.parsePlan | ||
| import org.apache.spark.sql.catalyst.parser.ParseException | ||
| import org.apache.spark.sql.catalyst.plans.logical.{CreateVariable, DefaultValueExpression, Project, SubqueryAlias} | ||
| import org.apache.spark.sql.test.SharedSparkSession | ||
| import org.apache.spark.sql.types.{Decimal, DecimalType, DoubleType, IntegerType, MapType, NullType, StringType} | ||
| import org.apache.spark.unsafe.types.UTF8String | ||
|
|
||
| class DeclareVariableParserSuite extends AnalysisTest with SharedSparkSession { | ||
|
|
||
| test("declare variable") { | ||
| comparePlans( | ||
| parsePlan("DECLARE var1 INT = 1"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("var1")), | ||
| DefaultValueExpression(Cast(Literal(1, IntegerType), IntegerType), "1"), | ||
| replace = false)) | ||
| comparePlans( | ||
| parsePlan("DECLARE var1 INT"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("var1")), | ||
| DefaultValueExpression(Literal(null, IntegerType), "null"), | ||
| replace = false)) | ||
| comparePlans( | ||
| parsePlan("DECLARE var1 = 1"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("var1")), | ||
| DefaultValueExpression(Literal(1, IntegerType), "1"), | ||
| replace = false)) | ||
| comparePlans( | ||
| parsePlan("DECLARE VARIABLE var1 = 1"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("var1")), | ||
| DefaultValueExpression(Literal(1, IntegerType), "1"), | ||
| replace = false)) | ||
| comparePlans( | ||
| parsePlan("DECLARE VAR var1 = 1"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("var1")), | ||
| DefaultValueExpression(Literal(1, IntegerType), "1"), | ||
| replace = false)) | ||
| comparePlans( | ||
| parsePlan("DECLARE VARIABLE var1 DEFAULT 1"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("var1")), | ||
| DefaultValueExpression(Literal(1, IntegerType), "1"), | ||
| replace = false)) | ||
| comparePlans( | ||
| parsePlan("DECLARE VARIABLE var1 INT DEFAULT 1"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("var1")), | ||
| DefaultValueExpression(Cast(Literal(1, IntegerType), IntegerType), "1"), | ||
| replace = false)) | ||
| comparePlans( | ||
| parsePlan("DECLARE VARIABLE system.session.var1 DEFAULT 1"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("system", "session", "var1")), | ||
| DefaultValueExpression(Literal(1, IntegerType), "1"), | ||
| replace = false)) | ||
| comparePlans( | ||
| parsePlan("DECLARE VARIABLE session.var1 DEFAULT 1"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("session", "var1")), | ||
| DefaultValueExpression(Literal(1, IntegerType), "1"), | ||
| replace = false)) | ||
| comparePlans( | ||
| parsePlan("DECLARE VARIABLE var1 STRING DEFAULT CURRENT_DATABASE()"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("var1")), | ||
| DefaultValueExpression( | ||
| Cast(UnresolvedFunction("CURRENT_DATABASE", Nil, isDistinct = false), StringType), | ||
| "CURRENT_DATABASE()"), | ||
| replace = false)) | ||
| comparePlans( | ||
| parsePlan("DECLARE VARIABLE var1 INT DEFAULT (SELECT c1 FROM VALUES(1) AS T(c1))"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("var1")), | ||
| DefaultValueExpression( | ||
| Cast(ScalarSubquery( | ||
| Project(UnresolvedAttribute("c1") :: Nil, | ||
| SubqueryAlias(Seq("T"), | ||
| UnresolvedInlineTable(Seq("c1"), Seq(Literal(1)) :: Nil)))), IntegerType), | ||
| "(SELECT c1 FROM VALUES(1) AS T(c1))"), | ||
| replace = false)) | ||
| } | ||
|
|
||
| test("declare or replace variable") { | ||
| comparePlans( | ||
| parsePlan("DECLARE OR REPLACE VARIABLE var1 = 1"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("var1")), | ||
| DefaultValueExpression(Literal(1, IntegerType), "1"), | ||
| replace = true)) | ||
| comparePlans( | ||
| parsePlan("DECLARE OR REPLACE VARIABLE var1 DOUBLE DEFAULT 1 + RAND(5)"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("var1")), | ||
| DefaultValueExpression( | ||
| Cast( | ||
| Add(Literal(1, IntegerType), | ||
| UnresolvedFunction("RAND", Seq(Literal(5, IntegerType)), isDistinct = false)), | ||
| DoubleType), | ||
| "1 + RAND(5)"), | ||
| replace = true)) | ||
| comparePlans( | ||
| parsePlan("DECLARE OR REPLACE VARIABLE var1 DEFAULT NULL"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("var1")), | ||
| DefaultValueExpression(Literal(null, NullType), "NULL"), | ||
| replace = true)) | ||
| comparePlans( | ||
| parsePlan("DECLARE OR REPLACE VARIABLE INT DEFAULT 5.0"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("INT")), | ||
| DefaultValueExpression(Literal(Decimal("5.0"), DecimalType(2, 1)), "5.0"), | ||
| replace = true)) | ||
| comparePlans( | ||
| parsePlan("DECLARE OR REPLACE VARIABLE var1 MAP<string, double> " + | ||
| "DEFAULT MAP('Hello', 5.1, 'World', -7.1E10)"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("var1")), | ||
| DefaultValueExpression(Cast( | ||
| UnresolvedFunction("MAP", Seq( | ||
| Literal(UTF8String.fromString("Hello"), StringType), | ||
| Literal(Decimal("5.1"), DecimalType(2, 1)), | ||
| Literal(UTF8String.fromString("World"), StringType), | ||
| Literal(-7.1E10, DoubleType)), isDistinct = false), | ||
| MapType(StringType, DoubleType)), | ||
| "MAP('Hello', 5.1, 'World', -7.1E10)"), | ||
| replace = true)) | ||
| comparePlans( | ||
| parsePlan("DECLARE OR REPLACE VARIABLE var1 INT DEFAULT NULL"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("var1")), | ||
| DefaultValueExpression(Cast(Literal(null, NullType), IntegerType), "NULL"), | ||
| replace = true)) | ||
| comparePlans( | ||
| parsePlan("DECLARE OR REPLACE VARIABLE var1 INT DEFAULT 1 / 0"), | ||
| CreateVariable( | ||
| UnresolvedIdentifier(Seq("var1")), | ||
| DefaultValueExpression(Cast( | ||
| Divide(Literal(1, IntegerType), Literal(0, IntegerType)), IntegerType), | ||
| "1 / 0"), | ||
| replace = true)) | ||
| } | ||
|
|
||
| test("declare variable - not support syntax") { | ||
| // IF NOT EXISTS | ||
| checkError( | ||
| exception = intercept[ParseException] { | ||
| parsePlan("DECLARE VARIABLE IF NOT EXISTS var1 INT") | ||
| }, | ||
| errorClass = "PARSE_SYNTAX_ERROR", | ||
| parameters = Map("error" -> "'EXISTS'", "hint" -> "") | ||
| ) | ||
|
|
||
| // The datatype or default value of a variable must be specified | ||
| val sqlText = "DECLARE VARIABLE var1" | ||
| checkError( | ||
| exception = intercept[ParseException] { | ||
| parsePlan(sqlText) | ||
| }, | ||
| errorClass = "INVALID_SQL_SYNTAX.VARIABLE_TYPE_OR_DEFAULT_REQUIRED", | ||
| parameters = Map.empty, | ||
| context = ExpectedContext(fragment = sqlText, start = 0, stop = 20) | ||
| ) | ||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
56
sql/core/src/test/scala/org/apache/spark/sql/execution/command/DropVariableParserSuite.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,56 @@ | ||
| /* | ||
| * 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.execution.command | ||
|
|
||
| import org.apache.spark.sql.catalyst.analysis.{AnalysisTest, UnresolvedIdentifier} | ||
| import org.apache.spark.sql.catalyst.parser.CatalystSqlParser.parsePlan | ||
| import org.apache.spark.sql.catalyst.parser.ParseException | ||
| import org.apache.spark.sql.catalyst.plans.logical.DropVariable | ||
| import org.apache.spark.sql.test.SharedSparkSession | ||
|
|
||
| class DropVariableParserSuite extends AnalysisTest with SharedSparkSession { | ||
|
|
||
| test("drop variable") { | ||
| comparePlans( | ||
| parsePlan("DROP TEMPORARY VARIABLE var1"), | ||
| DropVariable(UnresolvedIdentifier(Seq("var1")), ifExists = false)) | ||
| comparePlans( | ||
| parsePlan("DROP TEMPORARY VAR var1"), | ||
| DropVariable(UnresolvedIdentifier(Seq("var1")), ifExists = false)) | ||
| comparePlans( | ||
| parsePlan("DROP TEMPORARY VARIABLE IF EXISTS var1"), | ||
| DropVariable(UnresolvedIdentifier(Seq("var1")), ifExists = true)) | ||
| } | ||
|
|
||
| test("drop variable - not support syntax 'DROP VARIABLE|VAR'") { | ||
| checkError( | ||
| exception = intercept[ParseException] { | ||
| parsePlan("DROP VARIABLE var1") | ||
| }, | ||
| errorClass = "PARSE_SYNTAX_ERROR", | ||
| parameters = Map("error" -> "'VARIABLE'", "hint" -> "") | ||
| ) | ||
| checkError( | ||
| exception = intercept[ParseException] { | ||
| parsePlan("DROP VAR var1") | ||
| }, | ||
| errorClass = "PARSE_SYNTAX_ERROR", | ||
| parameters = Map("error" -> "'VAR'", "hint" -> "") | ||
| ) | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update only
one SQL insql-session-variables.sqlso that UT can cover it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we have a parser suite for variable? I think that's a better place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if not maybe we should create one, like
ShowTablesParserSuiteThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parser suitefor variable, let me create one.sql-session-variables.sqlassisted tests available. Do we still need to create some UT likeDeclareVariableSuiteBase,v1\DeclareVariableSuite,v2\DeclareVariableSuite... ?(I'm not sure, it looks a bit
duplicate, does it?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only support session variable today so we don't need cross-test with v1/v2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay