-
Notifications
You must be signed in to change notification settings - Fork 94
feat(spark): add some date functions #373
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,39 @@ | ||
| /* | ||
| * 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 io.substrait.spark.expression | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.{LeafExpression, Unevaluable} | ||
| import org.apache.spark.sql.types.{DataType, NullType} | ||
|
|
||
| /** | ||
| * For internal use only. This represents the equivalent of a Substrait enum parameter type for use | ||
| * during conversion. It must not become part of a final Spark logical plan. | ||
| * | ||
| * @param value | ||
| * The enum string value. | ||
| */ | ||
| case class Enum(value: String) extends LeafExpression with Unevaluable { | ||
| override def nullable: Boolean = false | ||
|
|
||
| override def dataType: DataType = NullType | ||
|
|
||
| override def equals(that: Any): Boolean = that match { | ||
| case Enum(other) => other == value | ||
| case _ => false | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,7 @@ import org.apache.spark.sql.types.DataType | |
|
|
||
| import com.google.common.collect.{ArrayListMultimap, Multimap} | ||
| import io.substrait.`type`.Type | ||
| import io.substrait.expression.{Expression => SExpression, ExpressionCreator, FunctionArg} | ||
| import io.substrait.expression.{EnumArg, Expression => SExpression, ExpressionCreator, FunctionArg} | ||
| import io.substrait.expression.Expression.FailureBehavior | ||
| import io.substrait.extension.SimpleExtension | ||
| import io.substrait.function.{ParameterizedType, ToTypeString} | ||
|
|
@@ -93,14 +93,28 @@ abstract class FunctionConverter[F <: SimpleExtension.Function, T](functions: Se | |
| (matcherMap, keyMap) | ||
| } | ||
|
|
||
| def getSparkExpressionFromSubstraitFunc(key: String, outputType: Type): Option[Sig] = { | ||
| val sigs = substraitFuncKeyToSig.get(key) | ||
| sigs.size() match { | ||
| case 0 => None | ||
| case 1 => Some(sigs.iterator().next()) | ||
| case _ => None | ||
| def getSparkExpressionFromSubstraitFunc( | ||
| key: String, | ||
| args: Seq[Expression]): Option[Expression] = { | ||
| val candidates = substraitFuncKeyToSig.get(key).asScala.toList | ||
| val sigs = if (candidates.length > 1) { | ||
| // attempt to disambiguate with the key (if it's been set) | ||
| val specific = candidates.filter { | ||
| case SpecialSig(_, _, Some(sig), _) if sig == key => true | ||
| case _ => false | ||
| } | ||
| if (specific.nonEmpty) { | ||
| specific | ||
| } else { | ||
| // no matching signature, so select the generic one(s) | ||
| candidates | ||
| } | ||
| } else { | ||
| candidates | ||
| } | ||
| sigs.headOption.map(sig => sig.makeCall(args)) | ||
| } | ||
|
|
||
| private def createFinder(name: String, functions: Seq[F]): FunctionFinder[F, T] = { | ||
| new FunctionFinder[F, T]( | ||
| name, | ||
|
|
@@ -237,10 +251,14 @@ class FunctionFinder[F <: SimpleExtension.Function, T]( | |
| val singularInputType: Option[SingularArgumentMatcher[F]], | ||
| val parent: FunctionConverter[F, T]) { | ||
|
|
||
| def attemptMatch(expression: Expression, operands: Seq[SExpression]): Option[T] = { | ||
| val opTypes = operands.map(_.getType) | ||
| def attemptMatch(expression: Expression, operands: Seq[FunctionArg]): Option[T] = { | ||
| val outputType = ToSubstraitType.apply(expression.dataType, expression.nullable) | ||
| val opTypesStr = opTypes.map(t => t.accept(ToTypeString.INSTANCE)) | ||
|
|
||
| val opTypesStr = operands.map { | ||
| case e: SExpression => e.getType.accept(ToTypeString.INSTANCE) | ||
| case t: Type => t.accept(ToTypeString.INSTANCE) | ||
|
Contributor
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. do you know if we have any tests for this (t: Type) case?
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. Yes, this gets driven by the last test in
Contributor
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. Don't the use the
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. Yes, sorry. The |
||
| case _: EnumArg => "req" | ||
| } | ||
|
|
||
| val possibleKeys = | ||
| Util.crossProduct(opTypesStr.map(s => Seq(s))).map(list => list.mkString("_")) | ||
|
|
@@ -251,11 +269,11 @@ class FunctionFinder[F <: SimpleExtension.Function, T]( | |
|
|
||
| if (operands.isEmpty) { | ||
| val variant = directMap(name + ":") | ||
| variant.validateOutputType(JavaConverters.bufferAsJavaList(operands.toBuffer), outputType) | ||
| // TODO validate the output type | ||
| Option(parent.generateBinding(expression, variant, operands, outputType)) | ||
| } else if (directMatchKey.isDefined) { | ||
| val variant = directMap(directMatchKey.get) | ||
| variant.validateOutputType(JavaConverters.bufferAsJavaList(operands.toBuffer), outputType) | ||
| // TODO validate the output type | ||
| val funcArgs: Seq[FunctionArg] = operands | ||
| Option(parent.generateBinding(expression, variant, funcArgs, outputType)) | ||
| } else if (singularInputType.isDefined) { | ||
|
|
@@ -277,9 +295,7 @@ class FunctionFinder[F <: SimpleExtension.Function, T]( | |
| .map( | ||
| declaration => { | ||
| val coercedArgs = coerceArguments(operands, leastRestrictiveSubstraitT) | ||
| declaration.validateOutputType( | ||
| JavaConverters.bufferAsJavaList(coercedArgs.toBuffer), | ||
| outputType) | ||
| // TODO validate the output type | ||
| val funcArgs: Seq[FunctionArg] = coercedArgs | ||
| parent.generateBinding(expression, declaration, funcArgs, outputType) | ||
| }) | ||
|
|
@@ -293,14 +309,15 @@ class FunctionFinder[F <: SimpleExtension.Function, T]( | |
| * Coerced types according to an expected output type. Coercion is only done for type mismatches, | ||
| * not for nullability or parameter mismatches. | ||
| */ | ||
| private def coerceArguments(arguments: Seq[SExpression], t: Type): Seq[SExpression] = { | ||
| arguments.map( | ||
| a => { | ||
| private def coerceArguments(arguments: Seq[FunctionArg], t: Type): Seq[FunctionArg] = { | ||
| arguments.map { | ||
| case a: SExpression => | ||
| if (FunctionFinder.isMatch(t, a.getType)) { | ||
| a | ||
| } else { | ||
| ExpressionCreator.cast(t, a, FailureBehavior.THROW_EXCEPTION) | ||
| } | ||
| }) | ||
| case other => other | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,13 +22,14 @@ import io.substrait.spark.logical.ToLogicalPlan | |
| import org.apache.spark.sql.Row | ||
| import org.apache.spark.sql.catalyst.expressions.{CaseWhen, Cast, Expression, In, InSubquery, ListQuery, Literal, MakeDecimal, NamedExpression, ScalarSubquery} | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.types.{DateType, Decimal} | ||
| import org.apache.spark.sql.types.Decimal | ||
| import org.apache.spark.substrait.SparkTypeUtil | ||
| import org.apache.spark.unsafe.types.UTF8String | ||
|
|
||
| import io.substrait.`type`.{StringTypeVisitor, Type} | ||
| import io.substrait.{expression => exp} | ||
| import io.substrait.expression.{Expression => SExpression} | ||
| import io.substrait.expression.{EnumArg, Expression => SExpression} | ||
| import io.substrait.extension.SimpleExtension | ||
| import io.substrait.util.DecimalUtil | ||
| import io.substrait.utils.Util | ||
|
|
||
|
|
@@ -162,10 +163,11 @@ class ToSparkExpression( | |
| override def visit(expr: SExpression.Cast): Expression = { | ||
| val childExp = expr.input().accept(this) | ||
| val tt = ToSparkType.convert(expr.getType) | ||
| val tz = childExp.dataType match { | ||
| case DateType => Some(SQLConf.get.getConf(SQLConf.SESSION_LOCAL_TIMEZONE)) | ||
| case _ => None | ||
| } | ||
| val tz = | ||
| if (Cast.needsTimeZone(childExp.dataType, tt)) | ||
|
Contributor
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. nice! |
||
| Some(SQLConf.get.getConf(SQLConf.SESSION_LOCAL_TIMEZONE)) | ||
| else | ||
| None | ||
| Cast(childExp, tt, tz) | ||
| } | ||
|
|
||
|
|
@@ -219,12 +221,19 @@ class ToSparkExpression( | |
| } | ||
| } | ||
|
|
||
| override def visitEnumArg( | ||
| fnDef: SimpleExtension.Function, | ||
| argIdx: Int, | ||
| e: EnumArg): Expression = { | ||
| Enum(e.value.orElse("")) | ||
| } | ||
|
|
||
| override def visit(expr: SExpression.ScalarFunctionInvocation): Expression = { | ||
| val eArgs = expr.arguments().asScala | ||
| val args = eArgs.zipWithIndex.map { | ||
| case (arg, i) => | ||
| arg.accept(expr.declaration(), i, this) | ||
| } | ||
| }.toList | ||
|
|
||
| expr.declaration.name match { | ||
| case "make_decimal" if expr.declaration.uri == SparkExtension.uri => | ||
|
|
@@ -238,8 +247,7 @@ class ToSparkExpression( | |
| } | ||
| case _ => | ||
| scalarFunctionConverter | ||
| .getSparkExpressionFromSubstraitFunc(expr.declaration().key(), expr.outputType()) | ||
| .flatMap(sig => Option(sig.makeCall(args))) | ||
| .getSparkExpressionFromSubstraitFunc(expr.declaration.key, args) | ||
| .getOrElse({ | ||
| val msg = String.format( | ||
| "Unable to convert scalar function %s(%s).", | ||
|
|
||
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.
makes sense, mind adding a docstring to explain what this is for though?