use of org.apache.iceberg.mapping.MappedField in project iceberg by apache.
the class TestSchemaAndMappingUpdate method testDeleteAndAddColumnReassign.
@Test
public void testDeleteAndAddColumnReassign() {
NameMapping mapping = MappingUtil.create(table.schema());
String mappingJson = NameMappingParser.toJson(mapping);
table.updateProperties().set(TableProperties.DEFAULT_NAME_MAPPING, mappingJson).commit();
// the original field ID
int startIdColumnId = table.schema().findField("id").fieldId();
table.updateSchema().deleteColumn("id").commit();
// add the same column name back to the table with a different field ID
table.updateSchema().addColumn("id", Types.StringType.get()).commit();
String updatedJson = table.properties().get(TableProperties.DEFAULT_NAME_MAPPING);
NameMapping updated = NameMappingParser.fromJson(updatedJson);
// the new field ID
int idColumnId = table.schema().findField("id").fieldId();
Set<Integer> changedIds = Sets.newHashSet(startIdColumnId, idColumnId);
validateUnchanged(Iterables.filter(mapping.asMappedFields().fields(), field -> !changedIds.contains(field.id())), updated);
MappedField newMapping = updated.find("id");
Assert.assertNotNull("Mapping for id column should exist", newMapping);
Assert.assertEquals("Mapping should use the new field ID", (Integer) idColumnId, newMapping.id());
Assert.assertNull("Should not contain a nested mapping", newMapping.nestedMapping());
MappedField updatedMapping = updated.find(startIdColumnId);
Assert.assertNotNull("Mapping for original id column should exist", updatedMapping);
Assert.assertEquals("Mapping should use the original field ID", (Integer) startIdColumnId, updatedMapping.id());
Assert.assertFalse("Should not use id as a name", updatedMapping.names().contains("id"));
Assert.assertNull("Should not contain a nested mapping", updatedMapping.nestedMapping());
}
use of org.apache.iceberg.mapping.MappedField in project iceberg by apache.
the class TestSchemaAndMappingUpdate method testAddStructColumn.
@Test
public void testAddStructColumn() {
NameMapping mapping = MappingUtil.create(table.schema());
String mappingJson = NameMappingParser.toJson(mapping);
table.updateProperties().set(TableProperties.DEFAULT_NAME_MAPPING, mappingJson).commit();
table.updateSchema().addColumn("location", Types.StructType.of(Types.NestedField.optional(1, "lat", Types.DoubleType.get()), Types.NestedField.optional(2, "long", Types.DoubleType.get()))).commit();
String updatedJson = table.properties().get(TableProperties.DEFAULT_NAME_MAPPING);
NameMapping updated = NameMappingParser.fromJson(updatedJson);
validateUnchanged(mapping, updated);
MappedField newMapping = updated.find("location");
Assert.assertNotNull("Mapping for new column should be added", newMapping);
Assert.assertEquals("Mapping should use the assigned field ID", (Integer) table.schema().findField("location").fieldId(), updated.find("location").id());
Assert.assertNotNull("Should contain a nested mapping", updated.find("location").nestedMapping());
Assert.assertEquals("Mapping should use the assigned field ID", (Integer) table.schema().findField("location.lat").fieldId(), updated.find("location.lat").id());
Assert.assertNull("Should not contain a nested mapping", updated.find("location.lat").nestedMapping());
Assert.assertEquals("Mapping should use the assigned field ID", (Integer) table.schema().findField("location.long").fieldId(), updated.find("location.long").id());
Assert.assertNull("Should not contain a nested mapping", updated.find("location.long").nestedMapping());
}
use of org.apache.iceberg.mapping.MappedField in project iceberg by apache.
the class TestSchemaAndMappingUpdate method testAddPrimitiveColumn.
@Test
public void testAddPrimitiveColumn() {
NameMapping mapping = MappingUtil.create(table.schema());
String mappingJson = NameMappingParser.toJson(mapping);
table.updateProperties().set(TableProperties.DEFAULT_NAME_MAPPING, mappingJson).commit();
table.updateSchema().addColumn("count", Types.LongType.get()).commit();
String updatedJson = table.properties().get(TableProperties.DEFAULT_NAME_MAPPING);
NameMapping updated = NameMappingParser.fromJson(updatedJson);
validateUnchanged(mapping, updated);
MappedField newMapping = updated.find("count");
Assert.assertNotNull("Mapping for new column should be added", newMapping);
Assert.assertEquals("Mapping should use the assigned field ID", (Integer) table.schema().findField("count").fieldId(), updated.find("count").id());
Assert.assertNull("Should not contain a nested mapping", updated.find("count").nestedMapping());
}
use of org.apache.iceberg.mapping.MappedField in project iceberg by apache.
the class TestSchemaAndMappingUpdate method testRenameAndRenameColumnReassign.
@Test
public void testRenameAndRenameColumnReassign() {
NameMapping mapping = MappingUtil.create(table.schema());
String mappingJson = NameMappingParser.toJson(mapping);
table.updateProperties().set(TableProperties.DEFAULT_NAME_MAPPING, mappingJson).commit();
// the original field ID
int startIdColumnId = table.schema().findField("id").fieldId();
table.updateSchema().renameColumn("id", "object_id").commit();
NameMapping afterRename = NameMappingParser.fromJson(table.properties().get(TableProperties.DEFAULT_NAME_MAPPING));
Assert.assertEquals("Renamed column should have both names", Sets.newHashSet("id", "object_id"), afterRename.find(startIdColumnId).names());
// rename the data column to the renamed column's old name
// also, rename the original column again to ensure its names are handled correctly
table.updateSchema().renameColumn("object_id", "oid").renameColumn("data", "id").commit();
String updatedJson = table.properties().get(TableProperties.DEFAULT_NAME_MAPPING);
NameMapping updated = NameMappingParser.fromJson(updatedJson);
// the new field ID
int idColumnId = table.schema().findField("id").fieldId();
Set<Integer> changedIds = Sets.newHashSet(startIdColumnId, idColumnId);
validateUnchanged(Iterables.filter(afterRename.asMappedFields().fields(), field -> !changedIds.contains(field.id())), updated);
MappedField newMapping = updated.find("id");
Assert.assertNotNull("Mapping for id column should exist", newMapping);
Assert.assertEquals("Renamed column should have both names", Sets.newHashSet("id", "data"), newMapping.names());
Assert.assertEquals("Mapping should use the new field ID", (Integer) idColumnId, newMapping.id());
Assert.assertNull("Should not contain a nested mapping", newMapping.nestedMapping());
MappedField updatedMapping = updated.find(startIdColumnId);
Assert.assertNotNull("Mapping for original id column should exist", updatedMapping);
Assert.assertEquals("Mapping should use the original field ID", (Integer) startIdColumnId, updatedMapping.id());
Assert.assertEquals("Should not use id as a name", Sets.newHashSet("object_id", "oid"), updatedMapping.names());
Assert.assertNull("Should not contain a nested mapping", updatedMapping.nestedMapping());
}
use of org.apache.iceberg.mapping.MappedField in project iceberg by apache.
the class ApplyNameMapping method struct.
@Override
public Type struct(GroupType struct, List<Type> types) {
MappedField field = nameMapping.find(currentPath());
List<Type> actualTypes = types.stream().filter(Objects::nonNull).collect(Collectors.toList());
Type structType = struct.withNewFields(actualTypes);
return field == null ? structType : structType.withId(field.id());
}
Aggregations