Skip to content

Commit

Permalink
Handle case-insensitivity for targetName in transform methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sl255051 committed Aug 1, 2024
1 parent b817cd4 commit 339df71
Show file tree
Hide file tree
Showing 3 changed files with 377 additions and 11 deletions.
47 changes: 36 additions & 11 deletions api/src/main/java/org/apache/iceberg/PartitionSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,14 @@ private void checkAndAddPartitionName(String name, Integer sourceColumnId) {
name);
}
}
Preconditions.checkArgument(!name.isEmpty(), "Cannot use empty partition name: %s", name);
String normalizedPartitionName = caseSensitive ? name : name.toLowerCase();
Preconditions.checkArgument(
!partitionNames.contains(name), "Cannot use partition name more than once: %s", name);
partitionNames.add(name);
!normalizedPartitionName.isEmpty(), "Cannot use empty partition name: %s", name);
Preconditions.checkArgument(
!partitionNames.contains(normalizedPartitionName),
"Cannot use partition name more than once: %s",
name);
partitionNames.add(normalizedPartitionName);
}

private void checkForRedundantPartitions(PartitionField field) {
Expand Down Expand Up @@ -452,9 +456,10 @@ private Types.NestedField findSourceColumn(String sourceName) {
Builder identity(String sourceName, String targetName) {
Types.NestedField sourceColumn = findSourceColumn(sourceName);
checkAndAddPartitionName(targetName, sourceColumn.fieldId());
String normalizedTargetName = caseSensitive ? targetName : targetName.toLowerCase();
PartitionField field =
new PartitionField(
sourceColumn.fieldId(), nextFieldId(), targetName, Transforms.identity());
sourceColumn.fieldId(), nextFieldId(), normalizedTargetName, Transforms.identity());
checkForRedundantPartitions(field);
fields.add(field);
return this;
Expand All @@ -467,8 +472,10 @@ public Builder identity(String sourceName) {
public Builder year(String sourceName, String targetName) {
checkAndAddPartitionName(targetName);
Types.NestedField sourceColumn = findSourceColumn(sourceName);
String normalizedTargetName = caseSensitive ? targetName : targetName.toLowerCase();
PartitionField field =
new PartitionField(sourceColumn.fieldId(), nextFieldId(), targetName, Transforms.year());
new PartitionField(
sourceColumn.fieldId(), nextFieldId(), normalizedTargetName, Transforms.year());
checkForRedundantPartitions(field);
fields.add(field);
return this;
Expand All @@ -481,8 +488,10 @@ public Builder year(String sourceName) {
public Builder month(String sourceName, String targetName) {
checkAndAddPartitionName(targetName);
Types.NestedField sourceColumn = findSourceColumn(sourceName);
String normalizedTargetName = caseSensitive ? targetName : targetName.toLowerCase();
PartitionField field =
new PartitionField(sourceColumn.fieldId(), nextFieldId(), targetName, Transforms.month());
new PartitionField(
sourceColumn.fieldId(), nextFieldId(), normalizedTargetName, Transforms.month());
checkForRedundantPartitions(field);
fields.add(field);
return this;
Expand All @@ -495,8 +504,10 @@ public Builder month(String sourceName) {
public Builder day(String sourceName, String targetName) {
checkAndAddPartitionName(targetName);
Types.NestedField sourceColumn = findSourceColumn(sourceName);
String normalizedTargetName = caseSensitive ? targetName : targetName.toLowerCase();
PartitionField field =
new PartitionField(sourceColumn.fieldId(), nextFieldId(), targetName, Transforms.day());
new PartitionField(
sourceColumn.fieldId(), nextFieldId(), normalizedTargetName, Transforms.day());
checkForRedundantPartitions(field);
fields.add(field);
return this;
Expand All @@ -509,8 +520,10 @@ public Builder day(String sourceName) {
public Builder hour(String sourceName, String targetName) {
checkAndAddPartitionName(targetName);
Types.NestedField sourceColumn = findSourceColumn(sourceName);
String normalizedTargetName = caseSensitive ? targetName : targetName.toLowerCase();
PartitionField field =
new PartitionField(sourceColumn.fieldId(), nextFieldId(), targetName, Transforms.hour());
new PartitionField(
sourceColumn.fieldId(), nextFieldId(), normalizedTargetName, Transforms.hour());
checkForRedundantPartitions(field);
fields.add(field);
return this;
Expand All @@ -523,9 +536,13 @@ public Builder hour(String sourceName) {
public Builder bucket(String sourceName, int numBuckets, String targetName) {
checkAndAddPartitionName(targetName);
Types.NestedField sourceColumn = findSourceColumn(sourceName);
String normalizedTargetName = caseSensitive ? targetName : targetName.toLowerCase();
fields.add(
new PartitionField(
sourceColumn.fieldId(), nextFieldId(), targetName, Transforms.bucket(numBuckets)));
sourceColumn.fieldId(),
nextFieldId(),
normalizedTargetName,
Transforms.bucket(numBuckets)));
return this;
}

Expand All @@ -536,9 +553,13 @@ public Builder bucket(String sourceName, int numBuckets) {
public Builder truncate(String sourceName, int width, String targetName) {
checkAndAddPartitionName(targetName);
Types.NestedField sourceColumn = findSourceColumn(sourceName);
String normalizedTargetName = caseSensitive ? targetName : targetName.toLowerCase();
fields.add(
new PartitionField(
sourceColumn.fieldId(), nextFieldId(), targetName, Transforms.truncate(width)));
sourceColumn.fieldId(),
nextFieldId(),
normalizedTargetName,
Transforms.truncate(width)));
return this;
}

Expand All @@ -550,9 +571,13 @@ public Builder alwaysNull(String sourceName, String targetName) {
Types.NestedField sourceColumn = findSourceColumn(sourceName);
checkAndAddPartitionName(
targetName, sourceColumn.fieldId()); // can duplicate a source column name
String normalizedTargetName = caseSensitive ? targetName : targetName.toLowerCase();
fields.add(
new PartitionField(
sourceColumn.fieldId(), nextFieldId(), targetName, Transforms.alwaysNull()));
sourceColumn.fieldId(),
nextFieldId(),
normalizedTargetName,
Transforms.alwaysNull()));
return this;
}

Expand Down
Loading

0 comments on commit 339df71

Please sign in to comment.