Search in sources :

Example 6 with DecimalData

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

the class BinaryStringDataUtil method toDecimal.

/**
 * Parses a {@link BinaryStringData} to {@link DecimalData}.
 *
 * @return DecimalData value if the parsing was successful.
 */
public static DecimalData toDecimal(BinaryStringData str, int precision, int scale) throws NumberFormatException {
    str.ensureMaterialized();
    DecimalData data;
    if (DecimalDataUtils.isByteArrayDecimal(precision) || DecimalDataUtils.isByteArrayDecimal(str.getSizeInBytes())) {
        data = toBigPrecisionDecimal(str, precision, scale);
    } else {
        int sizeInBytes = str.getSizeInBytes();
        data = toDecimalFromBytes(precision, scale, getTmpBytes(str, sizeInBytes), 0, sizeInBytes);
    }
    if (data == null) {
        throw numberFormatExceptionFor(str, "Overflow.");
    }
    return data;
}
Also used : DecimalData(org.apache.flink.table.data.DecimalData)

Example 7 with DecimalData

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

the class StreamRecordUtils method binaryrow.

/**
 * Receives a object array, generates a BinaryRowData based on the array.
 *
 * @param fields input object array
 * @return generated BinaryRowData.
 */
public static BinaryRowData binaryrow(Object... fields) {
    BinaryRowData row = new BinaryRowData(fields.length);
    BinaryRowWriter writer = new BinaryRowWriter(row);
    for (int j = 0; j < fields.length; j++) {
        Object value = fields[j];
        if (value == null) {
            writer.setNullAt(j);
        } else if (value instanceof Byte) {
            writer.writeByte(j, (Byte) value);
        } else if (value instanceof Short) {
            writer.writeShort(j, (Short) value);
        } else if (value instanceof Integer) {
            writer.writeInt(j, (Integer) value);
        } else if (value instanceof String) {
            writer.writeString(j, StringData.fromString((String) value));
        } else if (value instanceof Double) {
            writer.writeDouble(j, (Double) value);
        } else if (value instanceof Float) {
            writer.writeFloat(j, (Float) value);
        } else if (value instanceof Long) {
            writer.writeLong(j, (Long) value);
        } else if (value instanceof Boolean) {
            writer.writeBoolean(j, (Boolean) value);
        } else if (value instanceof byte[]) {
            writer.writeBinary(j, (byte[]) value);
        } else if (value instanceof DecimalData) {
            DecimalData decimal = (DecimalData) value;
            writer.writeDecimal(j, decimal, decimal.precision());
        } else if (value instanceof TimestampData) {
            TimestampData timestamp = (TimestampData) value;
            writer.writeTimestamp(j, timestamp, 3);
        } else if (value instanceof Tuple2 && ((Tuple2) value).f0 instanceof TimestampData) {
            TimestampData timestamp = (TimestampData) ((Tuple2) value).f0;
            writer.writeTimestamp(j, timestamp, (int) ((Tuple2) value).f1);
        } else if (value instanceof Tuple2 && ((Tuple2) value).f0 instanceof ArrayData) {
            ArrayData array = (ArrayData) ((Tuple2) value).f0;
            ArrayDataSerializer serializer = (ArrayDataSerializer) ((Tuple2) value).f1;
            writer.writeArray(j, array, serializer);
        } else if (value instanceof Tuple2 && ((Tuple2) value).f0 instanceof RowData) {
            RowData rowData = ((RowData) ((Tuple2) value).f0);
            RowDataSerializer serializer = (RowDataSerializer) ((Tuple2) value).f1;
            writer.writeRow(j, rowData, serializer);
        } else {
            throw new RuntimeException("Not support yet!");
        }
    }
    writer.complete();
    return row;
}
Also used : TimestampData(org.apache.flink.table.data.TimestampData) StringData.fromString(org.apache.flink.table.data.StringData.fromString) ArrayDataSerializer(org.apache.flink.table.runtime.typeutils.ArrayDataSerializer) DecimalData(org.apache.flink.table.data.DecimalData) RowData(org.apache.flink.table.data.RowData) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) GenericRowData(org.apache.flink.table.data.GenericRowData) Tuple2(org.apache.flink.api.java.tuple.Tuple2) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter) ArrayData(org.apache.flink.table.data.ArrayData) RowDataSerializer(org.apache.flink.table.runtime.typeutils.RowDataSerializer)

Example 8 with DecimalData

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

the class SortUtilTest method testNormalizedKey.

@Test
public void testNormalizedKey() {
    int len = 10;
    Random random = new Random();
    MemorySegment[] segments = new MemorySegment[len];
    MemorySegment[] compareSegs = new MemorySegment[len];
    for (int i = 0; i < len; i++) {
        segments[i] = MemorySegmentFactory.allocateUnpooledSegment(20);
        compareSegs[i] = MemorySegmentFactory.allocateUnpooledSegment(20);
    }
    {
        SortUtil.minNormalizedKey(segments[0], 0, 20);
        SortUtil.maxNormalizedKey(segments[1], 0, 20);
        for (int i = 0; i < len; i++) {
            byte[] rndBytes = new byte[20];
            random.nextBytes(rndBytes);
            segments[2].put(0, rndBytes);
            Assert.assertTrue(segments[0].compare(segments[2], 0, 0, 20) <= 0);
            Assert.assertTrue(segments[1].compare(segments[2], 0, 0, 20) >= 0);
        }
    }
    {
        DecimalData[] arr = new DecimalData[len];
        for (int i = 0; i < len; i++) {
            arr[i] = DecimalData.fromBigDecimal(new BigDecimal(random.nextInt()), 18, 0);
            SortUtil.putDecimalNormalizedKey(arr[i], segments[i], 0, 8);
        }
        Arrays.sort(arr, DecimalData::compareTo);
        for (int i = 0; i < len; i++) {
            SortUtil.putDecimalNormalizedKey(arr[i], compareSegs[i], 0, 8);
        }
        Arrays.sort(segments, (o1, o2) -> o1.compare(o2, 0, 0, 8));
        for (int i = 0; i < len; i++) {
            Assert.assertTrue(compareSegs[i].equalTo(segments[i], 0, 0, 8));
        }
    }
    {
        Float[] arr = new Float[len];
        for (int i = 0; i < len; i++) {
            arr[i] = random.nextFloat();
            SortUtil.putFloatNormalizedKey(arr[i], segments[i], 0, 4);
        }
        Arrays.sort(arr, Float::compareTo);
        for (int i = 0; i < len; i++) {
            SortUtil.putFloatNormalizedKey(arr[i], compareSegs[i], 0, 4);
        }
        Arrays.sort(segments, (o1, o2) -> o1.compare(o2, 0, 0, 4));
        for (int i = 0; i < len; i++) {
            Assert.assertTrue(compareSegs[i].equalTo(segments[i], 0, 0, 4));
        }
    }
    {
        Double[] arr = new Double[len];
        for (int i = 0; i < len; i++) {
            arr[i] = random.nextDouble();
            SortUtil.putDoubleNormalizedKey(arr[i], segments[i], 0, 8);
        }
        Arrays.sort(arr, Double::compareTo);
        for (int i = 0; i < len; i++) {
            SortUtil.putDoubleNormalizedKey(arr[i], compareSegs[i], 0, 8);
        }
        Arrays.sort(segments, (o1, o2) -> o1.compare(o2, 0, 0, 8));
        for (int i = 0; i < len; i++) {
            Assert.assertTrue(compareSegs[i].equalTo(segments[i], 0, 0, 8));
        }
    }
    {
        BinaryStringData[] arr = new BinaryStringData[len];
        for (int i = 0; i < len; i++) {
            arr[i] = BinaryStringData.fromString(String.valueOf(random.nextLong()));
            SortUtil.putStringNormalizedKey(arr[i], segments[i], 0, 8);
        }
        Arrays.sort(arr, StringData::compareTo);
        for (int i = 0; i < len; i++) {
            SortUtil.putStringNormalizedKey(arr[i], compareSegs[i], 0, 8);
        }
        Arrays.sort(segments, (o1, o2) -> o1.compare(o2, 0, 0, 8));
        for (int i = 0; i < len; i++) {
            Assert.assertTrue(compareSegs[i].equalTo(segments[i], 0, 0, 8));
        }
    }
}
Also used : MemorySegment(org.apache.flink.core.memory.MemorySegment) BinaryStringData(org.apache.flink.table.data.binary.BinaryStringData) MemorySegmentFactory(org.apache.flink.core.memory.MemorySegmentFactory) StringData(org.apache.flink.table.data.StringData) BigDecimal(java.math.BigDecimal) Arrays(java.util.Arrays) DecimalData(org.apache.flink.table.data.DecimalData) Test(org.junit.Test) Random(java.util.Random) Assert(org.junit.Assert) Random(java.util.Random) MemorySegment(org.apache.flink.core.memory.MemorySegment) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

DecimalData (org.apache.flink.table.data.DecimalData)8 BigDecimal (java.math.BigDecimal)3 Test (org.junit.Test)3 TimestampData (org.apache.flink.table.data.TimestampData)2 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 Arrays (java.util.Arrays)1 Random (java.util.Random)1 Schema (org.apache.avro.Schema)1 Utf8 (org.apache.avro.util.Utf8)1 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1 MemorySegment (org.apache.flink.core.memory.MemorySegment)1 MemorySegmentFactory (org.apache.flink.core.memory.MemorySegmentFactory)1 TableException (org.apache.flink.table.api.TableException)1 ArrayData (org.apache.flink.table.data.ArrayData)1 GenericRowData (org.apache.flink.table.data.GenericRowData)1 RowData (org.apache.flink.table.data.RowData)1 StringData (org.apache.flink.table.data.StringData)1 StringData.fromString (org.apache.flink.table.data.StringData.fromString)1 BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)1