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 1f67005372 [GLUTEN-13480][FLINK] Add support for SHA2 / SHA224 / 
SHA256 / SHA384 / SHA512 (#13484)
1f67005372 is described below

commit 1f6700537266687d1691743e6ba5cbfacef8f092
Author: TianJialong <[email protected]>
AuthorDate: Mon Nov 9 13:04:07 2026 +0800

    [GLUTEN-13480][FLINK] Add support for SHA2 / SHA224 / SHA256 / SHA384 / 
SHA512 (#13484)
---
 .../rexnode/functions/RexCallConverterFactory.java |  10 +
 .../rexnode/functions/Sha2RexCallConverter.java    |  68 ++++++
 .../gluten/rexnode/RexNodeConverterTest.java       | 135 ++++++++++++
 .../runtime/stream/custom/HashFunctionsTest.java   | 240 +++++++++++++++++++++
 4 files changed, 453 insertions(+)

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 b148fb4877..f452842617 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
@@ -108,6 +108,16 @@ public class RexCallConverterFactory {
           Map.entry(
               "REGEXP_EXTRACT", Arrays.asList(() -> new 
DefaultRexCallConverter("regexp_extract"))),
           Map.entry("LOWER", Arrays.asList(() -> new 
DefaultRexCallConverter("lower"))),
+          // Direct name mapping: the SHA-2 family. The fixed-length spellings
+          // are the sha2 core with the length bound on the velox side; the
+          // hashLength operand is routed by the velox sha2 signature set.
+          // SHA2 itself carries a converter-side validation of a constant
+          // hashLength (see Sha2RexCallConverter).
+          Map.entry("SHA2", Arrays.asList(() -> new Sha2RexCallConverter())),
+          Map.entry("SHA224", Arrays.asList(() -> new 
DefaultRexCallConverter("sha224"))),
+          Map.entry("SHA256", Arrays.asList(() -> new 
DefaultRexCallConverter("sha256"))),
+          Map.entry("SHA384", Arrays.asList(() -> new 
DefaultRexCallConverter("sha384"))),
+          Map.entry("SHA512", Arrays.asList(() -> new 
DefaultRexCallConverter("sha512"))),
           Map.entry("count_char", Arrays.asList(() -> new 
DefaultRexCallConverter("count_char"))),
           Map.entry("EXTRACT", Arrays.asList(() -> new 
DefaultRexCallConverter("extract"))),
           Map.entry("IS TRUE", Arrays.asList(() -> new 
IsTrueRexCallConverter())),
diff --git 
a/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/Sha2RexCallConverter.java
 
b/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/Sha2RexCallConverter.java
new file mode 100644
index 0000000000..3b8c413531
--- /dev/null
+++ 
b/gluten-flink/planner/src/main/java/org/apache/gluten/rexnode/functions/Sha2RexCallConverter.java
@@ -0,0 +1,68 @@
+/*
+ * 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.rexnode.functions;
+
+import org.apache.gluten.rexnode.RexConversionContext;
+import org.apache.gluten.rexnode.ValidationResult;
+
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+
+/**
+ * Converts Flink SHA2 to the velox flinksql sha2 function.
+ *
+ * <p>A constant hashLength is validated at conversion time: Flink checks the 
constant length in the
+ * generated operator's constructor ({@code StringCallGen#generateSha2} hands 
it to {@code
+ * CodeGeneratorContext#addReusableSha2MessageDigest}, which throws 
"Unsupported algorithm." for
+ * anything outside {224, 256, 384, 512}), so a job with a bad constant length 
fails regardless of
+ * input — even on an empty table — while velox rejects per row and would 
silently succeed on zero
+ * rows. Rejecting the constant form here keeps both stacks failing before any 
row is processed.
+ *
+ * <p>A NULL constant and a column reference cannot be checked at plan time 
and keep the existing
+ * runtime rejection in velox, matching Flink's per-row behavior.
+ */
+public class Sha2RexCallConverter extends DefaultRexCallConverter {
+
+  public Sha2RexCallConverter() {
+    super("sha2");
+  }
+
+  @Override
+  public ValidationResult isSuitable(RexCall callNode, RexConversionContext 
context) {
+    if (callNode.getOperands().size() == 2) {
+      RexNode hashLength = callNode.getOperands().get(1);
+      if (hashLength instanceof RexLiteral) {
+        // getValue() covers every integer width exactly 
(Byte/Short/Integer/Long) and is
+        // null for a NULL literal; longValue() avoids int-range truncation 
surprises.
+        Object value = ((RexLiteral) hashLength).getValue();
+        if (value instanceof Number) {
+          long length = ((Number) value).longValue();
+          if (length != 224 && length != 256 && length != 384 && length != 
512) {
+            return ValidationResult.failure(
+                String.format(
+                    "SHA2 with literal hashLength %d is not supported: 
supported lengths are"
+                        + " 224, 256, 384 and 512 (Flink rejects an 
unsupported constant length"
+                        + " in the generated operator's constructor as well)",
+                    length));
+          }
+        }
+      }
+    }
+    return ValidationResult.success();
+  }
+}
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 8809a15311..b9480da0a0 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
@@ -16,6 +16,14 @@
  */
 package org.apache.gluten.rexnode;
 
+import org.apache.gluten.rexnode.functions.RexCallConverterFactory;
+
+import io.github.zhztheplayer.velox4j.expression.CallTypedExpr;
+import io.github.zhztheplayer.velox4j.expression.ConstantTypedExpr;
+import io.github.zhztheplayer.velox4j.type.BigIntType;
+import io.github.zhztheplayer.velox4j.type.IntegerType;
+import io.github.zhztheplayer.velox4j.type.VarCharType;
+import io.github.zhztheplayer.velox4j.variant.BigIntValue;
 import io.github.zhztheplayer.velox4j.variant.IntegerValue;
 import io.github.zhztheplayer.velox4j.variant.VarCharValue;
 import io.github.zhztheplayer.velox4j.variant.Variant;
@@ -23,11 +31,15 @@ import io.github.zhztheplayer.velox4j.variant.Variant;
 import org.apache.flink.table.planner.calcite.FlinkRexBuilder;
 import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
 import org.apache.flink.table.planner.calcite.FlinkTypeSystem;
+import org.apache.flink.table.planner.functions.sql.FlinkSqlOperatorTable;
 
 import org.apache.calcite.avatica.util.TimeUnit;
 import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexCall;
+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.parser.SqlParserPos;
 import org.apache.calcite.sql.type.SqlTypeName;
@@ -36,6 +48,7 @@ import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
 public class RexNodeConverterTest {
 
@@ -88,6 +101,128 @@ public class RexNodeConverterTest {
     assertThat(((VarCharValue) variant).getValue()).isEqualTo("banana");
   }
 
+  @Test
+  public void testSha2FamilyCallsMapToVeloxNames() {
+    RexInputRef stringFieldRef =
+        
rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.VARCHAR), 1);
+    RexInputRef intFieldRef =
+        
rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.INTEGER), 2);
+    RexConversionContext context =
+        new RexConversionContext(java.util.Arrays.asList("ignored", "s", "n"));
+
+    RexCall sha2 =
+        (RexCall) rexBuilder.makeCall(FlinkSqlOperatorTable.SHA2, 
stringFieldRef, intFieldRef);
+    CallTypedExpr sha2Expr =
+        (CallTypedExpr)
+            RexCallConverterFactory.getConverter(sha2, 
context).toTypedExpr(sha2, context);
+    assertThat(sha2Expr.getFunctionName()).isEqualTo("sha2");
+    assertThat(sha2Expr.getReturnType()).isInstanceOf(VarCharType.class);
+    assertThat(sha2Expr.getInputs()).hasSize(2);
+
+    SqlFunction[] fixedLengthOps = {
+      FlinkSqlOperatorTable.SHA224,
+      FlinkSqlOperatorTable.SHA256,
+      FlinkSqlOperatorTable.SHA384,
+      FlinkSqlOperatorTable.SHA512
+    };
+    String[] veloxNames = {"sha224", "sha256", "sha384", "sha512"};
+    for (int i = 0; i < fixedLengthOps.length; i++) {
+      RexCall call = (RexCall) rexBuilder.makeCall(fixedLengthOps[i], 
stringFieldRef);
+      CallTypedExpr expr =
+          (CallTypedExpr)
+              RexCallConverterFactory.getConverter(call, 
context).toTypedExpr(call, context);
+      assertThat(expr.getFunctionName()).isEqualTo(veloxNames[i]);
+      assertThat(expr.getReturnType()).isInstanceOf(VarCharType.class);
+      assertThat(expr.getInputs()).hasSize(1);
+    }
+  }
+
+  @Test
+  public void testSha2LiteralInvalidLengthRejected() {
+    // Flink validates a constant hashLength in the generated operator's
+    // constructor (CodeGeneratorContext#addReusableSha2MessageDigest throws
+    // "Unsupported algorithm." for anything outside {224, 256, 384, 512}) and
+    // fails the job even on empty input; the conversion rejects the constant
+    // form the same way, while the column form keeps the per-row rejection.
+    RexInputRef stringFieldRef =
+        
rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.VARCHAR), 1);
+    RexConversionContext context =
+        new RexConversionContext(java.util.Arrays.asList("ignored", "s", "n"));
+
+    int[] invalidLengths = {0, 385, -1, 1};
+    for (int length : invalidLengths) {
+      RexLiteral literal =
+          (RexLiteral)
+              rexBuilder.makeLiteral(length, 
typeFactory.createSqlType(SqlTypeName.INTEGER), false);
+      RexCall call =
+          (RexCall) rexBuilder.makeCall(FlinkSqlOperatorTable.SHA2, 
stringFieldRef, literal);
+      assertThatThrownBy(() -> RexCallConverterFactory.getConverter(call, 
context))
+          .hasMessageContaining(
+              String.format("SHA2 with literal hashLength %d is not 
supported", length));
+    }
+
+    // The BIGINT form (a folded CAST(... AS BIGINT)) rejects too.
+    RexLiteral bigZero =
+        (RexLiteral)
+            rexBuilder.makeLiteral(0L, 
typeFactory.createSqlType(SqlTypeName.BIGINT), false);
+    RexCall bigCall =
+        (RexCall) rexBuilder.makeCall(FlinkSqlOperatorTable.SHA2, 
stringFieldRef, bigZero);
+    assertThatThrownBy(() -> RexCallConverterFactory.getConverter(bigCall, 
context))
+        .hasMessageContaining("SHA2 with literal hashLength 0 is not 
supported");
+  }
+
+  @Test
+  public void testSha2LiteralValidAndNullLengthStillConvert() {
+    RexInputRef stringFieldRef =
+        
rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.VARCHAR), 1);
+    RexConversionContext context =
+        new RexConversionContext(java.util.Arrays.asList("ignored", "s", "n"));
+
+    // A valid INT constant reaches velox intact.
+    RexLiteral intLength =
+        (RexLiteral)
+            rexBuilder.makeLiteral(256, 
typeFactory.createSqlType(SqlTypeName.INTEGER), false);
+    RexCall intCall =
+        (RexCall) rexBuilder.makeCall(FlinkSqlOperatorTable.SHA2, 
stringFieldRef, intLength);
+    CallTypedExpr intExpr =
+        (CallTypedExpr)
+            RexCallConverterFactory.getConverter(intCall, 
context).toTypedExpr(intCall, context);
+    assertThat(intExpr.getFunctionName()).isEqualTo("sha2");
+    assertThat(intExpr.getInputs()).hasSize(2);
+    ConstantTypedExpr length = (ConstantTypedExpr) intExpr.getInputs().get(1);
+    assertThat(length.getReturnType()).isInstanceOf(IntegerType.class);
+    assertThat(((IntegerValue) length.getValue()).getValue()).isEqualTo(256);
+
+    // A valid BIGINT constant routes through the int64 signature.
+    RexLiteral bigLength =
+        (RexLiteral)
+            rexBuilder.makeLiteral(512L, 
typeFactory.createSqlType(SqlTypeName.BIGINT), false);
+    RexCall bigCall =
+        (RexCall) rexBuilder.makeCall(FlinkSqlOperatorTable.SHA2, 
stringFieldRef, bigLength);
+    CallTypedExpr bigExpr =
+        (CallTypedExpr)
+            RexCallConverterFactory.getConverter(bigCall, 
context).toTypedExpr(bigCall, context);
+    assertThat(bigExpr.getFunctionName()).isEqualTo("sha2");
+    ConstantTypedExpr bigLengthExpr = (ConstantTypedExpr) 
bigExpr.getInputs().get(1);
+    assertThat(bigLengthExpr.getReturnType()).isInstanceOf(BigIntType.class);
+    assertThat(((BigIntValue) 
bigLengthExpr.getValue()).getValue()).isEqualTo(512L);
+
+    // A NULL constant cannot be checked at plan time; it converts like any
+    // other NULL carrier and keeps the per-row rejection in velox.
+    RexLiteral nullLength =
+        (RexLiteral) 
rexBuilder.makeNullLiteral(typeFactory.createSqlType(SqlTypeName.INTEGER));
+    RexCall nullCall =
+        (RexCall) rexBuilder.makeCall(FlinkSqlOperatorTable.SHA2, 
stringFieldRef, nullLength);
+    CallTypedExpr nullExpr =
+        (CallTypedExpr)
+            RexCallConverterFactory.getConverter(nullCall, 
context).toTypedExpr(nullCall, context);
+    assertThat(nullExpr.getFunctionName()).isEqualTo("sha2");
+    assertThat(nullExpr.getInputs()).hasSize(2);
+    Variant nullVariant = ((ConstantTypedExpr) 
nullExpr.getInputs().get(1)).getValue();
+    assertThat(nullVariant).isInstanceOf(VarCharValue.class);
+    assertThat(((VarCharValue) nullVariant).getValue()).isNull();
+  }
+
   @Test
   public void testNullLiteralConvertsToNullCarrier() {
     RexLiteral intervalLiteral =
diff --git 
a/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/HashFunctionsTest.java
 
b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/HashFunctionsTest.java
new file mode 100644
index 0000000000..a7a4f79cf7
--- /dev/null
+++ 
b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/HashFunctionsTest.java
@@ -0,0 +1,240 @@
+/*
+ * 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.table.api.TableResult;
+import org.apache.flink.types.Row;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+class HashFunctionsTest extends GlutenStreamingTestBase {
+
+  // Digests pinned from Flink's own test suite for "" and "test" (lowercase 
hex
+  // over the string's UTF-8 bytes); the multi-byte row pins the encoding and 
the
+  // long row pins inputs past one digest block.
+  private static final String SHA224_EMPTY =
+      "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
+  private static final String SHA256_EMPTY =
+      "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
+  private static final String SHA384_EMPTY =
+      "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc"
+          + "7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b";
+  private static final String SHA512_EMPTY =
+      "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d"
+          + "0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e";
+  private static final String SHA224_TEST =
+      "90a3ed9e32b2aaf4c61c410eb925426119e1a9dc53d4286ade99a809";
+  private static final String SHA256_TEST =
+      "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08";
+  private static final String SHA384_TEST =
+      "768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc"
+          + "4b7ef1ccb126255d196047dfedf17a0a9";
+  private static final String SHA512_TEST =
+      "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db"
+          + 
"27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff";
+  private static final String SHA224_UTF8 =
+      "b3f5c93d7b531d7e29266412f2c842f8a4b7286871f87d259c6eaf07";
+  private static final String SHA256_UTF8 =
+      "beca6335b20ff57ccc47403ef4d9e0b8fccb4442b3151c2e7d50050673d43172";
+  private static final String SHA384_UTF8 =
+      "5621250177cc297c02d4c7c2a950d541a52b5c478e1fa16ca5022316de898d7be5c"
+          + "66b16ad735295b48b84a47e986144";
+  private static final String SHA512_UTF8 =
+      "4b28a152c8e203ebb52e099301041e3cf704a56190d3097ec8b086a0f9bfb4b9d533"
+          + "ce71fc3bcf374359e506dc5f17322ec3911eac8dd8f5b35308d938ba0c26";
+  private static final String SHA224_LONG =
+      "e6e4a6be069cc9bead8b6050856d2b26da6b3f7efa0951e5fb3a54dd";
+  private static final String SHA256_LONG =
+      "74e7e5bb9d22d6db26bf76946d40fff3ea9f0346b884fd0694920fccfad15e33";
+  private static final String SHA384_LONG =
+      "ce6d4ea5442bc6c830bea1942d4860db9f7b96f0e9d2c3073ffe47a0e1166d95612d"
+          + "840ff15e5efdd23c1f273096da32";
+  private static final String SHA512_LONG =
+      "95cadc34aa46b9fdef432f62fe5bad8d9f475bfbecf797d5802bb5f2937a85d93ce48"
+          + "57a6262b03834c01c610d74cd1215f9a466dc6ad3dd15078e3309a03a6d";
+  private static final String SHA1_TEST = 
"a94a8fe5ccb19ba61c4c0873d391e987982fbbd3";
+  private static final String SHA1_UTF8 = 
"dabaa5fe7c47fb21be902480a13013f16a1ab6eb";
+  private static final String LONG_INPUT = 
"0123456789abcdefghijklmnopqrstuvwxyz";
+
+  @Test
+  void testSha2ColumnLength() {
+    List<Row> rows =
+        Arrays.asList(
+            Row.of("", 224), Row.of("test", 256), Row.of("你好世界", 384), 
Row.of(LONG_INPUT, 512));
+    createSimpleBoundedValuesTable("tblSha2", "s varchar, n int", rows);
+
+    String query = "select SHA2(s, n) from tblSha2";
+    runAndCheck(
+        query,
+        Arrays.asList(
+            "+I[" + SHA224_EMPTY + "]",
+            "+I[" + SHA256_TEST + "]",
+            "+I[" + SHA384_UTF8 + "]",
+            "+I[" + SHA512_LONG + "]"));
+  }
+
+  @Test
+  void testSha2UnsupportedLengthFails() {
+    // Flink rejects unsupported hash lengths; the velox flinksql function set
+    // fails the job with the same channel. 0 included (Flink 1.19.2 rejects 0
+    // outright, no Hive-style 0-means-256); 385 is a non-algorithm.
+    for (int length : new int[] {0, 385}) {
+      List<Row> rows = Arrays.asList(Row.of("test", length));
+      String tableName = "tblSha2Bad" + length;
+      createSimpleBoundedValuesTable(tableName, "s varchar, n int", rows);
+      tEnv().executeSql("CREATE TABLE printT (d VARCHAR) WITH ('connector' = 
'print')");
+      TableResult result =
+          tEnv().executeSql("insert into printT select SHA2(s, n) from " + 
tableName);
+      assertThat(result.getJobClient()).isPresent();
+      assertThatThrownBy(() -> 
result.getJobClient().get().getJobExecutionResult().get())
+          .hasStackTraceContaining("Unsupported SHA-2 hash length: " + length);
+      tEnv().executeSql("drop table if exists printT");
+    }
+  }
+
+  @Test
+  void testSha2ConstantLength() {
+    // Planner-folded INT and BIGINT literals route through the int32 and int64
+    // sha2 signatures.
+    List<Row> rows = Arrays.asList(Row.of("test"), Row.of("你好世界"));
+    createSimpleBoundedValuesTable("tblSha2Const", "s varchar", rows);
+
+    String query = "select SHA2(s, 256), SHA2(s, CAST(512 AS BIGINT)) from 
tblSha2Const";
+    runAndCheck(
+        query,
+        Arrays.asList(
+            "+I[" + SHA256_TEST + ", " + SHA512_TEST + "]",
+            "+I[" + SHA256_UTF8 + ", " + SHA512_UTF8 + "]"));
+  }
+
+  @Test
+  void testSha2ConstantInvalidLengthRejectedAtConversion() {
+    // Flink fails a job with a bad constant hashLength in the generated
+    // operator's constructor — even on empty input, since the check is
+    // input-independent; the conversion rejects it the same way, while the
+    // column form keeps the per-row runtime rejection.
+    List<Row> rows = Arrays.asList(Row.of("test"));
+    createSimpleBoundedValuesTable("tblSha2ConstBad", "s varchar", rows);
+    tEnv().executeSql("CREATE TABLE printT (d VARCHAR) WITH ('connector' = 
'print')");
+    assertThatThrownBy(
+            () -> tEnv().executeSql("insert into printT select SHA2(s, 0) from 
tblSha2ConstBad"))
+        .hasStackTraceContaining("SHA2 with literal hashLength 0 is not 
supported");
+    assertThatThrownBy(
+            () -> tEnv().executeSql("insert into printT select SHA2(s, 385) 
from tblSha2ConstBad"))
+        .hasStackTraceContaining("SHA2 with literal hashLength 385 is not 
supported");
+    // Literal 1 is rejected here even though a column length of 1 yields the
+    // SHA-1 digest (testSha2ColumnLengthOneIsSha1): Flink's literal gate
+    // rejects 1 at operator init while its column path accepts "SHA-1".
+    assertThatThrownBy(
+            () -> tEnv().executeSql("insert into printT select SHA2(s, 1) from 
tblSha2ConstBad"))
+        .hasStackTraceContaining("SHA2 with literal hashLength 1 is not 
supported");
+    tEnv().executeSql("drop table if exists printT");
+  }
+
+  @Test
+  void testSha2ColumnLengthOneIsSha1() {
+    // Flink's column path builds "SHA-" + length and "SHA-1" is a valid
+    // algorithm name there, so a column length of 1 yields the 20-byte SHA-1
+    // digest — while the literal form of 1 is rejected at conversion time
+    // (testSha2ConstantInvalidLengthRejectedAtConversion).
+    List<Row> rows = Arrays.asList(Row.of("test", 1), Row.of("你好世界", 1));
+    createSimpleBoundedValuesTable("tblSha2One", "s varchar, n int", rows);
+
+    String query = "select SHA2(s, n) from tblSha2One";
+    runAndCheck(query, Arrays.asList("+I[" + SHA1_TEST + "]", "+I[" + 
SHA1_UTF8 + "]"));
+  }
+
+  @Test
+  void testSha224() {
+    List<Row> rows = Arrays.asList(Row.of(""), Row.of("test"), Row.of("你好世界"), 
Row.of(LONG_INPUT));
+    createSimpleBoundedValuesTable("tblSha224", "s varchar", rows);
+
+    String query = "select SHA224(s) from tblSha224";
+    runAndCheck(
+        query,
+        Arrays.asList(
+            "+I[" + SHA224_EMPTY + "]",
+            "+I[" + SHA224_TEST + "]",
+            "+I[" + SHA224_UTF8 + "]",
+            "+I[" + SHA224_LONG + "]"));
+  }
+
+  @Test
+  void testSha256() {
+    List<Row> rows = Arrays.asList(Row.of(""), Row.of("test"), Row.of("你好世界"), 
Row.of(LONG_INPUT));
+    createSimpleBoundedValuesTable("tblSha256", "s varchar", rows);
+
+    String query = "select SHA256(s) from tblSha256";
+    runAndCheck(
+        query,
+        Arrays.asList(
+            "+I[" + SHA256_EMPTY + "]",
+            "+I[" + SHA256_TEST + "]",
+            "+I[" + SHA256_UTF8 + "]",
+            "+I[" + SHA256_LONG + "]"));
+  }
+
+  @Test
+  void testSha384() {
+    List<Row> rows = Arrays.asList(Row.of(""), Row.of("test"), Row.of("你好世界"), 
Row.of(LONG_INPUT));
+    createSimpleBoundedValuesTable("tblSha384", "s varchar", rows);
+
+    String query = "select SHA384(s) from tblSha384";
+    runAndCheck(
+        query,
+        Arrays.asList(
+            "+I[" + SHA384_EMPTY + "]",
+            "+I[" + SHA384_TEST + "]",
+            "+I[" + SHA384_UTF8 + "]",
+            "+I[" + SHA384_LONG + "]"));
+  }
+
+  @Test
+  void testSha512() {
+    List<Row> rows = Arrays.asList(Row.of(""), Row.of("test"), Row.of("你好世界"), 
Row.of(LONG_INPUT));
+    createSimpleBoundedValuesTable("tblSha512", "s varchar", rows);
+
+    String query = "select SHA512(s) from tblSha512";
+    runAndCheck(
+        query,
+        Arrays.asList(
+            "+I[" + SHA512_EMPTY + "]",
+            "+I[" + SHA512_TEST + "]",
+            "+I[" + SHA512_UTF8 + "]",
+            "+I[" + SHA512_LONG + "]"));
+  }
+
+  @Test
+  void testNullPropagation() {
+    List<Row> rows = Arrays.asList(Row.of("test", null), Row.of(null, 256), 
Row.of(null, null));
+    createSimpleBoundedValuesTable("tblSha2Null", "s varchar, n int", rows);
+
+    String query1 = "select SHA2(s, n) from tblSha2Null";
+    runAndCheck(query1, Arrays.asList("+I[null]", "+I[null]", "+I[null]"));
+
+    String query2 = "select SHA256(s) from tblSha2Null";
+    runAndCheck(query2, Arrays.asList("+I[" + SHA256_TEST + "]", "+I[null]", 
"+I[null]"));
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to