Skip to content

Commit

Permalink
Use date_trunc for DATE field in dynamic partition overwrite (#1310)
Browse files Browse the repository at this point in the history
  • Loading branch information
isha97 authored Oct 23, 2024
1 parent 14fa9b8 commit 2875a56
Showing 1 changed file with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -735,12 +735,24 @@ static String getQueryForTimePartitionedTable(
TimePartitioning timePartitioning) {
TimePartitioning.Type partitionType = timePartitioning.getType();
String partitionField = timePartitioning.getField();
FieldList allFields = destinationDefinition.getSchema().getFields();
Optional<LegacySQLTypeName> partitionFieldType =
allFields.stream()
.filter(field -> partitionField.equals(field.getName()))
.map(field -> field.getType())
.findFirst();
// Using timestamp_trunc on a DATE field results in $cast_to_DATETIME which prevents pruning
// when required_partition_filter is set, as it is not considered a monotonic function
String truncFuntion =
(partitionFieldType.isPresent() && partitionFieldType.get().equals(LegacySQLTypeName.DATE))
? "date_trunc"
: "timestamp_trunc";
String extractedPartitionedSource =
String.format("timestamp_trunc(`%s`, %s)", partitionField, partitionType.toString());
String.format("%s(`%s`, %s)", truncFuntion, partitionField, partitionType.toString());
String extractedPartitionedTarget =
String.format(
"timestamp_trunc(`%s`.`%s`, %s)", "target", partitionField, partitionType.toString());
FieldList allFields = destinationDefinition.getSchema().getFields();
"%s(`target`.`%s`, %s)", truncFuntion, partitionField, partitionType.toString());

String commaSeparatedFields =
allFields.stream().map(Field::getName).collect(Collectors.joining("`,`", "`", "`"));

Expand Down

0 comments on commit 2875a56

Please sign in to comment.