This is an automated email from the ASF dual-hosted git repository.
philo-he pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gluten.git
The following commit(s) were added to refs/heads/main by this push:
new a1ce039a70 [GLUTEN-13469][FLINK][VL] Add support for
CURRENT_TIMESTAMP, NOW, LOCALTIMESTAMP and LOCALTIME (#13470)
a1ce039a70 is described below
commit a1ce039a70b7726e2fed0003cce935983efd3e23
Author: zhangxiong <[email protected]>
AuthorDate: Fri Nov 13 16:17:03 2026 +0800
[GLUTEN-13469][FLINK][VL] Add support for CURRENT_TIMESTAMP, NOW,
LOCALTIMESTAMP and LOCALTIME (#13470)
---
.../apache/gluten/rexnode/RexNodeConverter.java | 109 ++++++++
.../rexnode/functions/RexCallConverterFactory.java | 10 +
.../gluten/rexnode/RexNodeConverterTest.java | 210 ++++++++++++++++
.../stream/custom/TemporalFunctionsTest.java | 273 +++++++++++++++++++++
.../gluten/util/LogicalTypeConverterTest.java | 71 ++++++
5 files changed, 673 insertions(+)
diff --git
a/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/RexNodeConverter.java
b/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/RexNodeConverter.java
index d7e329dc5d..039dcad4e4 100644
---
a/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/RexNodeConverter.java
+++
b/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/RexNodeConverter.java
@@ -52,6 +52,7 @@ import org.apache.calcite.rex.RexNode;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.util.NlsString;
+import org.apache.calcite.util.TimeString;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
@@ -63,12 +64,115 @@ import java.util.stream.Collectors;
/** Convertor to convert RexNode to velox TypedExpr */
public class RexNodeConverter {
+ // Flink TIME-typed values ride on the INTEGER milliseconds-of-day carrier.
+ // The designed carrier surfaces — the LOCALTIME / CURRENT_TIME direct
+ // projections, comparisons and other carrier predicates (IS [NOT] NULL,
+ // SEARCH) on the carrier, CAST between TIME-typed values and TIME literals
+ // (INTEGER constants) — keep consistent carrier semantics. Any other call
+ // whose own type or a direct operand is TIME would either fail against the
+ // velox signatures or silently evaluate the integer carrier as a plain
+ // number, so it is rejected here instead.
+ private static final Set<String> CARRIER_PROJECTION_OPERATORS =
+ Set.of("LOCALTIME", "CURRENT_TIME");
+
+ private static final Set<String> COMPARISON_OPERATORS = Set.of("=", "<>",
"<", "<=", ">", ">=");
+
+ // IS [NOT] NULL only checks the carrier's nullability and SEARCH compares
+ // carrier values (the Calcite form of IN / LIKE over a point or range set);
+ // both are carrier-isomorphic and lower to existing converters (isnull /
+ // isnotnull, and the SearchRexCallConverter).
+ private static final Set<String> CARRIER_PREDICATE_OPERATORS =
+ Set.of("IS NULL", "IS NOT NULL", "SEARCH");
+
+ private static final String TIME_UNSUPPORTED_SUFFIX =
+ "gluten-flink supports TIME-typed values only on the INTEGER
milliseconds-of-day"
+ + " carrier: the LOCALTIME / CURRENT_TIME direct projections,
comparisons"
+ + " and other carrier predicates (IS [NOT] NULL, SEARCH) on the
carrier,"
+ + " CAST between TIME-typed values and TIME literals keep carrier"
+ + " semantics; other TIME expressions (arithmetic, EXTRACT, CAST to
or"
+ + " away from TIME across other types, CASE, ...) lose TIME
semantics"
+ + " on the INTEGER carrier and are rejected.";
+
+ /**
+ * Rejects a RexCall that would silently integer-ize TIME semantics. Rules:
the LOCALTIME /
+ * CURRENT_TIME carrier projections, comparisons and other carrier
predicates (IS [NOT] NULL,
+ * SEARCH) and CAST between TIME-typed values pass (they keep carrier
semantics); any other call
+ * whose result type or any direct operand type is TIME fails with an
explicit planner-time error.
+ */
+ private static void maybeRejectTimeTypedCall(RexCall rexCall) {
+ String operatorName = rexCall.getOperator().getName();
+ if (CARRIER_PROJECTION_OPERATORS.contains(operatorName)) {
+ return;
+ }
+ boolean resultIsTime = rexCall.getType().getSqlTypeName() ==
SqlTypeName.TIME;
+ boolean hasTimeOperand = hasTimeOperand(rexCall);
+ if ("CAST".equals(operatorName)) {
+ // CAST between TIME-typed values keeps the carrier semantics (e.g.
+ // CAST(CURRENT_TIME AS TIME)); CAST to TIME from another type would
+ // lower to a velox cast(<type> AS INTEGER) that neither computes the
+ // milliseconds-of-day nor parses a time-of-day string, and CAST away
+ // from TIME (e.g. to VARCHAR) would expose the raw integer as the
+ // rendered value.
+ if (resultIsTime && !hasTimeOperand) {
+ throw new RuntimeException(
+ "Operator '"
+ + operatorName
+ + "' casts a non-TIME operand to TIME, which is not supported:
"
+ + TIME_UNSUPPORTED_SUFFIX
+ + " Offending node: "
+ + rexCall);
+ }
+ if (hasTimeOperand && !resultIsTime) {
+ throw new RuntimeException(
+ "Operator '"
+ + operatorName
+ + "' casts a TIME-typed operand away from TIME, which is not
supported: "
+ + TIME_UNSUPPORTED_SUFFIX
+ + " Offending node: "
+ + rexCall);
+ }
+ return;
+ }
+ if (COMPARISON_OPERATORS.contains(operatorName)
+ || CARRIER_PREDICATE_OPERATORS.contains(operatorName)) {
+ return;
+ }
+ if (resultIsTime) {
+ throw new RuntimeException(
+ "Operator '"
+ + operatorName
+ + "' produces a TIME-typed result, which is not supported: "
+ + TIME_UNSUPPORTED_SUFFIX
+ + " Offending node: "
+ + rexCall);
+ }
+ if (hasTimeOperand) {
+ throw new RuntimeException(
+ "Operator '"
+ + operatorName
+ + "' has a TIME-typed operand, which is not supported: "
+ + TIME_UNSUPPORTED_SUFFIX
+ + " Offending node: "
+ + rexCall);
+ }
+ }
+
+ private static boolean hasTimeOperand(RexCall rexCall) {
+ for (RexNode operand : rexCall.getOperands()) {
+ if (operand.getType().getSqlTypeName() == SqlTypeName.TIME) {
+ return true;
+ }
+ }
+ return false;
+ }
+
public static TypedExpr toTypedExpr(RexNode rexNode, RexConversionContext
context) {
if (rexNode instanceof RexLiteral) {
RexLiteral literal = (RexLiteral) rexNode;
return new ConstantTypedExpr(toType(literal.getType()),
toVariant(literal), null);
} else if (rexNode instanceof RexCall) {
RexCall rexCall = (RexCall) rexNode;
+ maybeRejectTimeTypedCall(rexCall);
if (rexCall.getOperator().getKind() == SqlKind.CAST &&
rexCall.getOperands().size() == 1) {
RexNode operand = rexCall.getOperands().get(0);
// An identity cast (types equal ignoring only nullability, e.g.
around folded
@@ -305,6 +409,11 @@ public class RexNodeConverter {
return new VarCharValue(comparable.toString());
case CHAR:
return new VarCharValue(((NlsString) comparable).getValue());
+ case TIME:
+ // Calcite holds TIME range bounds as TimeString — the raw form TIME
+ // RexLiterals store (see makeTimeLiteral); convert to the
+ // millisecond-of-day value the INTEGER carrier uses.
+ return new IntegerValue(((TimeString) comparable).getMillisOfDay());
default:
throw new RuntimeException("Unsupported range type: " + typeName);
}
diff --git
a/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/RexCallConverterFactory.java
b/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/RexCallConverterFactory.java
index 4979d06769..7bfc82a035 100644
---
a/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/RexCallConverterFactory.java
+++
b/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/RexCallConverterFactory.java
@@ -150,6 +150,16 @@ public class RexCallConverterFactory {
Map.entry(
"IS NOT FALSE", Arrays.asList(() -> new
DefaultRexCallConverter("is_not_false"))),
Map.entry("NOT", Arrays.asList(() -> new
DefaultRexCallConverter("not"))),
+ Map.entry(
+ "LOCALTIMESTAMP", Arrays.asList(() -> new
DefaultRexCallConverter("localtimestamp"))),
+ // LOCALTIME maps by name to the flinksql function returning the
+ // INTEGER carrier (milliseconds of day in the session timezone);
+ // the result type goes through toType(TIME(0)) -> IntegerType.
+ Map.entry("LOCALTIME", Arrays.asList(() -> new
DefaultRexCallConverter("localtime"))),
+ Map.entry(
+ "CURRENT_TIMESTAMP",
+ Arrays.asList(() -> new
DefaultRexCallConverter("current_timestamp"))),
+ Map.entry("NOW", Arrays.asList(() -> new
DefaultRexCallConverter("now"))),
Map.entry("CONCAT", Arrays.asList(() -> new
DefaultRexCallConverter("concat"))),
Map.entry("CONCAT_WS", Arrays.asList(() -> new
DefaultRexCallConverter("concat_ws"))),
// A literal negative length is rejected by SubstringRexCallConverter
diff --git
a/gluten-flink/ut/src/test/java/org/apache/gluten/rexnode/RexNodeConverterTest.java
b/gluten-flink/ut/src/test/java/org/apache/gluten/rexnode/RexNodeConverterTest.java
index bdb595557d..4d9a3cb7a9 100644
---
a/gluten-flink/ut/src/test/java/org/apache/gluten/rexnode/RexNodeConverterTest.java
+++
b/gluten-flink/ut/src/test/java/org/apache/gluten/rexnode/RexNodeConverterTest.java
@@ -33,6 +33,8 @@ import io.github.zhztheplayer.velox4j.variant.TimestampValue;
import io.github.zhztheplayer.velox4j.variant.VarCharValue;
import io.github.zhztheplayer.velox4j.variant.Variant;
+import
org.apache.flink.calcite.shaded.com.google.common.collect.ImmutableRangeSet;
+import org.apache.flink.calcite.shaded.com.google.common.collect.Range;
import org.apache.flink.table.planner.calcite.FlinkRexBuilder;
import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
import org.apache.flink.table.planner.calcite.FlinkTypeSystem;
@@ -46,13 +48,17 @@ import org.apache.calcite.rex.RexInputRef;
import org.apache.calcite.rex.RexLiteral;
import org.apache.calcite.sql.SqlFunction;
import org.apache.calcite.sql.SqlIntervalQualifier;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.util.Sarg;
import org.apache.calcite.util.TimeString;
import org.apache.calcite.util.TimestampString;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import java.util.List;
+
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -523,4 +529,208 @@ public class RexNodeConverterTest {
assertThat(((CallTypedExpr) expr).getFunctionName()).isEqualTo("cast");
assertThat(((CallTypedExpr) expr).getInputs()).hasSize(1);
}
+
+ // R1: CURRENT_TIMESTAMP maps by name to the flinksql current_timestamp
+ // with a Timestamp (TIMESTAMP_LTZ(3)) result.
+ @Test
+ public void testCurrentTimestampMapping() {
+ RexCall call = (RexCall)
rexBuilder.makeCall(SqlStdOperatorTable.CURRENT_TIMESTAMP);
+ TypedExpr expr = RexNodeConverter.toTypedExpr(call, new
RexConversionContext(List.of()));
+ assertThat(expr).isInstanceOf(CallTypedExpr.class);
+ assertThat(((CallTypedExpr)
expr).getFunctionName()).isEqualTo("current_timestamp");
+ assertThat(expr.getReturnType()).isInstanceOf(TimestampType.class);
+ }
+
+ // R2: NOW has no named static field — it is registered at runtime via
+ // FlinkSqlOperatorTable.dynamicFunctions (streaming mode here), matching
+ // the operator shape the Flink planner hands to the converter.
+ @Test
+ public void testNowMapping() {
+ RexCall call =
+ (RexCall)
+ rexBuilder.makeCall(
+ FlinkSqlOperatorTable.dynamicFunctions(false /* isBatchMode
*/).stream()
+ .filter(f -> f.getName().equals("NOW"))
+ .findFirst()
+ .get());
+ TypedExpr expr = RexNodeConverter.toTypedExpr(call, new
RexConversionContext(List.of()));
+ assertThat(expr).isInstanceOf(CallTypedExpr.class);
+ assertThat(((CallTypedExpr) expr).getFunctionName()).isEqualTo("now");
+ assertThat(expr.getReturnType()).isInstanceOf(TimestampType.class);
+ }
+
+ // R3: LOCALTIMESTAMP maps by name to the flinksql localtimestamp with a
+ // Timestamp (TIMESTAMP(3)) result.
+ @Test
+ public void testLocalTimestampMapping() {
+ RexCall call = (RexCall)
rexBuilder.makeCall(SqlStdOperatorTable.LOCALTIMESTAMP);
+ TypedExpr expr = RexNodeConverter.toTypedExpr(call, new
RexConversionContext(List.of()));
+ assertThat(expr).isInstanceOf(CallTypedExpr.class);
+ assertThat(((CallTypedExpr)
expr).getFunctionName()).isEqualTo("localtimestamp");
+ assertThat(expr.getReturnType()).isInstanceOf(TimestampType.class);
+ }
+
+ // R4: LOCALTIME maps by name to the flinksql localtime with an INTEGER
+ // result — the TIME(0) milliseconds-of-day carrier.
+ @Test
+ public void testLocalTimeMapping() {
+ RexCall call = (RexCall)
rexBuilder.makeCall(SqlStdOperatorTable.LOCALTIME);
+ TypedExpr expr = RexNodeConverter.toTypedExpr(call, new
RexConversionContext(List.of()));
+ assertThat(expr).isInstanceOf(CallTypedExpr.class);
+ assertThat(((CallTypedExpr)
expr).getFunctionName()).isEqualTo("localtime");
+ assertThat(expr.getReturnType()).isInstanceOf(IntegerType.class);
+ }
+
+ // G1: CAST(LOCALTIME AS STRING) is rejected — casting the TIME carrier
+ // away from TIME would expose the raw integer.
+ @Test
+ public void testCastLocalTimeToStringRejected() {
+ RexCall localtime = (RexCall)
rexBuilder.makeCall(SqlStdOperatorTable.LOCALTIME);
+ RexCall cast =
+ (RexCall)
rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.VARCHAR), localtime);
+ assertThatThrownBy(
+ () -> RexNodeConverter.toTypedExpr(cast, new
RexConversionContext(List.of())))
+ .isInstanceOf(RuntimeException.class)
+ .hasMessageContaining("CAST")
+ .hasMessageContaining("milliseconds-of-day carrier");
+ }
+
+ // G2: EXTRACT(HOUR FROM LOCALTIME) is rejected — the velox extract
+ // signature has no TIME form and would read the raw integer carrier.
+ @Test
+ public void testExtractHourFromLocalTimeRejected() {
+ RexCall localtime = (RexCall)
rexBuilder.makeCall(SqlStdOperatorTable.LOCALTIME);
+ RexCall extract =
+ (RexCall)
+ rexBuilder.makeCall(
+ SqlStdOperatorTable.EXTRACT,
rexBuilder.makeFlag(TimeUnit.HOUR), localtime);
+ assertThatThrownBy(
+ () -> RexNodeConverter.toTypedExpr(extract, new
RexConversionContext(List.of())))
+ .isInstanceOf(RuntimeException.class)
+ .hasMessageContaining("EXTRACT")
+ .hasMessageContaining("milliseconds-of-day carrier");
+ }
+
+ // G3: LOCALTIME < TIME '12:00:00' is supported — comparisons on the
+ // INTEGER carrier keep TIME ordering semantics (CURRENT_TIME comparisons
+ // are part of the same supported surface).
+ @Test
+ public void testLocalTimeComparisonAllowed() {
+ RexCall localtime = (RexCall)
rexBuilder.makeCall(SqlStdOperatorTable.LOCALTIME);
+ RexLiteral noon = rexBuilder.makeTimeLiteral(new TimeString(12, 0, 0), 0);
+ RexCall lessThan =
+ (RexCall) rexBuilder.makeCall(SqlStdOperatorTable.LESS_THAN,
localtime, noon);
+ TypedExpr expr = RexNodeConverter.toTypedExpr(lessThan, new
RexConversionContext(List.of()));
+ assertThat(expr).isInstanceOf(CallTypedExpr.class);
+ }
+
+ // G4: CASE WHEN b THEN LOCALTIME ELSE LOCALTIME END is rejected — the
+ // TIME-typed result would silently integer-ize.
+ @Test
+ public void testCaseOverLocalTimeRejected() {
+ RexCall localtime = (RexCall)
rexBuilder.makeCall(SqlStdOperatorTable.LOCALTIME);
+ RexInputRef condition =
+
rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.BOOLEAN), 0);
+ RexCall caseCall =
+ (RexCall) rexBuilder.makeCall(SqlStdOperatorTable.CASE, condition,
localtime, localtime);
+ assertThatThrownBy(
+ () -> RexNodeConverter.toTypedExpr(caseCall, new
RexConversionContext(List.of())))
+ .isInstanceOf(RuntimeException.class)
+ .hasMessageContaining("CASE")
+ .hasMessageContaining("milliseconds-of-day carrier");
+ }
+
+ // G5: the whitelist — the LOCALTIME direct projection itself converts
+ // (INTEGER carrier), and a TIME-typed InputRef flows through as the
+ // isomorphic carrier.
+ @Test
+ public void testLocalTimeDirectProjectionAndInputRefAllowed() {
+ RexCall localtime = (RexCall)
rexBuilder.makeCall(SqlStdOperatorTable.LOCALTIME);
+ TypedExpr expr =
+ RexNodeConverter.toTypedExpr(localtime, new
RexConversionContext(java.util.List.of("f0")));
+ assertThat(expr).isInstanceOf(CallTypedExpr.class);
+ assertThat(((CallTypedExpr)
expr).getFunctionName()).isEqualTo("localtime");
+ assertThat(expr.getReturnType()).isInstanceOf(IntegerType.class);
+
+ RexInputRef timeRef =
rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.TIME), 0);
+ TypedExpr refExpr =
+ RexNodeConverter.toTypedExpr(timeRef, new
RexConversionContext(java.util.List.of("f0")));
+ assertThat(refExpr).isInstanceOf(FieldAccessTypedExpr.class);
+ assertThat(((FieldAccessTypedExpr)
refExpr).getFieldName()).isEqualTo("f0");
+ assertThat(refExpr.getReturnType()).isInstanceOf(IntegerType.class);
+ }
+
+ // G6: CAST to TIME keeps carrier semantics only when the operand is already
+ // TIME — CAST(CURRENT_TIME AS TIME) (the upstream surface) passes, while
+ // CAST(<timestamp or varchar column> AS TIME) would lower to a velox
+ // cast(<type> AS INTEGER) that neither computes the milliseconds-of-day
+ // nor parses a time-of-day string, so it is rejected at plan time.
+ @Test
+ public void testCastToTimeFromNonTimeOperandRejected() {
+ RexCall castTime = (RexCall)
rexBuilder.makeCall(SqlStdOperatorTable.LOCALTIME);
+ RexCall cast =
+ (RexCall)
rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.TIME), castTime);
+ TypedExpr timeToTime = RexNodeConverter.toTypedExpr(cast, new
RexConversionContext(List.of()));
+ assertThat(timeToTime).isInstanceOf(CallTypedExpr.class);
+
+ RexInputRef tsRef =
+
rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.TIMESTAMP, 3), 0);
+ RexCall tsCast =
+ (RexCall)
rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.TIME), tsRef);
+ assertThatThrownBy(
+ () -> RexNodeConverter.toTypedExpr(tsCast, new
RexConversionContext(List.of())))
+ .isInstanceOf(RuntimeException.class)
+ .hasMessageContaining("CAST")
+ .hasMessageContaining("casts a non-TIME operand to TIME")
+ .hasMessageContaining("milliseconds-of-day carrier");
+
+ RexInputRef strRef =
rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.VARCHAR), 0);
+ RexCall strCast =
+ (RexCall)
rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.TIME), strRef);
+ assertThatThrownBy(
+ () -> RexNodeConverter.toTypedExpr(strCast, new
RexConversionContext(List.of())))
+ .isInstanceOf(RuntimeException.class)
+ .hasMessageContaining("CAST")
+ .hasMessageContaining("casts a non-TIME operand to TIME")
+ .hasMessageContaining("milliseconds-of-day carrier");
+ }
+
+ // G7: IS [NOT] NULL and SEARCH over a TIME-typed operand are
+ // carrier-isomorphic — a nullability check and a point / range search on
+ // the INTEGER carrier — and plan through the existing converters: IS [NOT]
+ // NULL lowers to isnull / isnotnull over the carrier, and SEARCH (the
+ // Calcite form of IN) lowers to "in" over the carrier millisecond-of-day
+ // points.
+ @Test
+ public void testCarrierPredicatesOverTimeAllowed() {
+ RexInputRef timeRef =
rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.TIME), 0);
+ RexConversionContext context = new RexConversionContext(List.of("t"));
+
+ RexCall isNotNull = (RexCall)
rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, timeRef);
+ TypedExpr isNotNullExpr = RexNodeConverter.toTypedExpr(isNotNull, context);
+ assertThat(isNotNullExpr).isInstanceOf(CallTypedExpr.class);
+ assertThat(((CallTypedExpr)
isNotNullExpr).getFunctionName()).isEqualTo("isnotnull");
+
+ RexCall isNull = (RexCall)
rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, timeRef);
+ TypedExpr isNullExpr = RexNodeConverter.toTypedExpr(isNull, context);
+ assertThat(isNullExpr).isInstanceOf(CallTypedExpr.class);
+ assertThat(((CallTypedExpr)
isNullExpr).getFunctionName()).isEqualTo("isnull");
+
+ TimeString noon = new TimeString(12, 0, 0);
+ TimeString halfPastNoon = new TimeString(12, 30, 0);
+ RexLiteral searchLiteral =
+ rexBuilder.makeSearchArgumentLiteral(
+ Sarg.of(
+ false,
+ ImmutableRangeSet.<TimeString>builder()
+ .add(Range.closed(noon, noon))
+ .add(Range.closed(halfPastNoon, halfPastNoon))
+ .build()),
+ typeFactory.createSqlType(SqlTypeName.TIME));
+ RexCall search =
+ (RexCall) rexBuilder.makeCall(SqlStdOperatorTable.SEARCH, timeRef,
searchLiteral);
+ TypedExpr searchExpr = RexNodeConverter.toTypedExpr(search, context);
+ assertThat(searchExpr).isInstanceOf(CallTypedExpr.class);
+ assertThat(((CallTypedExpr) searchExpr).getFunctionName()).isEqualTo("in");
+ }
}
diff --git
a/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/TemporalFunctionsTest.java
b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/TemporalFunctionsTest.java
new file mode 100644
index 0000000000..58d6f2487f
--- /dev/null
+++
b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/TemporalFunctionsTest.java
@@ -0,0 +1,273 @@
+/*
+ * 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.gluten.table.runtime.stream.custom;
+
+import org.apache.gluten.table.runtime.stream.common.GlutenStreamingTestBase;
+
+import org.apache.flink.core.execution.JobClient;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.TableResult;
+import org.apache.flink.types.Row;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import com.sun.jna.Library;
+import com.sun.jna.Native;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Native streaming execution tests for the temporal functions:
CURRENT_TIMESTAMP, NOW(),
+ * LOCALTIMESTAMP, LOCALTIME — same base as the other stream tests (bounded
values source, print
+ * sink). The GFV print sink renders TIMESTAMP_LTZ(3)/TIMESTAMP(3) as ISO
local strings (no zone
+ * suffix) and LOCALTIME as the bare INTEGER milliseconds-of-day carrier, so
the assertions parse
+ * those shapes. Every assertion is wall-clock bounded (never a fixed value)
plus the per-record
+ * consistency invariants.
+ */
+class TemporalFunctionsTest extends GlutenStreamingTestBase {
+
+ private static final long TIMEOUT_MS = 30000;
+ private static final long WINDOW_EPSILON_MS = 2000;
+ private static final long MILLIS_PER_DAY = 86_400_000L;
+
+ // dup2 fd=1 onto a file: Velox print sink writes to std::cout, which
bypasses System.setOut
+ // and goes straight to the process's fd=1.
+ private interface CLibrary extends Library {
+ int dup(int oldfd);
+
+ int dup2(int oldfd, int newfd);
+
+ int open(String path, int flags, int mode);
+
+ int close(int fd);
+ }
+
+ private static final CLibrary C_LIB = Native.load("c", CLibrary.class);
+ private static final int O_WRONLY = 1;
+ private static final int O_CREAT = 0100;
+ private static final int O_TRUNC = 01000;
+
+ @Override
+ @BeforeEach
+ public void before() throws Exception {
+ super.before();
+ }
+
+ // Runs the query through the print sink (same mechanism as runAndCheck) and
returns the
+ // collected output lines instead of asserting against a fixed list — the
temporal values are
+ // wall-clock dependent.
+ private List<String> runAndCollect(String query) throws Exception {
+ String printResultDirPath = System.getProperty("user.dir") + "/log/";
+ new File(printResultDirPath).mkdirs();
+ String printResultFilePath = printResultDirPath + "taskmanager.out";
+ File printResultFile = new File(printResultFilePath);
+ if (printResultFile.exists()) {
+ printResultFile.delete();
+ }
+
+ int savedStdout = C_LIB.dup(1);
+ int fileFd = C_LIB.open(printResultFilePath, O_WRONLY | O_CREAT | O_TRUNC,
0644);
+ if (fileFd < 0) {
+ C_LIB.close(savedStdout);
+ throw new FlinkRuntimeException("Failed to open " + printResultFilePath);
+ }
+ C_LIB.dup2(fileFd, 1);
+ try {
+ Table table = tEnv().sqlQuery(query);
+ createPrintSinkTable("printT", table.getResolvedSchema());
+ String newQuery = String.format("insert into %s %s", "printT", query);
+ TableResult tableResult = tEnv().executeSql(newQuery);
+ assertThat(tableResult.getJobClient().isPresent()).isTrue();
+ JobClient jobClient = tableResult.getJobClient().get();
+ try {
+ long startTime = System.currentTimeMillis();
+ while (printResultFile.length() == 0) {
+ if (System.currentTimeMillis() - startTime > TIMEOUT_MS) {
+ break;
+ }
+ Thread.sleep(10);
+ }
+ long fileSize = -1L;
+ startTime = System.currentTimeMillis();
+ while (printResultFile.length() > fileSize) {
+ if (System.currentTimeMillis() - startTime > TIMEOUT_MS) {
+ break;
+ }
+ fileSize = printResultFile.length();
+ Thread.sleep(3000);
+ }
+ } catch (InterruptedException ie) {
+ Thread.currentThread().interrupt();
+ throw new FlinkRuntimeException(ie);
+ } finally {
+ jobClient.cancel();
+ }
+ } finally {
+ C_LIB.dup2(savedStdout, 1);
+ C_LIB.close(fileFd);
+ C_LIB.close(savedStdout);
+ }
+
+ List<String> result = new ArrayList<>();
+ try (FileReader fr = new FileReader(printResultFile);
+ BufferedReader br = new BufferedReader(fr)) {
+ String line;
+ while ((line = br.readLine()) != null) {
+ result.add(line);
+ }
+ }
+ tEnv().executeSql("drop table if exists printT");
+ return result;
+ }
+
+ private static String[] fieldsOf(String line) {
+ int begin = line.indexOf('[');
+ int end = line.lastIndexOf(']');
+ return line.substring(begin + 1, end).split(", ");
+ }
+
+ // The printed TIMESTAMP_LTZ(3) value is the session-zone wall clock; parse
+ // it back to epoch millis.
+ private static long epochOfPrintedTimestamp(String printed, ZoneId
sessionTz) {
+ return
LocalDateTime.parse(printed).atZone(sessionTz).toInstant().toEpochMilli();
+ }
+
+ // The printed TIMESTAMP(3) value is a naive local wall clock; its "epoch"
+ // is the clock face read as UTC.
+ private static long epochOfPrintedNaiveTimestamp(String printed) {
+ return
LocalDateTime.parse(printed).atZone(ZoneId.of("UTC")).toInstant().toEpochMilli();
+ }
+
+ private static long millisOfDayOfPrintedNaiveTimestamp(String printed) {
+ LocalDateTime wall = LocalDateTime.parse(printed);
+ return wall.getHour() * 3_600_000L
+ + wall.getMinute() * 60_000L
+ + wall.getSecond() * 1_000L
+ + (wall.getNano() / 1_000_000L);
+ }
+
+ private static List<Row> sevenRows() {
+ List<Row> rows = new ArrayList<>();
+ for (int i = 1; i <= 7; i++) {
+ rows.add(Row.of(i));
+ }
+ return rows;
+ }
+
+ // E1: UTC session — per-record bounded wall-clock window, same-row
+ // CURRENT_TIMESTAMP == NOW (string equality at ms precision), LOCALTIME
+ // is the INTEGER carrier in [0, 86'400'000) and equals exactly the
+ // milliseconds-of-day derived from the same row's LOCALTIMESTAMP.
+ @Test
+ void testTemporalSameRowConsistencyUtc() throws Exception {
+ tEnv().getConfig().setLocalTimeZone(ZoneId.of("UTC"));
+ createSimpleBoundedValuesTable("tblTemporalUtc", "id int", sevenRows());
+ String query =
+ "select id, CURRENT_TIMESTAMP as cts, NOW() as nws, LOCALTIMESTAMP as
lts, LOCALTIME as ltime from tblTemporalUtc";
+
+ long t0 = System.currentTimeMillis();
+ List<String> lines = runAndCollect(query);
+ long t1 = System.currentTimeMillis();
+
+ assertThat(lines).hasSize(7);
+ for (String line : lines) {
+ String[] fields = fieldsOf(line);
+ assertThat(fields).hasSize(5);
+ long ct = epochOfPrintedTimestamp(fields[1], ZoneId.of("UTC"));
+ long nw = epochOfPrintedTimestamp(fields[2], ZoneId.of("UTC"));
+ long lt = epochOfPrintedNaiveTimestamp(fields[3]);
+ long localTime = Long.parseLong(fields[4]);
+
+ // Bounded wall-clock window (RC-07): never a fixed value.
+ assertThat(ct).isBetween(t0 - WINDOW_EPSILON_MS, t1 + WINDOW_EPSILON_MS);
+ assertThat(lt).isBetween(t0 - WINDOW_EPSILON_MS, t1 + WINDOW_EPSILON_MS);
+
+ // Same-row consistency (RC-01): one shared instant per record.
+ assertThat(fields[1]).isEqualTo(fields[2]);
+ assertThat(ct).isEqualTo(lt);
+
+ // LOCALTIME carrier (RC-02 domain): INTEGER in [0, 86'400'000) and
+ // exactly the ms-of-day of the same row's LOCALTIMESTAMP.
+ assertThat(localTime).isBetween(0L, MILLIS_PER_DAY - 1);
+
assertThat(localTime).isEqualTo(millisOfDayOfPrintedNaiveTimestamp(fields[3]));
+ }
+ }
+
+ // E2: Asia/Shanghai session — same invariants with the +8h session
+ // offset: LOCALTIMESTAMP leads CURRENT_TIMESTAMP by the zone offset, and
+ // LOCALTIME is the ms-of-day of the Shanghai clock face.
+ @Test
+ void testTemporalSameRowConsistencyShanghai() throws Exception {
+ ZoneId shanghai = ZoneId.of("Asia/Shanghai");
+ tEnv().getConfig().setLocalTimeZone(shanghai);
+ createSimpleBoundedValuesTable("tblTemporalSh", "id int", sevenRows());
+ String query =
+ "select id, CURRENT_TIMESTAMP as cts, NOW() as nws, LOCALTIMESTAMP as
lts, LOCALTIME as ltime from tblTemporalSh";
+
+ long t0 = System.currentTimeMillis();
+ List<String> lines = runAndCollect(query);
+ long t1 = System.currentTimeMillis();
+
+ assertThat(lines).hasSize(7);
+ long offsetMillis = 8L * 3600 * 1000;
+ for (String line : lines) {
+ String[] fields = fieldsOf(line);
+ assertThat(fields).hasSize(5);
+ // The print sink renders TIMESTAMP_LTZ(3) on the UTC clock face
+ // regardless of the session zone (known behavior, consistent with
+ // native Flink), so parse it as a naive UTC clock.
+ long ct = epochOfPrintedNaiveTimestamp(fields[1]);
+ long lt = epochOfPrintedNaiveTimestamp(fields[3]);
+ long localTime = Long.parseLong(fields[4]);
+
+ assertThat(ct).isBetween(t0 - WINDOW_EPSILON_MS, t1 + WINDOW_EPSILON_MS);
+
+ // Same-row alias equality.
+ assertThat(fields[1]).isEqualTo(fields[2]);
+
+ // LOCALTIMESTAMP = shared instant shifted by the session offset.
+ assertThat(lt - ct)
+ .isBetween(offsetMillis - WINDOW_EPSILON_MS, offsetMillis +
WINDOW_EPSILON_MS);
+
+ // LOCALTIME is the +8h clock face's ms-of-day.
+ assertThat(localTime).isBetween(0L, MILLIS_PER_DAY - 1);
+
assertThat(localTime).isEqualTo(millisOfDayOfPrintedNaiveTimestamp(fields[3]));
+ }
+ }
+
+ // E3: the same-row equality survives the filter path — the predicate
+ // comparing two cast renderings of the shared instant is always true, so
+ // all rows pass.
+ @Test
+ void testTemporalFilterSameRowEquality() throws Exception {
+ tEnv().getConfig().setLocalTimeZone(ZoneId.of("UTC"));
+ createSimpleBoundedValuesTable("tblTemporalFilter", "id int", sevenRows());
+ String query =
+ "select id from tblTemporalFilter"
+ + " where CAST(CURRENT_TIMESTAMP AS STRING) = CAST(NOW() AS
STRING)";
+ runAndCheck(query, List.of("+I[1]", "+I[2]", "+I[3]", "+I[4]", "+I[5]",
"+I[6]", "+I[7]"));
+ }
+}
diff --git
a/gluten-flink/ut/src/test/java/org/apache/gluten/util/LogicalTypeConverterTest.java
b/gluten-flink/ut/src/test/java/org/apache/gluten/util/LogicalTypeConverterTest.java
new file mode 100644
index 0000000000..dd9219f826
--- /dev/null
+++
b/gluten-flink/ut/src/test/java/org/apache/gluten/util/LogicalTypeConverterTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.gluten.util;
+
+import io.github.zhztheplayer.velox4j.type.IntegerType;
+import io.github.zhztheplayer.velox4j.type.TimestampType;
+import io.github.zhztheplayer.velox4j.type.Type;
+
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.TimeType;
+
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class LogicalTypeConverterTest {
+
+ // L1: TIME(0) maps to the INTEGER milliseconds-of-day carrier.
+ @Test
+ public void testTimeZeroMapsToInteger() {
+ Type type = LogicalTypeConverter.toVLType(new TimeType(0));
+ assertThat(type).isInstanceOf(IntegerType.class);
+ }
+
+ // L2: TIME(3) maps to INTEGER — the carrier does not carry the precision.
+ @Test
+ public void testTimeThreeMapsToInteger() {
+ Type type = LogicalTypeConverter.toVLType(new TimeType(3));
+ assertThat(type).isInstanceOf(IntegerType.class);
+ }
+
+ // L3: TIME(6) maps to INTEGER.
+ @Test
+ public void testTimeSixMapsToInteger() {
+ Type type = LogicalTypeConverter.toVLType(new TimeType(6));
+ assertThat(type).isInstanceOf(IntegerType.class);
+ }
+
+ // L4: TIME(9) maps to INTEGER.
+ @Test
+ public void testTimeNineMapsToInteger() {
+ Type type = LogicalTypeConverter.toVLType(new TimeType(9));
+ assertThat(type).isInstanceOf(IntegerType.class);
+ }
+
+ // L5: the neighboring anchor types keep mapping to Timestamp — TIME is
+ // not conflated with the timestamp family.
+ @Test
+ public void testTimestampAnchorsMapToTimestamp() {
+ Type timestamp =
+ LogicalTypeConverter.toVLType(new
org.apache.flink.table.types.logical.TimestampType(3));
+ assertThat(timestamp).isInstanceOf(TimestampType.class);
+
+ Type timestampLtz = LogicalTypeConverter.toVLType(new
LocalZonedTimestampType(3));
+ assertThat(timestampLtz).isInstanceOf(TimestampType.class);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]