[SPARK-56594][SQL] Add time_bucket scalar function#55535
Open
vranes wants to merge 1 commit intoapache:masterfrom
Open
[SPARK-56594][SQL] Add time_bucket scalar function#55535vranes wants to merge 1 commit intoapache:masterfrom
vranes wants to merge 1 commit intoapache:masterfrom
Conversation
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.
What changes were proposed in this pull request?
This PR adds a new scalar SQL function
time_bucket(bucket_size, ts[, origin])that aligns a timestamp to the start of a fixed-size interval bucket. Given a bucket size (day-time or year-month interval), a timestamp, and an optional origin, it returns the start of the half-open bucket[start, start + bucket_size)containing the timestamp.Buckets are anchored at
origin(default1970-01-01 00:00:00(UTC for TIMESTAMP)) and the grid extends infinitely in both directions. All bucketing is performed on UTC micros; the session time zone does not affect bucket alignment. For local wall-clock alignment in a DST zone, users can cast the TIMESTAMP to TIMESTAMP_NTZ.Changes:
TimeBucketexpression insql/catalyst/.../expressions/datetimeExpressions.scalawith anExpressionBuilderthat dispatches to two- or three-argument forms.timeBucketDTInterval/timeBucketYMIntervalinDateTimeUtils.scala, with overflow checks (Math.subtractExact,Math.multiplyExact) on extreme timestamps and origins.FunctionRegistry.functions.time_bucket(...).pyspark.sql.functions.time_bucket+ Connect variant.Why are the changes needed?
Aligning timestamps to fixed-size buckets (15 minutes, 1 hour, 1 month, etc.) is a common time-series pattern, but today users must assemble it manually, e.g., via
date_truncfor calendar-aligned buckets or unix-timestamp arithmetic for fixed-second buckets, neither of which supports arbitrary year-month intervals or a non-default origin.A
time_bucketfunction matches the idiom popularized by PostgreSQL / TimescaleDB and makes the operation safe, concise, and composable.Does this PR introduce any user-facing change?
Yes, a new function
time_bucketis available in SQL, Scala, and PySpark.Example:
How was this patch tested?
DateExpressionsSuite(codegen + interpreted paths, DT and YM intervals,TIMESTAMP/TIMESTAMP_NTZ, NULL propagation, negative/zero bucket-size validation,ExpressionBuilder).DateTimeUtilsSuitefortimeBucketDTInterval/timeBucketYMIntervalincluding boundary values, negative timestamps, and extreme-origin overflow paths.sql-tests/inputs/time-bucket.sqlcovering: DT + YM interval buckets, TIMESTAMP + TIMESTAMP_NTZ, explicit origins, DST-safe NTZ-cast pattern (America/Los_Angeles), NULL propagation, invalid inputs (non-foldable, wrong types, non-positive).Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code