-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-46536][SQL] Support GROUP BY calendar_interval_type #44538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
9c06dd4
cc127e8
635a8a0
f071a49
52d52c2
df2a24d
533d176
5c4de2b
9095245
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,7 +44,7 @@ | |
| * @since 3.0.0 | ||
| */ | ||
| @Unstable | ||
| public final class CalendarInterval implements Serializable { | ||
| public final class CalendarInterval implements Serializable, Comparable<CalendarInterval> { | ||
| // NOTE: If you're moving or renaming this file, you should also update Unidoc configuration | ||
| // specified in 'SparkBuild.scala'. | ||
| public final int months; | ||
|
|
@@ -127,4 +127,15 @@ private void appendUnit(StringBuilder sb, long value, String unit) { | |
| * @throws ArithmeticException if a numeric overflow occurs | ||
| */ | ||
| public Duration extractAsDuration() { return Duration.of(microseconds, ChronoUnit.MICROS); } | ||
|
|
||
| @Override | ||
| public int compareTo(CalendarInterval o) { | ||
| if (this.months != o.months) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comparing intervals does not necessarily short circuits via months. We could result in Besides, 1 month can be 28 ~ 30 days, making the legacy calendar interval type uncomparable
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add some comments to explain that this is alphabet ordering. It does not have actual meaning but just makes it possible to find identical interval instances. We should do the same thing for map type so that we can group by map values.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @stefankandic did you generate this using IDEA?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the comments. @cloud-fan method was generated by intellij but I implemented the logic |
||
| return Integer.compare(this.months, o.months); | ||
| } else if (this.days != o.days) { | ||
| return Integer.compare(this.days, o.days); | ||
| } else { | ||
| return Long.compare(this.microseconds, o.microseconds); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.hive.execution | ||
|
|
||
| import org.apache.spark.sql._ | ||
| import org.apache.spark.sql.catalyst.expressions.ExpressionEvalHelper | ||
| import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper | ||
| import org.apache.spark.sql.functions._ | ||
| import org.apache.spark.sql.hive.test.TestHiveSingleton | ||
| import org.apache.spark.sql.test.SQLTestUtils | ||
| import org.apache.spark.tags.SlowHiveTest | ||
| import org.apache.spark.unsafe.types.CalendarInterval | ||
|
|
||
| @SlowHiveTest | ||
| class SortAggregateSuite | ||
| extends QueryTest | ||
| with SQLTestUtils | ||
| with TestHiveSingleton | ||
| with ExpressionEvalHelper | ||
| with AdaptiveSparkPlanHelper { | ||
|
|
||
| import testImplicits._ | ||
|
|
||
| test("SPARK-46536 Support GROUP BY CalendarIntervalType") { | ||
stefankandic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // forces the use of sort aggregate by using min/max functions | ||
|
|
||
| val numRows = 50 | ||
| val numRepeat = 25 | ||
|
|
||
| val df = (0 to numRows) | ||
| .map(i => Tuple1(new CalendarInterval(i, i, i))) | ||
| .toDF("c0") | ||
|
|
||
| for (_ <- 0 until numRepeat) { | ||
| val shuffledDf = df.orderBy(rand()) | ||
|
|
||
| checkAnswer( | ||
| shuffledDf.agg(max("c0")), | ||
| Row(new CalendarInterval(numRows, numRows, numRows)) | ||
| ) | ||
|
|
||
| checkAnswer( | ||
| shuffledDf.agg(min("c0")), | ||
| Row(new CalendarInterval(0, 0, 0)) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. Please check the behavior
#27262
I'm not sure. @yaooqinn