Search in sources :

Example 1 with STRING_TYPE

use of org.apache.flink.table.types.logical.VarCharType.STRING_TYPE in project flink by apache.

the class MapAndMultisetToStringCastRule method generateCodeBlockInternal.

/* Example generated code for MAP<STRING, INTERVAL MONTH> -> CHAR(12):

    isNull$0 = _myInputIsNull;
    if (!isNull$0) {
        org.apache.flink.table.data.ArrayData keys$2 = _myInput.keyArray();
        org.apache.flink.table.data.ArrayData values$3 = _myInput.valueArray();
        builder$1.setLength(0);
        builder$1.append("{");
        for (int i$5 = 0; i$5 < _myInput.size(); i$5++) {
            if (builder$1.length() > 12) {
                break;
            }
            if (i$5 != 0) {
                builder$1.append(", ");
            }
            org.apache.flink.table.data.binary.BinaryStringData key$6 = org.apache.flink.table.data.binary.BinaryStringData.EMPTY_UTF8;
            boolean keyIsNull$7 = keys$2.isNullAt(i$5);
            int value$8 = -1;
            boolean valueIsNull$9 = values$3.isNullAt(i$5);
            if (!keyIsNull$7) {
                key$6 = ((org.apache.flink.table.data.binary.BinaryStringData) keys$2.getString(i$5));
                builder$1.append(key$6);
            } else {
                builder$1.append("NULL");
            }
            builder$1.append("=");
            if (!valueIsNull$9) {
                value$8 = values$3.getInt(i$5);
                isNull$2 = valueIsNull$9;
                if (!isNull$2) {
                    result$3 = org.apache.flink.table.data.binary.BinaryStringData.fromString("" + value$8);
                    isNull$2 = result$3 == null;
                } else {
                    result$3 = org.apache.flink.table.data.binary.BinaryStringData.EMPTY_UTF8;
                }
                builder$1.append(result$3);
            } else {
                builder$1.append("NULL");
            }
        }
        builder$1.append("}");
        java.lang.String resultString$4;
        resultString$4 = builder$1.toString();
        if (builder$1.length() > 12) {
            resultString$4 = builder$1.substring(0, java.lang.Math.min(builder$1.length(), 12));
        } else {
            if (resultString$.length() < 12) {
                int padLength$10;
                padLength$10 = 12 - resultString$.length();
                java.lang.StringBuilder sbPadding$11;
                sbPadding$11 = new java.lang.StringBuilder();
                for (int i$12 = 0; i$12 < padLength$10; i$12++) {
                    sbPadding$11.append(" ");
                }
                resultString$4 = resultString$4 + sbPadding$11.toString();
            }
        }
        result$1 = org.apache.flink.table.data.binary.BinaryStringData.fromString(resultString$4);
        isNull$0 = result$1 == null;
    } 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 keyType = inputLogicalType.is(LogicalTypeRoot.MULTISET) ? ((MultisetType) inputLogicalType).getElementType() : ((MapType) inputLogicalType).getKeyType();
    final LogicalType valueType = inputLogicalType.is(LogicalTypeRoot.MULTISET) ? new IntType(false) : ((MapType) inputLogicalType).getValueType();
    final String builderTerm = newName("builder");
    context.declareClassField(className(StringBuilder.class), builderTerm, constructorCall(StringBuilder.class));
    final String keyArrayTerm = newName("keys");
    final String valueArrayTerm = newName("values");
    final String resultStringTerm = newName("resultString");
    final int length = LogicalTypeChecks.getLength(targetLogicalType);
    CastRuleUtils.CodeWriter writer = new CastRuleUtils.CodeWriter().declStmt(ArrayData.class, keyArrayTerm, methodCall(inputTerm, "keyArray")).declStmt(ArrayData.class, valueArrayTerm, methodCall(inputTerm, "valueArray")).stmt(methodCall(builderTerm, "setLength", 0)).stmt(methodCall(builderTerm, "append", strLiteral("{"))).forStmt(methodCall(inputTerm, "size"), (indexTerm, loopBodyWriter) -> {
        String keyTerm = newName("key");
        String keyIsNullTerm = newName("keyIsNull");
        String valueTerm = newName("value");
        String valueIsNullTerm = newName("valueIsNull");
        CastCodeBlock keyCast = // Null check is done at the key array access level
        CastRuleProvider.generateAlwaysNonNullCodeBlock(context, keyTerm, keyType, STRING_TYPE);
        CastCodeBlock valueCast = // Null check is done at the value array access level
        CastRuleProvider.generateAlwaysNonNullCodeBlock(context, valueTerm, valueType, STRING_TYPE);
        Consumer<CastRuleUtils.CodeWriter> appendNonNullValue = bodyWriter -> bodyWriter.assignStmt(valueTerm, rowFieldReadAccess(indexTerm, valueArrayTerm, valueType)).append(valueCast).stmt(methodCall(builderTerm, "append", valueCast.getReturnTerm()));
        if (!context.legacyBehaviour() && couldTrim(length)) {
            loopBodyWriter.ifStmt(stringExceedsLength(builderTerm, length), CastRuleUtils.CodeWriter::breakStmt);
        }
        loopBodyWriter.ifStmt(indexTerm + " != 0", thenBodyWriter -> thenBodyWriter.stmt(methodCall(builderTerm, "append", strLiteral(", ")))).declPrimitiveStmt(keyType, keyTerm).declStmt(boolean.class, keyIsNullTerm, methodCall(keyArrayTerm, "isNullAt", indexTerm)).declPrimitiveStmt(valueType, valueTerm).declStmt(boolean.class, valueIsNullTerm, methodCall(valueArrayTerm, "isNullAt", indexTerm)).ifStmt("!" + keyIsNullTerm, thenBodyWriter -> thenBodyWriter.assignStmt(keyTerm, rowFieldReadAccess(indexTerm, keyArrayTerm, keyType)).append(keyCast).stmt(methodCall(builderTerm, "append", keyCast.getReturnTerm())), elseBodyWriter -> elseBodyWriter.stmt(methodCall(builderTerm, "append", nullLiteral(context.legacyBehaviour())))).stmt(methodCall(builderTerm, "append", strLiteral("=")));
        if (inputLogicalType.is(LogicalTypeRoot.MULTISET)) {
            appendNonNullValue.accept(loopBodyWriter);
        } else {
            loopBodyWriter.ifStmt("!" + valueIsNullTerm, appendNonNullValue, elseBodyWriter -> elseBodyWriter.stmt(methodCall(builderTerm, "append", nullLiteral(context.legacyBehaviour()))));
        }
    }).stmt(methodCall(builderTerm, "append", strLiteral("}")));
    return CharVarCharTrimPadCastRule.padAndTrimStringIfNeeded(writer, targetLogicalType, context.legacyBehaviour(), length, resultStringTerm, builderTerm).assignStmt(returnVariable, CastRuleUtils.staticCall(BINARY_STRING_DATA_FROM_STRING(), resultStringTerm)).toString();
}
Also used : STRING_TYPE(org.apache.flink.table.types.logical.VarCharType.STRING_TYPE) CastRuleUtils.strLiteral(org.apache.flink.table.planner.functions.casting.CastRuleUtils.strLiteral) BINARY_STRING_DATA_FROM_STRING(org.apache.flink.table.planner.codegen.calls.BuiltInMethods.BINARY_STRING_DATA_FROM_STRING) IntType(org.apache.flink.table.types.logical.IntType) CodeGenUtils.className(org.apache.flink.table.planner.codegen.CodeGenUtils.className) CharVarCharTrimPadCastRule.couldTrim(org.apache.flink.table.planner.functions.casting.CharVarCharTrimPadCastRule.couldTrim) MapType(org.apache.flink.table.types.logical.MapType) CodeGenUtils.rowFieldReadAccess(org.apache.flink.table.planner.codegen.CodeGenUtils.rowFieldReadAccess) CastRuleUtils.nullLiteral(org.apache.flink.table.planner.functions.casting.CastRuleUtils.nullLiteral) CastRuleUtils.methodCall(org.apache.flink.table.planner.functions.casting.CastRuleUtils.methodCall) Consumer(java.util.function.Consumer) ArrayData(org.apache.flink.table.data.ArrayData) CharVarCharTrimPadCastRule.stringExceedsLength(org.apache.flink.table.planner.functions.casting.CharVarCharTrimPadCastRule.stringExceedsLength) LogicalType(org.apache.flink.table.types.logical.LogicalType) CastRuleUtils.constructorCall(org.apache.flink.table.planner.functions.casting.CastRuleUtils.constructorCall) LogicalTypeFamily(org.apache.flink.table.types.logical.LogicalTypeFamily) CodeGenUtils.newName(org.apache.flink.table.planner.codegen.CodeGenUtils.newName) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) MultisetType(org.apache.flink.table.types.logical.MultisetType) LogicalTypeChecks(org.apache.flink.table.types.logical.utils.LogicalTypeChecks) Consumer(java.util.function.Consumer) LogicalType(org.apache.flink.table.types.logical.LogicalType) IntType(org.apache.flink.table.types.logical.IntType) ArrayData(org.apache.flink.table.data.ArrayData)

Example 2 with STRING_TYPE

use of org.apache.flink.table.types.logical.VarCharType.STRING_TYPE in project flink by apache.

the class ArrayToStringCastRule method generateCodeBlockInternal.

/* Example generated code for ARRAY<INT> -> CHAR(10)

    isNull$0 = _myInputIsNull;
    if (!isNull$0) {
        builder$1.setLength(0);
        builder$1.append("[");
        for (int i$3 = 0; i$3 < _myInput.size(); i$3++) {
            if (builder$1.length() > 10) {
                break;
            }
            if (i$3 != 0) {
                builder$1.append(", ");
            }
            int element$4 = -1;
            boolean elementIsNull$5 = _myInput.isNullAt(i$3);
            if (!elementIsNull$5) {
                element$4 = _myInput.getInt(i$3);
                isNull$2 = false;
                if (!isNull$2) {
                    result$3 = org.apache.flink.table.data.binary.BinaryStringData.fromString("" + element$4);
                    isNull$2 = result$3 == null;
                } else {
                    result$3 = org.apache.flink.table.data.binary.BinaryStringData.EMPTY_UTF8;
                }
                builder$1.append(result$3);
            } else {
                builder$1.append("NULL");
            }
        }
        builder$1.append("]");
        java.lang.String resultString$2;
        resultString$2 = builder$1.toString();
        if (builder$1.length() > 10) {
            resultString$2 = builder$1.substring(0, java.lang.Math.min(builder$1.length(), 10));
        } else {
            if (resultString$2.length() < 10) {
                int padLength$6;
                padLength$6 = 10 - resultString$2.length();
                java.lang.StringBuilder sbPadding$7;
                sbPadding$7 = new java.lang.StringBuilder();
                for (int i$8 = 0; i$8 < padLength$6; i$8++) {
                    sbPadding$7.append(" ");
                }
                resultString$2 = resultString$2 + sbPadding$7.toString();
            }
        }
        result$1 = org.apache.flink.table.data.binary.BinaryStringData.fromString(resultString$2);
        isNull$0 = result$1 == null;
    } 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));
    final String resultStringTerm = newName("resultString");
    final int length = LogicalTypeChecks.getLength(targetLogicalType);
    CastRuleUtils.CodeWriter writer = new CastRuleUtils.CodeWriter().stmt(methodCall(builderTerm, "setLength", 0)).stmt(methodCall(builderTerm, "append", strLiteral("["))).forStmt(methodCall(inputTerm, "size"), (indexTerm, loopBodyWriter) -> {
        String elementTerm = newName("element");
        String elementIsNullTerm = newName("elementIsNull");
        CastCodeBlock codeBlock = // Null check is done at the array access level
        CastRuleProvider.generateAlwaysNonNullCodeBlock(context, elementTerm, innerInputType, STRING_TYPE);
        if (!context.legacyBehaviour() && couldTrim(length)) {
            // Break if the target length is already exceeded
            loopBodyWriter.ifStmt(stringExceedsLength(builderTerm, length), CastRuleUtils.CodeWriter::breakStmt);
        }
        loopBodyWriter.ifStmt(indexTerm + " != 0", thenBodyWriter -> thenBodyWriter.stmt(methodCall(builderTerm, "append", strLiteral(", ")))).declPrimitiveStmt(innerInputType, elementTerm).declStmt(boolean.class, elementIsNullTerm, methodCall(inputTerm, "isNullAt", indexTerm)).ifStmt("!" + elementIsNullTerm, thenBodyWriter -> thenBodyWriter.assignStmt(elementTerm, rowFieldReadAccess(indexTerm, inputTerm, innerInputType)).append(codeBlock).stmt(methodCall(builderTerm, "append", codeBlock.getReturnTerm())), elseBodyWriter -> elseBodyWriter.stmt(methodCall(builderTerm, "append", nullLiteral(context.legacyBehaviour()))));
    }).stmt(methodCall(builderTerm, "append", strLiteral("]")));
    return CharVarCharTrimPadCastRule.padAndTrimStringIfNeeded(writer, targetLogicalType, context.legacyBehaviour(), length, resultStringTerm, builderTerm).assignStmt(returnVariable, CastRuleUtils.staticCall(BINARY_STRING_DATA_FROM_STRING(), resultStringTerm)).toString();
}
Also used : ArrayType(org.apache.flink.table.types.logical.ArrayType) STRING_TYPE(org.apache.flink.table.types.logical.VarCharType.STRING_TYPE) CastRuleUtils.strLiteral(org.apache.flink.table.planner.functions.casting.CastRuleUtils.strLiteral) BINARY_STRING_DATA_FROM_STRING(org.apache.flink.table.planner.codegen.calls.BuiltInMethods.BINARY_STRING_DATA_FROM_STRING) CodeGenUtils.className(org.apache.flink.table.planner.codegen.CodeGenUtils.className) CharVarCharTrimPadCastRule.couldTrim(org.apache.flink.table.planner.functions.casting.CharVarCharTrimPadCastRule.couldTrim) ArrayType(org.apache.flink.table.types.logical.ArrayType) CodeGenUtils.rowFieldReadAccess(org.apache.flink.table.planner.codegen.CodeGenUtils.rowFieldReadAccess) CastRuleUtils.nullLiteral(org.apache.flink.table.planner.functions.casting.CastRuleUtils.nullLiteral) CastRuleUtils.methodCall(org.apache.flink.table.planner.functions.casting.CastRuleUtils.methodCall) ArrayData(org.apache.flink.table.data.ArrayData) CharVarCharTrimPadCastRule.stringExceedsLength(org.apache.flink.table.planner.functions.casting.CharVarCharTrimPadCastRule.stringExceedsLength) LogicalType(org.apache.flink.table.types.logical.LogicalType) CastRuleUtils.constructorCall(org.apache.flink.table.planner.functions.casting.CastRuleUtils.constructorCall) LogicalTypeFamily(org.apache.flink.table.types.logical.LogicalTypeFamily) CodeGenUtils.newName(org.apache.flink.table.planner.codegen.CodeGenUtils.newName) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) LogicalTypeChecks(org.apache.flink.table.types.logical.utils.LogicalTypeChecks) LogicalType(org.apache.flink.table.types.logical.LogicalType)

Aggregations

ArrayData (org.apache.flink.table.data.ArrayData)2 CodeGenUtils.className (org.apache.flink.table.planner.codegen.CodeGenUtils.className)2 CodeGenUtils.newName (org.apache.flink.table.planner.codegen.CodeGenUtils.newName)2 CodeGenUtils.rowFieldReadAccess (org.apache.flink.table.planner.codegen.CodeGenUtils.rowFieldReadAccess)2 BINARY_STRING_DATA_FROM_STRING (org.apache.flink.table.planner.codegen.calls.BuiltInMethods.BINARY_STRING_DATA_FROM_STRING)2 CastRuleUtils.constructorCall (org.apache.flink.table.planner.functions.casting.CastRuleUtils.constructorCall)2 CastRuleUtils.methodCall (org.apache.flink.table.planner.functions.casting.CastRuleUtils.methodCall)2 CastRuleUtils.nullLiteral (org.apache.flink.table.planner.functions.casting.CastRuleUtils.nullLiteral)2 CastRuleUtils.strLiteral (org.apache.flink.table.planner.functions.casting.CastRuleUtils.strLiteral)2 CharVarCharTrimPadCastRule.couldTrim (org.apache.flink.table.planner.functions.casting.CharVarCharTrimPadCastRule.couldTrim)2 CharVarCharTrimPadCastRule.stringExceedsLength (org.apache.flink.table.planner.functions.casting.CharVarCharTrimPadCastRule.stringExceedsLength)2 LogicalType (org.apache.flink.table.types.logical.LogicalType)2 LogicalTypeFamily (org.apache.flink.table.types.logical.LogicalTypeFamily)2 LogicalTypeRoot (org.apache.flink.table.types.logical.LogicalTypeRoot)2 STRING_TYPE (org.apache.flink.table.types.logical.VarCharType.STRING_TYPE)2 LogicalTypeChecks (org.apache.flink.table.types.logical.utils.LogicalTypeChecks)2 Consumer (java.util.function.Consumer)1 ArrayType (org.apache.flink.table.types.logical.ArrayType)1 IntType (org.apache.flink.table.types.logical.IntType)1 MapType (org.apache.flink.table.types.logical.MapType)1