This is an automated email from the ASF dual-hosted git repository.
lzljs3620320 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git
The following commit(s) were added to refs/heads/master by this push:
new bcd1f834c [core] Support BinaryToStringCastRule in casting (#3944)
bcd1f834c is described below
commit bcd1f834cbc34da1ebae1f2c27c6aefb36ec3067
Author: xuzifu666 <[email protected]>
AuthorDate: Mon Aug 12 20:58:32 2024 +0800
[core] Support BinaryToStringCastRule in casting (#3944)
---
.../paimon/casting/BinaryToStringCastRule.java | 42 ++++++++++++++++++++++
.../org/apache/paimon/casting/CastExecutors.java | 3 +-
.../apache/paimon/casting/CastExecutorTest.java | 15 ++++++++
3 files changed, 59 insertions(+), 1 deletion(-)
diff --git
a/paimon-core/src/main/java/org/apache/paimon/casting/BinaryToStringCastRule.java
b/paimon-core/src/main/java/org/apache/paimon/casting/BinaryToStringCastRule.java
new file mode 100644
index 000000000..503ec9480
--- /dev/null
+++
b/paimon-core/src/main/java/org/apache/paimon/casting/BinaryToStringCastRule.java
@@ -0,0 +1,42 @@
+/*
+ * 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.paimon.casting;
+
+import org.apache.paimon.data.BinaryString;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.DataTypeFamily;
+
+/** {@link DataTypeFamily#BINARY_STRING} to {@link
DataTypeFamily#CHARACTER_STRING} cast rule. */
+public class BinaryToStringCastRule extends AbstractCastRule<byte[],
BinaryString> {
+
+ static final BinaryToStringCastRule INSTANCE = new
BinaryToStringCastRule();
+
+ private BinaryToStringCastRule() {
+ super(
+ CastRulePredicate.builder()
+ .input(DataTypeFamily.BINARY_STRING)
+ .target(DataTypeFamily.CHARACTER_STRING)
+ .build());
+ }
+
+ @Override
+ public CastExecutor<byte[], BinaryString> create(DataType inputType,
DataType targetType) {
+ return value -> BinaryString.fromBytes(value);
+ }
+}
diff --git
a/paimon-core/src/main/java/org/apache/paimon/casting/CastExecutors.java
b/paimon-core/src/main/java/org/apache/paimon/casting/CastExecutors.java
index beb7aa0a5..8134e0118 100644
--- a/paimon-core/src/main/java/org/apache/paimon/casting/CastExecutors.java
+++ b/paimon-core/src/main/java/org/apache/paimon/casting/CastExecutors.java
@@ -72,7 +72,8 @@ public class CastExecutors {
.addRule(TimeToTimestampCastRule.INSTANCE)
.addRule(TimestampToNumericPrimitiveCastRule.INSTANCE)
// To binary rules
- .addRule(BinaryToBinaryCastRule.INSTANCE);
+ .addRule(BinaryToBinaryCastRule.INSTANCE)
+ .addRule(BinaryToStringCastRule.INSTANCE);
}
/* ------- Entrypoint ------- */
diff --git
a/paimon-core/src/test/java/org/apache/paimon/casting/CastExecutorTest.java
b/paimon-core/src/test/java/org/apache/paimon/casting/CastExecutorTest.java
index 5c41f2b5f..e8c3e98ff 100644
--- a/paimon-core/src/test/java/org/apache/paimon/casting/CastExecutorTest.java
+++ b/paimon-core/src/test/java/org/apache/paimon/casting/CastExecutorTest.java
@@ -560,6 +560,21 @@ public class CastExecutorTest {
"12345678".getBytes());
}
+ @Test
+ public void testBinaryToString() {
+ // binary(5) to string(10)
+ compareCastResult(
+ CastExecutors.resolve(new VarBinaryType(5), new
VarCharType(10)),
+ "12345".getBytes(),
+ BinaryString.fromString("12345"));
+
+ // binary(20) to string(10)
+ compareCastResult(
+ CastExecutors.resolve(new VarBinaryType(20), new
VarCharType(10)),
+ "12345678".getBytes(),
+ BinaryString.fromString("12345678"));
+ }
+
// To binary rules
@Test