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));
}
}
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));
}
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);
}
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);
}
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;
}
Aggregations