[ 
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

When Iceberg writes a data file, it records a bounding box for each GEOMETRY 
column: the smallest
rectangle containing all values of that column in the file. These are stored in 
the file's
lower_bounds / upper_bounds. Hive already writes them, and 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 these bounds when reading. A query with a spatial filter reads 
every data file,
even files whose data is far away from the queried area:

{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. Proposal

Pass spatial filters to Iceberg, so it can skip files whose bounding box 
doesn't overlap the queried
area (the "query window").

Filters to support:
* ST_Intersects between a GEOMETRY column and a constant geometry, with the 
arguments in either order.
* The ST_* functions don't accept GEOMETRY yet, so today the column appears as
  ST_GeomFromWKB(CAST(col AS BINARY)). This form should be recognized first. 
Once the ST_* functions
  support GEOMETRY directly (separate follow-up), ST_Intersects(col, window) 
should be recognized too.
* ST_Contains, ST_Within and ST_EnvIntersects can be added later in the same 
way. Each of them can only
  be true for rows that intersect the window, so the same file skipping applies.

How it works:
* The query window is a constant, so Hive computes its bounding box once, at 
compile time.
* Hive converts the filter into Iceberg's spatial predicate, 
Expressions.stIntersects(column, boundingBox).
  During scan planning, Iceberg compares this box with each file's bounding box 
and skips files where
  the two don't overlap.
* Hive passes filters to Iceberg today through SearchArgument, which only 
supports simple comparisons
  (=, <, IN, IS NULL, ...) and can't express a function call. Spatial filters 
need a separate conversion.
* Hive still evaluates the original ST_Intersects on every row it reads. A file 
that isn't skipped can
  still contain rows that don't match. Pushdown only makes the query faster; 
the results stay the same.

Combining with other conditions:
* AND: push down the parts that can be converted.
* OR: push down only if every branch can be converted. Otherwise a file could 
be skipped even though
  another branch matches rows in it.
* NOT: don't push down. A bounding box can prove that a file has no rows in the 
window, but not that all
  of its rows are in the window, so NOT ST_Intersects(...) can't be used to 
skip files.
* Anything that can't be converted isn't pushed down. The query still works, 
just without file skipping.

h3. Dependencies

This needs spatial predicates in Iceberg's public expression API, which Iceberg 
1.12.0 (used by Hive
today) doesn't have. This ticket is blocked until an Iceberg release includes 
them:
* apache/iceberg#14101: adds the ST_INTERSECTS / ST_DISJOINT predicates.
* apache/iceberg#17175: uses them to skip files during scan planning 
(InclusiveMetricsEvaluator).

h3. Out of scope

* CRS handling: the query window is assumed to be in the column's CRS. 
ST_GeomFromText doesn't carry
  a CRS, and Hive doesn't reproject.
* GEOGRAPHY: not covered until that type is added. Its bounding boxes can cross 
the antimeridian, which
  Iceberg's bounding-box check already handles.

h3. Testing

* Unit tests for converting Hive filters to Iceberg expressions: supported 
filters, both argument orders,
  combinations with 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 that:
** results are the same with pushdown on and off;
** files outside the query window are skipped (checked via EXPLAIN or the 
number of scan tasks).

  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.

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.


> 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
> When Iceberg writes a data file, it records a bounding box for each GEOMETRY 
> column: the smallest
> rectangle containing all values of that column in the file. These are stored 
> in the file's
> lower_bounds / upper_bounds. Hive already writes them, and 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 these bounds when reading. A query with a spatial filter 
> reads every data file,
> even files whose data is far away from the queried area:
> {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. Proposal
> Pass spatial filters to Iceberg, so it can skip files whose bounding box 
> doesn't overlap the queried
> area (the "query window").
> Filters to support:
> * ST_Intersects between a GEOMETRY column and a constant geometry, with the 
> arguments in either order.
> * The ST_* functions don't accept GEOMETRY yet, so today the column appears as
>   ST_GeomFromWKB(CAST(col AS BINARY)). This form should be recognized first. 
> Once the ST_* functions
>   support GEOMETRY directly (separate follow-up), ST_Intersects(col, window) 
> should be recognized too.
> * ST_Contains, ST_Within and ST_EnvIntersects can be added later in the same 
> way. Each of them can only
>   be true for rows that intersect the window, so the same file skipping 
> applies.
> How it works:
> * The query window is a constant, so Hive computes its bounding box once, at 
> compile time.
> * Hive converts the filter into Iceberg's spatial predicate, 
> Expressions.stIntersects(column, boundingBox).
>   During scan planning, Iceberg compares this box with each file's bounding 
> box and skips files where
>   the two don't overlap.
> * Hive passes filters to Iceberg today through SearchArgument, which only 
> supports simple comparisons
>   (=, <, IN, IS NULL, ...) and can't express a function call. Spatial filters 
> need a separate conversion.
> * Hive still evaluates the original ST_Intersects on every row it reads. A 
> file that isn't skipped can
>   still contain rows that don't match. Pushdown only makes the query faster; 
> the results stay the same.
> Combining with other conditions:
> * AND: push down the parts that can be converted.
> * OR: push down only if every branch can be converted. Otherwise a file could 
> be skipped even though
>   another branch matches rows in it.
> * NOT: don't push down. A bounding box can prove that a file has no rows in 
> the window, but not that all
>   of its rows are in the window, so NOT ST_Intersects(...) can't be used to 
> skip files.
> * Anything that can't be converted isn't pushed down. The query still works, 
> just without file skipping.
> h3. Dependencies
> This needs spatial predicates in Iceberg's public expression API, which 
> Iceberg 1.12.0 (used by Hive
> today) doesn't have. This ticket is blocked until an Iceberg release includes 
> them:
> * apache/iceberg#14101: adds the ST_INTERSECTS / ST_DISJOINT predicates.
> * apache/iceberg#17175: uses them to skip files during scan planning 
> (InclusiveMetricsEvaluator).
> h3. Out of scope
> * CRS handling: the query window is assumed to be in the column's CRS. 
> ST_GeomFromText doesn't carry
>   a CRS, and Hive doesn't reproject.
> * GEOGRAPHY: not covered until that type is added. Its bounding boxes can 
> cross the antimeridian, which
>   Iceberg's bounding-box check already handles.
> h3. Testing
> * Unit tests for converting Hive filters to Iceberg expressions: supported 
> filters, both argument orders,
>   combinations with 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 that:
> ** results are the same with pushdown on and off;
> ** files outside the query window are skipped (checked via EXPLAIN or the 
> number of scan tasks).



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

Reply via email to