Search in sources :

Example 6 with BinaryStringData

use of org.apache.flink.table.data.binary.BinaryStringData in project flink by apache.

the class BinaryStringDataTest method testToDecimal.

@Test
public void testToDecimal() {
    class DecimalTestData {

        private String str;

        private int precision, scale;

        private DecimalTestData(String str, int precision, int scale) {
            this.str = str;
            this.precision = precision;
            this.scale = scale;
        }
    }
    DecimalTestData[] data = { new DecimalTestData("12.345", 5, 3), new DecimalTestData("-12.345", 5, 3), new DecimalTestData("+12345", 5, 0), new DecimalTestData("-12345", 5, 0), new DecimalTestData("12345.", 5, 0), new DecimalTestData("-12345.", 5, 0), new DecimalTestData(".12345", 5, 5), new DecimalTestData("-.12345", 5, 5), new DecimalTestData("+12.345E3", 5, 0), new DecimalTestData("-12.345e3", 5, 0), new DecimalTestData("12.345e-3", 6, 6), new DecimalTestData("-12.345E-3", 6, 6), new DecimalTestData("12345E3", 8, 0), new DecimalTestData("-12345e3", 8, 0), new DecimalTestData("12345e-3", 5, 3), new DecimalTestData("-12345E-3", 5, 3), new DecimalTestData("+.12345E3", 5, 2), new DecimalTestData("-.12345e3", 5, 2), new DecimalTestData(".12345e-3", 8, 8), new DecimalTestData("-.12345E-3", 8, 8), new DecimalTestData("1234512345.1234", 18, 8), new DecimalTestData("-1234512345.1234", 18, 8), new DecimalTestData("1234512345.1234", 12, 2), new DecimalTestData("-1234512345.1234", 12, 2), new DecimalTestData("1234512345.1299", 12, 2), new DecimalTestData("-1234512345.1299", 12, 2), new DecimalTestData("999999999999999999", 18, 0), new DecimalTestData("1234512345.1234512345", 20, 10), new DecimalTestData("-1234512345.1234512345", 20, 10), new DecimalTestData("1234512345.1234512345", 15, 5), new DecimalTestData("-1234512345.1234512345", 15, 5), new DecimalTestData("12345123451234512345E-10", 20, 10), new DecimalTestData("-12345123451234512345E-10", 20, 10), new DecimalTestData("12345123451234512345E-10", 15, 5), new DecimalTestData("-12345123451234512345E-10", 15, 5), new DecimalTestData("999999999999999999999", 21, 0), new DecimalTestData("-999999999999999999999", 21, 0), new DecimalTestData("0.00000000000000000000123456789123456789", 38, 38), new DecimalTestData("-0.00000000000000000000123456789123456789", 38, 38), new DecimalTestData("0.00000000000000000000123456789123456789", 29, 29), new DecimalTestData("-0.00000000000000000000123456789123456789", 29, 29), new DecimalTestData("123456789123E-27", 18, 18), new DecimalTestData("-123456789123E-27", 18, 18), new DecimalTestData("123456789999E-27", 18, 18), new DecimalTestData("-123456789999E-27", 18, 18), new DecimalTestData("123456789123456789E-36", 18, 18), new DecimalTestData("-123456789123456789E-36", 18, 18), new DecimalTestData("123456789999999999E-36", 18, 18), new DecimalTestData("-123456789999999999E-36", 18, 18) };
    for (DecimalTestData d : data) {
        assertEquals(DecimalData.fromBigDecimal(new BigDecimal(d.str), d.precision, d.scale), toDecimal(fromString(d.str), d.precision, d.scale));
    }
    BinaryRowData row = new BinaryRowData(data.length);
    BinaryRowWriter writer = new BinaryRowWriter(row);
    for (int i = 0; i < data.length; i++) {
        writer.writeString(i, BinaryStringData.fromString(data[i].str));
    }
    writer.complete();
    for (int i = 0; i < data.length; i++) {
        DecimalTestData d = data[i];
        assertEquals(DecimalData.fromBigDecimal(new BigDecimal(d.str), d.precision, d.scale), toDecimal((BinaryStringData) row.getString(i), d.precision, d.scale));
    }
}
Also used : BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter) BinaryStringData.blankString(org.apache.flink.table.data.binary.BinaryStringData.blankString) BinaryStringData(org.apache.flink.table.data.binary.BinaryStringData) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 7 with BinaryStringData

use of org.apache.flink.table.data.binary.BinaryStringData in project flink by apache.

the class BinaryStringDataTest method checkBasic.

private void checkBasic(String str, int len) {
    BinaryStringData s1 = fromString(str);
    BinaryStringData s2 = fromBytes(str.getBytes(StandardCharsets.UTF_8));
    assertEquals(s1.numChars(), len);
    assertEquals(s2.numChars(), len);
    assertEquals(s1.toString(), str);
    assertEquals(s2.toString(), str);
    assertEquals(s1, s2);
    assertEquals(s1.hashCode(), s2.hashCode());
    assertEquals(0, s1.compareTo(s2));
    assertTrue(s1.contains(s2));
    assertTrue(s2.contains(s1));
    assertTrue(s1.startsWith(s1));
    assertTrue(s1.endsWith(s1));
}
Also used : BinaryStringData(org.apache.flink.table.data.binary.BinaryStringData)

Example 8 with BinaryStringData

use of org.apache.flink.table.data.binary.BinaryStringData in project flink by apache.

the class BinaryStringDataTest method testLazy.

@Test
public void testLazy() {
    String javaStr = "haha";
    BinaryStringData str = BinaryStringData.fromString(javaStr);
    str.ensureMaterialized();
    // check reference same.
    assertSame(str.toString(), javaStr);
}
Also used : BinaryStringData.blankString(org.apache.flink.table.data.binary.BinaryStringData.blankString) BinaryStringData(org.apache.flink.table.data.binary.BinaryStringData) Test(org.junit.Test)

Example 9 with BinaryStringData

use of org.apache.flink.table.data.binary.BinaryStringData in project flink by apache.

the class StringDataSerializer method serialize.

@Override
public void serialize(StringData record, DataOutputView target) throws IOException {
    // BinaryStringData is the only implementation of StringData
    BinaryStringData string = (BinaryStringData) record;
    string.ensureMaterialized();
    target.writeInt(string.getSizeInBytes());
    BinarySegmentUtils.copyToView(string.getSegments(), string.getOffset(), string.getSizeInBytes(), target);
}
Also used : BinaryStringData(org.apache.flink.table.data.binary.BinaryStringData)

Example 10 with BinaryStringData

use of org.apache.flink.table.data.binary.BinaryStringData in project flink by apache.

the class RegistryAvroRowDataSeDeSchemaTest method address2RowData.

private static RowData address2RowData(Address address) {
    GenericRowData rowData = new GenericRowData(5);
    rowData.setField(0, address.getNum());
    rowData.setField(1, new BinaryStringData(address.getStreet().toString()));
    rowData.setField(2, new BinaryStringData(address.getCity().toString()));
    rowData.setField(3, new BinaryStringData(address.getState().toString()));
    rowData.setField(4, new BinaryStringData(address.getZip().toString()));
    return rowData;
}
Also used : GenericRowData(org.apache.flink.table.data.GenericRowData) BinaryStringData(org.apache.flink.table.data.binary.BinaryStringData)

Aggregations

BinaryStringData (org.apache.flink.table.data.binary.BinaryStringData)15 Test (org.junit.Test)7 BigDecimal (java.math.BigDecimal)2 Random (java.util.Random)2 MemorySegment (org.apache.flink.core.memory.MemorySegment)2 GenericRowData (org.apache.flink.table.data.GenericRowData)2 BinaryStringData.blankString (org.apache.flink.table.data.binary.BinaryStringData.blankString)2 Timestamp (java.sql.Timestamp)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)1 KryoSerializer (org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer)1 MemorySegmentFactory (org.apache.flink.core.memory.MemorySegmentFactory)1 RandomAccessInputView (org.apache.flink.runtime.io.disk.RandomAccessInputView)1 RandomAccessOutputView (org.apache.flink.runtime.io.disk.RandomAccessOutputView)1 DecimalData (org.apache.flink.table.data.DecimalData)1 GenericArrayData (org.apache.flink.table.data.GenericArrayData)1 GenericMapData (org.apache.flink.table.data.GenericMapData)1 StringData (org.apache.flink.table.data.StringData)1