-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[SPARK-57103][SQL] Add hashing for nanosecond timestamp types #56203
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ import org.apache.spark.sql.catalyst.expressions.codegen.GenerateMutableProjecti | |
| import org.apache.spark.sql.catalyst.util.{ArrayBasedMapData, CollationFactory, DateTimeUtils, GenericArrayData, IntervalUtils} | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.types.{ArrayType, StructType, _} | ||
| import org.apache.spark.unsafe.types.UTF8String | ||
| import org.apache.spark.unsafe.types.{TimestampNanosVal, UTF8String} | ||
| import org.apache.spark.util.ArrayImplicits._ | ||
|
|
||
| class HashExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { | ||
|
|
@@ -885,6 +885,50 @@ class HashExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { | |
| checkEvaluation(HiveHash(Seq(time)), -1567775210) | ||
| } | ||
|
|
||
| test("HashExpression supports nanosecond timestamp types") { | ||
| // (epochMicros, nanosWithinMicro) pairs covering zero/mid/max nanos, negative micros, and | ||
| // the Long epoch-micro boundaries. | ||
| val values = Seq( | ||
| TimestampNanosVal.fromParts(0L, 0.toShort), | ||
| TimestampNanosVal.fromParts(1L, 1.toShort), | ||
| TimestampNanosVal.fromParts(1234567890L, 999.toShort), | ||
| TimestampNanosVal.fromParts(-1L, 500.toShort), | ||
| TimestampNanosVal.fromParts(Long.MinValue, 0.toShort), | ||
| TimestampNanosVal.fromParts(Long.MaxValue, 999.toShort)) | ||
|
|
||
| Seq(TimestampNTZNanosType(9), TimestampLTZNanosType(9), | ||
| TimestampNTZNanosType(7), TimestampLTZNanosType(7)).foreach { dt => | ||
| (values.map(Literal.create(_, dt)) :+ Literal.create(null, dt)).foreach { lit => | ||
| // checkEvaluation asserts the interpreted, codegen, and unsafe paths all agree. | ||
|
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. Minor: with a |
||
| checkEvaluation(Murmur3Hash(Seq(lit), 42), Murmur3Hash(Seq(lit), 42).eval()) | ||
|
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. The |
||
| checkEvaluation(XxHash64(Seq(lit), 42L), XxHash64(Seq(lit), 42L).eval()) | ||
| checkEvaluation(HiveHash(Seq(lit)), HiveHash(Seq(lit)).eval()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("nanosecond timestamp hash is consistent with equality") { | ||
| val dt = TimestampNTZNanosType(9) | ||
| def lit(micros: Long, nanos: Short): Literal = | ||
| Literal.create(TimestampNanosVal.fromParts(micros, nanos), dt) | ||
|
|
||
| val a = lit(1234567890L, 123) | ||
| val aCopy = lit(1234567890L, 123) | ||
| val diffNanos = lit(1234567890L, 124) // same micros, different sub-micro nanos | ||
| val diffMicros = lit(1234567891L, 123) // different micros, same nanos | ||
|
|
||
| Seq[Expression => Any]( | ||
| e => Murmur3Hash(Seq(e), 42).eval(), | ||
| e => XxHash64(Seq(e), 42L).eval(), | ||
| e => HiveHash(Seq(e)).eval()).foreach { hash => | ||
| // Equal values hash equally. | ||
| assert(hash(a) === hash(aCopy)) | ||
| // Both fields contribute to the hash (guards against a dropped epochMicros/nanos field). | ||
| assert(hash(a) !== hash(diffNanos)) | ||
| assert(hash(a) !== hash(diffMicros)) | ||
| } | ||
| } | ||
|
|
||
| private def testHash(inputSchema: StructType): Unit = { | ||
| val inputGenerator = RandomDataGenerator.forType(inputSchema, nullable = false).get | ||
| val toRow = ExpressionEncoder(inputSchema).createSerializer() | ||
|
|
||
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.
All hash inputs here are
Literals, so the generated code embeds theTimestampNanosValas a reference object and the ordinal-read codegen path (CodeGenerator.getValue->getTimestampNTZNanos/getTimestampLTZNanos) is never exercised, and the value never round-trips through anUnsafeRowas a hash input.checkEvaluationWithUnsafeProjectionhere only projects the resultingint/longhash, not the nanos input.Since the motivation is hash-based GROUP BY / shuffle / joins (where the input is a
BoundReferencereading from a possibly-unsafe row), could we add aBoundReference-over-row case, e.g.:This drives the row read + unsafe round-trip that the literal-based tests skip.