deniskuzZ commented on code in PR #5123: URL: https://github.com/apache/hive/pull/5123#discussion_r1532337396
########## iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/compaction/IcebergMajorQueryCompactor.java: ########## @@ -44,22 +53,68 @@ public boolean run(CompactorContext context) throws IOException, HiveException, Map<String, String> tblProperties = context.getTable().getParameters(); LOG.debug("Initiating compaction for the {} table", compactTableName); - String compactionQuery = String.format("insert overwrite table %s select * from %<s", - compactTableName); + String partSpec = context.getCompactionInfo().partName; + String compactionQuery; + RewritePolicy rewritePolicy; + + if (partSpec == null) { + compactionQuery = String.format("insert overwrite table %s select * from %<s", + compactTableName); + rewritePolicy = RewritePolicy.ALL_PARTITIONS; + } else { + Table table = IcebergTableUtil.getTable(context.getConf(), context.getTable()); + PartitionData partitionData = DataFiles.data(table.spec(), partSpec); + context.getConf().set(Context.compactPartition, partSpec); + compactionQuery = String.format("insert overwrite table %1$s partition(%2$s) select * from %1$s where %3$s", + compactTableName, partDataToSQL(partitionData, partSpec, ","), + partDataToSQL(partitionData, partSpec, " and ")); + rewritePolicy = RewritePolicy.SINGLE_PARTITION; + } SessionState sessionState = setupQueryCompactionSession(context.getConf(), context.getCompactionInfo(), tblProperties); - HiveConf.setVar(context.getConf(), ConfVars.REWRITE_POLICY, RewritePolicy.ALL_PARTITIONS.name()); + HiveConf.setVar(context.getConf(), ConfVars.REWRITE_POLICY, rewritePolicy.name()); try { DriverUtils.runOnDriver(context.getConf(), sessionState, compactionQuery); LOG.info("Completed compaction for table {}", compactTableName); } catch (HiveException e) { - LOG.error("Error doing query based {} compaction", RewritePolicy.ALL_PARTITIONS.name(), e); + LOG.error("Error doing query based {} compaction", rewritePolicy.name(), e); throw new RuntimeException(e); } finally { sessionState.setCompaction(false); } return true; } + + private String partDataToSQL(PartitionData partitionData, String partSpec, String delimiter) { + StringBuilder sb = new StringBuilder(); + List<String> values = Arrays + .stream(partSpec.split("/")) + .map(x -> x.split("=")[1]) + .collect(Collectors.toList()); + + for (int i = 0; i < partitionData.size(); ++i) { Review Comment: why not use `Stream::collect(Collectors.joining())` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org