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
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,15 @@ public void testInLimitParquet() {
Assert.assertTrue("Should read if IN is not evaluated", shouldRead);
}

@Test
public void testParquetTypePromotion() {
Assume.assumeTrue("Only valid for Parquet", format == FileFormat.PARQUET);
Schema promotedSchema = new Schema(required(1, "id", Types.LongType.get()));
boolean shouldRead = new ParquetMetricsRowGroupFilter(promotedSchema, equal("id", INT_MIN_VALUE + 1), true)
.shouldRead(parquetSchema, rowGroupMetadata);
Assert.assertTrue("Should succeed with promoted schema", shouldRead);
}

private boolean shouldRead(Expression expression) {
return shouldRead(expression, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ static <T> Literal<T> fromParquetPrimitive(Type type, PrimitiveType parquetType,
}
}

static Function<Object, Object> converterFromParquet(PrimitiveType parquetType, Type icebergType) {
Function<Object, Object> fromParquet = converterFromParquet(parquetType);
if (icebergType != null) {
if (icebergType.typeId() == Type.TypeID.LONG &&
parquetType.getPrimitiveTypeName() == PrimitiveType.PrimitiveTypeName.INT32) {
return value -> ((Integer) fromParquet.apply(value)).longValue();
} else if (icebergType.typeId() == Type.TypeID.DOUBLE &&
parquetType.getPrimitiveTypeName() == PrimitiveType.PrimitiveTypeName.FLOAT) {
return value -> ((Float) fromParquet.apply(value)).doubleValue();
}
}

return fromParquet;
}

static Function<Object, Object> converterFromParquet(PrimitiveType type) {
if (type.getOriginalType() != null) {
switch (type.getOriginalType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.relocated.com.google.common.collect.Sets;
import org.apache.iceberg.types.Comparators;
import org.apache.iceberg.types.Type;
import org.apache.iceberg.types.Types.StructType;
import org.apache.iceberg.util.NaNUtil;
import org.apache.parquet.column.ColumnDescriptor;
Expand All @@ -49,13 +50,15 @@
import org.apache.parquet.schema.PrimitiveType;

public class ParquetDictionaryRowGroupFilter {
private final Schema schema;
private final Expression expr;

public ParquetDictionaryRowGroupFilter(Schema schema, Expression unbound) {
this(schema, unbound, true);
}

public ParquetDictionaryRowGroupFilter(Schema schema, Expression unbound, boolean caseSensitive) {
this.schema = schema;
StructType struct = schema.asStruct();
this.expr = Binder.bind(struct, Expressions.rewriteNot(unbound), caseSensitive);
}
Expand Down Expand Up @@ -96,8 +99,9 @@ private boolean eval(MessageType fileSchema, BlockMetaData rowGroup,
PrimitiveType colType = fileSchema.getType(desc.getPath()).asPrimitiveType();
if (colType.getId() != null) {
int id = colType.getId().intValue();
Type icebergType = schema.findType(id);
cols.put(id, desc);
conversions.put(id, ParquetConversions.converterFromParquet(colType));
conversions.put(id, ParquetConversions.converterFromParquet(colType, icebergType));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public boolean shouldRead(MessageType fileSchema, BlockMetaData rowGroup) {
private static final boolean ROWS_CANNOT_MATCH = false;

private class MetricsEvalVisitor extends BoundExpressionVisitor<Boolean> {
private Map<Integer, Statistics> stats = null;
private Map<Integer, Statistics<?>> stats = null;
private Map<Integer, Long> valueCounts = null;
private Map<Integer, Function<Object, Object>> conversions = null;

Expand All @@ -93,9 +93,10 @@ private boolean eval(MessageType fileSchema, BlockMetaData rowGroup) {
PrimitiveType colType = fileSchema.getType(col.getPath().toArray()).asPrimitiveType();
if (colType.getId() != null) {
int id = colType.getId().intValue();
Type icebergType = schema.findType(id);
stats.put(id, col.getStatistics());
valueCounts.put(id, col.getValueCount());
conversions.put(id, ParquetConversions.converterFromParquet(colType));
conversions.put(id, ParquetConversions.converterFromParquet(colType, icebergType));
}
}

Expand Down Expand Up @@ -502,4 +503,19 @@ static boolean hasNonNullButNoMinMax(Statistics statistics, long valueCount) {
return statistics.getNumNulls() < valueCount &&
(statistics.getMaxBytes() == null || statistics.getMinBytes() == null);
}

private static Function<Object, Object> converterFor(PrimitiveType parquetType, Type icebergType) {
Function<Object, Object> fromParquet = ParquetConversions.converterFromParquet(parquetType);
if (icebergType != null) {
if (icebergType.typeId() == Type.TypeID.LONG &&
parquetType.getPrimitiveTypeName() == PrimitiveType.PrimitiveTypeName.INT32) {
return value -> ((Integer) fromParquet.apply(value)).longValue();
} else if (icebergType.typeId() == Type.TypeID.DOUBLE &&
parquetType.getPrimitiveTypeName() == PrimitiveType.PrimitiveTypeName.FLOAT) {
return value -> ((Float) fromParquet.apply(value)).doubleValue();
}
}

return fromParquet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -884,4 +884,12 @@ public void testIntegerNotIn() {
.shouldRead(parquetSchema, rowGroupMetadata, dictionaryStore);
Assert.assertFalse("Should not read: notIn on no nulls column (empty string is within the set)", shouldRead);
}

@Test
public void testTypePromotion() {
Schema promotedSchema = new Schema(required(1, "id", LongType.get()));
boolean shouldRead = new ParquetDictionaryRowGroupFilter(promotedSchema, equal("id", INT_MIN_VALUE + 1), true)
.shouldRead(parquetSchema, rowGroupMetadata, dictionaryStore);
Assert.assertTrue("Should succeed with promoted schema", shouldRead);
}
}