[CALCITE-7187] Java UDF byte arrays cannot be mapped to VARBINARY#4855
[CALCITE-7187] Java UDF byte arrays cannot be mapped to VARBINARY#4855macroguo-ghy wants to merge 3 commits intoapache:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes Calcite enumerable code generation so Java UDF byte[] parameters/return values are consistently mapped to SQL VARBINARY (aligned with existing ByteString behavior), with tests and documentation updates.
Changes:
- Added
byte[]↔ByteStringconversion helpers inSqlFunctionsand exposed them viaBuiltInMethod. - Wired conversions into enumerable expression conversion paths (
EnumUtils) for UDF argument/return handling. - Added UDF test coverage and documented supported Java representations for
VARBINARY.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
testkit/src/main/java/org/apache/calcite/util/Smalls.java |
Adds UDF helpers returning/accepting byte[] for test scenarios. |
core/src/test/java/org/apache/calcite/test/UdfTest.java |
Registers new UDFs and adds regression tests for byte[] VARBINARY interop. |
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java |
Introduces conversion helpers between byte[] and ByteString. |
core/src/main/java/org/apache/calcite/util/BuiltInMethod.java |
Adds reflective method handles for the new conversion helpers. |
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumUtils.java |
Applies conversions during enumerable codegen type adaptation. |
site/_docs/reference.md |
Documents supported Java types for SQL VARBINARY in Java UDFs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| /** User-defined function with parameter type byte[]. */ | ||
| public static class ByteArrayLengthFunction { | ||
| public static int eval(byte[] bytes) { |
There was a problem hiding this comment.
ByteArrayLengthFunction.eval does not handle null input; if a SQL VARBINARY argument is NULL (or conversion yields null), this will throw a NullPointerException. Consider changing the signature to return a boxed Integer and return null when bytes is null (or otherwise define/implement the desired NULL semantics).
| public static int eval(byte[] bytes) { | |
| public static @Nullable Integer eval(@Nullable byte[] bytes) { | |
| if (bytes == null) { | |
| return null; | |
| } |
There was a problem hiding this comment.
UDF in Smalls is just for test
| For Java UDFs, `byte[]` and `ByteString` are supported Java representations of | ||
| SQL `VARBINARY` values for parameters and return types. Boxed byte arrays | ||
| (`Byte[]`) are not supported. |
There was a problem hiding this comment.
The doc mentions ByteString without qualification; since there are multiple ByteString types in the Java ecosystem, consider using the fully qualified name (org.apache.calcite.avatica.util.ByteString) or linking to the API docs to avoid ambiguity. Also, the sentence about Byte[] could be misread as "not supported at all"; if the intent is "not supported as a VARBINARY representation", consider stating that explicitly.
| For Java UDFs, `byte[]` and `ByteString` are supported Java representations of | |
| SQL `VARBINARY` values for parameters and return types. Boxed byte arrays | |
| (`Byte[]`) are not supported. | |
| For Java UDFs, `byte[]` and [`ByteString`]({{ site.apiRoot }}/org/apache/calcite/avatica/util/ByteString.html) are supported Java representations of | |
| SQL `VARBINARY` values for parameters and return types. Boxed byte arrays | |
| (`Byte[]`) are not supported as Java representations of SQL `VARBINARY` parameters or return types. |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
4f31499 to
fe6baff
Compare
|



Jira Link
CALCITE-7187
Changes Proposed
This PR fixes Java UDF
byte[]handling so Calcite maps SQLVARBINARYvalues consistently during enumerable code generation.
Reproduction:
Changes in this PR:
byte[]<->ByteStringconversion helpers inSqlFunctionsEnumUtilsandBuiltInMethodUdfTestcoverage forbyte[]return values andbyte[]parametersbyte[]andByteStringas supported Java representations of SQLVARBINARYfor Java UDFs