-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[BEAM-10925] Create ZetaSQL-specific subclass of ScalarFunctionImpl t… #13309
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 |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ | |
| import org.apache.beam.sdk.extensions.sql.impl.planner.BeamRelDataTypeSystem; | ||
| import org.apache.beam.sdk.extensions.sql.impl.udaf.StringAgg; | ||
| import org.apache.beam.sdk.extensions.sql.zetasql.DateTimeUtils; | ||
| import org.apache.beam.sdk.extensions.sql.zetasql.SqlAnalyzer; | ||
| import org.apache.beam.sdk.extensions.sql.zetasql.translation.impl.BeamBuiltinMethods; | ||
| import org.apache.beam.sdk.extensions.sql.zetasql.translation.impl.CastFunctionImpl; | ||
| import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.jdbc.JavaTypeFactoryImpl; | ||
|
|
@@ -82,58 +83,85 @@ public class SqlOperators { | |
| new UdafImpl<>(new StringAgg.StringAggString())); | ||
|
|
||
| public static final SqlOperator START_WITHS = | ||
| createUdfOperator("STARTS_WITH", BeamBuiltinMethods.STARTS_WITH_METHOD); | ||
| createUdfOperator( | ||
| "STARTS_WITH", | ||
| BeamBuiltinMethods.STARTS_WITH_METHOD, | ||
| SqlAnalyzer.ZETASQL_FUNCTION_GROUP_NAME); | ||
|
|
||
| public static final SqlOperator CONCAT = | ||
| createUdfOperator("CONCAT", BeamBuiltinMethods.CONCAT_METHOD); | ||
| createUdfOperator( | ||
| "CONCAT", BeamBuiltinMethods.CONCAT_METHOD, SqlAnalyzer.ZETASQL_FUNCTION_GROUP_NAME); | ||
|
|
||
| public static final SqlOperator REPLACE = | ||
| createUdfOperator("REPLACE", BeamBuiltinMethods.REPLACE_METHOD); | ||
| createUdfOperator( | ||
| "REPLACE", BeamBuiltinMethods.REPLACE_METHOD, SqlAnalyzer.ZETASQL_FUNCTION_GROUP_NAME); | ||
|
|
||
| public static final SqlOperator TRIM = createUdfOperator("TRIM", BeamBuiltinMethods.TRIM_METHOD); | ||
| public static final SqlOperator TRIM = | ||
| createUdfOperator( | ||
| "TRIM", BeamBuiltinMethods.TRIM_METHOD, SqlAnalyzer.ZETASQL_FUNCTION_GROUP_NAME); | ||
|
|
||
| public static final SqlOperator LTRIM = | ||
| createUdfOperator("LTRIM", BeamBuiltinMethods.LTRIM_METHOD); | ||
| createUdfOperator( | ||
| "LTRIM", BeamBuiltinMethods.LTRIM_METHOD, SqlAnalyzer.ZETASQL_FUNCTION_GROUP_NAME); | ||
|
|
||
| public static final SqlOperator RTRIM = | ||
| createUdfOperator("RTRIM", BeamBuiltinMethods.RTRIM_METHOD); | ||
| createUdfOperator( | ||
| "RTRIM", BeamBuiltinMethods.RTRIM_METHOD, SqlAnalyzer.ZETASQL_FUNCTION_GROUP_NAME); | ||
|
|
||
| public static final SqlOperator SUBSTR = | ||
| createUdfOperator("SUBSTR", BeamBuiltinMethods.SUBSTR_METHOD); | ||
| createUdfOperator( | ||
| "SUBSTR", BeamBuiltinMethods.SUBSTR_METHOD, SqlAnalyzer.ZETASQL_FUNCTION_GROUP_NAME); | ||
|
|
||
| public static final SqlOperator REVERSE = | ||
| createUdfOperator("REVERSE", BeamBuiltinMethods.REVERSE_METHOD); | ||
| createUdfOperator( | ||
| "REVERSE", BeamBuiltinMethods.REVERSE_METHOD, SqlAnalyzer.ZETASQL_FUNCTION_GROUP_NAME); | ||
|
|
||
| public static final SqlOperator CHAR_LENGTH = | ||
| createUdfOperator("CHAR_LENGTH", BeamBuiltinMethods.CHAR_LENGTH_METHOD); | ||
| createUdfOperator( | ||
| "CHAR_LENGTH", | ||
| BeamBuiltinMethods.CHAR_LENGTH_METHOD, | ||
| SqlAnalyzer.ZETASQL_FUNCTION_GROUP_NAME); | ||
|
|
||
| public static final SqlOperator ENDS_WITH = | ||
| createUdfOperator("ENDS_WITH", BeamBuiltinMethods.ENDS_WITH_METHOD); | ||
| createUdfOperator( | ||
| "ENDS_WITH", | ||
| BeamBuiltinMethods.ENDS_WITH_METHOD, | ||
| SqlAnalyzer.ZETASQL_FUNCTION_GROUP_NAME); | ||
|
|
||
| public static final SqlOperator LIKE = | ||
| createUdfOperator("LIKE", BeamBuiltinMethods.LIKE_METHOD, SqlSyntax.BINARY); | ||
| createUdfOperator( | ||
| "LIKE", | ||
| BeamBuiltinMethods.LIKE_METHOD, | ||
| SqlSyntax.BINARY, | ||
| SqlAnalyzer.ZETASQL_FUNCTION_GROUP_NAME); | ||
|
|
||
| public static final SqlOperator VALIDATE_TIMESTAMP = | ||
| createUdfOperator( | ||
| "validateTimestamp", | ||
| DateTimeUtils.class, | ||
| "validateTimestamp", | ||
| x -> NULLABLE_TIMESTAMP, | ||
| ImmutableList.of(TIMESTAMP)); | ||
| ImmutableList.of(TIMESTAMP), | ||
| SqlAnalyzer.ZETASQL_FUNCTION_GROUP_NAME); | ||
|
|
||
| public static final SqlOperator VALIDATE_TIME_INTERVAL = | ||
| createUdfOperator( | ||
| "validateIntervalArgument", | ||
| DateTimeUtils.class, | ||
| "validateTimeInterval", | ||
| x -> NULLABLE_BIGINT, | ||
| ImmutableList.of(BIGINT, OTHER)); | ||
| ImmutableList.of(BIGINT, OTHER), | ||
| SqlAnalyzer.ZETASQL_FUNCTION_GROUP_NAME); | ||
|
|
||
| public static final SqlOperator TIMESTAMP_OP = | ||
| createUdfOperator("TIMESTAMP", BeamBuiltinMethods.TIMESTAMP_METHOD); | ||
| createUdfOperator( | ||
| "TIMESTAMP", | ||
| BeamBuiltinMethods.TIMESTAMP_METHOD, | ||
| SqlAnalyzer.ZETASQL_FUNCTION_GROUP_NAME); | ||
|
|
||
| public static final SqlOperator DATE_OP = | ||
| createUdfOperator("DATE", BeamBuiltinMethods.DATE_METHOD); | ||
| createUdfOperator( | ||
| "DATE", BeamBuiltinMethods.DATE_METHOD, SqlAnalyzer.ZETASQL_FUNCTION_GROUP_NAME); | ||
|
|
||
| public static final SqlUserDefinedFunction CAST_OP = | ||
| new SqlUserDefinedFunction( | ||
|
|
@@ -158,7 +186,7 @@ public static SqlFunction createZetaSqlFunction(String name, SqlTypeName returnT | |
| SqlFunctionCategory.USER_DEFINED_FUNCTION); | ||
| } | ||
|
|
||
| private static SqlUserDefinedAggFunction createUdafOperator( | ||
| static SqlUserDefinedAggFunction createUdafOperator( | ||
| String name, SqlReturnTypeInference returnTypeInference, AggregateFunction function) { | ||
| return new SqlUserDefinedAggFunction( | ||
| new SqlIdentifier(name, SqlParserPos.ZERO), | ||
|
|
@@ -177,26 +205,24 @@ private static SqlUserDefinedFunction createUdfOperator( | |
| Class<?> methodClass, | ||
| String methodName, | ||
| SqlReturnTypeInference returnTypeInference, | ||
| List<RelDataType> paramTypes) { | ||
| List<RelDataType> paramTypes, | ||
| String funGroup) { | ||
|
||
| return new SqlUserDefinedFunction( | ||
| new SqlIdentifier(name, SqlParserPos.ZERO), | ||
| returnTypeInference, | ||
| null, | ||
| null, | ||
| paramTypes, | ||
| ScalarFunctionImpl.create(methodClass, methodName)); | ||
| ZetaSqlScalarFunctionImpl.create(methodClass, methodName, funGroup)); | ||
| } | ||
|
|
||
| // Helper function to create SqlUserDefinedFunction based on a function name and a method. | ||
| // SqlUserDefinedFunction will be able to pass through Calcite codegen and get proper function | ||
| // called. | ||
| private static SqlUserDefinedFunction createUdfOperator(String name, Method method) { | ||
| return createUdfOperator(name, method, SqlSyntax.FUNCTION); | ||
| static SqlUserDefinedFunction createUdfOperator(String name, Method method, String funGroup) { | ||
| return createUdfOperator(name, method, SqlSyntax.FUNCTION, funGroup); | ||
| } | ||
|
|
||
| private static SqlUserDefinedFunction createUdfOperator( | ||
| String name, Method method, final SqlSyntax syntax) { | ||
| Function function = ScalarFunctionImpl.create(method); | ||
| String name, Method method, final SqlSyntax syntax, String funGroup) { | ||
| Function function = ZetaSqlScalarFunctionImpl.create(method, funGroup); | ||
| final RelDataTypeFactory typeFactory = createTypeFactory(); | ||
|
|
||
| List<RelDataType> argTypes = new ArrayList<>(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * 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.beam.sdk.extensions.sql.zetasql.translation; | ||
|
|
||
| import java.lang.reflect.Method; | ||
| import org.apache.beam.sdk.extensions.sql.impl.ScalarFunctionImpl; | ||
| import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.adapter.enumerable.CallImplementor; | ||
| import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.schema.Function; | ||
| import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.schema.ScalarFunction; | ||
|
|
||
| /** ZetaSQL-specific extension to {@link ScalarFunctionImpl}. */ | ||
| public class ZetaSqlScalarFunctionImpl extends ScalarFunctionImpl { | ||
| /** | ||
| * ZetaSQL function group identifier. Different function groups may have divergent translation | ||
| * paths. | ||
| */ | ||
| public final String functionGroup; | ||
|
|
||
| private ZetaSqlScalarFunctionImpl( | ||
| Method method, CallImplementor implementor, String functionGroup) { | ||
| super(method, implementor); | ||
| this.functionGroup = functionGroup; | ||
| } | ||
|
|
||
| /** | ||
| * Creates {@link org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.schema.Function} from | ||
| * given class. | ||
| * | ||
| * <p>If a method of the given name is not found or it does not suit, returns {@code null}. | ||
| * | ||
| * @param clazz class that is used to implement the function | ||
| * @param methodName Method name (typically "eval") | ||
| * @param functionGroup ZetaSQL function group identifier. Different function groups may have | ||
| * divergent translation paths. | ||
| * @return created {@link ScalarFunction} or null | ||
| */ | ||
| public static Function create(Class<?> clazz, String methodName, String functionGroup) { | ||
| return create(findMethod(clazz, methodName)); | ||
| } | ||
|
|
||
| /** | ||
| * Creates {@link org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.schema.Function} from | ||
| * given method. When {@code eval} method does not suit, {@code null} is returned. | ||
| * | ||
| * @param method method that is used to implement the function | ||
| * @param functionGroup ZetaSQL function group identifier. Different function groups may have | ||
| * divergent translation paths. | ||
| * @return created {@link Function} or null | ||
| */ | ||
| public static Function create(Method method, String functionGroup) { | ||
| validateMethod(method); | ||
| CallImplementor implementor = createImplementor(method); | ||
| return new ZetaSqlScalarFunctionImpl(method, implementor, functionGroup); | ||
| } | ||
|
|
||
| /* | ||
| * Finds a method in a given class by name. | ||
| * @param clazz class to search method in | ||
| * @param name name of the method to find | ||
| * @return the first method with matching name or null when no method found | ||
| */ | ||
| private static Method findMethod(Class<?> clazz, String name) { | ||
| for (Method method : clazz.getMethods()) { | ||
| if (method.getName().equals(name) && !method.isBridge()) { | ||
| return method; | ||
| } | ||
| } | ||
| throw new NoSuchMethodError( | ||
| String.format("Method %s not found in class %s.", name, clazz.getName())); | ||
| } | ||
| } |
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.
for these
ZETASQL_FUNCTION_GROUP_NAME, eventually I think we will migrate it off fromcreateUdfOperator.