feat: implement Spark-compatible weekday function#22740
Open
sjhddh wants to merge 1 commit into
Open
Conversation
Implements the Spark `weekday` scalar function (issue apache#22599), returning the day of the week as a 0-indexed integer where Monday = 0 .. Sunday = 6. This differs from `dayofweek` (1-indexed, Sunday = 1). The implementation is modeled on the sibling `monthname` function and uses Arrow's `DatePart::DayOfWeekMonday0` kernel, which returns 0..=6 with Monday = 0 -- an exact match for Spark semantics. It accepts Date and implicitly coerces Timestamp input, returns Int32, and propagates null input to null output. Adds Rust unit tests and sqllogictest coverage for scalar/array/timestamp/ null/error cases. Closes apache#22599 Signed-off-by: sjhddh <151469562+sjhddh@users.noreply.github.com>
Jefffrey
approved these changes
Jun 4, 2026
| signature: Signature::coercible( | ||
| vec![Coercion::new_implicit( | ||
| TypeSignatureClass::Native(logical_date()), | ||
| vec![TypeSignatureClass::Timestamp], |
Contributor
There was a problem hiding this comment.
i think we need to accept coercion from string too?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
weekday#22599.Rationale for this change
Implements the Spark
weekdayfunction as part of the datafusion-spark function library (#15914). Spark'sweekdayreturns the day-of-week as a 0-indexed integer with Monday = 0 .. Sunday = 6, which differs from the existingdayofweek(1-indexed, Sunday = 1). This fills a gap for Spark-compatibility consumers.What changes are included in this PR?
SparkWeekDayscalar UDF indatafusion/spark/src/function/datetime/weekday.rs, modeled on the siblingmonthnamefunction.Int32.DatePart::DayOfWeekMonday0kernel, which is exactly 0=Monday .. 6=Sunday — a direct match for Spark semantics.mod.rs(UDF macro, doc export,functions()list).Are these changes tested?
Yes:
datafusion/sqllogictest/test_files/spark/datetime/weekday.slt: scalar dates for all 7 weekdays, array input, TIMESTAMP / TIMESTAMP_NTZ / LTZ coercion, null handling, and argument-type / zero-argument error cases. The original PySpark 3.5.5 reference (weekday('2009-07-30') = 3) is preserved and validated.Are there any user-facing changes?
Adds the new Spark-compatible scalar function
weekday. No breaking changes.Note: I noticed there is an existing draft PR #22601 for the same function (last updated 2026-05-28). It appears to be a stale draft, so I've opened this as a complete, tested implementation — happy to defer or collaborate if the draft author is still active.