|
20 | 20 |
|
21 | 21 | import static org.assertj.core.api.Assertions.assertThat;
|
22 | 22 |
|
| 23 | +import java.time.LocalDateTime; |
| 24 | +import java.time.ZoneOffset; |
| 25 | +import java.util.List; |
23 | 26 | import org.apache.iceberg.Parameter;
|
24 | 27 | import org.apache.iceberg.ParameterizedTestExtension;
|
25 | 28 | import org.apache.iceberg.Parameters;
|
26 | 29 | import org.apache.iceberg.PartitionSpec;
|
27 | 30 | import org.apache.iceberg.Table;
|
28 | 31 | import org.apache.iceberg.TableProperties;
|
29 | 32 | import org.apache.iceberg.TestHelpers;
|
| 33 | +import org.apache.iceberg.relocated.com.google.common.collect.Lists; |
30 | 34 | import org.apache.iceberg.spark.SparkCatalogConfig;
|
31 | 35 | import org.apache.iceberg.spark.source.SparkTable;
|
32 | 36 | import org.apache.spark.sql.connector.catalog.CatalogManager;
|
@@ -582,4 +586,44 @@ private void createTable(String schema, String spec) {
|
582 | 586 | tableName, schema, spec, TableProperties.FORMAT_VERSION, formatVersion);
|
583 | 587 | }
|
584 | 588 | }
|
| 589 | + |
| 590 | + private void runCreateAndDropPartitionField( |
| 591 | + String column, String partitionType, List<Object[]> expected, String predicate) { |
| 592 | + sql("DROP TABLE IF EXISTS %s", tableName); |
| 593 | + sql( |
| 594 | + "CREATE TABLE %s (col_int INTEGER, col_ts TIMESTAMP_NTZ, col_long BIGINT) USING ICEBERG TBLPROPERTIES ('format-version' = %d)", |
| 595 | + tableName, formatVersion); |
| 596 | + sql("INSERT INTO %s VALUES (1000, CAST('2024-03-01 19:25:00' as TIMESTAMP), 2100)", tableName); |
| 597 | + sql("ALTER TABLE %s ADD PARTITION FIELD %s AS col2_partition", tableName, partitionType); |
| 598 | + sql("INSERT INTO %s VALUES (2000, CAST('2024-04-01 19:25:00' as TIMESTAMP), 2200)", tableName); |
| 599 | + sql("ALTER TABLE %s DROP PARTITION FIELD col2_partition", tableName); |
| 600 | + sql("INSERT INTO %s VALUES (3000, CAST('2024-05-01 19:25:00' as TIMESTAMP), 2300)", tableName); |
| 601 | + sql("ALTER TABLE %s DROP COLUMN %s", tableName, column); |
| 602 | + |
| 603 | + assertEquals( |
| 604 | + "Should return correct data", |
| 605 | + expected, |
| 606 | + sql("SELECT * FROM %s WHERE %s ORDER BY col_int", tableName, predicate)); |
| 607 | + } |
| 608 | + |
| 609 | + @TestTemplate |
| 610 | + public void testDropPartitionAndUnderlyingField() { |
| 611 | + String predicateLong = "col_ts >= '2024-04-01 19:25:00'"; |
| 612 | + List<Object[]> expectedLong = |
| 613 | + Lists.newArrayList( |
| 614 | + new Object[] {2000, LocalDateTime.ofEpochSecond(1711999500, 0, ZoneOffset.UTC)}, |
| 615 | + new Object[] {3000, LocalDateTime.ofEpochSecond(1714591500, 0, ZoneOffset.UTC)}); |
| 616 | + runCreateAndDropPartitionField("col_long", "col_long", expectedLong, predicateLong); |
| 617 | + runCreateAndDropPartitionField( |
| 618 | + "col_long", "truncate(2, col_long)", expectedLong, predicateLong); |
| 619 | + runCreateAndDropPartitionField("col_long", "bucket(16, col_long)", expectedLong, predicateLong); |
| 620 | + |
| 621 | + String predicateTs = "col_long >= 2200"; |
| 622 | + List<Object[]> expectedTs = |
| 623 | + Lists.newArrayList(new Object[] {2000, 2200L}, new Object[] {3000, 2300L}); |
| 624 | + runCreateAndDropPartitionField("col_ts", "col_ts", expectedTs, predicateTs); |
| 625 | + runCreateAndDropPartitionField("col_ts", "year(col_ts)", expectedTs, predicateTs); |
| 626 | + runCreateAndDropPartitionField("col_ts", "month(col_ts)", expectedTs, predicateTs); |
| 627 | + runCreateAndDropPartitionField("col_ts", "day(col_ts)", expectedTs, predicateTs); |
| 628 | + } |
585 | 629 | }
|
0 commit comments