Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class Analyzer(
ResolveRandomSeed ::
TypeCoercion.typeCoercionRules(conf) ++
extendedResolutionRules : _*),
Batch("PostgreSQL Dialect", Once, PostgreSQLDialect.postgreSQLDialectRules: _*),
Batch("Post-Hoc Resolution", Once, postHocResolutionRules: _*),
Batch("Nondeterministic", Once,
PullOutNondeterministic),
Expand Down
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
Expand Up @@ -677,8 +677,9 @@ object TypeCoercion {
case d: Divide if d.dataType == DoubleType => d
case d: Divide if d.dataType.isInstanceOf[DecimalType] => d
case Divide(left, right) if isNumericOrNull(left) && isNumericOrNull(right) =>
val preferIntegralDivision = conf.usePostgreSQLDialect
(left.dataType, right.dataType) match {
case (_: IntegralType, _: IntegralType) if conf.preferIntegralDivision =>
case (_: IntegralType, _: IntegralType) if preferIntegralDivision =>
IntegralDivide(left, right)
case _ =>
Divide(Cast(left, DoubleType), Cast(right, DoubleType))
Expand Down
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})"
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ object StringUtils extends Logging {
"(?s)" + out.result() // (?s) enables dotall mode, causing "." to match new lines
}

// "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)
Set("t", "true", "y", "yes", "1").map(UTF8String.fromString)

private[this] val falseStrings =
Set("false", "fals", "fal", "fa", "f", "no", "n", "off", "of", "0").map(UTF8String.fromString)
Set("f", "false", "n", "no", "0").map(UTF8String.fromString)

// scalastyle:off caselocale
def isTrueString(s: UTF8String): Boolean = trueStrings.contains(s.toLowerCase.trim())
def isFalseString(s: UTF8String): Boolean = falseStrings.contains(s.toLowerCase.trim())
def isTrueString(s: UTF8String): Boolean = trueStrings.contains(s.toLowerCase)

def isFalseString(s: UTF8String): Boolean = falseStrings.contains(s.toLowerCase)
// scalastyle:on caselocale

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copy link
Member

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 pgSQL in sql/core/src/test/resources/sql-tests/inputs/pgSQL, postgreSQL -> pgSQL? Both names is ok, but I like a consistent name.

Copy link
Member Author

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 pgSQL to postgreSQL. I prefer the official full name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, ok to me.

* 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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 " +
Copy link
Member Author

Choose a reason for hiding this comment

The 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")
Expand Down Expand Up @@ -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)

Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1483,15 +1483,15 @@ class TypeCoercionSuite extends AnalysisTest {

test("SPARK-28395 Division operator support integral division") {
val rules = Seq(FunctionArgumentConversion, Division(conf))
Seq(true, false).foreach { preferIntegralDivision =>
withSQLConf(SQLConf.PREFER_INTEGRAL_DIVISION.key -> s"$preferIntegralDivision") {
val result1 = if (preferIntegralDivision) {
Seq(SQLConf.Dialect.SPARK, SQLConf.Dialect.POSTGRESQL).foreach { dialect =>
withSQLConf(SQLConf.DIALECT.key -> dialect.toString) {
val result1 = if (dialect == SQLConf.Dialect.POSTGRESQL) {
IntegralDivide(1L, 1L)
} else {
Divide(Cast(1L, DoubleType), Cast(1L, DoubleType))
}
ruleTest(rules, Divide(1L, 1L), result1)
val result2 = if (preferIntegralDivision) {
val result2 = if (dialect == SQLConf.Dialect.POSTGRESQL) {
IntegralDivide(1, Cast(1, ShortType))
} else {
Divide(Cast(1, DoubleType), Cast(Cast(1, ShortType), DoubleType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,35 +819,23 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
}

test("cast string to boolean") {
checkCast("true", true)
checkCast("tru", true)
checkCast("tr", true)
checkCast("t", true)
checkCast("true", true)
checkCast("tRUe", true)
checkCast(" tRue ", true)
checkCast(" tRu ", true)
checkCast("yes", true)
checkCast("ye", true)
checkCast("y", true)
checkCast("yes", true)
checkCast("1", true)
checkCast("on", true)

checkCast("false", false)
checkCast("fals", false)
checkCast("fal", false)
checkCast("fa", false)
checkCast("f", false)
checkCast(" fAlse ", false)
checkCast(" fAls ", false)
checkCast(" FAlsE ", false)
checkCast("no", false)
checkCast("false", false)
checkCast("FAlsE", false)
checkCast("n", false)
checkCast("no", false)
checkCast("0", false)
checkCast("off", false)
checkCast("of", false)

checkEvaluation(cast("o", BooleanType), null)
checkEvaluation(cast("abc", BooleanType), null)
checkEvaluation(cast("tru", BooleanType), null)
checkEvaluation(cast("fla", BooleanType), null)
checkEvaluation(cast("", BooleanType), null)
}

Expand Down
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)
}
}
Loading