-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-28997][SQL] Add spark.sql.dialect
#25697
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
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0e7f281
dialect
gengliangwang 094d58e
new package and expression
gengliangwang 9f14680
revise
gengliangwang 5a69400
address comments
gengliangwang bb657e9
fix test failure
gengliangwang 82a9e4d
revise
gengliangwang 72a1539
add NullIntolerant trait
gengliangwang af97b99
address comments
gengliangwang b518114
address more comments
gengliangwang 75aeda7
one more comment
gengliangwang 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
49 changes: 49 additions & 0 deletions
49
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/PostgreSQLDialect.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,49 @@ | ||
| /* | ||
| * 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.analysis | ||
|
|
||
| import org.apache.spark.internal.Logging | ||
| import org.apache.spark.sql.catalyst.expressions.Cast | ||
| import org.apache.spark.sql.catalyst.expressions.postgreSQL.PostgreCastStringToBoolean | ||
| import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.types.{BooleanType, StringType} | ||
|
|
||
| object PostgreSQLDialect { | ||
| val postgreSQLDialectRules: List[Rule[LogicalPlan]] = | ||
| CastStringToBoolean :: | ||
| Nil | ||
|
|
||
| object CastStringToBoolean extends Rule[LogicalPlan] with Logging { | ||
| override def apply(plan: LogicalPlan): LogicalPlan = { | ||
| // The SQL configuration `spark.sql.dialect` can be changed in runtime. | ||
| // To make sure the configuration is effective, we have to check it during rule execution. | ||
| val conf = SQLConf.get | ||
| if (conf.usePostgreSQLDialect) { | ||
| plan.transformExpressions { | ||
| case Cast(child, dataType, _) | ||
| if dataType == BooleanType && child.dataType == StringType => | ||
| PostgreCastStringToBoolean(child) | ||
| } | ||
| } else { | ||
| plan | ||
| } | ||
| } | ||
| } | ||
| } |
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
80 changes: 80 additions & 0 deletions
80
...ala/org/apache/spark/sql/catalyst/expressions/postgreSQL/PostgreCastStringToBoolean.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,80 @@ | ||
| /* | ||
| * 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.expressions.postgreSQL | ||
|
|
||
| import org.apache.spark.sql.catalyst.analysis.TypeCheckResult | ||
| import org.apache.spark.sql.catalyst.expressions.{Expression, NullIntolerant, UnaryExpression} | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode, JavaCode} | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.Block._ | ||
| import org.apache.spark.sql.catalyst.util.postgreSQL.StringUtils | ||
| import org.apache.spark.sql.types.{BooleanType, DataType, StringType} | ||
| import org.apache.spark.unsafe.types.UTF8String | ||
|
|
||
| case class PostgreCastStringToBoolean(child: Expression) | ||
| extends UnaryExpression with NullIntolerant { | ||
|
|
||
| override def checkInputDataTypes(): TypeCheckResult = { | ||
| if (child.dataType == StringType) { | ||
| TypeCheckResult.TypeCheckSuccess | ||
| } else { | ||
| TypeCheckResult.TypeCheckFailure( | ||
| s"The expression ${getClass.getSimpleName} only accepts string input data type") | ||
| } | ||
| } | ||
|
|
||
| override def nullSafeEval(input: Any): Any = { | ||
| val s = input.asInstanceOf[UTF8String].trim().toLowerCase() | ||
| if (StringUtils.isTrueString(s)) { | ||
| true | ||
| } else if (StringUtils.isFalseString(s)) { | ||
| false | ||
| } else { | ||
| null | ||
| } | ||
| } | ||
|
|
||
| override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
| val stringUtils = inline"${StringUtils.getClass.getName.stripSuffix("$")}" | ||
| val eval = child.genCode(ctx) | ||
| val javaType = JavaCode.javaType(dataType) | ||
| val preprocessedString = ctx.freshName("preprocessedString") | ||
| val castCode = | ||
| code""" | ||
| boolean ${ev.isNull} = ${eval.isNull}; | ||
| $javaType ${ev.value} = false; | ||
| if (!${eval.isNull}) { | ||
| UTF8String $preprocessedString = ${eval.value}.trim().toLowerCase(); | ||
| if ($stringUtils.isTrueString($preprocessedString)) { | ||
| ${ev.value} = true; | ||
| } else if ($stringUtils.isFalseString($preprocessedString)) { | ||
| ${ev.value} = false; | ||
| } else { | ||
| ${ev.isNull} = true; | ||
| } | ||
| } | ||
| """ | ||
| ev.copy(code = eval.code + castCode) | ||
| } | ||
|
|
||
| override def dataType: DataType = BooleanType | ||
|
|
||
| override def nullable: Boolean = true | ||
|
|
||
| override def toString: String = s"PostgreCastStringToBoolean($child as ${dataType.simpleString})" | ||
|
|
||
| override def sql: String = s"CAST(${child.sql} AS ${dataType.sql})" | ||
| } |
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
33 changes: 33 additions & 0 deletions
33
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/postgreSQL/StringUtils.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,33 @@ | ||
| /* | ||
| * 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.util.postgreSQL | ||
|
|
||
| import org.apache.spark.unsafe.types.UTF8String | ||
|
|
||
| object StringUtils { | ||
| // "true", "yes", "1", "false", "no", "0", and unique prefixes of these strings are accepted. | ||
| private[this] val trueStrings = | ||
| Set("true", "tru", "tr", "t", "yes", "ye", "y", "on", "1").map(UTF8String.fromString) | ||
|
|
||
| private[this] val falseStrings = | ||
| Set("false", "fals", "fal", "fa", "f", "no", "n", "off", "of", "0").map(UTF8String.fromString) | ||
|
|
||
| def isTrueString(s: UTF8String): Boolean = trueStrings.contains(s) | ||
|
|
||
| def isFalseString(s: UTF8String): Boolean = falseStrings.contains(s) | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -1589,12 +1589,22 @@ object SQLConf { | |
| .booleanConf | ||
| .createWithDefault(false) | ||
|
|
||
| val PREFER_INTEGRAL_DIVISION = buildConf("spark.sql.function.preferIntegralDivision") | ||
| .internal() | ||
| .doc("When true, will perform integral division with the / operator " + | ||
| "if both sides are integral types. This is for PostgreSQL test cases only.") | ||
| .booleanConf | ||
| .createWithDefault(false) | ||
| object Dialect extends Enumeration { | ||
| val SPARK, POSTGRESQL = Value | ||
| } | ||
|
|
||
| val DIALECT = | ||
| buildConf("spark.sql.dialect") | ||
| .doc("The specific features of the SQL language to be adopted, which are available when " + | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's have a follow-up PR to add wiki page for the PostgreSQL dialect behaviors. |
||
| "accessing the given database. Currently, Spark supports two database dialects, `Spark` " + | ||
| "and `PostgreSQL`. With `PostgreSQL` dialect, Spark will: " + | ||
| "1. perform integral division with the / operator if both sides are integral types; " + | ||
| "2. accept \"true\", \"yes\", \"1\", \"false\", \"no\", \"0\", and unique prefixes as " + | ||
| "input and trim input for the boolean data type.") | ||
| .stringConf | ||
| .transform(_.toUpperCase(Locale.ROOT)) | ||
| .checkValues(Dialect.values.map(_.toString)) | ||
| .createWithDefault(Dialect.SPARK.toString) | ||
|
|
||
| val ALLOW_CREATING_MANAGED_TABLE_USING_NONEMPTY_LOCATION = | ||
| buildConf("spark.sql.legacy.allowCreatingManagedTableUsingNonemptyLocation") | ||
|
|
@@ -2418,8 +2428,6 @@ class SQLConf extends Serializable with Logging { | |
|
|
||
| def eltOutputAsString: Boolean = getConf(ELT_OUTPUT_AS_STRING) | ||
|
|
||
| def preferIntegralDivision: Boolean = getConf(PREFER_INTEGRAL_DIVISION) | ||
|
|
||
| def allowCreatingManagedTableUsingNonemptyLocation: Boolean = | ||
| getConf(ALLOW_CREATING_MANAGED_TABLE_USING_NONEMPTY_LOCATION) | ||
|
|
||
|
|
@@ -2433,6 +2441,8 @@ class SQLConf extends Serializable with Logging { | |
|
|
||
| def ansiEnabled: Boolean = getConf(ANSI_ENABLED) | ||
|
|
||
| def usePostgreSQLDialect: Boolean = getConf(DIALECT) == Dialect.POSTGRESQL.toString() | ||
|
|
||
| def nestedSchemaPruningEnabled: Boolean = getConf(NESTED_SCHEMA_PRUNING_ENABLED) | ||
|
|
||
| def serializerNestedSchemaPruningEnabled: Boolean = | ||
|
|
||
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
59 changes: 59 additions & 0 deletions
59
...alyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/postgreSQL/CastSuite.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,59 @@ | ||
| /* | ||
| * 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.expressions.postgreSQL | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.catalyst.expressions.{ExpressionEvalHelper, Literal} | ||
|
|
||
| class CastSuite extends SparkFunSuite with ExpressionEvalHelper { | ||
| private def checkPostgreCastStringToBoolean(v: Any, expected: Any): Unit = { | ||
| checkEvaluation(PostgreCastStringToBoolean(Literal(v)), expected) | ||
| } | ||
|
|
||
| test("cast string to boolean") { | ||
| checkPostgreCastStringToBoolean("true", true) | ||
| checkPostgreCastStringToBoolean("tru", true) | ||
| checkPostgreCastStringToBoolean("tr", true) | ||
| checkPostgreCastStringToBoolean("t", true) | ||
| checkPostgreCastStringToBoolean("tRUe", true) | ||
| checkPostgreCastStringToBoolean(" tRue ", true) | ||
| checkPostgreCastStringToBoolean(" tRu ", true) | ||
| checkPostgreCastStringToBoolean("yes", true) | ||
| checkPostgreCastStringToBoolean("ye", true) | ||
| checkPostgreCastStringToBoolean("y", true) | ||
| checkPostgreCastStringToBoolean("1", true) | ||
| checkPostgreCastStringToBoolean("on", true) | ||
|
|
||
| checkPostgreCastStringToBoolean("false", false) | ||
| checkPostgreCastStringToBoolean("fals", false) | ||
| checkPostgreCastStringToBoolean("fal", false) | ||
| checkPostgreCastStringToBoolean("fa", false) | ||
| checkPostgreCastStringToBoolean("f", false) | ||
| checkPostgreCastStringToBoolean(" fAlse ", false) | ||
| checkPostgreCastStringToBoolean(" fAls ", false) | ||
| checkPostgreCastStringToBoolean(" FAlsE ", false) | ||
| checkPostgreCastStringToBoolean("no", false) | ||
| checkPostgreCastStringToBoolean("n", false) | ||
| checkPostgreCastStringToBoolean("0", false) | ||
| checkPostgreCastStringToBoolean("off", false) | ||
| checkPostgreCastStringToBoolean("of", false) | ||
|
|
||
| checkPostgreCastStringToBoolean("o", null) | ||
| checkPostgreCastStringToBoolean("abc", null) | ||
| checkPostgreCastStringToBoolean("", null) | ||
| } | ||
| } |
Oops, something went wrong.
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.
nit: Since we already have the dir named
pgSQLinsql/core/src/test/resources/sql-tests/inputs/pgSQL,postgreSQL->pgSQL? Both names is ok, but I like a consistent name.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.
How about a follow-up to change
pgSQLtopostgreSQL. I prefer the official full name.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.
Yea, ok to me.