@Nullable private LogicalType determineTimestampSpecLogicalType(Schema schema, String timestampSpecField) { for (Schema.Field field : schema.getFields()) { if (field.name().equals(timestampSpecField)) { return field.schema().getLogicalType(); } } return null; }
private int convertFromDate(Schema schema, Date date) { final LogicalType logicalType = schema.getLogicalType(); if (logicalType == LogicalTypes.date()) { // adopted from Apache Calcite final long time = date.getTime(); final long converted = time + (long) LOCAL_TZ.getOffset(time); return (int) (converted / 86400000L); } else { throw new RuntimeException("Unsupported date type."); } }
private int convertFromTime(Schema schema, Time date) { final LogicalType logicalType = schema.getLogicalType(); if (logicalType == LogicalTypes.timeMillis()) { // adopted from Apache Calcite final long time = date.getTime(); final long converted = time + (long) LOCAL_TZ.getOffset(time); return (int) (converted % 86400000L); } else { throw new RuntimeException("Unsupported time type."); } }
public boolean hasLogicalTypeField(Schema schema) { for (Schema.Field field : schema.getFields()) { if (field.schema().getLogicalType() != null) { return true; } } return false; }
private long convertFromTimestamp(Schema schema, Timestamp date) { final LogicalType logicalType = schema.getLogicalType(); if (logicalType == LogicalTypes.timestampMillis()) { // adopted from Apache Calcite final long time = date.getTime(); return time + (long) LOCAL_TZ.getOffset(time); } else { throw new RuntimeException("Unsupported timestamp type."); } }
private BigDecimal convertToDecimal(Schema schema, byte[] bytes) { final LogicalTypes.Decimal decimalType = (LogicalTypes.Decimal) schema.getLogicalType(); return new BigDecimal(new BigInteger(bytes), decimalType.getScale()); }
private byte[] convertFromDecimal(Schema schema, BigDecimal decimal) { final LogicalType logicalType = schema.getLogicalType(); if (logicalType instanceof LogicalTypes.Decimal) { final LogicalTypes.Decimal decimalType = (LogicalTypes.Decimal) logicalType; // rescale to target type final BigDecimal rescaled = decimal.setScale(decimalType.getScale(), BigDecimal.ROUND_UNNECESSARY); // byte array must contain the two's-complement representation of the // unscaled integer value in big-endian byte order return decimal.unscaledValue().toByteArray(); } else { throw new RuntimeException("Unsupported decimal type."); } }
public String conversionInstance(Schema schema) { if (schema == null || schema.getLogicalType() == null) { return "null"; } if (LogicalTypes.Decimal.class.equals(schema.getLogicalType().getClass()) && !enableDecimalLogicalType) { return "null"; } final Conversion<Object> conversion = specificData.getConversionFor(schema.getLogicalType()); if (conversion != null) { return "new " + conversion.getClass().getCanonicalName() + "()"; } return "null"; }
public static void copyLogicalTypes(final Schema from, final Schema to) { LogicalType logicalType = from.getLogicalType(); if (logicalType != null) { logicalType.addToSchema(to); } }
private String getConvertedLogicalType(Schema schema) { if (enableDecimalLogicalType || !(schema.getLogicalType() instanceof LogicalTypes.Decimal)) { Conversion<?> conversion = specificData .getConversionFor(schema.getLogicalType()); if (conversion != null) { return conversion.getConvertedType().getName(); } } return null; }
@Override public Object newRecord(Object old, Schema schema) { // SpecificData will try to instantiate the type returned by getClass, but // that is the converted class and can't be constructed. LogicalType logicalType = schema.getLogicalType(); if (logicalType != null) { Conversion<?> conversion = getConversionFor(schema.getLogicalType()); if (conversion != null) { return new GenericData.Record(schema); } } return super.newRecord(old, schema); } }
@Override public Object newRecord(Object old, Schema schema) { // SpecificData will try to instantiate the type returned by getClass, but // that is the converted class and can't be constructed. LogicalType logicalType = schema.getLogicalType(); if (logicalType != null) { Conversion<?> conversion = getConversionFor(schema.getLogicalType()); if (conversion != null) { return new GenericData.Record(schema); } } return super.newRecord(old, schema); } }
@Override public Object createFixed(Object old, Schema schema) { // SpecificData will try to instantiate the type returned by getClass, but // that is the converted class and can't be constructed. LogicalType logicalType = schema.getLogicalType(); if (logicalType != null) { Conversion<?> conversion = getConversionFor(schema.getLogicalType()); if (conversion != null) { return new GenericData.Fixed(schema); } } return super.createFixed(old, schema); }
@Override public Object createFixed(Object old, Schema schema) { // SpecificData will try to instantiate the type returned by getClass, but // that is the converted class and can't be constructed. LogicalType logicalType = schema.getLogicalType(); if (logicalType != null) { Conversion<?> conversion = getConversionFor(schema.getLogicalType()); if (conversion != null) { return new GenericData.Fixed(schema); } } return super.createFixed(old, schema); }
/** Called to write data.*/ protected void write(Schema schema, Object datum, Encoder out) throws IOException { LogicalType logicalType = schema.getLogicalType(); if (datum != null && logicalType != null) { Conversion<?> conversion = getData() .getConversionByClass(datum.getClass(), logicalType); writeWithoutConversion(schema, convert(schema, logicalType, conversion, datum), out); } else { writeWithoutConversion(schema, datum, out); } }
/** Called to write data.*/ protected void write(Schema schema, Object datum, Encoder out) throws IOException { LogicalType logicalType = schema.getLogicalType(); if (datum != null && logicalType != null) { Conversion<?> conversion = getData() .getConversionByClass(datum.getClass(), logicalType); writeWithoutConversion(schema, convert(schema, logicalType, conversion, datum), out); } else { writeWithoutConversion(schema, datum, out); } }
/** Called to read data.*/ protected Object read(Object old, Schema expected, ResolvingDecoder in) throws IOException { Object datum = readWithoutConversion(old, expected, in); LogicalType logicalType = expected.getLogicalType(); if (logicalType != null) { Conversion<?> conversion = getData().getConversionFor(logicalType); if (conversion != null) { return convert(datum, expected, logicalType, conversion); } } return datum; }
/** Called to read data.*/ protected Object read(Object old, Schema expected, ResolvingDecoder in) throws IOException { Object datum = readWithoutConversion(old, expected, in); LogicalType logicalType = expected.getLogicalType(); if (logicalType != null) { Conversion<?> conversion = getData().getConversionFor(logicalType); if (conversion != null) { return convert(datum, expected, logicalType, conversion); } } return datum; }
@Override protected void writeField(Object datum, Schema.Field f, Encoder out, Object state) throws IOException { if (datum instanceof SpecificRecordBase) { Conversion<?> conversion = ((SpecificRecordBase) datum).getConversion(f.pos()); Schema fieldSchema = f.schema(); LogicalType logicalType = fieldSchema.getLogicalType(); Object value = getData().getField(datum, f.name(), f.pos()); if (conversion != null && logicalType != null) { value = convert(fieldSchema, logicalType, conversion, value); } writeWithoutConversion(fieldSchema, value, out); } else { super.writeField(datum, f, out, state); } } }
private long getId(IndexedRecord referenceable, Schema schema) { Referenceable info = (Referenceable) schema.getLogicalType(); int idField = schema.getField(info.getIdFieldName()).pos(); return (Long) referenceable.get(idField); } }