slinkydeveloper commented on a change in pull request #17919:
URL: https://github.com/apache/flink/pull/17919#discussion_r760049221



##########
File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/RawToBinaryCastRule.java
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.flink.table.planner.functions.casting;
+
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeFamily;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.logical.utils.LogicalTypeChecks;
+
+import java.util.Arrays;
+
+import static org.apache.flink.table.codesplit.CodeSplitUtil.newName;
+import static 
org.apache.flink.table.planner.functions.casting.CastRuleUtils.accessField;
+import static 
org.apache.flink.table.planner.functions.casting.CastRuleUtils.methodCall;
+import static 
org.apache.flink.table.planner.functions.casting.CastRuleUtils.staticCall;
+
+/** {@link LogicalTypeRoot#RAW} to {@link LogicalTypeFamily#BINARY_STRING} 
cast rule. */
+public class RawToBinaryCastRule extends 
AbstractNullAwareCodeGeneratorCastRule<Object, byte[]> {
+
+    static final RawToBinaryCastRule INSTANCE = new RawToBinaryCastRule();
+
+    private RawToBinaryCastRule() {
+        super(
+                CastRulePredicate.builder()
+                        .input(LogicalTypeRoot.RAW)
+                        .target(LogicalTypeFamily.BINARY_STRING)
+                        .build());
+    }
+
+    /* Example generated code for BINARY(3):
+
+    byte[] deserializedByteArray$0 = result$2.toBytes(typeSerializer$5);
+    if (deserializedByteArray$0 != null) {

Review comment:
       I wonder if we need this null check, perhaps can you add a test case 
where the raw value is null and see if this null check is really needed?

##########
File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/BinaryToBinaryCastRule.java
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.flink.table.planner.functions.casting;
+
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeFamily;
+import org.apache.flink.table.types.logical.utils.LogicalTypeChecks;
+
+import java.util.Arrays;
+
+import static 
org.apache.flink.table.planner.functions.casting.CastRuleUtils.staticCall;
+
+/** {@link LogicalTypeFamily#BINARY_STRING} to {@link 
LogicalTypeFamily#BINARY_STRING} cast rule. */
+public class BinaryToBinaryCastRule

Review comment:
       No need to make it public

##########
File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/RawToBinaryCastRule.java
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.flink.table.planner.functions.casting;
+
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeFamily;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.logical.utils.LogicalTypeChecks;
+
+import java.util.Arrays;
+
+import static org.apache.flink.table.codesplit.CodeSplitUtil.newName;
+import static 
org.apache.flink.table.planner.functions.casting.CastRuleUtils.accessField;
+import static 
org.apache.flink.table.planner.functions.casting.CastRuleUtils.methodCall;
+import static 
org.apache.flink.table.planner.functions.casting.CastRuleUtils.staticCall;
+
+/** {@link LogicalTypeRoot#RAW} to {@link LogicalTypeFamily#BINARY_STRING} 
cast rule. */
+public class RawToBinaryCastRule extends 
AbstractNullAwareCodeGeneratorCastRule<Object, byte[]> {
+
+    static final RawToBinaryCastRule INSTANCE = new RawToBinaryCastRule();
+
+    private RawToBinaryCastRule() {
+        super(
+                CastRulePredicate.builder()
+                        .input(LogicalTypeRoot.RAW)
+                        .target(LogicalTypeFamily.BINARY_STRING)
+                        .build());
+    }
+
+    /* Example generated code for BINARY(3):
+
+    byte[] deserializedByteArray$0 = result$2.toBytes(typeSerializer$5);
+    if (deserializedByteArray$0 != null) {
+        if (3 >= deserializedByteArray$0.length) {
+            result$4 = deserializedByteArray$0;
+        } else {
+            result$4 = java.util.Arrays.copyOfRange(deserializedByteArray$0, 
0, 3);
+        }
+    } else {
+        result$4 = null;
+    }
+
+    */
+
+    @Override
+    protected String generateCodeBlockInternal(
+            CodeGeneratorCastRule.Context context,
+            String inputTerm,
+            String returnVariable,
+            LogicalType inputLogicalType,
+            LogicalType targetLogicalType) {
+        // Get length of target
+        final int targetLength = 
LogicalTypeChecks.getLength(targetLogicalType);
+
+        // Get serializer for RAW type
+        final String typeSerializer = 
context.declareTypeSerializer(inputLogicalType);
+        final String deserializedByteArrayTerm = 
newName("deserializedByteArray");
+
+        return new CastRuleUtils.CodeWriter()
+                .declStmt(
+                        byte[].class,
+                        deserializedByteArrayTerm,
+                        methodCall(inputTerm, "toBytes", typeSerializer))
+                .ifStmt(
+                        deserializedByteArrayTerm + " != null",
+                        thenWriter ->
+                                thenWriter.ifStmt(
+                                        targetLength
+                                                + " >= "
+                                                + 
accessField(deserializedByteArrayTerm, "length"),

Review comment:
       Can you swap this condition as it's easier to read?

##########
File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/BinaryToBinaryCastRule.java
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.flink.table.planner.functions.casting;
+
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeFamily;
+import org.apache.flink.table.types.logical.utils.LogicalTypeChecks;
+
+import java.util.Arrays;
+
+import static 
org.apache.flink.table.planner.functions.casting.CastRuleUtils.staticCall;
+
+/** {@link LogicalTypeFamily#BINARY_STRING} to {@link 
LogicalTypeFamily#BINARY_STRING} cast rule. */
+public class BinaryToBinaryCastRule
+        extends AbstractExpressionCodeGeneratorCastRule<byte[], byte[]> {
+
+    static final BinaryToBinaryCastRule INSTANCE = new 
BinaryToBinaryCastRule();
+
+    private BinaryToBinaryCastRule() {
+        super(
+                CastRulePredicate.builder()
+                        .input(LogicalTypeFamily.BINARY_STRING)
+                        .target(LogicalTypeFamily.BINARY_STRING)
+                        .build());
+    }
+
+    @Override
+    public String generateExpression(
+            CodeGeneratorCastRule.Context context,
+            String inputTerm,
+            LogicalType inputLogicalType,
+            LogicalType targetLogicalType) {
+        int inputLength = LogicalTypeChecks.getLength(inputLogicalType);
+        int targetLength = LogicalTypeChecks.getLength(targetLogicalType);
+
+        if (targetLength >= inputLength) {
+            return inputTerm;
+        } else {
+            return staticCall(Arrays.class, "copyOfRange", inputTerm, 0, 
targetLength);
+        }

Review comment:
       Unfortunately you can't assume that the input length is the same as the 
one defined in the type. In our codegen domain we can have a `byte[]` of length 
10 with type defined as `VARBINARY(20)` or even `BINARY(20)`. If you look 
carefully, even the `RawToBinaryCastRule` you introduced behaves the same when 
casting to `BINARY(20)` a raw which array has just size 10.
   
   Hence you need the check at runtime to avoid an unnecessary copy, for 
example in this case:
   
   * input `VARBINARY(30)` with value `byte[].length == 10`
   * target `VARBINARY(20)`
   
   The first branch of the if is fine IMO, because it's reasonable to assume 
that if the input length is minor or equal to the target length, then it's fine 
to just return the same term. The second branch on the other hand should do a 
length check with the ternary operator and skip the copy in case the runtime 
length is already less or equal to the target length.

##########
File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/BinaryToBinaryCastRule.java
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.flink.table.planner.functions.casting;
+
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeFamily;
+import org.apache.flink.table.types.logical.utils.LogicalTypeChecks;
+
+import java.util.Arrays;
+
+import static 
org.apache.flink.table.planner.functions.casting.CastRuleUtils.staticCall;
+
+/** {@link LogicalTypeFamily#BINARY_STRING} to {@link 
LogicalTypeFamily#BINARY_STRING} cast rule. */
+public class BinaryToBinaryCastRule
+        extends AbstractExpressionCodeGeneratorCastRule<byte[], byte[]> {
+
+    static final BinaryToBinaryCastRule INSTANCE = new 
BinaryToBinaryCastRule();
+
+    private BinaryToBinaryCastRule() {
+        super(
+                CastRulePredicate.builder()
+                        .input(LogicalTypeFamily.BINARY_STRING)
+                        .target(LogicalTypeFamily.BINARY_STRING)
+                        .build());
+    }
+
+    @Override
+    public String generateExpression(
+            CodeGeneratorCastRule.Context context,
+            String inputTerm,
+            LogicalType inputLogicalType,
+            LogicalType targetLogicalType) {
+        int inputLength = LogicalTypeChecks.getLength(inputLogicalType);
+        int targetLength = LogicalTypeChecks.getLength(targetLogicalType);
+
+        if (targetLength >= inputLength) {

Review comment:
       Swap this condition

##########
File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/StringToBinaryCastRule.java
##########
@@ -45,6 +49,14 @@ public String generateExpression(
             String inputTerm,
             LogicalType inputLogicalType,
             LogicalType targetLogicalType) {
-        return methodCall(inputTerm, "toBytes");
+        int inputLength = LogicalTypeChecks.getLength(inputLogicalType);
+        int targetLength = LogicalTypeChecks.getLength(targetLogicalType);
+
+        if (targetLength >= inputLength) {

Review comment:
       Swap this condition

##########
File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/RawToBinaryCastRule.java
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.flink.table.planner.functions.casting;
+
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeFamily;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.logical.utils.LogicalTypeChecks;
+
+import java.util.Arrays;
+
+import static org.apache.flink.table.codesplit.CodeSplitUtil.newName;
+import static 
org.apache.flink.table.planner.functions.casting.CastRuleUtils.accessField;
+import static 
org.apache.flink.table.planner.functions.casting.CastRuleUtils.methodCall;
+import static 
org.apache.flink.table.planner.functions.casting.CastRuleUtils.staticCall;
+
+/** {@link LogicalTypeRoot#RAW} to {@link LogicalTypeFamily#BINARY_STRING} 
cast rule. */
+public class RawToBinaryCastRule extends 
AbstractNullAwareCodeGeneratorCastRule<Object, byte[]> {

Review comment:
       remove public

##########
File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/StringToBinaryCastRule.java
##########
@@ -45,6 +49,14 @@ public String generateExpression(
             String inputTerm,
             LogicalType inputLogicalType,
             LogicalType targetLogicalType) {
-        return methodCall(inputTerm, "toBytes");
+        int inputLength = LogicalTypeChecks.getLength(inputLogicalType);
+        int targetLength = LogicalTypeChecks.getLength(targetLogicalType);
+
+        if (targetLength >= inputLength) {
+            return methodCall(inputTerm, "toBytes");
+        } else {
+            return staticCall(
+                    Arrays.class, "copyOfRange", methodCall(inputTerm, 
"toBytes"), 0, targetLength);
+        }

Review comment:
       Same comment as above about the unnecessary copy, but because you invoke 
`toBytes()` which is potentially expensive, convert this rule to a 
`AbstractNullAwareCodeGeneratorCastRule` and save the `toBytes` result in a 
local variable




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to