|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Altinity Inc and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + */ |
| 10 | +package com.altinity.ice.cli.internal.cmd; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.net.URISyntaxException; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | +import org.apache.iceberg.DataFile; |
| 17 | +import org.apache.iceberg.FileScanTask; |
| 18 | +import org.apache.iceberg.RewriteFiles; |
| 19 | +import org.apache.iceberg.Table; |
| 20 | +import org.apache.iceberg.TableScan; |
| 21 | +import org.apache.iceberg.catalog.TableIdentifier; |
| 22 | +import org.apache.iceberg.io.CloseableIterable; |
| 23 | +import org.apache.iceberg.rest.RESTCatalog; |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | + |
| 27 | +public final class Delete { |
| 28 | + |
| 29 | + private static final Logger logger = LoggerFactory.getLogger(Delete.class); |
| 30 | + |
| 31 | + private Delete() {} |
| 32 | + |
| 33 | + public static void run( |
| 34 | + RESTCatalog catalog, |
| 35 | + TableIdentifier tableId, |
| 36 | + List<com.altinity.ice.cli.Main.PartitionFilter> partitions, |
| 37 | + boolean dryRun) |
| 38 | + throws IOException, URISyntaxException { |
| 39 | + |
| 40 | + Table table = catalog.loadTable(tableId); |
| 41 | + TableScan scan = table.newScan(); |
| 42 | + if (partitions != null && !partitions.isEmpty()) { |
| 43 | + org.apache.iceberg.expressions.Expression expr = null; |
| 44 | + for (com.altinity.ice.cli.Main.PartitionFilter pf : partitions) { |
| 45 | + org.apache.iceberg.expressions.Expression e = null; |
| 46 | + for (Object value : pf.values()) { |
| 47 | + org.apache.iceberg.expressions.Expression valueExpr = |
| 48 | + org.apache.iceberg.expressions.Expressions.equal(pf.name(), value); |
| 49 | + e = (e == null) ? valueExpr : org.apache.iceberg.expressions.Expressions.or(e, valueExpr); |
| 50 | + } |
| 51 | + expr = (expr == null) ? e : org.apache.iceberg.expressions.Expressions.and(expr, e); |
| 52 | + } |
| 53 | + scan = scan.filter(expr); |
| 54 | + } |
| 55 | + List<DataFile> filesToDelete = new ArrayList<>(); |
| 56 | + |
| 57 | + try (CloseableIterable<FileScanTask> tasks = scan.planFiles()) { |
| 58 | + for (FileScanTask task : tasks) { |
| 59 | + filesToDelete.add(task.file()); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if (!filesToDelete.isEmpty()) { |
| 64 | + if (dryRun) { |
| 65 | + logger.info("Dry run: The following files would be deleted:"); |
| 66 | + for (DataFile file : filesToDelete) { |
| 67 | + logger.info(" {}", file.path()); |
| 68 | + } |
| 69 | + } else { |
| 70 | + RewriteFiles rewrite = table.newRewrite(); |
| 71 | + for (DataFile deleteFile : filesToDelete) { |
| 72 | + rewrite.deleteFile(deleteFile); |
| 73 | + } |
| 74 | + rewrite.commit(); |
| 75 | + logger.info("Partition(s) deleted."); |
| 76 | + } |
| 77 | + } else { |
| 78 | + logger.info("No files found for the partition(s)."); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments