raminqaf commented on code in PR #30019:
URL: https://github.com/apache/flink/pull/30019#discussion_r4626058695
##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/sql/FlinkSqlOperatorTable.java:
##########
@@ -1290,4 +1290,58 @@ public List<SqlGroupedWindowFunction>
getAuxiliaryFunctions() {
.operandTypeChecker(OperandTypes.NILADIC)
.notDeterministic()
.build();
+
+ //
--------------------------------------------------------------------------------------------
+ // VERSIONED OPERATORS
+ //
--------------------------------------------------------------------------------------------
+
+ /**
+ * Maps operators that are not a {@link VersionedSqlOperator} to the
{@link BuiltInSqlOperator}
+ * that defines their latest version in a compiled plan.
+ *
+ * <p>Use it for operators that cannot be replaced by a {@link
BuiltInSqlFunction}, for example
+ * because Calcite references them directly in the parser or {@code
RexBuilder}. When the
+ * runtime behavior of such an operator {@code OP} changes from version 1
to 2:
+ *
+ * <ul>
+ * <li>Add a public constant {@code OP_V1} built with {@code
+ * BuiltInSqlOperator.newBuilder(OP).version(1).internal().build()}.
Its name {@code
+ * $OP$1} registers it in this table for restoring compiled plans.
It is internal to not
+ * expose it when listing functions.
+ * <li>Add a private constant {@code OP_V2} built with {@code
+ * BuiltInSqlOperator.newBuilder(OP).version(2).build()}. It is
private to not register
+ * it, the planner keeps using {@code OP}.
+ * <li>Add the entry {@code OP -> OP_V2}. Compiled plans then contain
{@code $OP$2} for {@code
+ * OP}. {@code OP_V1} needs no entry, it is versioned itself and
restores {@code $OP$1} by
+ * name.
+ * <li>Match on both {@code OP} and {@code OP_V1} wherever the operator
is compared by
+ * identity, such as during code generation.
+ * </ul>
+ *
+ * <p>In summary:
+ *
+ * <pre>{@code
+ * FlinkSqlOperatorTable {
+ * // used during planning, compiled as $OP$2
+ * public static final SqlOperator OP = SqlStdOperatorTable.OP;
+ *
+ * // registered as $OP$1 but not listed, restored from older compiled
plans
+ * public static final BuiltInSqlOperator OP_V1 =
+ * BuiltInSqlOperator.newBuilder(OP).version(1).internal().build();
+ *
+ * // not registered, only defines the version of OP
+ * private static final BuiltInSqlOperator OP_V2 =
+ * BuiltInSqlOperator.newBuilder(OP).version(2).build();
+ *
+ * public static final Map<SqlOperator, BuiltInSqlOperator>
VERSIONED_OPERATORS =
+ * Map.of(OP, OP_V2);
+ * }
+ *
+ * ExprCodeGenerator {
+ * case OP => // latest behavior
+ * case OP_V1 => // behavior of version 1
+ * }
+ * }</pre>
+ */
+ public static final Map<SqlOperator, BuiltInSqlOperator>
VERSIONED_OPERATORS = Map.of();
Review Comment:
What happens to `$OP$1` plans if someone adds `OP -> OP_V2` but forgets the
public `OP_V1`? `deserializeInternalFunction` finds no `$OP$1`, strips the
version and returns the latest `OP`. Correct?
##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/sql/BuiltInSqlOperatorTest.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.flink.table.planner.functions.sql;
+
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
+import org.apache.flink.table.planner.calcite.FlinkTypeSystem;
+
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlSpecialOperator;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.junit.jupiter.api.Test;
+
+import java.math.BigDecimal;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link BuiltInSqlOperator}. */
+class BuiltInSqlOperatorTest {
+
+ private static final FlinkTypeFactory FACTORY =
+ new FlinkTypeFactory(
+ BuiltInSqlOperatorTest.class.getClassLoader(),
FlinkTypeSystem.INSTANCE);
+
+ @Test
+ void testVersionedCopy() {
+ final BuiltInSqlOperator item =
+
BuiltInSqlOperator.newBuilder(SqlStdOperatorTable.ITEM).version(2).build();
+
+ assertThat(item.getName()).isEqualTo("$ITEM$2");
+ assertThat(item.getQualifiedName()).isEqualTo("$ITEM$2");
+
assertThat(VersionedSqlOperator.toQualifiedName(item)).isEqualTo("$ITEM$2");
+ assertThat(VersionedSqlOperator.unwrapVersion(item)).contains(2);
+ assertThat(item.getKind()).isEqualTo(SqlKind.ITEM);
+
assertThat(item.getSyntax()).isEqualTo(SqlStdOperatorTable.ITEM.getSyntax());
+
+ // Operators that are not versioned use the default version
+
assertThat(VersionedSqlOperator.toQualifiedName(SqlStdOperatorTable.LESS_THAN))
+ .isEqualTo("$<$1");
+ }
+
+ @Test
+ void testTypeInferenceOfWrappedOperator() {
+ final RexBuilder rexBuilder = new RexBuilder(FACTORY);
+ final RexNode array =
+ rexBuilder.makeInputRef(
+
FACTORY.createArrayType(FACTORY.createSqlType(SqlTypeName.INTEGER), -1), 0);
+ final RexNode index = rexBuilder.makeExactLiteral(BigDecimal.ONE);
+ final BuiltInSqlOperator item =
+
BuiltInSqlOperator.newBuilder(SqlStdOperatorTable.ITEM).version(2).build();
+
+ assertThat(rexBuilder.makeCall(item, array, index).getType())
+ .isEqualTo(rexBuilder.makeCall(SqlStdOperatorTable.ITEM,
array, index).getType());
+ }
+
+ @Test
+ void testInternal() {
Review Comment:
nit: No test checks `$-$1` (maybe converting to a parametized test?)
##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/sql/VersionedSqlOperator.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.flink.table.planner.functions.sql;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.functions.BuiltInFunctionDefinition;
+import org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction;
+
+import org.apache.calcite.sql.SqlOperator;
+
+import javax.annotation.Nullable;
+
+import java.util.Optional;
+
+import static
org.apache.flink.table.functions.BuiltInFunctionDefinition.DEFAULT_VERSION;
+import static
org.apache.flink.table.functions.BuiltInFunctionDefinition.qualifyFunctionName;
+
+/**
+ * SQL version of {@link BuiltInFunctionDefinition} in cases where {@link
BridgingSqlFunction} does
+ * not apply. This is the case when the operator has a special parsing syntax
or uses other
+ * Calcite-specific features that are not exposed via {@link
BuiltInFunctionDefinition} yet.
+ *
+ * <p>The static helpers also cover regular {@link SqlOperator}s that are
listed in {@link
+ * FlinkSqlOperatorTable#VERSIONED_OPERATORS}. This allows to version
operators that are hard coded
+ * in Calcite (e.g. in the parser or {@code RexBuilder}) and can therefore not
be replaced by an
+ * instance of this interface.
+ *
+ * <p>If {@link SqlOperator} were an interface, this interface would extend
from it.
+ */
+@Internal
+public interface VersionedSqlOperator {
+
+ /**
+ * @see BuiltInFunctionDefinition#getVersion()
+ */
+ Optional<Integer> getVersion();
+
+ /**
+ * @see BuiltInFunctionDefinition#isInternal()
+ */
+ boolean isInternal();
+
+ /**
+ * @see BuiltInFunctionDefinition#getQualifiedName()
+ */
+ String getQualifiedName();
+
+ //
--------------------------------------------------------------------------------------------
+
+ static Optional<Integer> unwrapVersion(SqlOperator operator) {
+ final VersionedSqlOperator versionedOperator =
toVersionedOperator(operator);
+ if (versionedOperator != null) {
+ return versionedOperator.isInternal()
+ ? Optional.empty()
+ : versionedOperator.getVersion();
+ }
+ return Optional.of(DEFAULT_VERSION);
+ }
+
+ static boolean unwrapIsInternal(SqlOperator operator) {
+ final VersionedSqlOperator versionedOperator =
toVersionedOperator(operator);
+ if (versionedOperator != null) {
+ return versionedOperator.isInternal();
+ }
+ return false;
+ }
Review Comment:
Both of these methods are used in the tests only and are package private.
Should we move them to the test files instead?
##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/sql/FlinkSqlOperatorTable.java:
##########
@@ -1290,4 +1290,58 @@ public List<SqlGroupedWindowFunction>
getAuxiliaryFunctions() {
.operandTypeChecker(OperandTypes.NILADIC)
.notDeterministic()
.build();
+
+ //
--------------------------------------------------------------------------------------------
+ // VERSIONED OPERATORS
+ //
--------------------------------------------------------------------------------------------
+
+ /**
+ * Maps operators that are not a {@link VersionedSqlOperator} to the
{@link BuiltInSqlOperator}
+ * that defines their latest version in a compiled plan.
+ *
+ * <p>Use it for operators that cannot be replaced by a {@link
BuiltInSqlFunction}, for example
+ * because Calcite references them directly in the parser or {@code
RexBuilder}. When the
+ * runtime behavior of such an operator {@code OP} changes from version 1
to 2:
+ *
+ * <ul>
+ * <li>Add a public constant {@code OP_V1} built with {@code
+ * BuiltInSqlOperator.newBuilder(OP).version(1).internal().build()}.
Its name {@code
+ * $OP$1} registers it in this table for restoring compiled plans.
It is internal to not
+ * expose it when listing functions.
+ * <li>Add a private constant {@code OP_V2} built with {@code
+ * BuiltInSqlOperator.newBuilder(OP).version(2).build()}. It is
private to not register
+ * it, the planner keeps using {@code OP}.
+ * <li>Add the entry {@code OP -> OP_V2}. Compiled plans then contain
{@code $OP$2} for {@code
+ * OP}. {@code OP_V1} needs no entry, it is versioned itself and
restores {@code $OP$1} by
+ * name.
+ * <li>Match on both {@code OP} and {@code OP_V1} wherever the operator
is compared by
+ * identity, such as during code generation.
+ * </ul>
+ *
+ * <p>In summary:
+ *
+ * <pre>{@code
+ * FlinkSqlOperatorTable {
+ * // used during planning, compiled as $OP$2
+ * public static final SqlOperator OP = SqlStdOperatorTable.OP;
+ *
+ * // registered as $OP$1 but not listed, restored from older compiled
plans
+ * public static final BuiltInSqlOperator OP_V1 =
+ * BuiltInSqlOperator.newBuilder(OP).version(1).internal().build();
+ *
+ * // not registered, only defines the version of OP
+ * private static final BuiltInSqlOperator OP_V2 =
+ * BuiltInSqlOperator.newBuilder(OP).version(2).build();
+ *
+ * public static final Map<SqlOperator, BuiltInSqlOperator>
VERSIONED_OPERATORS =
+ * Map.of(OP, OP_V2);
+ * }
+ *
+ * ExprCodeGenerator {
+ * case OP => // latest behavior
+ * case OP_V1 => // behavior of version 1
+ * }
+ * }</pre>
+ */
Review Comment:
Should we merge these? I think having it as bullets and pseudo code is a bit
too much
##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/sql/BuiltInSqlOperator.java:
##########
@@ -20,76 +20,204 @@
import org.apache.flink.annotation.Internal;
import org.apache.flink.table.functions.BuiltInFunctionDefinition;
-import org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction;
+import org.apache.calcite.plan.Strong;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlCallBinding;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlOperandCountRange;
import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlOperatorBinding;
+import org.apache.calcite.sql.SqlSyntax;
+import org.apache.calcite.sql.SqlWriter;
+import org.apache.calcite.sql.validate.SqlMonotonicity;
+import org.apache.calcite.sql.validate.SqlValidator;
+import org.apache.calcite.sql.validate.SqlValidatorScope;
+
+import javax.annotation.Nullable;
import java.util.Optional;
+import java.util.function.Supplier;
import static
org.apache.flink.table.functions.BuiltInFunctionDefinition.DEFAULT_VERSION;
import static
org.apache.flink.table.functions.BuiltInFunctionDefinition.qualifyFunctionName;
+import static
org.apache.flink.table.functions.BuiltInFunctionDefinition.validateFunction;
+import static org.apache.flink.util.Preconditions.checkNotNull;
/**
- * SQL version of {@link BuiltInFunctionDefinition} in cases where {@link
BridgingSqlFunction} does
- * not apply. This is the case when the operator has a special parsing syntax
or uses other
- * Calcite-specific features that are not exposed via {@link
BuiltInFunctionDefinition} yet.
+ * A versioned copy of an existing {@link SqlOperator} that cannot be replaced
by a {@link
+ * BuiltInSqlFunction}, for example because Calcite references it directly in
the parser or {@code
+ * RexBuilder}.
+ *
+ * <p>The operator behaves like the wrapped operator during planning. The
latest version only
+ * defines the version of the wrapped operator in {@link
FlinkSqlOperatorTable#VERSIONED_OPERATORS}.
+ * Older versions are registered in {@link FlinkSqlOperatorTable} for
restoring compiled plans and
+ * must be matched on during code generation, see {@link
FlinkSqlOperatorTable#VERSIONED_OPERATORS}
+ * for details.
*
- * <p>If {@link SqlOperator} were an interface, this interface would extend
from it.
+ * <p>The name includes the version (e.g. {@code $ITEM$1}), which makes older
versions accessible
+ * for restoring compiled plans without conflicting with the wrapped operator.
Similar to {@link
+ * BuiltInFunctionDefinition}, older versions should be internal to not expose
them when listing
+ * functions. For internal operators, the version is only part of the name.
*/
@Internal
-public interface BuiltInSqlOperator {
+public final class BuiltInSqlOperator extends SqlOperator implements
VersionedSqlOperator {
Review Comment:
Should this wrapper be limited to scalar and special operators? Will
aggregate function work as well? Like `$SUM0`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]