[
https://issues.apache.org/jira/browse/HIVE-30252?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Gopinath reassigned HIVE-30252:
-------------------------------
Assignee: Gopinath
> Iceberg: A filter on a virtual column can lose rows, delete rows it does not
> match, or fail the query
> -----------------------------------------------------------------------------------------------------
>
> Key: HIVE-30252
> URL: https://issues.apache.org/jira/browse/HIVE-30252
> Project: Hive
> Issue Type: Bug
> Components: Iceberg integration
> Reporter: Denys Kuzmenko
> Assignee: Gopinath
> Priority: Major
> Labels: correctness, pull-request-available
>
> *Problem*
> {{HiveIcebergStorageHandler.decomposePredicate}} removes the children of the
> top-level filter expression that reference {{FILE__PATH}},
> {{PARTITION__SPEC__ID}}, {{PARTITION__KEY}}, {{PARTITION__NAME}},
> {{ROW__LINEAGE__ID}} or {{LAST__UPDATED__SEQUENCE__NUMBER}}. It does this
> without checking that the expression is an AND.
> Under an AND, this is right for a scan. Under any other root, the pushed
> predicate is wrong:
> * Under an OR, it holds for fewer rows. Iceberg prunes the data files that
> match only the removed part, and their rows are missing from the result.
> There is no error.
> * Under BETWEEN, IN or a comparison, it is malformed, and BETWEEN fails the
> query.
> The same narrowed predicate goes to
> {{SessionStateUtil.setConflictDetectionFilter}}, so a DML with such a WHERE
> clause checks too few files for conflicting concurrent writes.
> A DELETE that runs as a metadata delete uses the pushed predicate as its
> delete condition. So even under an AND, it deletes rows that the removed part
> excludes.
> *Reproducer*
> {code:sql}
> create table t_or(id int, v string) stored by iceberg stored as orc
> tblproperties('format-version'='2');
> insert into t_or values (1, 'a');
> insert into t_or values (2, 'b');
> select * from t_or where id = 1 or PARTITION__SPEC__ID = 0;
> select * from t_or where id = 1 or FILE__PATH is not null;
> select * from t_or where id = 1 or v = 'b';
> {code}
> * expected for all three: (1, a), (2, b)
> * actual: the first two return only (1, a); the third, without a virtual
> column, is correct
> The result is the same with {{hive.fetch.task.conversion=none}}. EXPLAIN of
> the first query:
> {noformat}
> filterExpr: (id = 1) (type: boolean)
> predicate: ((PARTITION__SPEC__ID = 0) or (id = 1)) (type: boolean)
> {noformat}
> A nested OR, such as {{id = 1 or (id = 2 and FILE__PATH = 'x')}}, is narrowed
> the same way.
> *More cases*
> These were found with qtests ({{TestIcebergCliDriver}}). They ran on the
> HIVE-30113 branch, where {{decomposePredicate}} differs from master only by
> the rename of {{PARTITION__HASH}} to {{PARTITION__KEY}}. Each table is ORC,
> format-version 2, with (1, 'a') and (2, 'b') in separate data files unless
> noted.
> *1. A SELECT loses rows from files that are read, not only from pruned
> files.* With both rows in one data file, {{select * from t where id = 1 or
> PARTITION__SPEC__ID = 0}} returns only (1, a). On a one-file table, the same
> DELETE and UPDATE as in 3 were correct, so DML tests need tables with several
> files.
> *2. Other ORs lose rows the same way.*
> * More than two operands: {{id = 1 or v = 'x' or PARTITION__SPEC__ID = 0}}
> returns only (1, a).
> * NOT of an AND, which Hive turns into an OR: {{not (id = 1 and
> PARTITION__SPEC__ID = 1)}} pushes {{id <> 1}} and returns only (2, b).
> *3. DML under an OR changes too few rows.*
> * {{delete from t where id = 1 or PARTITION__SPEC__ID = 0}} leaves (2, b).
> With {{hive.optimize.delete.metadata.only=true}}, the default, the metadata
> delete is built from the narrowed predicate, so only the files matching {{id
> = 1}} are deleted. This happens on merge-on-read and copy-on-write tables.
> With the metadata delete turned off, a merge-on-read DELETE also leaves (2,
> b).
> * {{update t set v = 'z' where id = 1 or PARTITION__SPEC__ID = 0}} updates
> only (1, a).
> *4. Other root functions get malformed operands.* The operands that read the
> column are removed whatever the root function is:
> * BETWEEN breaks the plan. For {{select * from t where PARTITION__SPEC__ID
> between 0 and 1}}, removing the column from {{GenericUDFBetween}}'s 4
> operands leaves 3, and {{getDisplayString}} throws
> {{ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3}}. In
> the qtest the query fails at compile time, where
> {{OpProcFactory.pushFilterToStorageHandler}} logs the pushed predicate at
> DEBUG. EXPLAIN calls {{getExprString}} too. So does
> {{HiveInputFormat.pushFilters}}, unconditionally; that one is from reading
> the code, not tested.
> * IN and comparisons push nonsense. {{PARTITION__SPEC__ID in (0, 1)}} pushes
> {{(0) IN (1)}}. {{PARTITION__SPEC__ID = id - 1}} pushes {{(id - 1)}} of type
> int. The results are correct only because the conversion to a search argument
> fails and Iceberg gets no filter.
> *5. A DELETE under an AND deletes rows that do not match.*
> With {{hive.optimize.delete.metadata.only=true}},
> {{DeleteSemanticAnalyzer.tryMetadataUpdate}} uses the TableScan
> {{filterExpr}} as the delete condition, and under an AND that filter leaves
> out the conjuncts that read a virtual column. When the remaining conjuncts
> match whole data files, by partition or by column metrics, the metadata
> delete removes those files, including the rows the left-out conjunct
> excludes. With one row per data file:
> {code:sql}
> delete from t where id = 1 and PARTITION__SPEC__ID = 1; -- expected: nothing
> deleted; actual: (1, a) deleted
> {code}
> {{FILE__PATH = 'no-such-file'}} in place of {{PARTITION__SPEC__ID = 1}} gives
> the same result, and so does a copy-on-write table. With the metadata delete
> off, both rows remain.
> Only a WHERE clause written by the user can do this. Hive's own rewrites
> (copy-on-write DELETE, UPDATE and MERGE, and compaction) filter only on
> {{FILE__PATH is not null}}, alone or as a conjunct of an AND, and they don't
> use the metadata delete.
> *Fix*
> PR #6971:
> * {{decomposePredicate}} removes only the conjuncts of a top-level AND that
> reference these columns, and pushes nothing when any other root references
> one.
> * {{DeleteSemanticAnalyzer}} does not use the metadata delete when the
> filter on the scan reads a virtual column.
> With it:
> * every query and DML above returns or changes the expected rows;
> * BETWEEN no longer fails;
> * IN and the comparison push no filter;
> * {{id = 1 and PARTITION__SPEC__ID = 0}} still pushes {{id = 1}}.
> {{iceberg_virtual_column_pushdown.q}} covers:
> * the selects under OR, NOT of an AND, BETWEEN and AND;
> * a DELETE under AND (merge-on-read and copy-on-write) and under OR;
> * an UPDATE under OR.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)