Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/src/test/java/org/apache/iceberg/util/RandomUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public static Object generateDictionaryEncodablePrimitive(
case LONG:
case TIME:
case TIMESTAMP:
case TIMESTAMP_NANO:
return (long) value;
case STRING:
return String.valueOf(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@

import org.apache.flink.table.data.RowData;
import org.apache.flink.table.types.logical.LogicalType;
import org.apache.flink.table.types.logical.NullType;

public class FlinkRowData {

private FlinkRowData() {}

public static RowData.FieldGetter createFieldGetter(LogicalType fieldType, int fieldPos) {
if (fieldType instanceof NullType) {
return rowData -> null;
}

RowData.FieldGetter flinkFieldGetter = RowData.createFieldGetter(fieldType, fieldPos);
return rowData -> {
// Be sure to check for null values, even if the field is required. Flink
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.flink.table.types.logical.LocalZonedTimestampType;
import org.apache.flink.table.types.logical.LogicalType;
import org.apache.flink.table.types.logical.MapType;
import org.apache.flink.table.types.logical.NullType;
import org.apache.flink.table.types.logical.RowType;
import org.apache.flink.table.types.logical.TimeType;
import org.apache.flink.table.types.logical.TimestampType;
Expand Down Expand Up @@ -85,6 +86,8 @@ public LogicalType map(Types.MapType map, LogicalType keyResult, LogicalType val
@Override
public LogicalType primitive(Type.PrimitiveType primitive) {
switch (primitive.typeId()) {
case UNKNOWN:
return new NullType();
case BOOLEAN:
return new BooleanType();
case INTEGER:
Expand Down Expand Up @@ -113,6 +116,15 @@ public LogicalType primitive(Type.PrimitiveType primitive) {
// MICROS
return new TimestampType(6);
}
case TIMESTAMP_NANO:
Types.TimestampNanoType timestamp9 = (Types.TimestampNanoType) primitive;
if (timestamp9.shouldAdjustToUTC()) {
// NANOS
return new LocalZonedTimestampType(9);
} else {
// NANOS
return new TimestampType(9);
}
case STRING:
return new VarCharType(VarCharType.MAX_LENGTH);
case UUID:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ public ValueWriter<?> primitive(LogicalType type, Schema primitive) {
case "timestamp-micros":
return FlinkValueWriters.timestampMicros();

case "timestamp-nanos":
return FlinkValueWriters.timestampNanos();

case "decimal":
LogicalTypes.Decimal decimal = (LogicalTypes.Decimal) logicalType;
return FlinkValueWriters.decimal(decimal.getPrecision(), decimal.getScale());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -274,17 +272,11 @@ public Optional<ParquetValueReader<?>> visit(
public Optional<ParquetValueReader<?>> visit(
LogicalTypeAnnotation.TimestampLogicalTypeAnnotation timestampLogicalType) {
if (timestampLogicalType.getUnit() == LogicalTypeAnnotation.TimeUnit.MILLIS) {
if (timestampLogicalType.isAdjustedToUTC()) {
return Optional.of(new MillisToTimestampTzReader(desc));
} else {
return Optional.of(new MillisToTimestampReader(desc));
}
return Optional.of(new MillisToTimestampReader(desc));
} else if (timestampLogicalType.getUnit() == LogicalTypeAnnotation.TimeUnit.MICROS) {
if (timestampLogicalType.isAdjustedToUTC()) {
return Optional.of(new MicrosToTimestampTzReader(desc));
} else {
return Optional.of(new MicrosToTimestampReader(desc));
}
return Optional.of(new MicrosToTimestampReader(desc));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, the readers were converting values to LocalDateTime or OffsetDateTime and then Flink would convert those values back to a (millis, nanosOfMilli) pair. This involved a lot of unnecessary date/time logic in both Iceberg and Flink as well as readers to produce the separate types.

Now, the conversion to Flink is direct and doesn't go through Java date/time classes. That avoids all time zone calculations and should be quicker.

} else if (timestampLogicalType.getUnit() == LogicalTypeAnnotation.TimeUnit.NANOS) {
return Optional.of(new NanosToTimestampReader(desc));
}

return LogicalTypeAnnotation.LogicalTypeAnnotationVisitor.super.visit(timestampLogicalType);
Expand Down Expand Up @@ -412,25 +404,17 @@ public DecimalData read(DecimalData ignored) {
}
}

private static class MicrosToTimestampTzReader
private static class NanosToTimestampReader
extends ParquetValueReaders.UnboxedReader<TimestampData> {
MicrosToTimestampTzReader(ColumnDescriptor desc) {
NanosToTimestampReader(ColumnDescriptor desc) {
super(desc);
}

@Override
public TimestampData read(TimestampData ignored) {
long value = readLong();
return TimestampData.fromLocalDateTime(
Instant.ofEpochSecond(
Math.floorDiv(value, 1000_000L), Math.floorMod(value, 1000_000L) * 1000L)
.atOffset(ZoneOffset.UTC)
.toLocalDateTime());
}

@Override
public long readLong() {
return column.nextLong();
return TimestampData.fromEpochMillis(
Math.floorDiv(value, 1_000_000L), Math.floorMod(value, 1_000_000));
}
}

Expand All @@ -442,15 +426,9 @@ private static class MicrosToTimestampReader

@Override
public TimestampData read(TimestampData ignored) {
long value = readLong();
return TimestampData.fromInstant(
Instant.ofEpochSecond(
Math.floorDiv(value, 1000_000L), Math.floorMod(value, 1000_000L) * 1000L));
}

@Override
public long readLong() {
return column.nextLong();
long micros = readLong();
return TimestampData.fromEpochMillis(
Math.floorDiv(micros, 1000L), Math.floorMod(micros, 1000) * 1000);
}
}

Expand All @@ -465,30 +443,6 @@ public TimestampData read(TimestampData ignored) {
long millis = readLong();
return TimestampData.fromEpochMillis(millis);
}

@Override
public long readLong() {
return column.nextLong();
}
}

private static class MillisToTimestampTzReader
extends ParquetValueReaders.UnboxedReader<TimestampData> {
MillisToTimestampTzReader(ColumnDescriptor desc) {
super(desc);
}

@Override
public TimestampData read(TimestampData ignored) {
long millis = readLong();
return TimestampData.fromLocalDateTime(
Instant.ofEpochMilli(millis).atOffset(ZoneOffset.UTC).toLocalDateTime());
}

@Override
public long readLong() {
return column.nextLong();
}
}

private static class StringReader extends ParquetValueReaders.PrimitiveReader<StringData> {
Expand Down
Loading