matriv commented on a change in pull request #17658: URL: https://github.com/apache/flink/pull/17658#discussion_r743578729
########## File path: flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/rules/BinaryToStringCastRule.java ########## @@ -0,0 +1,55 @@ +/* + * 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.planner.functions.casting.CastRulePredicate; +import org.apache.flink.table.planner.functions.casting.CodeGeneratorCastRule; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.LogicalTypeFamily; + +import java.nio.charset.StandardCharsets; + +import static org.apache.flink.table.planner.functions.casting.rules.CastRuleUtils.accessStaticField; +import static org.apache.flink.table.planner.functions.casting.rules.CastRuleUtils.constructorCall; + +/** + * {@link LogicalTypeFamily#BINARY_STRING} to {@link LogicalTypeFamily#CHARACTER_STRING} cast rule. + */ +public class BinaryToStringCastRule extends AbstractCharacterFamilyTargetRule<byte[]> { + + public static final BinaryToStringCastRule INSTANCE = new BinaryToStringCastRule(); + + private BinaryToStringCastRule() { + super( + CastRulePredicate.builder() + .input(LogicalTypeFamily.BINARY_STRING) + .target(LogicalTypeFamily.CHARACTER_STRING) + .build()); + } + + @Override + public String generateStringExpression( + CodeGeneratorCastRule.Context context, + String inputTerm, + LogicalType inputLogicalType, + LogicalType targetLogicalType) { + return constructorCall( Review comment: I would say yes, imho, it's better not to mix this refactoring with addressing more issues. ########## File path: flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/rules/CastRuleUtils.java ########## @@ -36,4 +49,141 @@ static String functionCall(String functionName, Object... args) { static String functionCall(Method staticMethod, Object... args) { return functionCall(CodeGenUtils.qualifyMethod(staticMethod), args); } + + static String constructorCall(Class<?> clazz, Object... args) { + return functionCall("new " + className(clazz), args); + } + + static String methodCall(String instanceTerm, String methodName, Object... args) { + return functionCall(instanceTerm + "." + methodName, args); + } + + static String newArray(String innerType, String arraySize) { + return "new " + innerType + "[" + arraySize + "]"; + } + + static String stringConcat(Object... args) { + return Arrays.stream(args).map(Object::toString).collect(Collectors.joining(" + ")); + } + + static String accessStaticField(Class<?> clazz, String fieldName) { + return className(clazz) + "." + fieldName; + } + + static String ternaryOperator(String condition, String ifTrue, String ifFalse) { + return "((" + condition + ") ? (" + ifTrue + ") : (" + ifFalse + "))"; + } + + static String strLiteral() { + return "\"\""; + } + + static String strLiteral(String str) { + return "\"" + StringEscapeUtils.escapeJava(str) + "\""; + } + + static final class CodeWriter { + StringBuilder builder = new StringBuilder(); + + public CodeWriter declStmt(String varType, String varName, String value) { + return stmt(varType + " " + varName + " = " + value); + } + + public CodeWriter declStmt(Class<?> clazz, String varName, String value) { + return declStmt(className(clazz), varName, value); + } + + public CodeWriter declPrimitiveStmt(LogicalType logicalType, String varName, String value) { + return declStmt(primitiveTypeTermForType(logicalType), varName, value); + } + + public CodeWriter declPrimitiveStmt(LogicalType logicalType, String varName) { + return declStmt( + primitiveTypeTermForType(logicalType), + varName, + primitiveDefaultValue(logicalType)); + } + + public CodeWriter declStmt(String varType, String varName) { + return stmt(varType + " " + varName); + } + + public CodeWriter declStmt(Class<?> clazz, String varName) { + return declStmt(className(clazz), varName); + } + + public CodeWriter assignStmt(String varName, String value) { + return stmt(varName + " = " + value); + } + + public CodeWriter assignArrayStmt(String varName, String index, String value) { + return stmt(varName + "[" + index + "] = " + value); + } + + public CodeWriter stmt(String stmt) { + builder.append(stmt).append(';').append('\n'); + return this; + } + + public CodeWriter forStmt( + String upperBound, BiConsumer<String, CodeWriter> bodyWriterConsumer) { + final String indexTerm = newName("i"); + final CodeWriter innerWriter = new CodeWriter(); + + builder.append("for (int ") + .append(indexTerm) + .append(" = 0; ") + .append(indexTerm) + .append(" < ") + .append(upperBound) + .append("; ") + .append(indexTerm) + .append("++) {\n"); + bodyWriterConsumer.accept(indexTerm, innerWriter); + builder.append(innerWriter).append("}\n"); + + return this; + } + + public CodeWriter ifStmt(String condition, Consumer<CodeWriter> bodyWriterConsumer) { + final CodeWriter innerWriter = new CodeWriter(); + + builder.append("if (").append(condition).append(") {\n"); + bodyWriterConsumer.accept(innerWriter); + builder.append(innerWriter).append("}\n"); + + return this; + } + + public CodeWriter ifStmt( + String condition, + Consumer<CodeWriter> thenWriterConsumer, + Consumer<CodeWriter> elseWriterConsumer) { + final CodeWriter thenWriter = new CodeWriter(); + final CodeWriter elseWriter = new CodeWriter(); + + builder.append("if (").append(condition).append(") {\n"); + thenWriterConsumer.accept(thenWriter); + builder.append(thenWriter).append("} else {\n"); + elseWriterConsumer.accept(elseWriter); + builder.append(elseWriter).append("}\n"); + + return this; + } + + public CodeWriter append(CastCodeBlock codeBlock) { + builder.append(codeBlock.getCode()); + return this; + } + + public CodeWriter append(String codeBlock) { Review comment: +1 -- 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]
