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

Dmitriy Fingerman updated HIVE-30292:
-------------------------------------
    Description: 
Follow-up to HIVE-30240 (basic GEOMETRY data type support).

h3. Problem

Iceberg stores a per-file bounding box for each GEOMETRY column (lower_bounds / 
upper_bounds).
Hive already writes these bounds; they can be seen in the files metadata table:

{code:sql}
SELECT readable_metrics.geom.lower_bound, readable_metrics.geom.upper_bound 
FROM db.places.files;
{code}

Hive doesn't use them, so a spatial filter reads every data file:

{code:sql}
SELECT * FROM places
WHERE ST_Intersects(ST_GeomFromWKB(CAST(geom AS BINARY)),
                    ST_GeomFromText('POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))'));
{code}

h3. Proposed design

Push spatial filters on GEOMETRY columns down to Iceberg scan planning, so that 
files whose bounding
box doesn't intersect the query window are skipped.

h3. Proposal

* *Which filters are pushed down:* ST_Intersects between a GEOMETRY column and 
a constant geometry (the "query window"), with the arguments in either order.
** Today the ST_* functions don't accept GEOMETRY directly, so the column 
appears wrapped as ST_GeomFromWKB(CAST(col AS BINARY)). That wrapped form is 
what we need to recognize first.
** After the follow-up that adds native GEOMETRY support to the ST_* functions, 
the direct form ST_Intersects(col, window) must be recognized as well.
** ST_Contains, ST_Within and ST_EnvIntersects can be added later with the same 
logic. Each of them can only be true for a row that intersects the window, so 
files that don't intersect the window can be skipped for them too.
* *Query window bounding box:* the window is a constant, so Hive can compute 
its bounding box (min/max x and y) once at compile time, not per row.
* *Translating the filter for Iceberg:* convert the recognized filter into 
Iceberg's spatial predicate, Expressions.stIntersects(column, boundingBox). 
Iceberg compares that box with each file's stored bounding box during scan 
planning and skips files where the two don't overlap.
** Hive currently passes filters to Iceberg as a SearchArgument, which only 
supports comparisons like =, <, IN and IS NULL and can't carry a function call. 
Spatial filters need a separate translation step.
* *The original filter still runs:* bounding boxes only rule out whole files. A 
file that isn't skipped can still contain rows that don't match, so Hive must 
keep evaluating the original ST_Intersects on every row it reads. Pushdown 
makes the query faster but never changes its result.
* *Combining with other conditions:* the result must stay correct when the 
spatial filter is mixed with other filters.
** AND: push down whichever parts can be converted.
** OR: push down only if every branch can be converted. Otherwise a file could 
be skipped even though another branch would match rows in it.
** NOT: don't push down. A bounding box can show that a file has no rows inside 
the window, but never that all of its rows are inside it, so NOT 
ST_Intersects(...) can't be used to skip files.
** Anything that can't be converted simply isn't pushed down. The query still 
works, just without file skipping.

h3. Dependencies

* Iceberg spatial predicates in the public expression API:
** apache/iceberg#14101 (ST_INTERSECTS / ST_DISJOINT)
** apache/iceberg#17175 (pruning in InclusiveMetricsEvaluator)
* Neither is available in Iceberg 1.12.0, which Hive uses today. This ticket is 
blocked until a release includes them.

h3. Scope notes

* *CRS:* the query window is assumed to be in the column's CRS. ST_GeomFromText 
has no CRS, and Hive does no reprojection.
* *GEOGRAPHY:* out of scope until that type exists. Its bounding boxes can wrap 
around the antimeridian; Iceberg's evaluator already handles that.

h3. Testing

* Unit tests for the Hive-expression-to-Iceberg-expression conversion: 
supported shapes, argument order, nested AND/OR/NOT, and filters that must not 
be converted.
* A q-file test with several data files whose bounding boxes don't overlap, 
checking:
** query results are unchanged with pushdown on and off;
** non-matching files are skipped, through EXPLAIN or scan task counts.

  was:
Follow-up to HIVE-30240 (basic GEOMETRY data type support).

h3. Problem

Iceberg stores a per-file bounding box for each GEOMETRY column (lower_bounds / 
upper_bounds).
Hive already writes these bounds; they can be seen in the files metadata table:

{code:sql}
SELECT readable_metrics.geom.lower_bound, readable_metrics.geom.upper_bound 
FROM db.places.files;
{code}

Hive doesn't use them, so a spatial filter reads every data file:

{code:sql}
SELECT * FROM places
WHERE ST_Intersects(ST_GeomFromWKB(CAST(geom AS BINARY)),
                    ST_GeomFromText('POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))'));
{code}

h3. Proposed design

Push spatial filters on GEOMETRY columns down to Iceberg scan planning, so that 
files whose bounding
box doesn't intersect the query window are skipped.

* *Recognize the predicate:* ST_Intersects(<geometry column>, <constant 
geometry>), in either argument order.
** For now this means the ST_GeomFromWKB(CAST(col AS BINARY)) wrapper.
** After the ESRI-functions follow-up, also the direct form ST_Intersects(col, 
...).
** Candidates for later: ST_Contains, ST_Within and ST_EnvIntersects. The 
column side must intersect the window for all of them, so they can be pruned 
with the same bounding-box test.
* *Build the query window:* compute the constant's bounding box at compile time.
* *Convert the filter:* translate it to Iceberg's spatial predicate 
(Expressions.stIntersects(column, BoundingBox)) in the Iceberg filter 
conversion.
** Iceberg's existing filter path goes through SearchArgument, which can't 
express this, so it needs a separate conversion path.
* *Keep the original filter:* pruning only skips whole files, so the exact 
ST_Intersects must still run on the remaining rows.
* *Pass through safely:* AND/OR/NOT should combine correctly. Anything that 
can't be converted is simply not pushed down.

h3. Dependencies

* Iceberg spatial predicates in the public expression API:
** apache/iceberg#14101 (ST_INTERSECTS / ST_DISJOINT)
** apache/iceberg#17175 (pruning in InclusiveMetricsEvaluator)
* Neither is available in Iceberg 1.12.0, which Hive uses today. This ticket is 
blocked until a release includes them.

h3. Scope notes

* *CRS:* the query window is assumed to be in the column's CRS. ST_GeomFromText 
has no CRS, and Hive does no reprojection.
* *GEOGRAPHY:* out of scope until that type exists. Its bounding boxes can wrap 
around the antimeridian; Iceberg's evaluator already handles that.

h3. Testing

* Unit tests for the Hive-expression-to-Iceberg-expression conversion: 
supported shapes, argument order, nested AND/OR/NOT, and filters that must not 
be converted.
* A q-file test with several data files whose bounding boxes don't overlap, 
checking:
** query results are unchanged with pushdown on and off;
** non-matching files are skipped, through EXPLAIN or scan task counts.


> Iceberg: Spatial predicate pushdown for GEOMETRY columns
> --------------------------------------------------------
>
>                 Key: HIVE-30292
>                 URL: https://issues.apache.org/jira/browse/HIVE-30292
>             Project: Hive
>          Issue Type: New Feature
>            Reporter: Dmitriy Fingerman
>            Assignee: Dmitriy Fingerman
>            Priority: Major
>
> Follow-up to HIVE-30240 (basic GEOMETRY data type support).
> h3. Problem
> Iceberg stores a per-file bounding box for each GEOMETRY column (lower_bounds 
> / upper_bounds).
> Hive already writes these bounds; they can be seen in the files metadata 
> table:
> {code:sql}
> SELECT readable_metrics.geom.lower_bound, readable_metrics.geom.upper_bound 
> FROM db.places.files;
> {code}
> Hive doesn't use them, so a spatial filter reads every data file:
> {code:sql}
> SELECT * FROM places
> WHERE ST_Intersects(ST_GeomFromWKB(CAST(geom AS BINARY)),
>                     ST_GeomFromText('POLYGON ((0 0, 10 0, 10 10, 0 10, 0 
> 0))'));
> {code}
> h3. Proposed design
> Push spatial filters on GEOMETRY columns down to Iceberg scan planning, so 
> that files whose bounding
> box doesn't intersect the query window are skipped.
> h3. Proposal
> * *Which filters are pushed down:* ST_Intersects between a GEOMETRY column 
> and a constant geometry (the "query window"), with the arguments in either 
> order.
> ** Today the ST_* functions don't accept GEOMETRY directly, so the column 
> appears wrapped as ST_GeomFromWKB(CAST(col AS BINARY)). That wrapped form is 
> what we need to recognize first.
> ** After the follow-up that adds native GEOMETRY support to the ST_* 
> functions, the direct form ST_Intersects(col, window) must be recognized as 
> well.
> ** ST_Contains, ST_Within and ST_EnvIntersects can be added later with the 
> same logic. Each of them can only be true for a row that intersects the 
> window, so files that don't intersect the window can be skipped for them too.
> * *Query window bounding box:* the window is a constant, so Hive can compute 
> its bounding box (min/max x and y) once at compile time, not per row.
> * *Translating the filter for Iceberg:* convert the recognized filter into 
> Iceberg's spatial predicate, Expressions.stIntersects(column, boundingBox). 
> Iceberg compares that box with each file's stored bounding box during scan 
> planning and skips files where the two don't overlap.
> ** Hive currently passes filters to Iceberg as a SearchArgument, which only 
> supports comparisons like =, <, IN and IS NULL and can't carry a function 
> call. Spatial filters need a separate translation step.
> * *The original filter still runs:* bounding boxes only rule out whole files. 
> A file that isn't skipped can still contain rows that don't match, so Hive 
> must keep evaluating the original ST_Intersects on every row it reads. 
> Pushdown makes the query faster but never changes its result.
> * *Combining with other conditions:* the result must stay correct when the 
> spatial filter is mixed with other filters.
> ** AND: push down whichever parts can be converted.
> ** OR: push down only if every branch can be converted. Otherwise a file 
> could be skipped even though another branch would match rows in it.
> ** NOT: don't push down. A bounding box can show that a file has no rows 
> inside the window, but never that all of its rows are inside it, so NOT 
> ST_Intersects(...) can't be used to skip files.
> ** Anything that can't be converted simply isn't pushed down. The query still 
> works, just without file skipping.
> h3. Dependencies
> * Iceberg spatial predicates in the public expression API:
> ** apache/iceberg#14101 (ST_INTERSECTS / ST_DISJOINT)
> ** apache/iceberg#17175 (pruning in InclusiveMetricsEvaluator)
> * Neither is available in Iceberg 1.12.0, which Hive uses today. This ticket 
> is blocked until a release includes them.
> h3. Scope notes
> * *CRS:* the query window is assumed to be in the column's CRS. 
> ST_GeomFromText has no CRS, and Hive does no reprojection.
> * *GEOGRAPHY:* out of scope until that type exists. Its bounding boxes can 
> wrap around the antimeridian; Iceberg's evaluator already handles that.
> h3. Testing
> * Unit tests for the Hive-expression-to-Iceberg-expression conversion: 
> supported shapes, argument order, nested AND/OR/NOT, and filters that must 
> not be converted.
> * A q-file test with several data files whose bounding boxes don't overlap, 
> checking:
> ** query results are unchanged with pushdown on and off;
> ** non-matching files are skipped, through EXPLAIN or scan task counts.



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

Reply via email to