aokolnychyi commented on a change in pull request #2240: URL: https://github.com/apache/iceberg/pull/2240#discussion_r690803655
########## File path: core/src/main/java/org/apache/iceberg/MetricsConfig.java ########## @@ -101,10 +116,22 @@ public static MetricsConfig fromProperties(Map<String, String> props) { } spec.columnModes.put(columnAlias, mode); }); - Review comment: nit: unnecessary change? ########## File path: core/src/main/java/org/apache/iceberg/MetricsConfig.java ########## @@ -87,6 +97,11 @@ public static MetricsConfig fromProperties(Map<String, String> props) { spec.defaultMode = MetricsModes.fromString(DEFAULT_WRITE_METRICS_MODE_DEFAULT); } + // First set sorted column with sorted column default (can be overridden by user) + MetricsMode sortedColDefaultMode = sortColumnDefaultMode(spec.defaultMode); + Set<String> sortedCols = SortOrderUtil.sortedColumns(order); Review comment: nit: names are slightly inconsistent: `sort` vs `sorted` ########## File path: core/src/main/java/org/apache/iceberg/MetricsConfig.java ########## @@ -87,6 +97,11 @@ public static MetricsConfig fromProperties(Map<String, String> props) { spec.defaultMode = MetricsModes.fromString(DEFAULT_WRITE_METRICS_MODE_DEFAULT); } + // First set sorted column with sorted column default (can be overridden by user) + MetricsMode sortedColDefaultMode = sortColumnDefaultMode(spec.defaultMode); + Set<String> sortedCols = SortOrderUtil.sortedColumns(order); + sortedCols.stream().forEach(sc -> spec.columnModes.put(sc, sortedColDefaultMode)); Review comment: nit: just `sortedCols.forEach()`? ########## File path: core/src/test/java/org/apache/iceberg/TestMetrics.java ########## @@ -597,6 +609,101 @@ public void testTruncateBinaryMetricsMode() throws IOException { assertBounds(1, Types.BinaryType.get(), expectedMinBounds, expectedMaxBounds, metrics); } + @Test + public void testSortedColumnMetrics() throws IOException { + File tableDir = temp.newFolder(); + tableDir.delete(); // created by table create + + SortOrder sortOrder = SortOrder.builderFor(SIMPLE_SCHEMA) + .asc("booleanCol") + .asc("intCol") + .asc("longCol") + .asc("decimalCol") + .asc("stringCol") + .asc("dateCol").build(); + + Table table = + TestTables.create(tableDir, "test", SIMPLE_SCHEMA, PartitionSpec.unpartitioned(), sortOrder, FORMAT_V2); + table.updateProperties().set(TableProperties.DEFAULT_WRITE_METRICS_MODE, "none").commit(); + + Record firstRecord = GenericRecord.create(SIMPLE_SCHEMA); + firstRecord.setField("booleanCol", true); + firstRecord.setField("intCol", Integer.MIN_VALUE); + firstRecord.setField("longCol", Long.MIN_VALUE); + firstRecord.setField("floatCol", Float.NaN); + firstRecord.setField("doubleCol", 2.0D); + firstRecord.setField("decimalCol", new BigDecimal("0.00")); + firstRecord.setField("stringCol", "AAA"); + firstRecord.setField("dateCol", DateTimeUtil.dateFromDays(1500)); + firstRecord.setField("timeCol", DateTimeUtil.timeFromMicros(2000L)); + firstRecord.setField("timestampColAboveEpoch", DateTimeUtil.timestampFromMicros(0L)); + firstRecord.setField("fixedCol", fixed); + firstRecord.setField("binaryCol", ByteBuffer.wrap("S".getBytes())); + firstRecord.setField("timestampColBelowEpoch", DateTimeUtil.timestampFromMicros(0L)); + + Record secondRecord = GenericRecord.create(SIMPLE_SCHEMA); + + secondRecord.setField("booleanCol", false); + secondRecord.setField("intCol", Integer.MAX_VALUE); + secondRecord.setField("longCol", Long.MAX_VALUE); + secondRecord.setField("floatCol", Float.NaN); + secondRecord.setField("doubleCol", 2.0D); + secondRecord.setField("decimalCol", new BigDecimal("10.00")); + secondRecord.setField("stringCol", "ZZZ"); + secondRecord.setField("dateCol", DateTimeUtil.dateFromDays(3000)); + secondRecord.setField("timeCol", DateTimeUtil.timeFromMicros(2000L)); + secondRecord.setField("timestampColAboveEpoch", DateTimeUtil.timestampFromMicros(0L)); + secondRecord.setField("fixedCol", fixed); + secondRecord.setField("binaryCol", ByteBuffer.wrap("S".getBytes())); + secondRecord.setField("timestampColBelowEpoch", DateTimeUtil.timestampFromMicros(0L)); + Review comment: nit: extra line ########## File path: core/src/test/java/org/apache/iceberg/TestMetrics.java ########## @@ -106,9 +113,14 @@ private static final Record FLOAT_DOUBLE_RECORD_1 = createRecordWithFloatAndDouble(1.2F, 3.4D); private static final Record FLOAT_DOUBLE_RECORD_2 = createRecordWithFloatAndDouble(5.6F, 7.8D); private static final Record NAN_ONLY_RECORD = createRecordWithFloatAndDouble(Float.NaN, Double.NaN); - Review comment: nit: this removes the separation between static and instance vars. ########## File path: core/src/test/java/org/apache/iceberg/TestMetrics.java ########## @@ -597,6 +609,101 @@ public void testTruncateBinaryMetricsMode() throws IOException { assertBounds(1, Types.BinaryType.get(), expectedMinBounds, expectedMaxBounds, metrics); } + @Test + public void testSortedColumnMetrics() throws IOException { + File tableDir = temp.newFolder(); + tableDir.delete(); // created by table create + + SortOrder sortOrder = SortOrder.builderFor(SIMPLE_SCHEMA) + .asc("booleanCol") + .asc("intCol") + .asc("longCol") + .asc("decimalCol") + .asc("stringCol") + .asc("dateCol").build(); + + Table table = + TestTables.create(tableDir, "test", SIMPLE_SCHEMA, PartitionSpec.unpartitioned(), sortOrder, FORMAT_V2); Review comment: nit: if you define a var `spec`, this would fit on one line ########## File path: data/src/test/java/org/apache/iceberg/io/TestWriterMetrics.java ########## @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iceberg.io; + +import java.io.File; +import java.nio.ByteBuffer; +import java.util.Map; +import org.apache.iceberg.FileFormat; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SortOrder; +import org.apache.iceberg.TableProperties; +import org.apache.iceberg.TestTables; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.types.Conversions; +import org.apache.iceberg.types.Types; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + + +import static org.apache.iceberg.types.Types.NestedField.optional; +import static org.apache.iceberg.types.Types.NestedField.required; + +@RunWith(Parameterized.class) +public abstract class TestWriterMetrics<T> { + + private static final int FORMAT_V2 = 2; + + protected static final Types.NestedField ID_FIELD = required(1, "id", Types.IntegerType.get()); + protected static final Types.NestedField DATA_FIELD = optional(2, "data", Types.StringType.get()); + + protected static final Types.StructType NESTED_FIELDS = Types.StructType.of( + required(4, "booleanField", Types.BooleanType.get()), + optional(5, "longValue", Types.LongType.get())); + + protected static final Types.NestedField STRUCT_FIELD = optional(3, "structField", NESTED_FIELDS); + + // create a schema with all supported fields + protected static final Schema SCHEMA = new Schema( + ID_FIELD, + DATA_FIELD, + STRUCT_FIELD + ); + + protected static final SortOrder sortOrder = + SortOrder.builderFor(SCHEMA).asc("id").asc("structField.longValue").build(); + + protected static final Map<String, String> properties = + ImmutableMap.of(TableProperties.DEFAULT_WRITE_METRICS_MODE, "none"); + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + protected FileFormat fileFormat; + protected TestTables.TestTable table = null; + protected File metadataDir = null; + private File tableDir = null; + private OutputFileFactory fileFactory = null; + + @Parameterized.Parameters(name = "FileFormat = {0}") + public static Object[][] parameters() { + return new Object[][] { + {FileFormat.ORC}, + {FileFormat.PARQUET} + }; + } + + public TestWriterMetrics(FileFormat fileFormat) { + this.fileFormat = fileFormat; + } + + protected abstract WriterFactory newWriterFactory(Schema dataSchema); Review comment: nit: parameterize it as `WriterFactory<T>`? ########## File path: data/src/main/java/org/apache/iceberg/data/BaseWriterFactory.java ########## @@ -85,7 +85,7 @@ protected BaseWriterFactory(Table table, FileFormat dataFileFormat, Schema dataS OutputFile outputFile = file.encryptingOutputFile(); EncryptionKeyMetadata keyMetadata = file.keyMetadata(); Map<String, String> properties = table.properties(); - MetricsConfig metricsConfig = MetricsConfig.fromProperties(properties); + MetricsConfig metricsConfig = MetricsConfig.forTable(table); Review comment: Can we also update the equality delete branch below? ########## File path: core/src/main/java/org/apache/iceberg/util/SortOrderUtil.java ########## @@ -64,4 +68,16 @@ static SortOrder buildSortOrder(Schema schema, PartitionSpec spec, SortOrder sor return builder.build(); } + + public static Set<String> sortedColumns(SortOrder sortOrder) { + if (sortOrder == null) { + return Collections.emptySet(); + } else { + return sortOrder.fields().stream() + .map(SortField::sourceId) + .map(sid -> sortOrder.schema().findColumnName(sid)) Review comment: I am not sure it is always correct as not all transforms are order preserving. For example, if we sort by `bucket(id, 8)`, it does not mean the data is sorted by `id`. That's why we added `Transform#preservesOrder`. I think you have to filter this stream to include only sort fields whose transforms are order preserving. ########## File path: core/src/test/java/org/apache/iceberg/TestSortOrder.java ########## @@ -315,4 +318,15 @@ public void testEmptySortOrder() { SortOrder order = SortOrder.builderFor(SCHEMA).build(); Assert.assertEquals("Order must be unsorted", SortOrder.unsorted(), order); } + + @Test + public void testSortOrderColumnNames() { Review comment: We should probably add a test with and without order preserving transforms. ########## File path: data/src/test/java/org/apache/iceberg/io/TestWriterMetrics.java ########## @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iceberg.io; + +import java.io.File; +import java.nio.ByteBuffer; +import java.util.Map; +import org.apache.iceberg.FileFormat; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SortOrder; +import org.apache.iceberg.TableProperties; +import org.apache.iceberg.TestTables; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.types.Conversions; +import org.apache.iceberg.types.Types; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + + +import static org.apache.iceberg.types.Types.NestedField.optional; +import static org.apache.iceberg.types.Types.NestedField.required; + +@RunWith(Parameterized.class) +public abstract class TestWriterMetrics<T> { + + private static final int FORMAT_V2 = 2; + + protected static final Types.NestedField ID_FIELD = required(1, "id", Types.IntegerType.get()); + protected static final Types.NestedField DATA_FIELD = optional(2, "data", Types.StringType.get()); + + protected static final Types.StructType NESTED_FIELDS = Types.StructType.of( + required(4, "booleanField", Types.BooleanType.get()), + optional(5, "longValue", Types.LongType.get())); + + protected static final Types.NestedField STRUCT_FIELD = optional(3, "structField", NESTED_FIELDS); + + // create a schema with all supported fields + protected static final Schema SCHEMA = new Schema( + ID_FIELD, + DATA_FIELD, + STRUCT_FIELD + ); + + protected static final SortOrder sortOrder = + SortOrder.builderFor(SCHEMA).asc("id").asc("structField.longValue").build(); + + protected static final Map<String, String> properties = + ImmutableMap.of(TableProperties.DEFAULT_WRITE_METRICS_MODE, "none"); + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + protected FileFormat fileFormat; + protected TestTables.TestTable table = null; + protected File metadataDir = null; + private File tableDir = null; + private OutputFileFactory fileFactory = null; + + @Parameterized.Parameters(name = "FileFormat = {0}") + public static Object[][] parameters() { + return new Object[][] { + {FileFormat.ORC}, + {FileFormat.PARQUET} + }; + } + + public TestWriterMetrics(FileFormat fileFormat) { + this.fileFormat = fileFormat; + } + + protected abstract WriterFactory newWriterFactory(Schema dataSchema); + + Review comment: nit: extra line? ########## File path: data/src/test/java/org/apache/iceberg/io/TestWriterMetrics.java ########## @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iceberg.io; + +import java.io.File; +import java.nio.ByteBuffer; +import java.util.Map; +import org.apache.iceberg.FileFormat; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SortOrder; +import org.apache.iceberg.TableProperties; +import org.apache.iceberg.TestTables; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.types.Conversions; +import org.apache.iceberg.types.Types; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + + Review comment: nit: extra line ########## File path: core/src/test/java/org/apache/iceberg/TestMetricsModes.java ########## @@ -19,22 +19,39 @@ package org.apache.iceberg; +import java.io.File; import java.util.Map; import org.apache.iceberg.MetricsModes.Counts; import org.apache.iceberg.MetricsModes.Full; import org.apache.iceberg.MetricsModes.None; import org.apache.iceberg.MetricsModes.Truncate; import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.types.Types; +import org.junit.After; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; + +import static org.apache.iceberg.types.Types.NestedField.required; public class TestMetricsModes { @Rule public ExpectedException exceptionRule = ExpectedException.none(); + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + private static final int FORMAT_V2 = 2; + + @After + public void after() { + TestTables.clearTables(); + } + + Review comment: nit: extra line ########## File path: core/src/main/java/org/apache/iceberg/io/DataWriter.java ########## @@ -86,4 +86,8 @@ public DataFile toDataFile() { Preconditions.checkState(dataFile != null, "Cannot create data file from unclosed writer"); return dataFile; } + + public FileAppender<T> appender() { Review comment: Do we need to expose it? It looks like it is only used in tests and only to obtain the final metrics. I think you can get the same by using `DataFile#lowerBounds` and `DataFile#upperBounds`. It seems `DataWriter` already exposes `toDataFile` that you can use. ########## File path: data/src/test/java/org/apache/iceberg/io/TestWriterMetrics.java ########## @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iceberg.io; + +import java.io.File; +import java.nio.ByteBuffer; +import java.util.Map; +import org.apache.iceberg.FileFormat; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SortOrder; +import org.apache.iceberg.TableProperties; +import org.apache.iceberg.TestTables; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.types.Conversions; +import org.apache.iceberg.types.Types; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + + +import static org.apache.iceberg.types.Types.NestedField.optional; +import static org.apache.iceberg.types.Types.NestedField.required; + +@RunWith(Parameterized.class) +public abstract class TestWriterMetrics<T> { + + private static final int FORMAT_V2 = 2; + + protected static final Types.NestedField ID_FIELD = required(1, "id", Types.IntegerType.get()); + protected static final Types.NestedField DATA_FIELD = optional(2, "data", Types.StringType.get()); + + protected static final Types.StructType NESTED_FIELDS = Types.StructType.of( + required(4, "booleanField", Types.BooleanType.get()), + optional(5, "longValue", Types.LongType.get())); + + protected static final Types.NestedField STRUCT_FIELD = optional(3, "structField", NESTED_FIELDS); + + // create a schema with all supported fields + protected static final Schema SCHEMA = new Schema( + ID_FIELD, + DATA_FIELD, + STRUCT_FIELD + ); + + protected static final SortOrder sortOrder = + SortOrder.builderFor(SCHEMA).asc("id").asc("structField.longValue").build(); + + protected static final Map<String, String> properties = + ImmutableMap.of(TableProperties.DEFAULT_WRITE_METRICS_MODE, "none"); + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + protected FileFormat fileFormat; + protected TestTables.TestTable table = null; + protected File metadataDir = null; + private File tableDir = null; + private OutputFileFactory fileFactory = null; + + @Parameterized.Parameters(name = "FileFormat = {0}") + public static Object[][] parameters() { + return new Object[][] { + {FileFormat.ORC}, + {FileFormat.PARQUET} + }; + } + + public TestWriterMetrics(FileFormat fileFormat) { + this.fileFormat = fileFormat; + } + + protected abstract WriterFactory newWriterFactory(Schema dataSchema); + + + protected abstract T toRow(Integer id, String data, boolean boolValue, Long longValue); + + @Before + public void setupTable() throws Exception { + this.tableDir = temp.newFolder(); + tableDir.delete(); // created by table create + + this.metadataDir = new File(tableDir, "metadata"); + + this.table = TestTables.create( + tableDir, + "test", + SCHEMA, + PartitionSpec.unpartitioned(), + sortOrder, + FORMAT_V2); + table.updateProperties().set(TableProperties.DEFAULT_WRITE_METRICS_MODE, "none").commit(); + + this.fileFactory = OutputFileFactory.builderFor(table, 1, 1).format(fileFormat).build(); + } + + @After + public void after() { + TestTables.clearTables(); + } + + @Test + public void verifySortedColMetric() throws Exception { + T row = toRow(3, "3", true, 3L); + FileAppender<T> appender = newWriterFactory(SCHEMA).newDataWriter( Review comment: I think you can use `DataWriter#toDataFile` to get accesses to bounds and avoid exposing the appender. ########## File path: parquet/src/main/java/org/apache/iceberg/parquet/Parquet.java ########## @@ -125,7 +125,7 @@ private WriteBuilder(OutputFile file) { public WriteBuilder forTable(Table table) { schema(table.schema()); setAll(table.properties()); - metricsConfig(MetricsConfig.fromProperties(table.properties())); + metricsConfig(MetricsConfig.forTable(table)); Review comment: Shall we do the same for ORC and Avro? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org