-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-16730][SQL] Implement function aliases for type casts #14362
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 all commits
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.sql.catalyst.expressions.Cast | ||
| import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| /** | ||
| * An analyzer rule that handles function aliases. | ||
| */ | ||
| object SubstituteFunctionAliases extends Rule[LogicalPlan] { | ||
| def apply(plan: LogicalPlan): LogicalPlan = plan.resolveExpressions { | ||
| // SPARK-16730: The following functions are aliases for cast in Hive. | ||
| case u: UnresolvedFunction | ||
| if u.name.database.isEmpty && u.children.size == 1 && !u.isDistinct => | ||
| u.name.funcName.toLowerCase match { | ||
| case "boolean" => Cast(u.children.head, BooleanType) | ||
| case "tinyint" => Cast(u.children.head, ByteType) | ||
| case "smallint" => Cast(u.children.head, ShortType) | ||
| case "int" => Cast(u.children.head, IntegerType) | ||
| case "bigint" => Cast(u.children.head, LongType) | ||
| case "float" => Cast(u.children.head, FloatType) | ||
| case "double" => Cast(u.children.head, DoubleType) | ||
| case "decimal" => Cast(u.children.head, DecimalType.USER_DEFAULT) | ||
|
Contributor
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. I'm using whatever cast as decimal is using here, but I think it is a bug to by default cast to USER_DEFAULT, which has scale = 0. |
||
| case "date" => Cast(u.children.head, DateType) | ||
| case "timestamp" => Cast(u.children.head, TimestampType) | ||
| case "binary" => Cast(u.children.head, BinaryType) | ||
| case "string" => Cast(u.children.head, StringType) | ||
| case _ => u | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /* | ||
| * 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.sql.catalyst.FunctionIdentifier | ||
| import org.apache.spark.sql.catalyst.expressions.{Cast, Expression, Literal} | ||
| import org.apache.spark.sql.catalyst.plans.PlanTest | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| /** Unit tests for [[SubstituteFunctionAliases]]. */ | ||
| class SubstituteFunctionAliasesSuite extends PlanTest { | ||
|
|
||
| private def ruleTest(initial: Expression, transformed: Expression): Unit = { | ||
| ruleTest(SubstituteFunctionAliases, initial, transformed) | ||
| } | ||
|
|
||
| private def func(name: String, arg: Any, isDistinct: Boolean = false): UnresolvedFunction = { | ||
| UnresolvedFunction(name, Literal(arg) :: Nil, isDistinct) | ||
| } | ||
|
|
||
| test("boolean") { | ||
| ruleTest(func("boolean", 10), Cast(Literal(10), BooleanType)) | ||
| } | ||
|
|
||
| test("tinyint") { | ||
| ruleTest(func("tinyint", 10), Cast(Literal(10), ByteType)) | ||
| } | ||
|
|
||
| test("smallint") { | ||
| ruleTest(func("smallint", 10), Cast(Literal(10), ShortType)) | ||
| } | ||
|
|
||
| test("int") { | ||
| ruleTest(func("int", 10), Cast(Literal(10), IntegerType)) | ||
| } | ||
|
|
||
| test("bigint") { | ||
| ruleTest(func("bigint", 10), Cast(Literal(10), LongType)) | ||
| } | ||
|
|
||
| test("float") { | ||
| ruleTest(func("float", 10), Cast(Literal(10), FloatType)) | ||
| } | ||
|
|
||
| test("double") { | ||
| ruleTest(func("double", 10), Cast(Literal(10), DoubleType)) | ||
| } | ||
|
|
||
| test("decimal") { | ||
| ruleTest(func("decimal", 10), Cast(Literal(10), DecimalType.USER_DEFAULT)) | ||
| } | ||
|
|
||
| test("binary") { | ||
| ruleTest(func("binary", 10), Cast(Literal(10), BinaryType)) | ||
| } | ||
|
|
||
| test("string") { | ||
| ruleTest(func("string", 10), Cast(Literal(10), StringType)) | ||
| } | ||
|
|
||
| test("function is not an alias for cast if it has a database defined") { | ||
| val f = UnresolvedFunction( | ||
| FunctionIdentifier("int", database = Option("db")), Literal(10) :: Nil, isDistinct = false) | ||
| ruleTest(f, f) | ||
| } | ||
|
|
||
| test("function is not an alias for cast if it has zero input arg") { | ||
| val f = UnresolvedFunction("int", Nil, isDistinct = false) | ||
| ruleTest(f, f) | ||
| } | ||
|
|
||
| test("function is not an alias for cast if it has more than one input args") { | ||
| val f = UnresolvedFunction("int", Literal(10) :: Literal(11) :: Nil, isDistinct = false) | ||
| ruleTest(f, f) | ||
| } | ||
|
|
||
| test("function is not an alias for cast if it is distinct (aggregate function)") { | ||
| val f = UnresolvedFunction("int", Literal(10) :: Nil, isDistinct = true) | ||
| ruleTest(f, f) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -200,24 +200,6 @@ class TypeCoercionSuite extends PlanTest { | |
| widenTest(ArrayType(IntegerType), StructType(Seq()), None) | ||
| } | ||
|
|
||
| private def ruleTest(rule: Rule[LogicalPlan], initial: Expression, transformed: Expression) { | ||
| ruleTest(Seq(rule), initial, transformed) | ||
| } | ||
|
|
||
| private def ruleTest( | ||
| rules: Seq[Rule[LogicalPlan]], | ||
| initial: Expression, | ||
| transformed: Expression): Unit = { | ||
| val testRelation = LocalRelation(AttributeReference("a", IntegerType)()) | ||
| val analyzer = new RuleExecutor[LogicalPlan] { | ||
| override val batches = Seq(Batch("Resolution", FixedPoint(3), rules: _*)) | ||
| } | ||
|
|
||
| comparePlans( | ||
| analyzer.execute(Project(Seq(Alias(initial, "a")()), testRelation)), | ||
| Project(Seq(Alias(transformed, "a")()), testRelation)) | ||
| } | ||
|
|
||
| test("cast NullType for expressions that implement ExpectsInputTypes") { | ||
|
Contributor
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. I moved this into PlanTest |
||
| import TypeCoercionSuite._ | ||
|
|
||
|
|
||
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.
can we use
FunctionRegisterto handle these?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 you mean putting in FunctionRegistry?
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.
yup, but not sure if it can work
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.
+1 for this. I think you can make it work. Implement a method that creates the same as
FunctionRegistry.expression[T <: Expression](name: String)method, e.g.:and use that method to register these casts...
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.
That was in #14364