|
| 1 | +/* |
| 2 | + * Copyright 2025 Snowflake Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/* |
| 19 | + * partition_pushdown.c |
| 20 | + * |
| 21 | + * Utilities for pushing down partitioned Iceberg writes to DuckDB using |
| 22 | + * the PARTITION_BY clause in COPY TO. Supports identity and temporal |
| 23 | + * (year, month, day, hour) partition transforms. |
| 24 | + */ |
| 25 | +#include "postgres.h" |
| 26 | + |
| 27 | +#include "utils/builtins.h" |
| 28 | + |
| 29 | +#include "pg_lake/fdw/partition_pushdown.h" |
| 30 | +#include "pg_lake/fdw/partition_transform.h" |
| 31 | +#include "pg_lake/iceberg/api/partitioning.h" |
| 32 | +#include "pg_lake/iceberg/manifest_spec.h" |
| 33 | + |
| 34 | +/* prefix for synthetic partition columns in COPY TO queries */ |
| 35 | +#define PARTITION_COLUMN_PREFIX "__part_" |
| 36 | + |
| 37 | + |
| 38 | +static char *PartitionTransformToDuckDBExpression(IcebergPartitionTransform * transform); |
| 39 | + |
| 40 | + |
| 41 | +/* |
| 42 | + * AllPartitionTransformsPushdownable returns true if every transform in the |
| 43 | + * list can be expressed as a DuckDB SQL expression for PARTITION_BY. |
| 44 | + */ |
| 45 | +bool |
| 46 | +AllPartitionTransformsPushdownable(List *transforms) |
| 47 | +{ |
| 48 | + if (transforms == NIL) |
| 49 | + return false; |
| 50 | + |
| 51 | + ListCell *cell = NULL; |
| 52 | + |
| 53 | + foreach(cell, transforms) |
| 54 | + { |
| 55 | + IcebergPartitionTransform *transform = lfirst(cell); |
| 56 | + char *expr = PartitionTransformToDuckDBExpression(transform); |
| 57 | + |
| 58 | + if (expr == NULL) |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + return true; |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +/* |
| 67 | + * PartitionTransformToDuckDBExpression returns a DuckDB SQL expression that |
| 68 | + * computes the Iceberg partition value for the given transform. |
| 69 | + * |
| 70 | + * Returns NULL for transforms that cannot be pushed down (bucket, truncate, void). |
| 71 | + * |
| 72 | + * The expressions produce Iceberg-compatible partition values: |
| 73 | + * - year: integer years since 1970 |
| 74 | + * - month: integer months since Jan 1970 |
| 75 | + * - day: integer days since 1970-01-01 |
| 76 | + * - hour: integer hours since 1970-01-01T00:00:00 |
| 77 | + * - identity: the column value (with type-specific casts for date/timestamp) |
| 78 | + */ |
| 79 | +static char * |
| 80 | +PartitionTransformToDuckDBExpression(IcebergPartitionTransform * transform) |
| 81 | +{ |
| 82 | + const char *col = quote_identifier(transform->columnName); |
| 83 | + Oid typeOid = transform->pgType.postgresTypeOid; |
| 84 | + |
| 85 | + switch (transform->type) |
| 86 | + { |
| 87 | + case PARTITION_TRANSFORM_IDENTITY: |
| 88 | + { |
| 89 | + /* |
| 90 | + * Identity partitions use the column value directly. DuckDB |
| 91 | + * writes the value in its default text format into the |
| 92 | + * Hive-style path (e.g. __part_0=2025-01-15), and |
| 93 | + * DeserializePartitionValueFromPGText parses it back using |
| 94 | + * the transform's result PG type. |
| 95 | + */ |
| 96 | + return psprintf("%s", col); |
| 97 | + } |
| 98 | + |
| 99 | + case PARTITION_TRANSFORM_YEAR: |
| 100 | + return psprintf("(year(%s) - 1970)", col); |
| 101 | + |
| 102 | + case PARTITION_TRANSFORM_MONTH: |
| 103 | + return psprintf("((year(%s) - 1970) * 12 + month(%s) - 1)", col, col); |
| 104 | + |
| 105 | + case PARTITION_TRANSFORM_DAY: |
| 106 | + return psprintf("datediff('day', date '1970-01-01', %s::date)", col); |
| 107 | + |
| 108 | + case PARTITION_TRANSFORM_HOUR: |
| 109 | + return psprintf("datediff('hour', timestamp '1970-01-01', %s::timestamp)", col); |
| 110 | + |
| 111 | + case PARTITION_TRANSFORM_BUCKET: |
| 112 | + case PARTITION_TRANSFORM_TRUNCATE: |
| 113 | + case PARTITION_TRANSFORM_VOID: |
| 114 | + return NULL; |
| 115 | + } |
| 116 | + |
| 117 | + return NULL; |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +/* |
| 122 | + * GetPartitionByExpressions returns a list of String values with the DuckDB |
| 123 | + * SQL expressions for each partition transform. These are passed to |
| 124 | + * WriteQueryResultTo which wraps the query with synthetic partition columns |
| 125 | + * after validation wrapping. |
| 126 | + */ |
| 127 | +List * |
| 128 | +GetPartitionByExpressions(List *transforms) |
| 129 | +{ |
| 130 | + List *exprs = NIL; |
| 131 | + ListCell *cell = NULL; |
| 132 | + |
| 133 | + foreach(cell, transforms) |
| 134 | + { |
| 135 | + IcebergPartitionTransform *transform = lfirst(cell); |
| 136 | + char *expr = PartitionTransformToDuckDBExpression(transform); |
| 137 | + |
| 138 | + Assert(expr != NULL); |
| 139 | + |
| 140 | + exprs = lappend(exprs, makeString(expr)); |
| 141 | + } |
| 142 | + |
| 143 | + return exprs; |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | +/* |
| 148 | + * ParsePartitionValuesFromPath extracts partition values from the Hive-style |
| 149 | + * directory path produced by DuckDB COPY TO with PARTITION_BY. |
| 150 | + * |
| 151 | + * A path like: |
| 152 | + * s3://bucket/data/abc123/__part_0=54/__part_1=us-east/data_0.parquet |
| 153 | + * |
| 154 | + * is parsed to extract __part_0=54 and __part_1=us-east, which are then |
| 155 | + * converted to the proper Iceberg binary format using the partition transforms. |
| 156 | + */ |
| 157 | +Partition * |
| 158 | +ParsePartitionValuesFromPath(char *filePath, List *transforms) |
| 159 | +{ |
| 160 | + int numTransforms = list_length(transforms); |
| 161 | + Partition *partition = palloc0(sizeof(Partition)); |
| 162 | + |
| 163 | + partition->fields = palloc0(sizeof(PartitionField) * numTransforms); |
| 164 | + partition->fields_length = numTransforms; |
| 165 | + |
| 166 | + for (int partIndex = 0; partIndex < numTransforms; partIndex++) |
| 167 | + { |
| 168 | + IcebergPartitionTransform *transform = list_nth(transforms, partIndex); |
| 169 | + |
| 170 | + /* build the search key: "__part_N=" */ |
| 171 | + char *searchKey = psprintf(PARTITION_COLUMN_PREFIX "%d=", partIndex); |
| 172 | + int searchKeyLen = strlen(searchKey); |
| 173 | + |
| 174 | + /* find this key in the path */ |
| 175 | + char *found = strstr(filePath, searchKey); |
| 176 | + |
| 177 | + if (found == NULL) |
| 178 | + { |
| 179 | + ereport(ERROR, |
| 180 | + (errcode(ERRCODE_INTERNAL_ERROR), |
| 181 | + errmsg("could not find partition key %s in path %s", |
| 182 | + searchKey, filePath))); |
| 183 | + } |
| 184 | + |
| 185 | + /* extract the value (from after '=' up to the next '/' or end) */ |
| 186 | + char *valueStart = found + searchKeyLen; |
| 187 | + char *valueEnd = strchr(valueStart, '/'); |
| 188 | + int valueLen = (valueEnd != NULL) ? |
| 189 | + (valueEnd - valueStart) : strlen(valueStart); |
| 190 | + |
| 191 | + char *valueText = pnstrdup(valueStart, valueLen); |
| 192 | + |
| 193 | + /* populate the partition field */ |
| 194 | + PartitionField *field = &partition->fields[partIndex]; |
| 195 | + |
| 196 | + field->field_id = transform->partitionFieldId; |
| 197 | + field->field_name = pstrdup(transform->partitionFieldName); |
| 198 | + field->value_type = GetTransformResultAvroType(transform); |
| 199 | + |
| 200 | + if (strcmp(valueText, "NULL") == 0) |
| 201 | + { |
| 202 | + /* NULL partition value */ |
| 203 | + field->value = NULL; |
| 204 | + field->value_length = 0; |
| 205 | + } |
| 206 | + else |
| 207 | + { |
| 208 | + /* |
| 209 | + * Convert the text value to Iceberg binary format. The text |
| 210 | + * representation matches the transform's result PG type. |
| 211 | + */ |
| 212 | + field->value = DeserializePartitionValueFromPGText( |
| 213 | + transform, valueText, &field->value_length); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + return partition; |
| 218 | +} |
0 commit comments