-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[SPARK-29837][SQL] PostgreSQL dialect: cast to boolean #26463
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
17 commits
Select commit
Hold shift + click to select a range
75d51ba
postgresql dialect: cast to boolean
Ngone51 53c9a9b
fix PostgreSQLDialectQuerySuite
Ngone51 2ab1847
fix postgreSQL/boolean.sql
Ngone51 6c2c0c7
nullable should be false now
Ngone51 6ad100c
check unsupported types in checkInputDataTypes()
Ngone51 101a026
private[catalyst] -> protected[this]
Ngone51 be08daf
remove unused import
Ngone51 95caf40
return TypeCheckSuccess
Ngone51 8ebd5aa
use collect()
Ngone51 0b9a78d
use child.nullable
Ngone51 fda48c0
excludes unsupported types explicitly
Ngone51 36f60ee
protected[this] -> protected
Ngone51 b7c6baf
throw exception for PostgreCastToBoolean.ansiEnabled
Ngone51 b9711ae
include supportted types explicitly
Ngone51 68c7a13
adjust tests
Ngone51 999ec61
missing BooleanType
Ngone51 a178b79
leave cast( bool as bool) to optimizer
Ngone51 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
80 changes: 0 additions & 80 deletions
80
...ala/org/apache/spark/sql/catalyst/expressions/postgreSQL/PostgreCastStringToBoolean.scala
This file was deleted.
Oops, something went wrong.
83 changes: 83 additions & 0 deletions
83
...ain/scala/org/apache/spark/sql/catalyst/expressions/postgreSQL/PostgreCastToBoolean.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,83 @@ | ||
/* | ||
* 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.{CastBase, Expression, TimeZoneAwareExpression} | ||
import org.apache.spark.sql.catalyst.expressions.codegen.Block._ | ||
import org.apache.spark.sql.catalyst.util.postgreSQL.StringUtils | ||
import org.apache.spark.sql.types._ | ||
import org.apache.spark.unsafe.types.UTF8String | ||
|
||
case class PostgreCastToBoolean(child: Expression, timeZoneId: Option[String]) | ||
extends CastBase { | ||
|
||
override protected def ansiEnabled = | ||
throw new UnsupportedOperationException("PostgreSQL dialect doesn't support ansi mode") | ||
|
||
override def withTimeZone(timeZoneId: String): TimeZoneAwareExpression = | ||
copy(timeZoneId = Option(timeZoneId)) | ||
|
||
override def checkInputDataTypes(): TypeCheckResult = child.dataType match { | ||
case StringType | IntegerType | NullType => | ||
TypeCheckResult.TypeCheckSuccess | ||
case _ => | ||
TypeCheckResult.TypeCheckFailure(s"cannot cast type ${child.dataType} to boolean") | ||
} | ||
|
||
override def castToBoolean(from: DataType): Any => Any = from match { | ||
case StringType => | ||
buildCast[UTF8String](_, str => { | ||
val s = str.trim().toLowerCase() | ||
if (StringUtils.isTrueString(s)) { | ||
true | ||
} else if (StringUtils.isFalseString(s)) { | ||
false | ||
} else { | ||
throw new IllegalArgumentException(s"invalid input syntax for type boolean: $s") | ||
} | ||
}) | ||
case IntegerType => | ||
super.castToBoolean(from) | ||
} | ||
|
||
override def castToBooleanCode(from: DataType): CastFunction = from match { | ||
case StringType => | ||
val stringUtils = inline"${StringUtils.getClass.getName.stripSuffix("$")}" | ||
(c, evPrim, evNull) => | ||
code""" | ||
if ($stringUtils.isTrueString($c.trim().toLowerCase())) { | ||
$evPrim = true; | ||
} else if ($stringUtils.isFalseString($c.trim().toLowerCase())) { | ||
$evPrim = false; | ||
} else { | ||
throw new IllegalArgumentException("invalid input syntax for type boolean: $c"); | ||
} | ||
""" | ||
|
||
case IntegerType => | ||
super.castToBooleanCode(from) | ||
} | ||
|
||
override def dataType: DataType = BooleanType | ||
|
||
override def nullable: Boolean = child.nullable | ||
|
||
override def toString: String = s"PostgreCastToBoolean($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
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.
This looks dangerous. Whenever the code checked the flag ansiEnabled, we will throw an exception. We are removing the pgSQL dialect, this will be updated anyway.