-
Notifications
You must be signed in to change notification settings - Fork 29k
[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
Changes from 8 commits
0e7f281
094d58e
9f14680
5a69400
bb657e9
82a9e4d
72a1539
af97b99
b518114
75aeda7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /* | ||
| * 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] | ||
| 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 castCode = | ||
| code""" | ||
| boolean ${ev.isNull} = ${eval.isNull}; | ||
| $javaType ${ev.value} = false; | ||
| if (!${eval.isNull}) { | ||
| if ($stringUtils.isTrueString(${eval.value})) { | ||
| ${ev.value} = true; | ||
| } else if ($stringUtils.isFalseString(${eval.value})) { | ||
| ${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})" | ||
| } |
| 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.trim().toLowerCase()) | ||
|
||
|
|
||
| def isFalseString(s: UTF8String): Boolean = falseStrings.contains(s.trim().toLowerCase()) | ||
| } | ||
| 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 " + | ||
|
Member
Author
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 = | ||
|
|
||
| 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) | ||
| } | ||
| } |
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.