This is an automated email from the ASF dual-hosted git repository.
tkalkirill pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git
The following commit(s) were added to refs/heads/master by this push:
new e7f1bc8bfb4 IGNITE-29031 SQL Calcite: Support byte[] in UDF and UDTF
parameters and results (#13543)
e7f1bc8bfb4 is described below
commit e7f1bc8bfb463f85258423da9cc1c29d4f9675bc
Author: Kirill Tkalenko <[email protected]>
AuthorDate: Fri Oct 2 17:33:54 2026 +0300
IGNITE-29031 SQL Calcite: Support byte[] in UDF and UDTF parameters and
results (#13543)
---
.../query/calcite/exec/exp/ConverterUtils.java | 17 ++--
.../processors/query/calcite/util/TypeUtils.java | 6 +-
.../OperatorsExtensionIntegrationTest.java | 105 +++++++++++++++++++++
.../UserDefinedFunctionsIntegrationTest.java | 90 +++++++++++++++++-
4 files changed, 204 insertions(+), 14 deletions(-)
diff --git
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/ConverterUtils.java
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/ConverterUtils.java
index 55dc1c71173..c4a2c4cc1cc 100644
---
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/ConverterUtils.java
+++
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/ConverterUtils.java
@@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.apache.calcite.adapter.enumerable.RexImpTable;
+import org.apache.calcite.avatica.util.ByteString;
import org.apache.calcite.linq4j.tree.ConstantExpression;
import org.apache.calcite.linq4j.tree.ConstantUntypedNull;
import org.apache.calcite.linq4j.tree.Expression;
@@ -101,11 +102,6 @@ public class ConverterUtils {
/** Converts from internal representation to JDBC representation used by
* arguments of user-defined functions. For example, converts date values
from
* {@code int} to {@link java.sql.Date}. */
- private static Expression fromInternal(Expression operand, Type
targetType) {
- return fromInternal(operand, operand.getType(), targetType);
- }
-
- /** */
private static Expression fromInternal(Expression operand,
Type fromType, Type targetType) {
if (operand == ConstantUntypedNull.INSTANCE)
@@ -132,6 +128,9 @@ public class ConverterUtils {
if (isA(fromType, Primitive.LONG))
return
Expressions.call(BuiltInMethod.INTERNAL_TO_TIMESTAMP.method, operand);
}
+ else if (targetType == byte[].class && fromType == ByteString.class)
+ return
Expressions.call(BuiltInMethod.BYTE_STRING_TO_BYTE_ARRAY.method, operand);
+
if (Primitive.is(operand.type)
&& Primitive.isBox(targetType)) {
// E.g. operand is "int", target is "Long", generate "(long)
operand".
@@ -180,7 +179,7 @@ public class ConverterUtils {
return operand;
// Preserve Calcite's calendar conversion for JDBC dates and
timestamps.
- Expression converted = fromInternal(operand, targetType);
+ Expression converted = fromInternal(operand, operand.getType(),
targetType);
if (root == null || converted != operand ||
!TypeUtils.isConvertableType(targetType))
return converted;
@@ -279,6 +278,12 @@ public class ConverterUtils {
if (toType == BigDecimal.class)
throw new AssertionError("For conversion to decimal,
ConverterUtils#convertToDecimal method should be used instead.");
+ if (fromType == byte[].class && toType == ByteString.class)
+ return
Expressions.call(BuiltInMethod.BYTE_ARRAY_TO_BYTE_STRING.method, operand);
+
+ if (fromType == ByteString.class && toType == byte[].class)
+ return
Expressions.call(BuiltInMethod.BYTE_STRING_TO_BYTE_ARRAY.method, operand);
+
// E.g. from "Short" to "int".
// Generate "x.intValue()".
final Primitive toPrimitive = Primitive.of(toType);
diff --git
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/util/TypeUtils.java
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/util/TypeUtils.java
index 4c75d7113e8..b1f856966cb 100644
---
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/util/TypeUtils.java
+++
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/util/TypeUtils.java
@@ -365,12 +365,12 @@ public class TypeUtils {
}
/** */
- public static Object toInternal(DataContext ctx, Object val) {
+ public static @Nullable Object toInternal(DataContext ctx, @Nullable
Object val) {
return val == null ? null : toInternal(ctx, val, val.getClass());
}
/** */
- public static Object toInternal(DataContext ctx, Object val, Type
storageType) {
+ public static @Nullable Object toInternal(DataContext ctx, @Nullable
Object val, Type storageType) {
if (val == null)
return null;
else if (storageType == java.sql.Date.class)
@@ -458,7 +458,7 @@ public class TypeUtils {
}
/** */
- public static Object fromInternal(DataContext ctx, Object val, Type
storageType) {
+ public static @Nullable Object fromInternal(DataContext ctx, @Nullable
Object val, Type storageType) {
if (val == null)
return null;
else if (storageType == java.sql.Date.class && val instanceof Integer)
diff --git
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/OperatorsExtensionIntegrationTest.java
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/OperatorsExtensionIntegrationTest.java
index db261920fe5..666351b26de 100644
---
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/OperatorsExtensionIntegrationTest.java
+++
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/OperatorsExtensionIntegrationTest.java
@@ -80,6 +80,8 @@ import org.apache.ignite.plugin.PluginContext;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+
/**
* Tests SQL engine extension with plugin.
*/
@@ -140,6 +142,16 @@ public class OperatorsExtensionIntegrationTest extends
AbstractBasicIntegrationT
OperatorsExtensionIntegrationTest.class.getMethod("toNumber", String.class),
NullPolicy.STRICT
);
+
+ RexImpTable.INSTANCE.defineReflective(
+ OperatorTable.REFLECTIVE_BINARY_LENGTH,
+
OperatorsExtensionIntegrationTest.class.getMethod("binaryLength", byte[].class)
+ );
+
+ RexImpTable.INSTANCE.defineReflective(
+ OperatorTable.REFLECTIVE_BINARY_VALUE,
+
OperatorsExtensionIntegrationTest.class.getMethod("binaryValue")
+ );
}
catch (NoSuchMethodException e) {
throw new RuntimeException(e);
@@ -156,6 +168,11 @@ public class OperatorsExtensionIntegrationTest extends
AbstractBasicIntegrationT
RexImpTable.FALSE_EXPR
), NullPolicy.ARG0, false
));
+
+ // A plugin can declare a Java return type while its
implementor returns an internal SQL value.
+
RexImpTable.INSTANCE.define(OperatorTable.JAVA_BINARY_IDENTITY,
RexImpTable.createRexCallImplementor(
+ (translator, call, translatedOperands) ->
translatedOperands.get(0), NullPolicy.ARG0, false
+ ));
}
});
}
@@ -171,6 +188,54 @@ public class OperatorsExtensionIntegrationTest extends
AbstractBasicIntegrationT
.returns(Timestamp.valueOf("2021-01-01 00:00:00")).check();
}
+ /** */
+ @Test
+ public void testByteArrayFunctions() {
+ assertQuery("SELECT
REFLECTIVE_BINARY_LENGTH(x'010203')").returns(3).check();
+ assertQuery("SELECT
REFLECTIVE_BINARY_LENGTH(?)").withParams((Object)new byte[] {1, 2,
3}).returns(3).check();
+ assertQuery("SELECT
REFLECTIVE_BINARY_LENGTH(REFLECTIVE_BINARY_VALUE())").returns(3).check();
+
+ assertQuery("SELECT
OCTET_LENGTH(REFLECTIVE_BINARY_VALUE())").returns(3).check();
+
+ assertQuery("SELECT REFLECTIVE_BINARY_VALUE()")
+ .withResultChecker(rows -> {
+ assertEquals(1, rows.size());
+ assertEquals(1, rows.get(0).size());
+ assertArrayEquals(new byte[] {1, 2, 3},
(byte[])rows.get(0).get(0));
+ })
+ .check();
+ }
+
+ /** */
+ @Test
+ public void testBinaryFunctionWithJavaReturnType() {
+ assertQuery("SELECT JAVA_BINARY_IDENTITY(x'010203'),
JAVA_BINARY_IDENTITY(x''), "
+ + "JAVA_BINARY_IDENTITY(CAST(NULL AS VARBINARY))")
+ .withResultChecker(rows -> {
+ assertEquals(1, rows.size());
+ assertEquals(3, rows.get(0).size());
+ assertArrayEquals(new byte[] {1, 2, 3},
(byte[])rows.get(0).get(0));
+ assertArrayEquals(new byte[0], (byte[])rows.get(0).get(1));
+ assertNull(rows.get(0).get(2));
+ })
+ .check();
+ }
+
+ /** */
+ @Test
+ public void testBinaryFunctionWithJavaReturnTypeDynamicParameters() {
+ for (byte[] bytes : new byte[][] {new byte[] {1, 2, 3}, new byte[0],
null}) {
+ assertQuery("SELECT JAVA_BINARY_IDENTITY(?)")
+ .withParams((Object)bytes)
+ .withResultChecker(rows -> {
+ assertEquals(1, rows.size());
+ assertEquals(1, rows.get(0).size());
+ assertArrayEquals(bytes, (byte[])rows.get(0).get(0));
+ })
+ .check();
+ }
+ }
+
/** */
@Test
public void testOperatorsCallsInViews() {
@@ -346,6 +411,16 @@ public class OperatorsExtensionIntegrationTest extends
AbstractBasicIntegrationT
return new BigDecimal(s);
}
+ /** Implementor for {@code REFLECTIVE_BINARY_LENGTH} function. */
+ public static int binaryLength(byte[] bytes) {
+ return bytes.length;
+ }
+
+ /** Implementor for {@code REFLECTIVE_BINARY_VALUE} function. */
+ public static byte[] binaryValue() {
+ return new byte[] {1, 2, 3};
+ }
+
/** Extended operator table. */
public static class OperatorTable extends ReflectiveSqlOperatorTable {
/** */
@@ -393,6 +468,36 @@ public class OperatorsExtensionIntegrationTest extends
AbstractBasicIntegrationT
/** */
public static final SqlAggFunction TEST_COUNT_PAIRS = new
SqlTestCountPairsAggFunction();
+
+ /** */
+ public static final SqlFunction REFLECTIVE_BINARY_LENGTH = new
SqlFunction(
+ "REFLECTIVE_BINARY_LENGTH",
+ SqlKind.OTHER_FUNCTION,
+ ReturnTypes.INTEGER_NULLABLE,
+ null,
+ OperandTypes.BINARY,
+ SqlFunctionCategory.USER_DEFINED_FUNCTION
+ );
+
+ /** */
+ public static final SqlFunction REFLECTIVE_BINARY_VALUE = new
SqlFunction(
+ "REFLECTIVE_BINARY_VALUE",
+ SqlKind.OTHER_FUNCTION,
+ opBinding ->
opBinding.getTypeFactory().createSqlType(SqlTypeName.VARBINARY),
+ null,
+ OperandTypes.NILADIC,
+ SqlFunctionCategory.USER_DEFINED_FUNCTION
+ );
+
+ /** */
+ public static final SqlFunction JAVA_BINARY_IDENTITY = new SqlFunction(
+ "JAVA_BINARY_IDENTITY",
+ SqlKind.OTHER_FUNCTION,
+ opBinding ->
opBinding.getTypeFactory().createJavaType(byte[].class),
+ null,
+ OperandTypes.BINARY,
+ SqlFunctionCategory.USER_DEFINED_FUNCTION
+ );
}
/** Extended convertlet table. */
diff --git
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/UserDefinedFunctionsIntegrationTest.java
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/UserDefinedFunctionsIntegrationTest.java
index b6a445bdfad..ea8c0d3b7a9 100644
---
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/UserDefinedFunctionsIntegrationTest.java
+++
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/UserDefinedFunctionsIntegrationTest.java
@@ -63,6 +63,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static
org.apache.ignite.internal.processors.query.calcite.CalciteQueryProcessor.IGNITE_CALCITE_USE_QUERY_BLOCKING_TASK_EXECUTOR;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
/**
* Integration test for user defined functions.
@@ -511,6 +512,43 @@ public class UserDefinedFunctionsIntegrationTest extends
AbstractBasicIntegratio
assertQuery("SELECT udf.decimalToInt(5.3)").returns(5).check();
}
+ /** */
+ @Test
+ public void testBinaryFunctions() {
+ client.getOrCreateCache(new CacheConfiguration<>("binary-functions")
+ .setSqlSchema("PUBLIC")
+ .setSqlFunctionClasses(BinaryFunctionsLibrary.class));
+
+ byte[] bytes = {1, 2, 3};
+ Consumer<List<List<?>>> binaryResultChecker = rows -> {
+ assertEquals(1, rows.size());
+ assertEquals(1, rows.get(0).size());
+ assertArrayEquals(bytes, (byte[])rows.get(0).get(0));
+ };
+
+ // Scalar function arguments.
+ assertQuery("SELECT binaryLength(x'010203')").returns(3).check();
+ assertQuery("SELECT
binaryLength(?)").withParams(bytes).returns(3).check();
+
+ // Scalar function results.
+ assertQuery("SELECT
binaryValue()").withResultChecker(binaryResultChecker).check();
+ assertQuery("SELECT
binaryEcho(x'010203')").withResultChecker(binaryResultChecker).check();
+ assertQuery("SELECT
binaryEcho(?)").withParams(bytes).withResultChecker(binaryResultChecker).check();
+ assertQuery("SELECT OCTET_LENGTH(binaryValue())").returns(3).check();
+
+ // Table function results.
+ assertQuery("SELECT * FROM
binaryTableValue()").withResultChecker(binaryResultChecker).check();
+ assertQuery("SELECT * FROM
binaryTable(?)").withParams(bytes).withResultChecker(binaryResultChecker).check();
+ assertQuery("SELECT OCTET_LENGTH(bytes) FROM
binaryTableValue()").returns(3).check();
+ assertQuery("SELECT binaryLength(bytes) FROM
binaryTableValue()").returns(3).check();
+
+ // Table function arguments.
+ assertQuery("SELECT * FROM
binaryTableLength(x'010203')").returns(3).check();
+ assertQuery("SELECT * FROM
binaryTableLength(?)").withParams(bytes).returns(3).check();
+ assertQuery("SELECT * FROM
TABLE(binaryTableLength(binaryValue()))").returns(3).check();
+ assertQuery("SELECT * FROM
binaryTable(x'010203')").withResultChecker(binaryResultChecker).check();
+ }
+
/** */
@Test
public void testBinaryObjectFunctions() {
@@ -693,8 +731,7 @@ public class UserDefinedFunctionsIntegrationTest extends
AbstractBasicIntegratio
for (int i = 0; i < exp.length; i++) {
Object actual = rows.get(0).get(i);
- assertEquals("Unexpected value type at index " + i,
exp[i].getClass(), actual.getClass());
- assertEquals("Unexpected value at index " + i, exp[i],
actual);
+ assertEqualsArraysAware("Unexpected value at index " + i,
exp[i], actual);
}
})
.check();
@@ -710,9 +747,10 @@ public class UserDefinedFunctionsIntegrationTest extends
AbstractBasicIntegratio
assertQuery("SELECT * FROM serializableTableValues()")
.withResultChecker(rows -> {
assertEquals(1, rows.size());
- assertEquals(1, rows.get(0).size());
+ assertEquals(2, rows.get(0).size());
assertEquals(Date.class, rows.get(0).get(0).getClass());
assertEquals(Date.valueOf("2020-01-01"), rows.get(0).get(0));
+ assertArrayEquals(new byte[] {1, 2, 3},
(byte[])rows.get(0).get(1));
})
.check();
}
@@ -1058,6 +1096,7 @@ public class UserDefinedFunctionsIntegrationTest extends
AbstractBasicIntegratio
Object[] temporalValues = temporalValues();
return new Object[] {
+ new byte[] {1, 2, 3},
true,
(byte)1,
(short)2,
@@ -1502,6 +1541,45 @@ public class UserDefinedFunctionsIntegrationTest extends
AbstractBasicIntegratio
"with the same name and parameters is already registered").build();
}
+ /** */
+ public static class BinaryFunctionsLibrary {
+ /** */
+ @QuerySqlFunction
+ public static int binaryLength(byte[] bytes) {
+ return bytes.length;
+ }
+
+ /** */
+ @QuerySqlFunction
+ public static byte[] binaryValue() {
+ return new byte[] {1, 2, 3};
+ }
+
+ /** */
+ @QuerySqlFunction
+ public static byte[] binaryEcho(byte[] bytes) {
+ return bytes;
+ }
+
+ /** */
+ @QuerySqlTableFunction(columnTypes = {int.class}, columnNames =
{"LENGTH"})
+ public static Iterable<Object[]> binaryTableLength(byte[] bytes) {
+ return Collections.singletonList(new Object[] {bytes.length});
+ }
+
+ /** */
+ @QuerySqlTableFunction(columnTypes = {byte[].class}, columnNames =
{"BYTES"})
+ public static Iterable<Object[]> binaryTableValue() {
+ return Collections.singletonList(new Object[] {new byte[] {1, 2,
3}});
+ }
+
+ /** */
+ @QuerySqlTableFunction(columnTypes = {byte[].class}, columnNames =
{"BYTES"})
+ public static Iterable<Object[]> binaryTable(byte[] bytes) {
+ return Collections.singletonList(new Object[] {bytes});
+ }
+ }
+
/** */
public static class BinaryObjectFunctionsLibrary {
/** */
@@ -1630,9 +1708,9 @@ public class UserDefinedFunctionsIntegrationTest extends
AbstractBasicIntegratio
/** */
public static class SerializableFunctionsLibrary {
/** */
- @QuerySqlTableFunction(columnTypes = {Serializable.class}, columnNames
= {"D"})
+ @QuerySqlTableFunction(columnTypes = {Serializable.class,
Serializable.class}, columnNames = {"D", "B"})
public static Iterable<Object[]> serializableTableValues() {
- return Collections.singletonList(new Object[]
{Date.valueOf("2020-01-01")});
+ return Collections.singletonList(new Object[]
{Date.valueOf("2020-01-01"), new byte[] {1, 2, 3}});
}
}
@@ -1685,9 +1763,11 @@ public class UserDefinedFunctionsIntegrationTest extends
AbstractBasicIntegratio
Object.class,
Object.class,
Object.class,
+ Object.class,
Object.class
},
columnNames = {
+ "BYTES",
"BOOLEAN_VALUE",
"BYTE_VALUE",
"SHORT_VALUE",