use of org.apache.inlong.sort.formats.common.DateFormatInfo in project incubator-inlong by apache.
the class TableFormatUtils method deriveFormatInfo.
/**
* Derive the format information for the given type.
*
* @param logicalType The type whose format is derived.
* @return The format information for the given type.
*/
public static FormatInfo deriveFormatInfo(LogicalType logicalType) {
if (logicalType instanceof VarCharType) {
return StringFormatInfo.INSTANCE;
} else if (logicalType instanceof BooleanType) {
return BooleanFormatInfo.INSTANCE;
} else if (logicalType instanceof TinyIntType) {
return ByteFormatInfo.INSTANCE;
} else if (logicalType instanceof SmallIntType) {
return ShortFormatInfo.INSTANCE;
} else if (logicalType instanceof IntType) {
return IntFormatInfo.INSTANCE;
} else if (logicalType instanceof BigIntType) {
return LongFormatInfo.INSTANCE;
} else if (logicalType instanceof FloatType) {
return FloatFormatInfo.INSTANCE;
} else if (logicalType instanceof DoubleType) {
return DoubleFormatInfo.INSTANCE;
} else if (logicalType instanceof DecimalType) {
return DecimalFormatInfo.INSTANCE;
} else if (logicalType instanceof DateType) {
return new DateFormatInfo();
} else if (logicalType instanceof TimeType) {
return new TimeFormatInfo();
} else if (logicalType instanceof TimestampType) {
return new TimestampFormatInfo();
} else if (logicalType instanceof LocalZonedTimestampType) {
return new LocalZonedTimestampFormatInfo();
} else if (logicalType instanceof ArrayType) {
ArrayType arrayType = (ArrayType) logicalType;
LogicalType elementType = arrayType.getElementType();
FormatInfo elementFormatInfo = deriveFormatInfo(elementType);
return new ArrayFormatInfo(elementFormatInfo);
} else if (logicalType instanceof MapType) {
MapType mapType = (MapType) logicalType;
LogicalType keyType = mapType.getKeyType();
LogicalType valueType = mapType.getValueType();
FormatInfo keyFormatInfo = deriveFormatInfo(keyType);
FormatInfo valueFormatInfo = deriveFormatInfo(valueType);
return new MapFormatInfo(keyFormatInfo, valueFormatInfo);
} else if (logicalType instanceof RowType) {
RowType rowType = (RowType) logicalType;
List<RowType.RowField> rowFields = rowType.getFields();
String[] fieldNames = new String[rowFields.size()];
FormatInfo[] fieldFormatInfos = new FormatInfo[rowFields.size()];
for (int i = 0; i < rowFields.size(); ++i) {
RowType.RowField rowField = rowFields.get(i);
fieldNames[i] = rowField.getName();
fieldFormatInfos[i] = deriveFormatInfo(rowField.getType());
}
return new RowFormatInfo(fieldNames, fieldFormatInfos);
} else if (logicalType instanceof BinaryType) {
return BinaryFormatInfo.INSTANCE;
} else if (logicalType instanceof NullType) {
return NullFormatInfo.INSTANCE;
} else {
throw new UnsupportedOperationException();
}
}
use of org.apache.inlong.sort.formats.common.DateFormatInfo in project incubator-inlong by apache.
the class CustomDateFormatDeserializationSchemaWrapperTest method testFromStringToDateAndTime.
@Test
public void testFromStringToDateAndTime() throws IOException, ClassNotFoundException, ParseException {
FieldInfo[] fieldInfos = new FieldInfo[] { new FieldInfo("f1", new DateFormatInfo()), new FieldInfo("f2", new TimeFormatInfo()), new FieldInfo("f3", new TimestampFormatInfo()), new FieldInfo("f4", StringFormatInfo.INSTANCE) };
DeserializationSchema<Row> stringSchema = DeserializationSchemaFactory.build(fieldInfos, null);
CustomDateFormatDeserializationSchemaWrapper schemaWrapper = new CustomDateFormatDeserializationSchemaWrapper(stringSchema, extractFormatInfos(fieldInfos));
Row testRow = Row.of("2022-02-15", "15:52:30", "2022-02-15 15:52:30", "don't convert");
Row resultRow = schemaWrapper.fromStringToDateAndTime(testRow);
assertTrue(resultRow.getField(0) instanceof Date);
assertTrue(resultRow.getField(1) instanceof Time);
assertTrue(resultRow.getField(2) instanceof Timestamp);
final Row expectedRow = Row.of(Date.valueOf("2022-02-15"), Time.valueOf("15:52:30"), Timestamp.valueOf("2022-02-15 15:52:30"), "don't convert");
assertEquals(expectedRow, resultRow);
}
use of org.apache.inlong.sort.formats.common.DateFormatInfo in project incubator-inlong by apache.
the class CsvDeserializationSchemaTest method testNullLiteral.
@Test
public void testNullLiteral() throws Exception {
String nullLiteral = "n/a";
Consumer<CsvDeserializationSchema.Builder> config = builder -> builder.setNullLiteral(nullLiteral);
testBasicDeserialization(config, StringFormatInfo.INSTANCE, null, nullLiteral);
testBasicDeserialization(config, BooleanFormatInfo.INSTANCE, null, nullLiteral);
testBasicDeserialization(config, ByteFormatInfo.INSTANCE, null, nullLiteral);
testBasicDeserialization(config, ShortFormatInfo.INSTANCE, null, nullLiteral);
testBasicDeserialization(config, IntFormatInfo.INSTANCE, null, nullLiteral);
testBasicDeserialization(config, LongFormatInfo.INSTANCE, null, nullLiteral);
testBasicDeserialization(config, FloatFormatInfo.INSTANCE, null, nullLiteral);
testBasicDeserialization(config, DoubleFormatInfo.INSTANCE, null, nullLiteral);
testBasicDeserialization(config, DecimalFormatInfo.INSTANCE, null, nullLiteral);
testBasicDeserialization(config, new DateFormatInfo("dd/MM/yyyy"), null, nullLiteral);
testBasicDeserialization(config, new TimeFormatInfo("ss/mm/hh"), null, nullLiteral);
testBasicDeserialization(config, new TimestampFormatInfo("dd/MM/yyyy hh:mm:ss"), null, nullLiteral);
}
use of org.apache.inlong.sort.formats.common.DateFormatInfo in project incubator-inlong by apache.
the class CsvSerializationSchemaTest method testNormal.
@Test
public void testNormal() {
Consumer<CsvSerializationSchema.Builder> config = builder -> {
};
testBasicSerialization(config, StringFormatInfo.INSTANCE, "hello", "hello");
testBasicSerialization(config, BooleanFormatInfo.INSTANCE, true, "true");
testBasicSerialization(config, ByteFormatInfo.INSTANCE, (byte) 124, "124");
testBasicSerialization(config, ShortFormatInfo.INSTANCE, (short) 10000, "10000");
testBasicSerialization(config, IntFormatInfo.INSTANCE, 1234567, "1234567");
testBasicSerialization(config, LongFormatInfo.INSTANCE, 12345678910L, "12345678910");
testBasicSerialization(config, FloatFormatInfo.INSTANCE, 0.33333334f, "0.33333334");
testBasicSerialization(config, DoubleFormatInfo.INSTANCE, 0.33333333332, "0.33333333332");
testBasicSerialization(config, DecimalFormatInfo.INSTANCE, new BigDecimal("1234.0000000000000000000000001"), "1234.0000000000000000000000001");
testBasicSerialization(config, new DateFormatInfo("dd/MM/yyyy"), Date.valueOf("2020-03-22"), "22/03/2020");
testBasicSerialization(config, new TimeFormatInfo("ss/mm/hh"), Time.valueOf("11:12:13"), "13/12/11");
testBasicSerialization(config, new TimestampFormatInfo("dd/MM/yyyy hh:mm:ss"), Timestamp.valueOf("2020-03-22 11:12:13"), "22/03/2020 11:12:13");
}
use of org.apache.inlong.sort.formats.common.DateFormatInfo in project incubator-inlong by apache.
the class CsvSerializationSchemaTest method testNullLiteral.
@Test
public void testNullLiteral() {
String nullLiteral = "n/a";
Consumer<CsvSerializationSchema.Builder> config = builder -> builder.setNullLiteral(nullLiteral);
testBasicSerialization(config, StringFormatInfo.INSTANCE, null, nullLiteral);
testBasicSerialization(config, BooleanFormatInfo.INSTANCE, null, nullLiteral);
testBasicSerialization(config, ByteFormatInfo.INSTANCE, null, nullLiteral);
testBasicSerialization(config, ShortFormatInfo.INSTANCE, null, nullLiteral);
testBasicSerialization(config, IntFormatInfo.INSTANCE, null, nullLiteral);
testBasicSerialization(config, LongFormatInfo.INSTANCE, null, nullLiteral);
testBasicSerialization(config, FloatFormatInfo.INSTANCE, null, nullLiteral);
testBasicSerialization(config, DoubleFormatInfo.INSTANCE, null, nullLiteral);
testBasicSerialization(config, DecimalFormatInfo.INSTANCE, null, nullLiteral);
testBasicSerialization(config, new DateFormatInfo("dd/MM/yyyy"), null, nullLiteral);
testBasicSerialization(config, new TimeFormatInfo("ss/mm/hh"), null, nullLiteral);
testBasicSerialization(config, new TimestampFormatInfo("dd/MM/yyyy hh:mm:ss"), null, nullLiteral);
}
Aggregations