[SPARK-58044][SQL] Support the TIME data type in the aggregate function avg#57338
Open
marcuslin123 wants to merge 2 commits into
Open
[SPARK-58044][SQL] Support the TIME data type in the aggregate function avg#57338marcuslin123 wants to merge 2 commits into
marcuslin123 wants to merge 2 commits into
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?
Add
TimeTypesupport to theavg/meanaggregate function. PreviouslyAVG()only accepted numeric and ANSI interval types and rejected TIME. Since TIME is internally aLongnumber of nanoseconds since midnight, it can be averaged like the interval types.The changes:
Average.scala: acceptAnyTimeTypeininputTypes; map a TIME input to a TIME output of the same precision inresultType; accumulate the sum asLongType; and add a TIME branch toevaluateExpressionthat divides the summed nanoseconds by the count and re-tags the result asTimeType(precision).PreciseTimestampConversionrather than a regular cast. A regularCAST(time AS BIGINT)is lossy for this purpose — it truncates to whole seconds — so it would corrupt the average.PreciseTimestampConversionreinterprets the underlyingLongnanoseconds without changing the value.TimeDivideexpression (inintervalExpressions.scala, modeled onDivideDTInterval) that divides aLong-nanosecond sum by the count with HALF_UP rounding and returns aTimeType(precision).DivideDTIntervalcould not be reused because its output type is hardcoded toDayTimeIntervalType, and there is noCAST(BIGINT AS TIME)to re-wrap the result.TypeUtils.checkForAnsiIntervalOrNumericOrTimeTypehelper.Sum's existingcheckForAnsiIntervalOrNumericTypeis intentionally left unchanged, sinceSumdoes not (yet) support TIME.The output preserves the input precision (e.g.
avgover aTIME(3)column yieldsTIME(3)). This mirrors howmin/max/percentilealready support TIME.Why are the changes needed?
TIME is orderable and quantifiable, and
min,max,percentile,approx_percentile, etc. already support it.avgwas an inconsistent gap: to average a TIME column, users had to cast to a numeric type, average, and cast back. Native support lets them writeavg(time_col)directly.Does this PR introduce any user-facing change?
Yes.
avg/meannow accepts TIME columns and returns a TIME average (same precision). Previously it raised aDATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPEerror.How was this patch tested?
Added a test in
DataFrameAggregateSuitecovering: basic average, sub-second rounding, NULL handling, all-null/empty input (returns NULL), precision preservation (TIME(3)), grouped average, and the SQLavgsurface over TIME literals. Updated the expected error message inExpressionTypeCheckingSuite(avg's required-type list now includes TIME). FullDataFrameAggregateSuite(168 tests) passes with no regressions, including the existing interval-avg test.Was this patch authored or co-authored using generative AI tooling?
Generative AI tooling (Claude Code) was used as an assistive tool for implementation guidance.