use of org.apache.flink.table.data.writer.BinaryRowWriter in project flink by apache.
the class SortCodeGeneratorTest method row.
private BinaryRowData row(int i, Object[][] values) {
BinaryRowData row = new BinaryRowData(inputType.getFieldCount());
BinaryRowWriter writer = new BinaryRowWriter(row);
for (int j = 0; j < inputType.getFieldCount(); j++) {
Object value = values[j][i];
if (value == null) {
writer.setNullAt(j);
} else {
BinaryWriter.write(writer, j, value, inputType.getTypeAt(j), InternalSerializers.create(inputType.getTypeAt(j)));
}
}
writer.complete();
return row;
}
use of org.apache.flink.table.data.writer.BinaryRowWriter in project flink by apache.
the class BinaryStringDataTest method testToNumeric.
@Test
public void testToNumeric() {
// Test to integer.
assertEquals(Byte.parseByte("123"), toByte(fromString("123")));
assertEquals(Byte.parseByte("123"), toByte(fromString("+123")));
assertEquals(Byte.parseByte("-123"), toByte(fromString("-123")));
assertEquals(Short.parseShort("123"), toShort(fromString("123")));
assertEquals(Short.parseShort("123"), toShort(fromString("+123")));
assertEquals(Short.parseShort("-123"), toShort(fromString("-123")));
assertEquals(Integer.parseInt("123"), toInt(fromString("123")));
assertEquals(Integer.parseInt("123"), toInt(fromString("+123")));
assertEquals(Integer.parseInt("-123"), toInt(fromString("-123")));
assertEquals(Long.parseLong("1234567890"), toLong(fromString("1234567890")));
assertEquals(Long.parseLong("+1234567890"), toLong(fromString("+1234567890")));
assertEquals(Long.parseLong("-1234567890"), toLong(fromString("-1234567890")));
// Test decimal string to integer.
assertEquals(Integer.parseInt("123"), toInt(fromString("123.456789")));
assertEquals(Long.parseLong("123"), toLong(fromString("123.456789")));
// Test negative cases.
assertThrows(NumberFormatException.class, () -> toInt(fromString("1a3.456789")));
assertThrows(NumberFormatException.class, () -> toInt(fromString("123.a56789")));
// Test composite in BinaryRowData.
BinaryRowData row = new BinaryRowData(20);
BinaryRowWriter writer = new BinaryRowWriter(row);
writer.writeString(0, BinaryStringData.fromString("1"));
writer.writeString(1, BinaryStringData.fromString("123"));
writer.writeString(2, BinaryStringData.fromString("12345"));
writer.writeString(3, BinaryStringData.fromString("123456789"));
writer.complete();
assertEquals(Byte.parseByte("1"), toByte(((BinaryStringData) row.getString(0))));
assertEquals(Short.parseShort("123"), toShort(((BinaryStringData) row.getString(1))));
assertEquals(Integer.parseInt("12345"), toInt(((BinaryStringData) row.getString(2))));
assertEquals(Long.parseLong("123456789"), toLong(((BinaryStringData) row.getString(3))));
}
use of org.apache.flink.table.data.writer.BinaryRowWriter in project flink by apache.
the class BinaryStringDataTest method testToUpperLowerCase.
@Test
public void testToUpperLowerCase() {
assertEquals(fromString("我是中国人"), fromString("我是中国人").toLowerCase());
assertEquals(fromString("我是中国人"), fromString("我是中国人").toUpperCase());
assertEquals(fromString("abcdefg"), fromString("aBcDeFg").toLowerCase());
assertEquals(fromString("ABCDEFG"), fromString("aBcDeFg").toUpperCase());
assertEquals(fromString("!@#$%^*"), fromString("!@#$%^*").toLowerCase());
assertEquals(fromString("!@#$%^*"), fromString("!@#$%^*").toLowerCase());
// Test composite in BinaryRowData.
BinaryRowData row = new BinaryRowData(20);
BinaryRowWriter writer = new BinaryRowWriter(row);
writer.writeString(0, BinaryStringData.fromString("a"));
writer.writeString(1, BinaryStringData.fromString("我是中国人"));
writer.writeString(3, BinaryStringData.fromString("aBcDeFg"));
writer.writeString(5, BinaryStringData.fromString("!@#$%^*"));
writer.complete();
assertEquals(fromString("A"), ((BinaryStringData) row.getString(0)).toUpperCase());
assertEquals(fromString("我是中国人"), ((BinaryStringData) row.getString(1)).toUpperCase());
assertEquals(fromString("我是中国人"), ((BinaryStringData) row.getString(1)).toLowerCase());
assertEquals(fromString("ABCDEFG"), ((BinaryStringData) row.getString(3)).toUpperCase());
assertEquals(fromString("abcdefg"), ((BinaryStringData) row.getString(3)).toLowerCase());
assertEquals(fromString("!@#$%^*"), ((BinaryStringData) row.getString(5)).toUpperCase());
assertEquals(fromString("!@#$%^*"), ((BinaryStringData) row.getString(5)).toLowerCase());
}
use of org.apache.flink.table.data.writer.BinaryRowWriter 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.writer.BinaryRowWriter in project flink by apache.
the class BinaryArrayDataTest method testArray.
@Test
public void testArray() {
// 1.array test
BinaryArrayData array = new BinaryArrayData();
BinaryArrayWriter writer = new BinaryArrayWriter(array, 3, 4);
writer.writeInt(0, 6);
writer.setNullInt(1);
writer.writeInt(2, 666);
writer.complete();
assertEquals(array.getInt(0), 6);
assertTrue(array.isNullAt(1));
assertEquals(array.getInt(2), 666);
// 2.test write to binary row.
{
BinaryRowData row2 = new BinaryRowData(1);
BinaryRowWriter writer2 = new BinaryRowWriter(row2);
writer2.writeArray(0, array, new ArrayDataSerializer(DataTypes.INT().getLogicalType()));
writer2.complete();
BinaryArrayData array2 = (BinaryArrayData) row2.getArray(0);
assertEquals(array2, array);
assertEquals(array2.getInt(0), 6);
assertTrue(array2.isNullAt(1));
assertEquals(array2.getInt(2), 666);
}
// 3.test write var seg array to binary row.
{
BinaryArrayData array3 = splitArray(array);
BinaryRowData row2 = new BinaryRowData(1);
BinaryRowWriter writer2 = new BinaryRowWriter(row2);
writer2.writeArray(0, array3, new ArrayDataSerializer(DataTypes.INT().getLogicalType()));
writer2.complete();
BinaryArrayData array2 = (BinaryArrayData) row2.getArray(0);
assertEquals(array2, array);
assertEquals(array2.getInt(0), 6);
assertTrue(array2.isNullAt(1));
assertEquals(array2.getInt(2), 666);
}
}
Aggregations