Yuming Wang created SPARK-62378:
-----------------------------------
Summary: Support unwrap date to string type in
UnwrapCastInBinaryComparison
Key: SPARK-62378
URL: https://issues.apache.org/jira/browse/SPARK-62378
Project: Spark
Issue Type: Improvement
Components: SQL
Affects Versions: 5.0.0
Reporter: Yuming Wang
Extend {{UnwrapCastInBinaryComparison}} to handle {{DateType -> StringType}}
casts in binary comparisons, e.g.:
{code:sql}
SELECT * FROM t WHERE CAST(date_col AS STRING) < '2024-01-01'
{code}
Today this predicate is left as-is: every row pays for a {{Cast(Date, String)}}
(date formatting + {{UTF8String}} allocation) followed by a string comparison,
and the predicate can't be pushed down to sources that only understand {{Date}}
comparisons (e.g. Hive metastore partition pruning).
The rule rewrites it to a plain {{Date}}-to-{{Date}} comparison once the string
literal round-trips exactly through {{Cast(_, DateType)}} -> {{Cast(_,
StringType)}} (rejecting non-canonical literals like {{'2024-1-1'}}). For the
range predicates ({{<}}, {{<=}}, {{>}}, {{>=}}), {{DateType}}'s documented
{{[0001-01-01, 9999-12-31]}} range (not otherwise enforced by Spark) is
additionally guarded at runtime, since {{Cast(Date, String)}} sign-prefixes
out-of-range years under the default time parser policy. The rewrite is skipped
entirely under {{spark.sql.legacy.timeParserPolicy=LEGACY}}, where {{Cast(Date,
String)}} uses {{SimpleDateFormat}} and does not sign-prefix out-of-range
years, breaking the invariant the rewrite depends on.
A microbenchmark filtering 10M rows with {{CAST(date_col AS STRING) <
'literal'}} shows about a 6x speedup once the cast is unwrapped to a direct
date comparison (per-row cost drops from ~177ns to ~28ns), and the rewritten
predicate is also able to be pushed down (e.g. to Hive metastore partition
pruning) where the original {{Cast}}-based predicate could not.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]