Skip to content

Commit 8375e74

Browse files
authored
Finalize split iceberg deduplication example (#107)
1 parent 1893ea6 commit 8375e74

10 files changed

Lines changed: 124 additions & 90 deletions

File tree

.github/workflows/build.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ jobs:
8888
- example: iceberg-dedup
8989
path: iceberg-data-deduplication
9090
test_commands: |
91-
compile data_deduplication_package.json
91+
compile data_compaction_package.json
92+
compile data_delete_package.json
9293
9394
- example: iot-sensor
9495
path: iot-sensor-metrics

iceberg-data-deduplication/data_deduplication.sqrl renamed to iceberg-data-deduplication/data_compaction.sqrl

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,38 @@ IMPORT tables.{{tableName}}.*;
55
IMPORT tables.compaction_tracker.*;
66
IMPORT udfs.read_partition_sizes;
77

8+
-- Collect previously compacted partitions
9+
_PrevCompactions :=
10+
SELECT partition_id,
11+
MAX(partition_time_bucket) AS partition_time_bucket
12+
FROM CompactionTracker
13+
WHERE `action` = 'overwrite'
14+
GROUP BY partition_id;
15+
816
_AllPartitions :=
9-
SELECT partition_map['{{partitionCol}}'] AS partition_id,
10-
CAST(partition_map['{{timeBucketCol}}'] AS BIGINT) AS time_bucket,
11-
partition_size
12-
FROM TABLE(read_partition_sizes(
13-
'{{warehouse}}', '{{catalogType}}', '{{catalogName}}', '{{databaseName}}', '{{tableName}}'));
17+
SELECT p.*
18+
FROM (
19+
SELECT partition_map['{{partitionCol}}'] AS partition_id,
20+
CAST(partition_map['{{timeBucketCol}}'] AS BIGINT) AS time_bucket,
21+
partition_size
22+
FROM TABLE(read_partition_sizes('{{warehouse}}', '{{catalogType}}', '{{catalogName}}', '{{databaseName}}', '{{tableName}}'))
23+
) p
24+
-- filter out partitions that have been compacted already
25+
LEFT JOIN _PrevCompactions pc ON pc.partition_id = p.partition_id
26+
WHERE pc.partition_id IS NULL
27+
OR pc.partition_time_bucket < p.time_bucket;
1428

1529
_Partitions :=
1630
SELECT * FROM _AllPartitions
17-
WHERE time_bucket <= FLOOR((CAST('${DEPLOYMENT_TIMESTAMP}' AS BIGINT) - {{bufferSeconds}}) / {{bucketSeconds}}) * {{bucketSeconds}};
31+
WHERE time_bucket <= FLOOR((CAST('${DEPLOYMENT_TIMESTAMP}' AS BIGINT) - {{bufferSeconds}} * 1000) / ({{bucketSeconds}} * 1000)) * {{bucketSeconds}} * 1000;
1832

1933
_PartitionSizing :=
2034
SELECT partition_id,
35+
time_bucket,
2136
SUM(CASE WHEN time_bucket = 0 THEN partition_size ELSE 0 END) AS base_size,
2237
SUM(CASE WHEN time_bucket > 0 THEN partition_size ELSE 0 END) AS new_size
2338
FROM _Partitions
24-
GROUP BY partition_id;
39+
GROUP BY partition_id, time_bucket;
2540

2641
_PartitionSizing.total_size := base_size + new_size;
2742
_PartitionSizing.new_rel_percentage := CAST(new_size AS DOUBLE) / GREATEST(total_size, 1) * 100;
@@ -30,6 +45,7 @@ _PartitionSizing.score := new_rel_percentage + new_abs_percentage;
3045

3146
_PartitionPriority :=
3247
SELECT partition_id,
48+
time_bucket,
3349
new_size,
3450
total_size,
3551
new_rel_percentage,
@@ -45,9 +61,10 @@ _PartitionsToCompact :=
4561
SELECT DISTINCT '{{tableName}}' AS table_name,
4662
'${DEPLOYMENT_ID}' AS job_id,
4763
partition_id,
64+
time_bucket,
4865
new_rel_percentage,
4966
new_abs_percentage,
50-
FLOOR((CAST('${DEPLOYMENT_TIMESTAMP}' AS BIGINT) - {{bufferSeconds}}) / {{bucketSeconds}}) * {{bucketSeconds}} AS max_time_bucket
67+
FLOOR((CAST('${DEPLOYMENT_TIMESTAMP}' AS BIGINT) - {{bufferSeconds}} * 1000) / ({{bucketSeconds}} * 1000)) * {{bucketSeconds}} * 1000 AS max_time_bucket
5168
FROM _PartitionPriority
5269
WHERE cumulative_total_size <= {{maxTotalPartitionSizeInMB}} * 1024 * 1024
5370
OR row_num <= 1;
@@ -69,7 +86,7 @@ INSERT OVERWRITE {{tableName}}
6986
SELECT * FROM _DistInputData
7087
-- filter only when "deleteFlagCol" has a value
7188
{{#deleteFlagCol}}
72-
WHERE {{deleteFlagCol}} IS NOT TRUE
89+
WHERE {{{deleteFlagCol}}} IS NOT TRUE
7390
{{/deleteFlagCol}}
7491
-- sort by partition column, and optionally any other column(s)
7592
ORDER BY {{partitionCol}} {{#sortKey}}, {{sortKey}}{{/sortKey}};
@@ -78,10 +95,12 @@ _CompactionResult :=
7895
SELECT CAST(table_name AS STRING),
7996
CAST(job_id AS STRING),
8097
CAST(partition_id AS STRING),
98+
'{{partitionCol}}' AS partition_col_name,
99+
time_bucket AS partition_time_bucket,
81100
new_rel_percentage,
82101
new_abs_percentage,
83102
max_time_bucket,
84-
'overwrite' AS action,
103+
'overwrite' AS `action`,
85104
NOW() AS action_time
86105
FROM _PartitionsToCompact;
87106

iceberg-data-deduplication/data_deduplication_package.json renamed to iceberg-data-deduplication/data_compaction_package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"version": "1",
33
"enabled-engines": ["flink"],
44
"script": {
5-
"main": "data_deduplication.sqrl",
5+
"main": "data_compaction.sqrl",
66
"config": {
77
"warehouse": "sqrl-iceberg-data",
88
"catalogType": "hadoop",

iceberg-data-deduplication/data_delete.sqrl

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,50 @@ _DataToDelete :=
55
SELECT table_name,
66
job_id,
77
partition_id,
8+
partition_col_name,
89
new_rel_percentage,
910
new_abs_percentage,
1011
max_time_bucket
1112
FROM (
1213
SELECT table_name,
1314
job_id,
1415
partition_id,
16+
partition_col_name,
1517
new_rel_percentage,
1618
new_abs_percentage,
1719
max_time_bucket,
18-
action,
20+
`action`,
1921
ROW_NUMBER() OVER (PARTITION BY table_name, partition_id ORDER BY action_time DESC) AS row_num
2022
FROM CompactionTracker
21-
WHERE action_time < NOW() - INTERVAL '{{commit_wait_sec}}' SECOND AND action_time > NOW() - INTERVAL '24' HOUR -- time buffer to ensure commit happened and stop attempting to delete
23+
WHERE action_time < NOW() - INTERVAL '{{commitWaitSec}}' SECOND {{stopDeleteFilter}} -- time buffer to ensure commit happened and stop attempting to delete based on config
2224
)
23-
WHERE row_num = 1 AND action <> 'delete'; -- compacted partitions haven't been deleted yet
25+
WHERE row_num = 1 AND `action` <> 'delete'; -- filter compacted partitions that haven't been deleted yet
2426

25-
--TODO: need extra safety: validate that the selected partitions should be deleted by checking that the last partition update time < partition 0 update time
27+
-- TODO: Validate that the selected partitions should be deleted by checking that the last partition update time < partition 0 update time
2628

2729
_DeleteFnResult :=
28-
SELECT delete_duplicated_data(
29-
'{{warehouse}}',
30-
'{{catalogType}}',
31-
'{{catalogName}}',
32-
'{{databaseName}}',
33-
MAX(table_name),
34-
MAX(max_time_bucket),
35-
COLLECT(MAP['{{partitionCol}}', partition_id])) AS res
30+
SELECT *, delete_duplicated_data(
31+
'{{warehouse}}',
32+
'{{catalogType}}',
33+
'{{catalogName}}',
34+
'{{databaseName}}',
35+
table_name,
36+
partition_col_name,
37+
partition_id,
38+
max_time_bucket) AS res
3639
FROM _DataToDelete;
3740

3841
_DeleteResult :=
39-
SELECT
40-
d.*,
41-
CASE WHEN fn.res = true THEN 'delete' ELSE 'delete_failed' END AS action,
42-
NOW() AS action_time
43-
FROM _DataToDelete AS d
44-
CROSS JOIN (SELECT res FROM _DeleteFnResult LIMIT 1) AS fn;
42+
SELECT table_name,
43+
job_id,
44+
partition_id,
45+
partition_col_name,
46+
CAST(NULL AS BIGINT) AS partition_time_bucket,
47+
new_rel_percentage,
48+
new_abs_percentage,
49+
max_time_bucket,
50+
CASE WHEN res = true THEN 'delete' ELSE 'delete_failed' END AS `action`,
51+
NOW() AS action_time
52+
FROM _DeleteFnResult;
4553

4654
INSERT INTO CompactionTracker SELECT * FROM _DeleteResult;

iceberg-data-deduplication/data_delete_package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"catalogType": "hadoop",
99
"catalogName": "mycatalog",
1010
"databaseName": "mydatabase",
11-
"partitionCol": "account_id",
12-
"commit_wait_sec": 1
11+
"commitWaitSec": 1,
12+
"stopDeleteFilter": ""
1313
}
1414
},
1515
"engines": {

iceberg-data-deduplication/tables/compaction_tracker.sqrl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
CREATE TABLE `CompactionTracker` (
2-
`table_name` STRING NOT NULL,
3-
`job_id` STRING NOT NULL,
4-
`partition_id` STRING NOT NULL,
5-
`new_rel_percentage` DOUBLE NOT NULL,
6-
`new_abs_percentage` DOUBLE NOT NULL,
7-
`max_time_bucket` BIGINT NOT NULL,
8-
`action` STRING NOT NULL,
9-
`action_time` TIMESTAMP(3) WITH LOCAL TIME ZONE NOT NULL
2+
`table_name` STRING NOT NULL,
3+
`job_id` STRING NOT NULL,
4+
`partition_id` STRING NOT NULL,
5+
`partition_col_name` STRING NOT NULL,
6+
`partition_time_bucket` BIGINT,
7+
`new_rel_percentage` DOUBLE NOT NULL,
8+
`new_abs_percentage` DOUBLE NOT NULL,
9+
`max_time_bucket` BIGINT NOT NULL,
10+
`action` STRING NOT NULL,
11+
`action_time` TIMESTAMP(3) WITH LOCAL TIME ZONE NOT NULL
1012
) PARTITIONED BY (`table_name`)
1113
WITH (
1214
'connector' = 'iceberg',
93 Bytes
Binary file not shown.

iceberg-data-deduplication/udfs/pom.xml

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
12
<project xmlns="http://maven.apache.org/POM/4.0.0"
2-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
45
<modelVersion>4.0.0</modelVersion>
56

67
<groupId>com.myudf</groupId>
78
<artifactId>myudf</artifactId>
89
<version>0.1.0-SNAPSHOT</version>
910

1011
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1113
<maven.compiler.source>17</maven.compiler.source>
1214
<maven.compiler.target>17</maven.compiler.target>
1315
</properties>
@@ -16,7 +18,7 @@
1618
<dependency>
1719
<groupId>org.projectlombok</groupId>
1820
<artifactId>lombok</artifactId>
19-
<version>1.18.42</version>
21+
<version>1.18.46</version>
2022
<scope>provided</scope>
2123
</dependency>
2224

@@ -29,7 +31,7 @@
2931
<dependency>
3032
<groupId>org.apache.flink</groupId>
3133
<artifactId>flink-table-api-java-bridge</artifactId>
32-
<version>1.19.3</version>
34+
<version>2.2.0</version>
3335
<scope>provided</scope>
3436
</dependency>
3537

@@ -42,9 +44,22 @@
4244

4345
<dependency>
4446
<groupId>org.apache.iceberg</groupId>
45-
<artifactId>iceberg-flink-runtime-1.19</artifactId>
46-
<version>1.9.2</version>
47+
<artifactId>iceberg-flink-runtime-2.0</artifactId>
48+
<version>1.10.1</version>
4749
<scope>provided</scope>
4850
</dependency>
4951
</dependencies>
52+
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<artifactId>maven-jar-plugin</artifactId>
58+
<version>3.4.2</version>
59+
<configuration>
60+
<outputDirectory>${project.basedir}</outputDirectory>
61+
</configuration>
62+
</plugin>
63+
</plugins>
64+
</build>
5065
</project>

0 commit comments

Comments
 (0)