szehon-ho commented on a change in pull request #2240:
URL: https://github.com/apache/iceberg/pull/2240#discussion_r691712499
##########
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:
Updated
##########
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:
Done
##########
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:
Yea it makes sense. I wonder a bit the use case for sorting by
bucket(id, 8), but yea it does not make too much sense to auto-promote them.
##########
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:
Fixed
##########
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:
Added separation
##########
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:
Fixed
##########
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:
Done
##########
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:
Renamed method to sortedColumnDefaultValue
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]