|
31 | 31 | import java.nio.charset.StandardCharsets; |
32 | 32 | import java.util.ArrayList; |
33 | 33 | import java.util.Collections; |
| 34 | +import java.util.HashMap; |
34 | 35 | import java.util.Iterator; |
35 | 36 | import java.util.LinkedList; |
36 | 37 | import java.util.List; |
|
96 | 97 | import org.apache.iceberg.Table; |
97 | 98 | import org.apache.iceberg.TableProperties; |
98 | 99 | import org.apache.iceberg.avro.Avro; |
99 | | -import org.apache.iceberg.catalog.Catalog; |
100 | 100 | import org.apache.iceberg.catalog.TableIdentifier; |
101 | 101 | import org.apache.iceberg.exceptions.AlreadyExistsException; |
102 | 102 | import org.apache.iceberg.exceptions.NoSuchTableException; |
@@ -531,17 +531,23 @@ private Table getOrCreateTable(String filePath, FileFormat format) throws IOExce |
531 | 531 | org.apache.iceberg.Schema schema = getSchema(filePath, format); |
532 | 532 | PartitionSpec spec = PartitionUtils.toPartitionSpec(partitionFields, schema); |
533 | 533 | SortOrder sortOrder = SortOrderUtils.toSortOrder(sortFields, schema); |
534 | | - |
535 | | - Catalog.TableBuilder builder = |
536 | | - catalogConfig |
537 | | - .catalog() |
538 | | - .buildTable(tableId, schema) |
539 | | - .withPartitionSpec(spec) |
540 | | - .withSortOrder(sortOrder); |
541 | | - if (tableProps != null) { |
542 | | - builder.withProperties(tableProps); |
| 534 | + Map<String, String> properties = |
| 535 | + tableProps != null ? new HashMap<>(tableProps) : new HashMap<>(); |
| 536 | + if (properties.get(TableProperties.DEFAULT_NAME_MAPPING) == null) { |
| 537 | + // Forces Name based resolution instead of position based resolution |
| 538 | + NameMapping mapping = MappingUtil.create(schema); |
| 539 | + String mappingJson = NameMappingParser.toJson(mapping); |
| 540 | + properties.put(TableProperties.DEFAULT_NAME_MAPPING, mappingJson); |
543 | 541 | } |
544 | | - return builder.create(); |
| 542 | + |
| 543 | + return catalogConfig |
| 544 | + .catalog() |
| 545 | + .buildTable(tableId, schema) |
| 546 | + .withPartitionSpec(spec) |
| 547 | + .withSortOrder(sortOrder) |
| 548 | + .withProperties(properties) |
| 549 | + .create(); |
| 550 | + |
545 | 551 | } catch (AlreadyExistsException e2) { // if table already exists, just load it |
546 | 552 | return catalogConfig.catalog().loadTable(TableIdentifier.parse(identifier)); |
547 | 553 | } |
|
0 commit comments