-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HIVE-27031: Addendum: Iceberg: Implement Copy-On-Write for Delete que…
…ries (Denys Kuzmenko, reviewed by Krisztian Kasa, Butao Zhang) Closes #4700
- Loading branch information
Showing
18 changed files
with
4,014 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...r/src/main/java/org/apache/iceberg/mr/hive/writer/HiveIcebergCopyOnWriteRecordWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.iceberg.mr.hive.writer; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.hadoop.io.Writable; | ||
import org.apache.iceberg.DataFile; | ||
import org.apache.iceberg.DataFiles; | ||
import org.apache.iceberg.PartitionSpec; | ||
import org.apache.iceberg.Schema; | ||
import org.apache.iceberg.data.GenericRecord; | ||
import org.apache.iceberg.data.Record; | ||
import org.apache.iceberg.deletes.PositionDelete; | ||
import org.apache.iceberg.io.ClusteredDataWriter; | ||
import org.apache.iceberg.io.DataWriteResult; | ||
import org.apache.iceberg.io.FileIO; | ||
import org.apache.iceberg.io.FileWriterFactory; | ||
import org.apache.iceberg.io.OutputFileFactory; | ||
import org.apache.iceberg.mr.hive.FilesForCommit; | ||
import org.apache.iceberg.mr.hive.IcebergAcidUtil; | ||
import org.apache.iceberg.mr.mapred.Container; | ||
import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
|
||
class HiveIcebergCopyOnWriteRecordWriter extends HiveIcebergWriterBase { | ||
|
||
private final int currentSpecId; | ||
|
||
private final GenericRecord rowDataTemplate; | ||
private final List<DataFile> referencedDataFiles; | ||
|
||
HiveIcebergCopyOnWriteRecordWriter(Schema schema, Map<Integer, PartitionSpec> specs, int currentSpecId, | ||
FileWriterFactory<Record> fileWriterFactory, OutputFileFactory fileFactory, FileIO io, | ||
long targetFileSize) { | ||
super(schema, specs, io, | ||
new ClusteredDataWriter<>(fileWriterFactory, fileFactory, io, targetFileSize)); | ||
this.currentSpecId = currentSpecId; | ||
this.rowDataTemplate = GenericRecord.create(schema); | ||
this.referencedDataFiles = Lists.newArrayList(); | ||
} | ||
|
||
@Override | ||
public void write(Writable row) throws IOException { | ||
Record record = ((Container<Record>) row).get(); | ||
PositionDelete<Record> positionDelete = IcebergAcidUtil.getPositionDelete(record, rowDataTemplate); | ||
int specId = IcebergAcidUtil.parseSpecId(record); | ||
Record rowData = positionDelete.row(); | ||
|
||
if (positionDelete.pos() < 0) { | ||
DataFile dataFile = | ||
DataFiles.builder(specs.get(specId)) | ||
.withPath(positionDelete.path().toString()) | ||
.withPartition(partition(rowData, specId)) | ||
.withFileSizeInBytes(0) | ||
.withRecordCount(0) | ||
.build(); | ||
referencedDataFiles.add(dataFile); | ||
} else { | ||
writer.write(rowData, specs.get(currentSpecId), partition(rowData, currentSpecId)); | ||
} | ||
} | ||
|
||
@Override | ||
public FilesForCommit files() { | ||
List<DataFile> dataFiles = ((DataWriteResult) writer.result()).dataFiles(); | ||
return FilesForCommit.onlyData(dataFiles, referencedDataFiles); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
iceberg/iceberg-handler/src/test/queries/positive/delete_iceberg_copy_on_write_partitioned.q
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
set hive.explain.user=false; | ||
|
||
drop table if exists tbl_ice; | ||
create external table tbl_ice(a int, b string, c int) partitioned by spec (bucket(16, a), truncate(3, b)) stored by iceberg tblproperties ('format-version'='2', 'write.delete.mode'='copy-on-write'); | ||
|
||
-- delete using simple predicates | ||
insert into tbl_ice values (1, 'one', 50), (2, 'two', 51), (3, 'three', 52), (4, 'four', 53), (5, 'five', 54), (111, 'one', 55), (333, 'two', 56); | ||
explain delete from tbl_ice where b in ('one', 'four') or a = 22; | ||
|
||
delete from tbl_ice where b in ('one', 'four') or a = 22; | ||
select * from tbl_ice order by a; | ||
-- (2, 'two', 51), (3, 'three', 52), (5, 'five', 54), (333, 'two', 56) | ||
|
||
-- delete using subqueries referencing the same table | ||
insert into tbl_ice values (444, 'hola', 800), (555, 'schola', 801); | ||
explain delete from tbl_ice where a in (select a from tbl_ice where a <= 5) or c in (select c from tbl_ice where c > 800); | ||
|
||
delete from tbl_ice where a in (select a from tbl_ice where a <= 5) or c in (select c from tbl_ice where c > 800); | ||
select * from tbl_ice order by a; | ||
-- (333, 'two', 56), (444, 'hola', 800) | ||
|
||
-- delete using a join subquery between the same table & another table | ||
drop table if exists tbl_ice_other; | ||
create external table tbl_ice_other(a int, b string) stored by iceberg; | ||
insert into tbl_ice_other values (10, 'ten'), (333, 'hundred'); | ||
explain delete from tbl_ice where a in (select t1.a from tbl_ice t1 join tbl_ice_other t2 on t1.a = t2.a); | ||
|
||
delete from tbl_ice where a in (select t1.a from tbl_ice t1 join tbl_ice_other t2 on t1.a = t2.a); | ||
select * from tbl_ice order by a; | ||
-- (444, 'hola', 800) | ||
|
||
-- delete using a join subquery between the same table & a non-iceberg table | ||
drop table if exists tbl_standard_other; | ||
create external table tbl_standard_other(a int, b string) stored as orc; | ||
insert into tbl_standard_other values (10, 'ten'), (444, 'tutu'); | ||
explain delete from tbl_ice where a in (select t1.a from tbl_ice t1 join tbl_ice_other t2 on t1.a = t2.a); | ||
|
||
delete from tbl_ice where a in (select t1.a from tbl_ice t1 join tbl_standard_other t2 on t1.a = t2.a); | ||
select count(*) from tbl_ice; | ||
-- 0 |
Oops, something went wrong.