Search in sources :

Example 1 with MultisetType

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

the class AvroSchemaConverter method extractValueTypeToAvroMap.

public static LogicalType extractValueTypeToAvroMap(LogicalType type) {
    LogicalType keyType;
    LogicalType valueType;
    if (type instanceof MapType) {
        MapType mapType = (MapType) type;
        keyType = mapType.getKeyType();
        valueType = mapType.getValueType();
    } else {
        MultisetType multisetType = (MultisetType) type;
        keyType = multisetType.getElementType();
        valueType = new IntType();
    }
    if (!keyType.is(LogicalTypeFamily.CHARACTER_STRING)) {
        throw new UnsupportedOperationException("Avro format doesn't support non-string as key type of map. " + "The key type is: " + keyType.asSummaryString());
    }
    return valueType;
}
Also used : LogicalType(org.apache.flink.table.types.logical.LogicalType) MultisetType(org.apache.flink.table.types.logical.MultisetType) MapType(org.apache.flink.table.types.logical.MapType) IntType(org.apache.flink.table.types.logical.IntType)

Example 2 with MultisetType

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

the class LogicalTypesTest method testMultisetType.

@Test
public void testMultisetType() {
    assertThat(new MultisetType(new TimestampType())).satisfies(baseAssertions("MULTISET<TIMESTAMP(6)>", "MULTISET<TIMESTAMP(6)>", new Class[] { Map.class, HashMap.class, TreeMap.class }, new Class[] { Map.class }, new LogicalType[] { new TimestampType() }, new MultisetType(new SmallIntType())));
    assertThat(new MultisetType(new MultisetType(new TimestampType()))).satisfies(baseAssertions("MULTISET<MULTISET<TIMESTAMP(6)>>", "MULTISET<MULTISET<TIMESTAMP(6)>>", new Class[] { Map.class, HashMap.class, TreeMap.class }, new Class[] { Map.class }, new LogicalType[] { new MultisetType(new TimestampType()) }, new MultisetType(new MultisetType(new SmallIntType()))));
}
Also used : SmallIntType(org.apache.flink.table.types.logical.SmallIntType) HashMap(java.util.HashMap) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) TimestampType(org.apache.flink.table.types.logical.TimestampType) ZonedTimestampType(org.apache.flink.table.types.logical.ZonedTimestampType) LogicalType(org.apache.flink.table.types.logical.LogicalType) TreeMap(java.util.TreeMap) MultisetType(org.apache.flink.table.types.logical.MultisetType) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Example 3 with MultisetType

use of org.apache.flink.table.types.logical.MultisetType 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 4 with MultisetType

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

the class LogicalTypeJsonSerdeTest method testLogicalTypeSerde.

private static List<LogicalType> testLogicalTypeSerde() {
    final List<LogicalType> types = Arrays.asList(new BooleanType(), new TinyIntType(), new SmallIntType(), new IntType(), new BigIntType(), new FloatType(), new DoubleType(), new DecimalType(10), new DecimalType(15, 5), CharType.ofEmptyLiteral(), new CharType(), new CharType(5), VarCharType.ofEmptyLiteral(), new VarCharType(), new VarCharType(5), BinaryType.ofEmptyLiteral(), new BinaryType(), new BinaryType(100), VarBinaryType.ofEmptyLiteral(), new VarBinaryType(), new VarBinaryType(100), new DateType(), new TimeType(), new TimeType(3), new TimestampType(), new TimestampType(3), new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3), new TimestampType(false, TimestampKind.ROWTIME, 3), new ZonedTimestampType(), new ZonedTimestampType(3), new ZonedTimestampType(false, TimestampKind.ROWTIME, 3), new LocalZonedTimestampType(), new LocalZonedTimestampType(3), new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3), new LocalZonedTimestampType(false, TimestampKind.ROWTIME, 3), new DayTimeIntervalType(DayTimeIntervalType.DayTimeResolution.DAY_TO_HOUR), new DayTimeIntervalType(false, DayTimeIntervalType.DayTimeResolution.DAY_TO_HOUR, 3, 6), new YearMonthIntervalType(YearMonthIntervalType.YearMonthResolution.YEAR_TO_MONTH), new YearMonthIntervalType(false, YearMonthIntervalType.YearMonthResolution.MONTH, 2), new ZonedTimestampType(), new LocalZonedTimestampType(), new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3), new SymbolType<>(), new ArrayType(new IntType(false)), new ArrayType(new LocalZonedTimestampType(false, TimestampKind.ROWTIME, 3)), new ArrayType(new ZonedTimestampType(false, TimestampKind.ROWTIME, 3)), new ArrayType(new TimestampType()), new ArrayType(CharType.ofEmptyLiteral()), new ArrayType(VarCharType.ofEmptyLiteral()), new ArrayType(BinaryType.ofEmptyLiteral()), new ArrayType(VarBinaryType.ofEmptyLiteral()), new MapType(new BigIntType(), new IntType(false)), new MapType(new TimestampType(false, TimestampKind.ROWTIME, 3), new ZonedTimestampType()), new MapType(CharType.ofEmptyLiteral(), CharType.ofEmptyLiteral()), new MapType(VarCharType.ofEmptyLiteral(), VarCharType.ofEmptyLiteral()), new MapType(BinaryType.ofEmptyLiteral(), BinaryType.ofEmptyLiteral()), new MapType(VarBinaryType.ofEmptyLiteral(), VarBinaryType.ofEmptyLiteral()), new MultisetType(new IntType(false)), new MultisetType(new TimestampType()), new MultisetType(new TimestampType(true, TimestampKind.ROWTIME, 3)), new MultisetType(CharType.ofEmptyLiteral()), new MultisetType(VarCharType.ofEmptyLiteral()), new MultisetType(BinaryType.ofEmptyLiteral()), new MultisetType(VarBinaryType.ofEmptyLiteral()), RowType.of(new BigIntType(), new IntType(false), new VarCharType(200)), RowType.of(new LogicalType[] { new BigIntType(), new IntType(false), new VarCharType(200) }, new String[] { "f1", "f2", "f3" }), RowType.of(new TimestampType(false, TimestampKind.ROWTIME, 3), new TimestampType(false, TimestampKind.REGULAR, 3), new ZonedTimestampType(false, TimestampKind.ROWTIME, 3), new ZonedTimestampType(false, TimestampKind.REGULAR, 3), new LocalZonedTimestampType(false, TimestampKind.ROWTIME, 3), new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3), new LocalZonedTimestampType(false, TimestampKind.REGULAR, 3)), RowType.of(CharType.ofEmptyLiteral(), VarCharType.ofEmptyLiteral(), BinaryType.ofEmptyLiteral(), VarBinaryType.ofEmptyLiteral()), // registered structured type
    StructuredType.newBuilder(ObjectIdentifier.of("cat", "db", "structuredType"), PojoClass.class).attributes(Arrays.asList(new StructuredType.StructuredAttribute("f0", new IntType(true)), new StructuredType.StructuredAttribute("f1", new BigIntType(true)), new StructuredType.StructuredAttribute("f2", new VarCharType(200), "desc"))).comparison(StructuredType.StructuredComparison.FULL).setFinal(false).setInstantiable(false).superType(StructuredType.newBuilder(ObjectIdentifier.of("cat", "db", "structuredType2")).attributes(Collections.singletonList(new StructuredType.StructuredAttribute("f0", new BigIntType(false)))).build()).description("description for StructuredType").build(), // unregistered structured type
    StructuredType.newBuilder(PojoClass.class).attributes(Arrays.asList(new StructuredType.StructuredAttribute("f0", new IntType(true)), new StructuredType.StructuredAttribute("f1", new BigIntType(true)), new StructuredType.StructuredAttribute("f2", new VarCharType(200), "desc"))).build(), // registered distinct type
    DistinctType.newBuilder(ObjectIdentifier.of("cat", "db", "distinctType"), new VarCharType(5)).build(), DistinctType.newBuilder(ObjectIdentifier.of("cat", "db", "distinctType"), new VarCharType(false, 5)).build(), // custom RawType
    new RawType<>(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE), // external RawType
    new RawType<>(Row.class, ExternalSerializer.of(DataTypes.ROW(DataTypes.INT(), DataTypes.STRING()))));
    final List<LogicalType> mutableTypes = new ArrayList<>(types);
    // RawType for MapView
    addRawTypesForMapView(mutableTypes, new VarCharType(100), new VarCharType(100));
    addRawTypesForMapView(mutableTypes, new VarCharType(100), new BigIntType());
    addRawTypesForMapView(mutableTypes, new BigIntType(), new VarCharType(100));
    addRawTypesForMapView(mutableTypes, new BigIntType(), new BigIntType());
    // RawType for ListView
    addRawTypesForListView(mutableTypes, new VarCharType(100));
    addRawTypesForListView(mutableTypes, new BigIntType());
    // RawType for custom MapView
    mutableTypes.add(DataViewUtils.adjustDataViews(MapView.newMapViewDataType(DataTypes.STRING().toInternal(), DataTypes.STRING().bridgedTo(byte[].class)), false).getLogicalType());
    final List<LogicalType> allTypes = new ArrayList<>();
    // consider nullable
    for (LogicalType type : mutableTypes) {
        allTypes.add(type.copy(true));
        allTypes.add(type.copy(false));
    }
    // ignore nullable for NullType
    allTypes.add(new NullType());
    return allTypes;
}
Also used : LocalDateTime(java.time.LocalDateTime) VarBinaryType(org.apache.flink.table.types.logical.VarBinaryType) ArrayList(java.util.ArrayList) LogicalType(org.apache.flink.table.types.logical.LogicalType) BigIntType(org.apache.flink.table.types.logical.BigIntType) MapType(org.apache.flink.table.types.logical.MapType) TinyIntType(org.apache.flink.table.types.logical.TinyIntType) IntType(org.apache.flink.table.types.logical.IntType) BigIntType(org.apache.flink.table.types.logical.BigIntType) SmallIntType(org.apache.flink.table.types.logical.SmallIntType) FloatType(org.apache.flink.table.types.logical.FloatType) TimeType(org.apache.flink.table.types.logical.TimeType) StructuredType(org.apache.flink.table.types.logical.StructuredType) ArrayType(org.apache.flink.table.types.logical.ArrayType) PojoClass(org.apache.flink.table.planner.plan.nodes.exec.serde.DataTypeJsonSerdeTest.PojoClass) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) TimestampType(org.apache.flink.table.types.logical.TimestampType) ZonedTimestampType(org.apache.flink.table.types.logical.ZonedTimestampType) VarCharType(org.apache.flink.table.types.logical.VarCharType) DateType(org.apache.flink.table.types.logical.DateType) MultisetType(org.apache.flink.table.types.logical.MultisetType) BinaryType(org.apache.flink.table.types.logical.BinaryType) VarBinaryType(org.apache.flink.table.types.logical.VarBinaryType) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) ZonedTimestampType(org.apache.flink.table.types.logical.ZonedTimestampType) BooleanType(org.apache.flink.table.types.logical.BooleanType) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) TinyIntType(org.apache.flink.table.types.logical.TinyIntType) DayTimeIntervalType(org.apache.flink.table.types.logical.DayTimeIntervalType) SmallIntType(org.apache.flink.table.types.logical.SmallIntType) DoubleType(org.apache.flink.table.types.logical.DoubleType) DecimalType(org.apache.flink.table.types.logical.DecimalType) CharType(org.apache.flink.table.types.logical.CharType) VarCharType(org.apache.flink.table.types.logical.VarCharType) Row(org.apache.flink.types.Row) NullType(org.apache.flink.table.types.logical.NullType) YearMonthIntervalType(org.apache.flink.table.types.logical.YearMonthIntervalType)

Example 5 with MultisetType

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

the class DataTypePrecisionFixer method visit.

@Override
public DataType visit(CollectionDataType collectionDataType) {
    DataType elementType = collectionDataType.getElementDataType();
    switch(logicalType.getTypeRoot()) {
        case ARRAY:
            ArrayType arrayType = (ArrayType) logicalType;
            DataType newArrayElementType = elementType.accept(new DataTypePrecisionFixer(arrayType.getElementType()));
            return DataTypes.ARRAY(newArrayElementType).bridgedTo(collectionDataType.getConversionClass());
        case MULTISET:
            MultisetType multisetType = (MultisetType) logicalType;
            DataType newMultisetElementType = elementType.accept(new DataTypePrecisionFixer(multisetType.getElementType()));
            return DataTypes.MULTISET(newMultisetElementType).bridgedTo(collectionDataType.getConversionClass());
        default:
            throw new UnsupportedOperationException("Unsupported logical type : " + logicalType);
    }
}
Also used : ArrayType(org.apache.flink.table.types.logical.ArrayType) DataType(org.apache.flink.table.types.DataType) AtomicDataType(org.apache.flink.table.types.AtomicDataType) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) CollectionDataType(org.apache.flink.table.types.CollectionDataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) MultisetType(org.apache.flink.table.types.logical.MultisetType)

Aggregations

MultisetType (org.apache.flink.table.types.logical.MultisetType)6 LogicalType (org.apache.flink.table.types.logical.LogicalType)5 ArrayType (org.apache.flink.table.types.logical.ArrayType)3 IntType (org.apache.flink.table.types.logical.IntType)3 MapType (org.apache.flink.table.types.logical.MapType)3 LocalZonedTimestampType (org.apache.flink.table.types.logical.LocalZonedTimestampType)2 SmallIntType (org.apache.flink.table.types.logical.SmallIntType)2 TimestampType (org.apache.flink.table.types.logical.TimestampType)2 ZonedTimestampType (org.apache.flink.table.types.logical.ZonedTimestampType)2 LocalDateTime (java.time.LocalDateTime)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Consumer (java.util.function.Consumer)1 JsonNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode)1 TableException (org.apache.flink.table.api.TableException)1 ArrayData (org.apache.flink.table.data.ArrayData)1 CodeGenUtils.className (org.apache.flink.table.planner.codegen.CodeGenUtils.className)1 CodeGenUtils.newName (org.apache.flink.table.planner.codegen.CodeGenUtils.newName)1