Weiwei Su created SPARK-62271:
---------------------------------
Summary: Push down conservative literal prefix StartsWith for LIKE
patterns containing '_' to enable DSv2 / Iceberg file pruning
Key: SPARK-62271
URL: https://issues.apache.org/jira/browse/SPARK-62271
Project: Spark
Issue Type: Bug
Components: Optimizer, SQL
Affects Versions: 3.5.5
Environment: Apache Spark 3.5.5 + Apache Iceberg 1.11.0 (REST Catalog,
format-version 2); cross-checked against Trino 470 and DuckDB 1.5.5 on the same
frozen Iceberg snapshot
Reporter: Weiwei Su
h3. Problem Statement
When a query filters a string column on an Apache Iceberg (or other DataSource
V2) table using a {{LIKE}} pattern that starts with a non-empty literal prefix
followed by an underscore {{_}} (either as an unescaped single-character
wildcard or an escaped literal {{\_}}), Spark Catalyst does not push any prefix
predicate down to the DataSource V2 {{ScanBuilder}}. Instead, {{BatchScan}}
receives only {{<col> IS NOT NULL}}, forcing a full scan of all data files even
when Iceberg column-level {{lower_bounds}} / {{upper_bounds}} metrics can prune
the majority of files.
By contrast, *Trino 470* and *DuckDB 1.5.5* querying the exact same frozen
Iceberg table snapshot extract the literal prefix before the first wildcard and
push a conservative range/prefix filter directly into the Iceberg scan, while
retaining the full {{LIKE}} predicate as a residual post-scan filter for
exactness.
h3. Minimal Reproducer
{code:sql}
CREATE TABLE iceberg.default.t_like_repro (
id BIGINT,
city STRING
) USING iceberg;
INSERT INTO iceberg.default.t_like_repro VALUES
(1, 'prefix_16_bytes_alpha_001'),
(2, 'prefix_16_bytes_omega_999'),
(3, 'other_city_001'),
(4, 'zeta_city_002');
-- Case A: No underscore -> Spark pushes StringStartsWith('prefix') to Iceberg
BatchScan
EXPLAIN FORMATTED
SELECT id, city
FROM iceberg.default.t_like_repro
WHERE city LIKE 'prefix%';
-- Case B: Contains '_' -> Spark drops prefix pushdown; BatchScan only receives
`city IS NOT NULL`
EXPLAIN FORMATTED
SELECT id, city
FROM iceberg.default.t_like_repro
WHERE city LIKE 'prefix_16_bytes_alpha%';
{code}
h3. Observed Cross-Engine Physical Plans (Case B)
*Apache Spark 3.5.5 + Iceberg 1.11.0* (drops prefix pushdown completely):
{code:text}
(1) BatchScan iceberg.default.t_main
iceberg.default.t_main (branch=null) [filters=city IS NOT NULL, groupedBy=]
(3) Filter
Condition : (isnotnull(city) AND city LIKE prefix_16_bytes_alpha%)
{code}
*Trino 470* (same Iceberg snapshot — extracts literal prefix {{'prefix'}} into
a conservative range filter pushed to scan while keeping {{$like(...)}} as
residual):
{code:text}
ScanFilter[
table = iceberg:default.t_main$data@...,
filterPredicate = ((city >= varchar 'prefix') AND (city < varchar 'prefiy')
AND $like(...))
]
{code}
*DuckDB 1.5.5* (same Iceberg snapshot — pushes filter directly into
{{ICEBERG_SCAN}}).
h3. Root Cause & Context
# Catalyst's {{LikeSimplification}} converts simple {{LIKE 'abc%'}} patterns
into {{StartsWith(col, 'abc')}}, which DSv2 translates into {{STARTS_WITH}} for
Iceberg manifest/file pruning.
# When the pattern contains {{_}} (unescaped {{_}} or escaped {{\_}}),
{{LikeSimplification}} skips simplification:
#* For escaped characters, SPARK-33677 skipped {{LikeSimplification}} when
{{escapeChar}} is present, and SPARK-38168 was closed as Won't Fix before DSv2
/ Iceberg file pruning became the dominant cost factor (see discussion in
https://github.com/apache/iceberg/issues/14995).
#* For unescaped {{_}} (e.g. {{LIKE 'prefix_16_bytes_alpha%'}}), the pattern is
not equivalent to {{StartsWith}} alone, so {{LikeSimplification}} cannot
replace {{Like}} in-place — and Spark has no rule deriving a conservative
auxiliary {{StartsWith}} predicate for DSv2 pushdown.
h3. Proposed Enhancement
In {{V2ScanRelationPushDown}} / {{DataSourceStrategy}} (or as a non-destructive
predicate derivation during filter pushdown):
# For {{Like(attr, Literal(pattern), escapeChar)}} where {{pattern}} begins
with a non-empty literal prefix {{P}} before the first wildcard ({{%}}, {{_}},
or {{escapeChar}}), emit an auxiliary pushdown predicate {{StartsWith(attr,
P)}} (DSv2 {{STARTS_WITH}}) to
{{SupportsPushDownV2Filters.pushPredicates(...)}} (guarded on binary /
UTF8_BINARY collation, matching existing {{StartsWith}} pushdown).
# Always retain the original {{Like(...)}} expression as a post-scan residual
{{Filter}} so query semantics remain 100% identical.
h3. Impact
Eliminates full-table / full-partition scans on Iceberg and Parquet DSv2 tables
for real-world {{LIKE}} queries over snake_case identifiers and file paths
containing {{_}}, bringing Spark DSv2 pushdown to parity with Trino and DuckDB.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]