Skip to content
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

[SPARK-16730][SQL] Implement function aliases for type casts #14362

Closed
wants to merge 1 commit into from

Conversation

petermaxlee
Copy link
Contributor

What changes were proposed in this pull request?

Spark 1.x supports using the Hive type name as function names for doing casts, e.g.

SELECT int(1.0);
SELECT string(2.0);

The above query would work in Spark 1.x because Spark 1.x fail back to Hive for unimplemented functions, and break in Spark 2.0 because the fall back was removed.

This patch implements function aliases using an analyzer rule for the following cast functions:

  • boolean
  • tinyint
  • smallint
  • int
  • bigint
  • float
  • double
  • decimal
  • date
  • timestamp
  • binary
  • string

How was this patch tested?

Added unit tests for SubstituteFunctionAliases as well as end-to-end tests for SQLCompatibilityFunctionSuite.

@@ -200,24 +200,6 @@ class TypeCoercionSuite extends PlanTest {
widenTest(ArrayType(IntegerType), StructType(Seq()), None)
}

private def ruleTest(rule: Rule[LogicalPlan], initial: Expression, transformed: Expression) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved this into PlanTest

@petermaxlee
Copy link
Contributor Author

@cloud-fan and @hvanhovell can you take a look?

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

can we use FunctionRegister to handle these?

Copy link
Contributor Author

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?

Copy link
Contributor

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

Copy link
Contributor

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.:

def cast(name: String, dt: DataType): (String, (ExpressionInfo, FunctionBuilder)) = {
  val info = new ExpressionInfo(classOf[Cast].getName, name)
  name -> (info, Cast(_, dt))
}

and use that method to register these casts...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was in #14364

@SparkQA
Copy link

SparkQA commented Jul 26, 2016

Test build #62869 has finished for PR 14362 at commit 37b7127.

  • This patch fails PySpark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@petermaxlee
Copy link
Contributor Author

Closing this one in favor of #14364

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants