amogh-jahagirdar commented on code in PR #18331: URL: https://github.com/apache/iceberg/pull/18331#discussion_r4150002915
########## core/src/main/java/org/apache/iceberg/SchemaOrder.java: ########## @@ -0,0 +1,123 @@ +/* + * 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; + +import java.util.List; +import java.util.function.Supplier; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.types.Type; +import org.apache.iceberg.types.TypeUtil; +import org.apache.iceberg.types.Types; + +/** + * Utility methods for working with fields in schema definition order. + * + * <p>Schema order is a pre-order traversal of fields in the schema. This produces fields in the + * same left-to-right order as a type definition. For example, the schema <code> + * id bigint, data string, point struct<x float, y float></code> produces fields in schema + * order: [ id, data, point, point.x, point.y ] + */ +public class SchemaOrder { Review Comment: Maybe we want to make it package private for now? I'm still not sure if there's any advantage to this vs just putting it in TypeUtil but I also do like how this class has a single focus and can be a single point of entry for everything that needs consistent field orderings ########## core/src/test/java/org/apache/iceberg/TestMetricsConfig.java: ########## @@ -288,4 +464,62 @@ public void testLimitingMetricsFieldIds() { .isEqualTo(MetricsModes.Truncate.withLength(16)); assertThat(wider.columnMode(4)).isEqualTo(MetricsModes.None.get()); } + + @Test + public void testMetricsConfigKryoSerialization() throws Exception { + Map<String, String> metricsConfig = + ImmutableMap.of( + TableProperties.DEFAULT_WRITE_METRICS_MODE, + "counts", + TableProperties.METRICS_MODE_COLUMN_CONF_PREFIX + "col1", + "full", + TableProperties.METRICS_MODE_COLUMN_CONF_PREFIX + "col2", + "truncate(16)"); + + Schema schema = + new Schema( + Types.NestedField.required(1, "col1", Types.IntegerType.get()), + Types.NestedField.optional(2, "col2", Types.StringType.get()), + Types.NestedField.optional(3, "col3", Types.StringType.get())); + + MetricsConfig config = MetricsTestUtil.from(metricsConfig, schema); + MetricsConfig deserialized = TestHelpers.KryoHelpers.roundTripSerialize(config); + + assertThat(deserialized.columnMode(1)).asString().isEqualTo(MetricsModes.Full.get().toString()); + assertThat(deserialized.columnMode(2)) + .asString() + .isEqualTo(MetricsModes.Truncate.withLength(16).toString()); + assertThat(deserialized.columnMode(3)) Review Comment: For these serialization tests, should we add assertions that the deserialized instance also has `metricFieldIds` in the expected order? ########## core/src/main/java/org/apache/iceberg/MetricsConfig.java: ########## @@ -367,15 +367,15 @@ private static Map<String, MetricsMode> partitionColumnModes(PartitionSpec spec) private static Map<Integer, String> idToName( Schema schema, Map<String, MetricsMode> columnModes) { if (schema != null) { - ImmutableMap.Builder<Integer, String> builder = ImmutableMap.builder(); - for (String name : columnModes.keySet()) { - Types.NestedField field = schema.findField(name); - if (field != null) { - builder.put(field.fieldId(), name); + Map<Integer, String> idToName = Maps.newLinkedHashMap(); + for (Integer fieldId : SchemaOrder.allFieldIds(schema)) { Review Comment: Minor: can we use primitive int as we loop over the fields? -- 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]
