[ 
https://issues.apache.org/jira/browse/HIVE-30252?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Denys Kuzmenko updated HIVE-30252:
----------------------------------
    Labels: correctness pull-request-available  (was: pull-request-available)

> Iceberg: Pushing down a filter that reads a virtual column outside a 
> top-level AND loses rows or fails
> ------------------------------------------------------------------------------------------------------
>
>                 Key: HIVE-30252
>                 URL: https://issues.apache.org/jira/browse/HIVE-30252
>             Project: Hive
>          Issue Type: Bug
>          Components: Iceberg integration
>            Reporter: Denys Kuzmenko
>            Priority: Minor
>              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}}, without 
> checking that the expression is an AND. For an OR this narrows the pushed 
> predicate: Iceberg prunes the data files that match only the    
>   removed part, and their rows are missing from the result. There is no 
> error.                                                                        
>                                                            
>                                                                               
>                                                                               
>                                                      
>   The same narrowed predicate goes to 
> {{SessionStateUtil.setConflictDetectionFilter}}, so a DML with such a WHERE 
> clause checks too few files for conflicting concurrent writes.                
>                  
>                                                                               
>                                                                               
>                                                      
>   *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.                                                        
>                                                             
>                                                                               
>                                                                               
>                                                      
>   *Proposed fix*                                                              
>                                                                               
>                                                      
>   Remove only the conjuncts of a top-level AND that reference these columns. 
> Push nothing when any other expression references one.     
> *More cases*, 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 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, 
> {{DeleteSemanticAnalyzer.tryMetadataUpdate}} builds the metadata delete from 
> the TableScan {{filterExpr}}. That is 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.
> *Fix.* PR #6971, which pushes nothing when a root other than AND reads a 
> skipped virtual column, fixes all of the above. With it applied:
>  * every query and DML above returns 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}}.
> The PR's qtest covers the two OR selects and the AND. BETWEEN, NOT of an AND, 
> DELETE (metadata delete on and off) and UPDATE with an OR should be added, on 
> tables with several data files.
>                                                                          



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to