Repository navigation
Conversation
…dispatch Register serdes for the twelve ExtractANSIInterval* and ExtractInterval* expressions so they run through the JVM codegen dispatcher instead of falling back to Spark. This also keeps date +/- DAY interval in Comet, which Spark rewrites to DateAdd over ExtractANSIIntervalDays. Closes apache#6193
comphead
left a comment
There was a problem hiding this comment.
All twelve ExtractIntervalPart subclasses (ExtractANSIInterval{Years,Months,Days,Hours,Minutes,Seconds} and ExtractInterval{...}) are registered, and they match v3.5.8, v4.0.1 and v4.1.1 (same names, same IntervalUtils.getX bodies, DecimalType(8, 6) for seconds). The CometCodegenDispatch objects follow the existing CometMakeDTInterval pattern, so no shim is needed. The fixture covers year-month, day-time and calendar intervals, NULL, negatives and the exact Int and Long boundaries, and it replaces the deleted Scala case. The date +/- DAY interval rewrite (DateAdd(l, ExtractANSIIntervalDays(r)), with UnaryMinus for subtract) is covered. The PR adds twelve generated spark.comet.expression.Extract*Interval*.enabled keys through the enable_expr table, and the Implementation column of expressions.md stays generated, so only the hand-written notes change. I did not run anything. I could not run the cross-PR overlap query because gh was blocked, so I did not check the overlap with #5864 beyond what the PR body says.
sunchao
left a comment
There was a problem hiding this comment.
The new interval fixture passed on Spark 4.1.3. I found a correctness regression in date arithmetic when the date is NULL. I reproduced it for both signs and tested a fix in CometDateAdd.
Automated review by Codex.
sunchao
left a comment
There was a problem hiding this comment.
Thanks for fixing the null-date case. The updated SQL fixture passed on Spark 4.1.3. I compared a7230f82 with d7fd610b using the two rows in test_date_day_overflow. I found four more cases where the base matched Spark and this revision changes whether the query raises. I'd address those before merging. I also verified that the date subtraction example from #6193 already works here.
Automated review by Codex.
|
|
||
| object CometExtractANSIIntervalSeconds extends CometCodegenDispatch[ExtractANSIIntervalSeconds] | ||
|
|
||
| object CometExtractANSIIntervalYears extends CometCodegenDispatch[ExtractANSIIntervalYears] |
There was a problem hiding this comment.
Two extractors can now silently skip an ANSI overflow. I reproduced this on test_date_day_overflow with ANSI and subexpression elimination enabled.
SELECT extract(YEAR FROM make_ym_interval(year(d), n * 24)),
extract(MONTH FROM make_ym_interval(month(d), n * 24))
FROM test_date_day_overflowSpark computes the shared n * 24 before either null check and raises ARITHMETIC_OVERFLOW. Comet compiles each extractor separately, so both skip the multiplication when d is null. I got (NULL, NULL) and (2026, 0) instead. With subexpression elimination off, both engines return those rows.
This is related to #7480. Could we fall back when separate kernels would skip a shared expression that can throw, and cover both settings?
| object CometDateAdd extends CometScalarFunction[DateAdd]("date_add") | ||
| object CometDateAdd extends CometScalarFunction[DateAdd]("date_add") { | ||
| override def convert(expr: DateAdd, inputs: Seq[Attribute], binding: Boolean): Option[Expr] = { | ||
| withNullShortCircuit(expr, super.convert(expr, inputs, binding)) |
There was a problem hiding this comment.
The new guard fixes the single-use cast. It still evaluates too much when Spark shares just part of the expression.
SELECT n + 1, d + CAST(n + 1 AS INTERVAL DAY)
FROM test_date_day_overflowWith ANSI and subexpression elimination on, Spark shares only n + 1. It still skips the cast on the null date and returns (106751993, NULL) and (2, 2024-01-03). canSkipEvaluation finds the shared addition anywhere under the right operand and disables the guard for the entire operand. Comet then raises CAST_OVERFLOW.
Could we keep the unshared cast conditional, or fall back for partial sharing? The existing test where Spark shares the whole extractor should still raise.
|
|
||
| object CometMakeDTInterval extends CometCodegenDispatch[MakeDTInterval] | ||
|
|
||
| object CometExtractANSIIntervalDays extends CometCodegenDispatch[ExtractANSIIntervalDays] |
There was a problem hiding this comment.
This also enables projections where the parent evaluates the extractor on rows Spark skips. For example, with ANSI on,
SELECT date_sub(d, extract(DAY FROM CAST(n AS INTERVAL DAY)))
FROM test_date_day_overflowSpark returns NULL and 2023-12-31. Comet raises CAST_OVERFLOW. Explicit date_sub uses DateSub, so neither of the DateAdd tests covers it. I reproduced the same failure with year(d) + extract(...) and year(d) = extract(...).
Could we guard DateSub too and keep these projections on Spark wherever a native parent can evaluate a throwing extractor on skipped rows? CometMakeDate.unsupportedParentReason has a similar check.
| classOf[ExtractANSIIntervalMonths] -> CometExtractANSIIntervalMonths, | ||
| classOf[ExtractANSIIntervalSeconds] -> CometExtractANSIIntervalSeconds, | ||
| classOf[ExtractANSIIntervalYears] -> CometExtractANSIIntervalYears, | ||
| classOf[ExtractIntervalDays] -> CometExtractIntervalDays, |
There was a problem hiding this comment.
Calendar DAY extraction itself can throw even with ANSI off. With Comet shuffle enabled, AQE off, and the two fixture rows in one Parquet file, I ran
SET spark.sql.ansi.enabled=false;
SELECT n FROM test_date_day_overflow
ORDER BY n, extract(DAY FROM make_interval(0, 0, 0, 2147483647, n))
LIMIT 1Spark returns 1 because the first keys differ. Comet TopK evaluates both keys and hits integer overflow in IntervalUtils.getDays when it adds the whole days from the hours to Int.MaxValue. Replacing the second key with extract(DAY FROM CAST(n AS INTERVAL DAY)) gives CAST_OVERFLOW instead. With ANSI on, the existing fallback handles both.
Could we include these cases in raisesInAnyMode so the fallback for later sort keys (#7246) applies with ANSI off, and cover them in topk_later_key_ansi.sql?
Which issue does this PR close?
Closes #6193.
Rationale for this change
As the issue states, Spark lowers
extract(field FROM interval)anddate_part('field', interval)to one of twelveExtractIntervalPartsubclasses, and since none of them have a serde, a projection that uses one falls back to Spark.We want these twelve expressions to run through the JVM codegen dispatcher instead.
This also keeps
date +/- DAY intervalin Comet. Spark rewrites it toDateAdd(date, ExtractANSIIntervalDays(interval)), andDateAddis already native, so the extractor was the only reason those projections fell back.What changes are included in this PR?
ExtractANSIInterval*andExtractInterval*expressions asCometCodegenDispatchserdes indatetime.scalaandQueryPlanSerde.temporalExpressions. Each one is a singleIntervalUtils.getX(child)call, and the classes and result types are the same on every supported Spark version, so no shim is needed.CometDateAddinwithNullShortCircuit, so a NULL date skips the interval operand as it does in Spark. Without it, an out-of-rangeCAST(n AS INTERVAL DAY)on a row with a NULL date raisesCAST_OVERFLOWin Comet, where Spark returns NULL.extract,date_partanddatepartrows inexpressions.md.interval date_partcase fromexplain comet, which used it as an unsupported example.How are these changes tested?
A new SQL file test,
expressions/datetime/extract_interval.sql, checks every extractor withexpect_dispatch(...), usingextract,date_partanddatepart, over Parquet data with negative, NULL and out-of-range values. It covers:INTERVAL MONTHandINTERVAL HOUR TO SECONDdate +/- CAST(n AS INTERVAL DAY)anddate +/- INTERVAL '30' DAYfor theDateAddrewritedate +/- (d1 - d2)from the issue, over three date columns with NULLs and a negative difference. The dispatcher compilesSubtractDatesinto the extractor's kernel, so this does not depend on feat: route date and timestamp interval arithmetic through codegen dispatch #5864.+and-in separate projections, and still raisesCAST_OVERFLOWwhen both appear in one projection, where Spark shares the extractor and evaluates it for every row#5864 adds
+/-notes toexpressions.mdsaying a DAY interval column falls back becauseExtractANSIIntervalDayshas no serde. Whichever of the two PRs merges second should drop that sentence.