Search in sources :

Example 6 with GenericArrayData

use of org.apache.flink.table.data.GenericArrayData in project flink by apache.

the class JsonToRowDataConverters method createArrayConverter.

private JsonToRowDataConverter createArrayConverter(ArrayType arrayType) {
    JsonToRowDataConverter elementConverter = createConverter(arrayType.getElementType());
    final Class<?> elementClass = LogicalTypeUtils.toInternalConversionClass(arrayType.getElementType());
    return jsonNode -> {
        final ArrayNode node = (ArrayNode) jsonNode;
        final Object[] array = (Object[]) Array.newInstance(elementClass, node.size());
        for (int i = 0; i < node.size(); i++) {
            final JsonNode innerNode = node.get(i);
            array[i] = elementConverter.convert(innerNode);
        }
        return new GenericArrayData(array);
    };
}
Also used : ISO8601_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT(org.apache.flink.formats.common.TimeFormats.ISO8601_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT) Array(java.lang.reflect.Array) GenericArrayData(org.apache.flink.table.data.GenericArrayData) IntType(org.apache.flink.table.types.logical.IntType) LocalDateTime(java.time.LocalDateTime) HashMap(java.util.HashMap) 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) TemporalQueries(java.time.temporal.TemporalQueries) ArrayNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode) TemporalAccessor(java.time.temporal.TemporalAccessor) BigDecimal(java.math.BigDecimal) SQL_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT(org.apache.flink.formats.common.TimeFormats.SQL_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT) TextNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.TextNode) GenericRowData(org.apache.flink.table.data.GenericRowData) DecimalType(org.apache.flink.table.types.logical.DecimalType) LogicalTypeFamily(org.apache.flink.table.types.logical.LogicalTypeFamily) GenericMapData(org.apache.flink.table.data.GenericMapData) Map(java.util.Map) LocalTime(java.time.LocalTime) ZoneOffset(java.time.ZoneOffset) ISO_LOCAL_DATE(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE) LogicalTypeUtils(org.apache.flink.table.types.logical.utils.LogicalTypeUtils) 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) Iterator(java.util.Iterator) TimestampData(org.apache.flink.table.data.TimestampData) 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) IOException(java.io.IOException) 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) StringData(org.apache.flink.table.data.StringData) LogicalType(org.apache.flink.table.types.logical.LogicalType) LocalDate(java.time.LocalDate) Internal(org.apache.flink.annotation.Internal) GenericArrayData(org.apache.flink.table.data.GenericArrayData) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) ArrayNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode)

Example 7 with GenericArrayData

use of org.apache.flink.table.data.GenericArrayData in project flink by apache.

the class OrcBulkRowDataWriterTest method initInput.

@Before
public void initInput() {
    input = new ArrayList<>();
    fieldTypes = new LogicalType[4];
    fieldTypes[0] = new VarCharType();
    fieldTypes[1] = new IntType();
    List<RowType.RowField> arrayRowFieldList = Collections.singletonList(new RowType.RowField("_col2_col0", new VarCharType()));
    fieldTypes[2] = new ArrayType(new RowType(arrayRowFieldList));
    List<RowType.RowField> mapRowFieldList = Arrays.asList(new RowType.RowField("_col3_col0", new VarCharType()), new RowType.RowField("_col3_col1", new TimestampType()));
    fieldTypes[3] = new MapType(new VarCharType(), new RowType(mapRowFieldList));
    {
        GenericRowData rowData = new GenericRowData(4);
        rowData.setField(0, new BinaryStringData("_col_0_string_1"));
        rowData.setField(1, 1);
        GenericRowData arrayValue1 = new GenericRowData(1);
        arrayValue1.setField(0, new BinaryStringData("_col_2_row_0_string_1"));
        GenericRowData arrayValue2 = new GenericRowData(1);
        arrayValue2.setField(0, new BinaryStringData("_col_2_row_1_string_1"));
        GenericArrayData arrayData = new GenericArrayData(new Object[] { arrayValue1, arrayValue2 });
        rowData.setField(2, arrayData);
        GenericRowData mapValue1 = new GenericRowData(2);
        mapValue1.setField(0, new BinaryStringData(("_col_3_map_value_string_1")));
        mapValue1.setField(1, TimestampData.fromTimestamp(new Timestamp(3600000)));
        Map<StringData, RowData> mapDataMap = new HashMap<>();
        mapDataMap.put(new BinaryStringData("_col_3_map_key_1"), mapValue1);
        GenericMapData mapData = new GenericMapData(mapDataMap);
        rowData.setField(3, mapData);
        input.add(rowData);
    }
    {
        GenericRowData rowData = new GenericRowData(4);
        rowData.setField(0, new BinaryStringData("_col_0_string_2"));
        rowData.setField(1, 2);
        GenericRowData arrayValue1 = new GenericRowData(1);
        arrayValue1.setField(0, new BinaryStringData("_col_2_row_0_string_2"));
        GenericRowData arrayValue2 = new GenericRowData(1);
        arrayValue2.setField(0, new BinaryStringData("_col_2_row_1_string_2"));
        GenericArrayData arrayData = new GenericArrayData(new Object[] { arrayValue1, arrayValue2 });
        rowData.setField(2, arrayData);
        GenericRowData mapValue1 = new GenericRowData(2);
        mapValue1.setField(0, new BinaryStringData(("_col_3_map_value_string_2")));
        mapValue1.setField(1, TimestampData.fromTimestamp(new Timestamp(3600000)));
        Map<StringData, RowData> mapDataMap = new HashMap<>();
        mapDataMap.put(new BinaryStringData("_col_3_map_key_2"), mapValue1);
        GenericMapData mapData = new GenericMapData(mapDataMap);
        rowData.setField(3, mapData);
        input.add(rowData);
    }
}
Also used : GenericMapData(org.apache.flink.table.data.GenericMapData) GenericArrayData(org.apache.flink.table.data.GenericArrayData) RowType(org.apache.flink.table.types.logical.RowType) Timestamp(java.sql.Timestamp) MapType(org.apache.flink.table.types.logical.MapType) IntType(org.apache.flink.table.types.logical.IntType) ArrayType(org.apache.flink.table.types.logical.ArrayType) TimestampType(org.apache.flink.table.types.logical.TimestampType) GenericRowData(org.apache.flink.table.data.GenericRowData) VarCharType(org.apache.flink.table.types.logical.VarCharType) BinaryStringData(org.apache.flink.table.data.binary.BinaryStringData) Map(java.util.Map) HashMap(java.util.HashMap) Before(org.junit.Before)

Example 8 with GenericArrayData

use of org.apache.flink.table.data.GenericArrayData in project flink by apache.

the class CsvToRowDataConverters method createArrayConverter.

private CsvToRowDataConverter createArrayConverter(ArrayType arrayType) {
    final CsvToRowDataConverter elementConverter = createNullableConverter(arrayType.getElementType());
    final Class<?> elementClass = LogicalTypeUtils.toInternalConversionClass(arrayType.getElementType());
    return jsonNode -> {
        final ArrayNode node = (ArrayNode) jsonNode;
        final Object[] array = (Object[]) Array.newInstance(elementClass, node.size());
        for (int i = 0; i < node.size(); i++) {
            final JsonNode innerNode = node.get(i);
            array[i] = elementConverter.convert(innerNode);
        }
        return new GenericArrayData(array);
    };
}
Also used : Array(java.lang.reflect.Array) GenericArrayData(org.apache.flink.table.data.GenericArrayData) LocalDateTime(java.time.LocalDateTime) SQL_TIMESTAMP_FORMAT(org.apache.flink.formats.common.TimeFormats.SQL_TIMESTAMP_FORMAT) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) 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) GenericRowData(org.apache.flink.table.data.GenericRowData) DecimalType(org.apache.flink.table.types.logical.DecimalType) LocalTime(java.time.LocalTime) TimeType(org.apache.flink.table.types.logical.TimeType) LogicalTypeUtils(org.apache.flink.table.types.logical.utils.LogicalTypeUtils) RowData(org.apache.flink.table.data.RowData) TimestampData(org.apache.flink.table.data.TimestampData) DecimalData(org.apache.flink.table.data.DecimalData) IOException(java.io.IOException) ArrayType(org.apache.flink.table.types.logical.ArrayType) Serializable(java.io.Serializable) Date(java.sql.Date) Converter(org.apache.flink.formats.common.Converter) StringData(org.apache.flink.table.data.StringData) LogicalType(org.apache.flink.table.types.logical.LogicalType) DateTimeFormatter(java.time.format.DateTimeFormatter) Internal(org.apache.flink.annotation.Internal) GenericArrayData(org.apache.flink.table.data.GenericArrayData) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) ArrayNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode)

Example 9 with GenericArrayData

use of org.apache.flink.table.data.GenericArrayData in project flink by apache.

the class ArrayDataAssert method asGeneric.

public ArrayDataAssert asGeneric(LogicalType logicalType) {
    GenericArrayData actual = InternalDataUtils.toGenericArray(this.actual, logicalType);
    return new ArrayDataAssert(actual).usingComparator((x, y) -> {
        // Avoid converting actual again
        x = x == actual ? x : InternalDataUtils.toGenericArray(x, logicalType);
        y = y == actual ? y : InternalDataUtils.toGenericArray(y, logicalType);
        if (Objects.equals(x, y)) {
            return 0;
        }
        return Objects.hashCode(x) < Objects.hashCode(y) ? -1 : 1;
    });
}
Also used : GenericArrayData(org.apache.flink.table.data.GenericArrayData)

Example 10 with GenericArrayData

use of org.apache.flink.table.data.GenericArrayData in project flink by apache.

the class InternalDataUtils method toGenericArray.

static GenericArrayData toGenericArray(ArrayData arrayData, LogicalType logicalType) {
    final LogicalType innerElement = ((ArrayType) logicalType).getElementType();
    final ArrayData.ElementGetter elementGetter = ArrayData.createElementGetter(innerElement);
    final Object[] newArray = new Object[arrayData.size()];
    for (int i = 0; i < arrayData.size(); i++) {
        if (arrayData.isNullAt(i)) {
            newArray[i] = null;
        } else {
            newArray[i] = toGenericInternalData(elementGetter.getElementOrNull(arrayData, i), innerElement);
        }
    }
    return new GenericArrayData(newArray);
}
Also used : ArrayType(org.apache.flink.table.types.logical.ArrayType) GenericArrayData(org.apache.flink.table.data.GenericArrayData) LogicalType(org.apache.flink.table.types.logical.LogicalType) GenericArrayData(org.apache.flink.table.data.GenericArrayData) ArrayData(org.apache.flink.table.data.ArrayData)

Aggregations

GenericArrayData (org.apache.flink.table.data.GenericArrayData)11 GenericRowData (org.apache.flink.table.data.GenericRowData)7 StringData (org.apache.flink.table.data.StringData)6 ArrayType (org.apache.flink.table.types.logical.ArrayType)6 RowType (org.apache.flink.table.types.logical.RowType)6 LogicalType (org.apache.flink.table.types.logical.LogicalType)5 Array (java.lang.reflect.Array)4 RowData (org.apache.flink.table.data.RowData)4 LogicalTypeUtils (org.apache.flink.table.types.logical.utils.LogicalTypeUtils)4 Serializable (java.io.Serializable)3 LocalTime (java.time.LocalTime)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Internal (org.apache.flink.annotation.Internal)3 DecimalData (org.apache.flink.table.data.DecimalData)3 GenericMapData (org.apache.flink.table.data.GenericMapData)3 TimestampData (org.apache.flink.table.data.TimestampData)3 DecimalType (org.apache.flink.table.types.logical.DecimalType)3 IOException (java.io.IOException)2 BigDecimal (java.math.BigDecimal)2