matriv commented on a change in pull request #17658:
URL: https://github.com/apache/flink/pull/17658#discussion_r742139129



##########
File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/rules/ArrayToStringCastRule.java
##########
@@ -0,0 +1,169 @@
+/*
+ * 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.rules;
+
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.planner.functions.casting.CastCodeBlock;
+import org.apache.flink.table.planner.functions.casting.CastRulePredicate;
+import org.apache.flink.table.planner.functions.casting.CastRuleProvider;
+import org.apache.flink.table.planner.functions.casting.CodeGeneratorCastRule;
+import org.apache.flink.table.types.logical.ArrayType;
+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 static org.apache.flink.table.planner.codegen.CodeGenUtils.className;
+import static org.apache.flink.table.planner.codegen.CodeGenUtils.newName;
+import static 
org.apache.flink.table.planner.codegen.CodeGenUtils.rowFieldReadAccess;
+import static 
org.apache.flink.table.planner.codegen.calls.BuiltInMethods.BINARY_STRING_DATA_FROM_STRING;
+import static 
org.apache.flink.table.planner.functions.casting.rules.CastRuleUtils.constructorCall;
+import static 
org.apache.flink.table.planner.functions.casting.rules.CastRuleUtils.functionCall;
+import static 
org.apache.flink.table.planner.functions.casting.rules.CastRuleUtils.methodCall;
+import static 
org.apache.flink.table.planner.functions.casting.rules.CastRuleUtils.strLiteral;
+
+/** {@link LogicalTypeRoot#ARRAY} to {@link 
LogicalTypeFamily#CHARACTER_STRING} cast rule. */
+public class ArrayToStringCastRule
+        extends AbstractNullAwareCodeGeneratorCastRule<ArrayData, String> {
+
+    public static final ArrayToStringCastRule INSTANCE = new 
ArrayToStringCastRule();
+
+    private ArrayToStringCastRule() {
+        super(
+                CastRulePredicate.builder()
+                        .predicate(
+                                (input, target) ->
+                                        input.is(LogicalTypeRoot.ARRAY)
+                                                && 
target.is(LogicalTypeFamily.CHARACTER_STRING)
+                                                && CastRuleProvider.exists(
+                                                        ((ArrayType) 
input).getElementType(),
+                                                        target))
+                        .build());
+    }
+
+    /* Example generated code for ARRAY<INT>:
+
+    isNull$0 = _myInputIsNull;
+    if (!isNull$0) {
+        builder$1.delete(0, builder$1.length());
+        builder$1.append("[");
+        for (int i$2 = 0; i$2 < _myInput.size(); i$2++) {
+            if (i$2 != 0) {
+                builder$1.append(", ");
+            }
+            int element$3 = -1;
+            boolean elementIsNull$4 = _myInput.isNullAt(i$2);
+            if (!elementIsNull$4) {
+                element$3 = _myInput.getInt(i$2);
+                result$2 = 
org.apache.flink.table.data.binary.BinaryStringData.fromString("" + element$3);
+                builder$1.append(result$2);
+            } else {
+                builder$1.append("NULL");
+            }
+        }
+        builder$1.append("]");
+        result$1 = 
org.apache.flink.table.data.binary.BinaryStringData.fromString(builder$1.toString());
+    } else {
+        result$1 = 
org.apache.flink.table.data.binary.BinaryStringData.EMPTY_UTF8;
+    }
+
+    */
+    @Override
+    protected String generateCodeBlockInternal(
+            CodeGeneratorCastRule.Context context,
+            String inputTerm,
+            String returnVariable,
+            LogicalType inputLogicalType,
+            LogicalType targetLogicalType) {
+        final LogicalType innerInputType = ((ArrayType) 
inputLogicalType).getElementType();
+
+        final String builderTerm = newName("builder");
+        context.declareClassField(
+                className(StringBuilder.class), builderTerm, 
constructorCall(StringBuilder.class));
+
+        return new CastRuleUtils.CodeWriter()
+                .stmt(methodCall(builderTerm, "delete", 0, 
methodCall(builderTerm, "length")))

Review comment:
       `setLength(0)` could be used instead. I see that delete makes an array 
copy, where the setLength(0) just "truncates" the buffer.




-- 
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