Skip to content

Commit 5eeeabd

Browse files
committed
add tests
1 parent e90dad9 commit 5eeeabd

File tree

2 files changed

+93
-9
lines changed

2 files changed

+93
-9
lines changed

data/src/main/java/org/apache/iceberg/data/GenericAppenderFactory.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.io.UncheckedIOException;
2323
import java.util.Map;
24+
import java.util.function.Supplier;
2425
import org.apache.iceberg.FileFormat;
2526
import org.apache.iceberg.MetricsConfig;
2627
import org.apache.iceberg.PartitionSpec;
@@ -53,14 +54,16 @@ public class GenericAppenderFactory implements FileAppenderFactory<Record> {
5354
private final Schema posDeleteRowSchema;
5455
private final Map<String, String> config = Maps.newHashMap();
5556

57+
private static final String WRITE_METRICS_PREFIX = "write.metadata.metrics.";
58+
5659
@Deprecated
5760
public GenericAppenderFactory(Schema schema) {
58-
this(null, schema, PartitionSpec.unpartitioned(), null, null, null);
61+
this(schema, PartitionSpec.unpartitioned());
5962
}
6063

6164
@Deprecated
6265
public GenericAppenderFactory(Schema schema, PartitionSpec spec) {
63-
this(null, schema, spec, null, null, null);
66+
this(schema, spec, null, null, null);
6467
}
6568

6669
@Deprecated
@@ -70,17 +73,18 @@ public GenericAppenderFactory(
7073
int[] equalityFieldIds,
7174
Schema eqDeleteRowSchema,
7275
Schema posDeleteRowSchema) {
73-
this(null, schema, spec, equalityFieldIds, eqDeleteRowSchema, posDeleteRowSchema);
76+
this(null, schema, spec, null, equalityFieldIds, eqDeleteRowSchema, posDeleteRowSchema);
7477
}
7578

7679
public GenericAppenderFactory(Table table) {
77-
this(table, null, null, null, null, null);
80+
this(table, null, null, null, null, null, null);
7881
}
7982

8083
public GenericAppenderFactory(
8184
Table table,
8285
Schema schema,
8386
PartitionSpec spec,
87+
Map<String, String> config,
8488
int[] equalityFieldIds,
8589
Schema eqDeleteRowSchema,
8690
Schema posDeleteRowSchema) {
@@ -97,17 +101,33 @@ public GenericAppenderFactory(
97101
this.spec = spec;
98102
}
99103

104+
if (config != null) {
105+
this.config.putAll(config);
106+
}
107+
100108
this.equalityFieldIds = equalityFieldIds;
101109
this.eqDeleteRowSchema = eqDeleteRowSchema;
102110
this.posDeleteRowSchema = posDeleteRowSchema;
103111
}
104112

105113
public GenericAppenderFactory set(String property, String value) {
114+
if (property.startsWith(WRITE_METRICS_PREFIX) && table != null) {
115+
throw new IllegalArgumentException(
116+
String.format(
117+
"Cannot set metrics property: %s directly. Use table properties instead.", property));
118+
}
119+
106120
config.put(property, value);
107121
return this;
108122
}
109123

110124
public GenericAppenderFactory setAll(Map<String, String> properties) {
125+
if (properties.keySet().stream().anyMatch(k -> k.startsWith(WRITE_METRICS_PREFIX))
126+
&& table != null) {
127+
throw new IllegalArgumentException(
128+
"Cannot set metrics properties directly. Use table properties instead.");
129+
}
130+
111131
config.putAll(properties);
112132
return this;
113133
}
@@ -120,7 +140,7 @@ public FileAppender<Record> newAppender(OutputFile outputFile, FileFormat fileFo
120140
@Override
121141
public FileAppender<Record> newAppender(
122142
EncryptedOutputFile encryptedOutputFile, FileFormat fileFormat) {
123-
MetricsConfig metricsConfig = metricsConfig();
143+
MetricsConfig metricsConfig = applyMetricsConfig(() -> MetricsConfig.forTable(table));
124144

125145
try {
126146
switch (fileFormat) {
@@ -181,7 +201,7 @@ public EqualityDeleteWriter<Record> newEqDeleteWriter(
181201
Preconditions.checkNotNull(
182202
eqDeleteRowSchema,
183203
"Equality delete row schema shouldn't be null when creating equality-delete writer");
184-
MetricsConfig metricsConfig = metricsConfig();
204+
MetricsConfig metricsConfig = applyMetricsConfig(() -> MetricsConfig.forTable(table));
185205

186206
try {
187207
switch (format) {
@@ -235,7 +255,7 @@ public EqualityDeleteWriter<Record> newEqDeleteWriter(
235255
@Override
236256
public PositionDeleteWriter<Record> newPosDeleteWriter(
237257
EncryptedOutputFile file, FileFormat format, StructLike partition) {
238-
MetricsConfig metricsConfig = metricsConfig();
258+
MetricsConfig metricsConfig = applyMetricsConfig(() -> MetricsConfig.forPositionDelete(table));
239259

240260
try {
241261
switch (format) {
@@ -282,12 +302,12 @@ public PositionDeleteWriter<Record> newPosDeleteWriter(
282302
}
283303
}
284304

285-
private MetricsConfig metricsConfig() {
305+
private MetricsConfig applyMetricsConfig(Supplier<MetricsConfig> metricsConfigSupplier) {
286306
MetricsConfig metricsConfig;
287307
if (table == null) {
288308
metricsConfig = MetricsConfig.fromProperties(config);
289309
} else {
290-
metricsConfig = MetricsConfig.forTable(table);
310+
metricsConfig = metricsConfigSupplier.get();
291311
}
292312

293313
return metricsConfig;

data/src/test/java/org/apache/iceberg/TestGenericAppenderFactory.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,21 @@
1818
*/
1919
package org.apache.iceberg;
2020

21+
import static org.assertj.core.api.Assertions.assertThatNoException;
22+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
23+
2124
import java.util.List;
25+
import java.util.Map;
2226
import org.apache.iceberg.data.GenericAppenderFactory;
2327
import org.apache.iceberg.data.GenericRecord;
2428
import org.apache.iceberg.data.Record;
2529
import org.apache.iceberg.io.FileAppenderFactory;
2630
import org.apache.iceberg.io.TestAppenderFactory;
2731
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
32+
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
2833
import org.apache.iceberg.util.ArrayUtil;
2934
import org.apache.iceberg.util.StructLikeSet;
35+
import org.junit.jupiter.api.TestTemplate;
3036

3137
public class TestGenericAppenderFactory extends TestAppenderFactory<Record> {
3238

@@ -36,8 +42,10 @@ public class TestGenericAppenderFactory extends TestAppenderFactory<Record> {
3642
protected FileAppenderFactory<Record> createAppenderFactory(
3743
List<Integer> equalityFieldIds, Schema eqDeleteSchema, Schema posDeleteRowSchema) {
3844
return new GenericAppenderFactory(
45+
table,
3946
table.schema(),
4047
table.spec(),
48+
Maps.newHashMap(),
4149
ArrayUtil.toIntArray(equalityFieldIds),
4250
eqDeleteSchema,
4351
posDeleteRowSchema);
@@ -54,4 +62,60 @@ protected StructLikeSet expectedRowSet(Iterable<Record> records) {
5462
records.forEach(set::add);
5563
return set;
5664
}
65+
66+
@TestTemplate
67+
void illegalSetConfig() {
68+
GenericAppenderFactory appenderFactory =
69+
(GenericAppenderFactory) createAppenderFactory(null, null, null);
70+
71+
assertThatThrownBy(
72+
() ->
73+
appenderFactory.set(
74+
TableProperties.METRICS_MAX_INFERRED_COLUMN_DEFAULTS,
75+
MetricsModes.None.get().toString()))
76+
.as("Should not allow setting metrics property if the table was provided")
77+
.isInstanceOf(IllegalArgumentException.class)
78+
.hasMessageContaining(
79+
"Cannot set metrics property: " + TableProperties.METRICS_MAX_INFERRED_COLUMN_DEFAULTS);
80+
}
81+
82+
@TestTemplate
83+
void illegalSetAllConfigs() {
84+
GenericAppenderFactory appenderFactory =
85+
(GenericAppenderFactory) createAppenderFactory(null, null, null);
86+
87+
Map<String, String> properties =
88+
ImmutableMap.of(
89+
TableProperties.METRICS_MAX_INFERRED_COLUMN_DEFAULTS,
90+
"10",
91+
TableProperties.METRICS_MODE_COLUMN_CONF_PREFIX + "id",
92+
MetricsModes.Full.get().toString());
93+
94+
assertThatThrownBy(() -> appenderFactory.setAll(properties))
95+
.as("Should not allow setting metrics property if the table was provided")
96+
.isInstanceOf(IllegalArgumentException.class)
97+
.hasMessageContaining("Cannot set metrics properties directly");
98+
}
99+
100+
@TestTemplate
101+
void setConfigExcludeMetrics() {
102+
GenericAppenderFactory appenderFactory =
103+
(GenericAppenderFactory) createAppenderFactory(null, null, null);
104+
assertThatNoException().isThrownBy(() -> appenderFactory.set("key1", "value1"));
105+
assertThatNoException()
106+
.isThrownBy(() -> appenderFactory.setAll(ImmutableMap.of("key2", "value2")));
107+
}
108+
109+
@TestTemplate
110+
void setConfigWithoutTable() {
111+
GenericAppenderFactory appenderFactory = new GenericAppenderFactory(SCHEMA);
112+
assertThatNoException()
113+
.isThrownBy(
114+
() -> appenderFactory.set(TableProperties.METRICS_MAX_INFERRED_COLUMN_DEFAULTS, "10"));
115+
assertThatNoException()
116+
.isThrownBy(
117+
() ->
118+
appenderFactory.setAll(
119+
ImmutableMap.of(TableProperties.DEFAULT_WRITE_METRICS_MODE, "full")));
120+
}
57121
}

0 commit comments

Comments
 (0)