Skip to content

feat: implement Spark-compatible weekday function#22740

Open
sjhddh wants to merge 1 commit into
apache:mainfrom
sjhddh:feat/spark-weekday
Open

feat: implement Spark-compatible weekday function#22740
sjhddh wants to merge 1 commit into
apache:mainfrom
sjhddh:feat/spark-weekday

Conversation

@sjhddh
Copy link
Copy Markdown

@sjhddh sjhddh commented Jun 3, 2026

Which issue does this PR close?

Rationale for this change

Implements the Spark weekday function as part of the datafusion-spark function library (#15914). Spark's weekday returns the day-of-week as a 0-indexed integer with Monday = 0 .. Sunday = 6, which differs from the existing dayofweek (1-indexed, Sunday = 1). This fills a gap for Spark-compatibility consumers.

What changes are included in this PR?

  • New SparkWeekDay scalar UDF in datafusion/spark/src/function/datetime/weekday.rs, modeled on the sibling monthname function.
  • Accepts Date and (implicitly coerced) Timestamp input; returns Int32.
  • Uses Arrow's DatePart::DayOfWeekMonday0 kernel, which is exactly 0=Monday .. 6=Sunday — a direct match for Spark semantics.
  • Null input propagates to null output for both scalar and array paths.
  • Registered in the datetime function mod.rs (UDF macro, doc export, functions() list).

Are these changes tested?

Yes:

  • Rust unit tests: return-field nullability, scalar evaluation (incl. null), and array evaluation.
  • sqllogictest coverage in 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.

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>
@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) spark labels Jun 3, 2026
signature: Signature::coercible(
vec![Coercion::new_implicit(
TypeSignatureClass::Native(logical_date()),
vec![TypeSignatureClass::Timestamp],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we need to accept coercion from string too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

spark sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Spark weekday

3 participants