Search in sources :

Example 6 with ArrayType

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

the class RowDataToJsonConverters method createArrayConverter.

private RowDataToJsonConverter createArrayConverter(ArrayType type) {
    final LogicalType elementType = type.getElementType();
    final RowDataToJsonConverter elementConverter = createConverter(elementType);
    final ArrayData.ElementGetter elementGetter = ArrayData.createElementGetter(elementType);
    return (mapper, reuse, value) -> {
        ArrayNode node;
        // reuse could be a NullNode if last record is null.
        if (reuse == null || reuse.isNull()) {
            node = mapper.createArrayNode();
        } else {
            node = (ArrayNode) reuse;
            node.removeAll();
        }
        ArrayData array = (ArrayData) value;
        int numElements = array.size();
        for (int i = 0; i < numElements; i++) {
            Object element = elementGetter.getElementOrNull(array, i);
            node.add(elementConverter.convert(mapper, null, element));
        }
        return node;
    };
}
Also used : ISO8601_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT(org.apache.flink.formats.common.TimeFormats.ISO8601_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT) Arrays(java.util.Arrays) IntType(org.apache.flink.table.types.logical.IntType) SQL_TIMESTAMP_FORMAT(org.apache.flink.formats.common.TimeFormats.SQL_TIMESTAMP_FORMAT) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) MapType(org.apache.flink.table.types.logical.MapType) RowType(org.apache.flink.table.types.logical.RowType) ArrayNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode) BigDecimal(java.math.BigDecimal) SQL_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT(org.apache.flink.formats.common.TimeFormats.SQL_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT) LogicalTypeFamily(org.apache.flink.table.types.logical.LogicalTypeFamily) ObjectMapper(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper) LocalTime(java.time.LocalTime) ZoneOffset(java.time.ZoneOffset) ISO_LOCAL_DATE(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE) MultisetType(org.apache.flink.table.types.logical.MultisetType) ISO8601_TIMESTAMP_FORMAT(org.apache.flink.formats.common.TimeFormats.ISO8601_TIMESTAMP_FORMAT) RowData(org.apache.flink.table.data.RowData) TimestampData(org.apache.flink.table.data.TimestampData) MapData(org.apache.flink.table.data.MapData) ObjectNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode) TableException(org.apache.flink.table.api.TableException) DecimalData(org.apache.flink.table.data.DecimalData) ArrayType(org.apache.flink.table.types.logical.ArrayType) TimestampFormat(org.apache.flink.formats.common.TimestampFormat) Serializable(java.io.Serializable) SQL_TIME_FORMAT(org.apache.flink.formats.common.TimeFormats.SQL_TIME_FORMAT) ArrayData(org.apache.flink.table.data.ArrayData) LogicalType(org.apache.flink.table.types.logical.LogicalType) LocalDate(java.time.LocalDate) Internal(org.apache.flink.annotation.Internal) LogicalType(org.apache.flink.table.types.logical.LogicalType) ArrayNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode) ArrayData(org.apache.flink.table.data.ArrayData)

Example 7 with ArrayType

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

the class LogicalTypesTest method testArrayType.

@Test
public void testArrayType() {
    assertThat(new ArrayType(new TimestampType())).satisfies(baseAssertions("ARRAY<TIMESTAMP(6)>", "ARRAY<TIMESTAMP(6)>", new Class[] { java.sql.Timestamp[].class, java.time.LocalDateTime[].class, List.class, ArrayList.class }, new Class[] { java.sql.Timestamp[].class, java.time.LocalDateTime[].class, List.class }, new LogicalType[] { new TimestampType() }, new ArrayType(new SmallIntType())));
    assertThat(new ArrayType(new ArrayType(new TimestampType()))).satisfies(baseAssertions("ARRAY<ARRAY<TIMESTAMP(6)>>", "ARRAY<ARRAY<TIMESTAMP(6)>>", new Class[] { java.sql.Timestamp[][].class, java.time.LocalDateTime[][].class }, new Class[] { java.sql.Timestamp[][].class, java.time.LocalDateTime[][].class }, new LogicalType[] { new ArrayType(new TimestampType()) }, new ArrayType(new ArrayType(new SmallIntType()))));
    final LogicalType nestedArray = new ArrayType(new ArrayType(new TimestampType()));
    assertThat(nestedArray).doesNotSupportInputConversion(java.sql.Timestamp[].class).doesNotSupportOutputConversion(java.sql.Timestamp[].class);
}
Also used : ArrayType(org.apache.flink.table.types.logical.ArrayType) LocalDateTime(java.time.LocalDateTime) SmallIntType(org.apache.flink.table.types.logical.SmallIntType) ArrayList(java.util.ArrayList) 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) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 8 with ArrayType

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

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

the class ArrayToArrayCastRule method generateCodeBlockInternal.

@SuppressWarnings("rawtypes")
@Override
protected String generateCodeBlockInternal(CodeGeneratorCastRule.Context context, String inputTerm, String returnVariable, LogicalType inputLogicalType, LogicalType targetLogicalType) {
    final LogicalType innerInputType = ((ArrayType) inputLogicalType).getElementType();
    final LogicalType innerTargetType = ((ArrayType) targetLogicalType).getElementType();
    final String innerTargetTypeTerm = arrayElementType(innerTargetType);
    final String arraySize = methodCall(inputTerm, "size");
    final String objArrayTerm = newName("objArray");
    return new CastRuleUtils.CodeWriter().declStmt(innerTargetTypeTerm + "[]", objArrayTerm, newArray(innerTargetTypeTerm, arraySize)).forStmt(arraySize, (index, loopWriter) -> {
        CastCodeBlock codeBlock = // Null check is done at the array access level
        CastRuleProvider.generateAlwaysNonNullCodeBlock(context, rowFieldReadAccess(index, inputTerm, innerInputType), innerInputType, innerTargetType);
        if (innerTargetType.isNullable()) {
            loopWriter.ifStmt("!" + methodCall(inputTerm, "isNullAt", index), thenWriter -> thenWriter.append(codeBlock).assignArrayStmt(objArrayTerm, index, codeBlock.getReturnTerm()));
        } else {
            loopWriter.append(codeBlock).assignArrayStmt(objArrayTerm, index, codeBlock.getReturnTerm());
        }
    }).assignStmt(returnVariable, constructorCall(GenericArrayData.class, objArrayTerm)).toString();
}
Also used : ArrayType(org.apache.flink.table.types.logical.ArrayType) LogicalType(org.apache.flink.table.types.logical.LogicalType)

Example 10 with ArrayType

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

ArrayType (org.apache.flink.table.types.logical.ArrayType)28 LogicalType (org.apache.flink.table.types.logical.LogicalType)18 RowType (org.apache.flink.table.types.logical.RowType)18 DecimalType (org.apache.flink.table.types.logical.DecimalType)11 MapType (org.apache.flink.table.types.logical.MapType)11 TimestampType (org.apache.flink.table.types.logical.TimestampType)10 IntType (org.apache.flink.table.types.logical.IntType)8 ArrayList (java.util.ArrayList)7 GenericRowData (org.apache.flink.table.data.GenericRowData)7 RowData (org.apache.flink.table.data.RowData)7 VarCharType (org.apache.flink.table.types.logical.VarCharType)7 LocalTime (java.time.LocalTime)6 Internal (org.apache.flink.annotation.Internal)6 LocalZonedTimestampType (org.apache.flink.table.types.logical.LocalZonedTimestampType)6 Serializable (java.io.Serializable)5 Array (java.lang.reflect.Array)5 LocalDate (java.time.LocalDate)5 LocalDateTime (java.time.LocalDateTime)5 Map (java.util.Map)5 JsonNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode)5