joelrobin18 opened a new pull request, #60219:
URL: https://github.com/apache/spark/pull/60219
### What changes were proposed in this pull request?
This PR adds hash support for GEOMETRY and GEOGRAPHY values. It also fixes
the generated comparison code for these types.
1. `InterpretedHashFunction.hash` has a new case for `BinaryView`.
`BinaryView` is the physical value of GEOMETRY and GEOGRAPHY. The hash uses the
physical bytes of the value.
2. `HashExpression.computeHashWithTailRec` has a new case for `GeometryType`
and `GeographyType`. The generated code hashes the same bytes as the
interpreted path.
3. `HiveHash` overrides the new `genHashBinaryView` method, because
`HiveHasher` does not use a seed.
4. `CodegenContext.genComp` generates `compareTo` for `GeometryType` and
`GeographyType`. Before this change, it generated `compare`. `BinaryView` has
0`GeometryType` and `GeographyType`. Before this change, it generated
`compare`. `BinaryView` has no `compare` method. The interpreted ordering in
`PhysicalBinaryViewType` already uses `compareTo`.
This change does not make GEOMETRY or GEOGRAPHY orderable in SQL. `ORDER BY`
and comparison operators on these types still fail with
`DATATYPE_MISMATCH.INVALID_ORDERING_TYPE`.
### Why are the changes needed?
The analyzer accepts `GROUP BY`, `DISTINCT`, `DISTRIBUTE BY`,
`approx_count_distinct` and the hash functions on GEOMETRY and GEOGRAPHY
values. The hash code has no case for these types. Thus these queries fail with
`scala.MatchError` or `INTERNAL_ERROR`.
Example:
```sql
SELECT g, COUNT(*) AS n
FROM (SELECT ST_GeomFromWKB(X'0101000000000000000000F03F0000000000000040',
4326) AS g FROM range(10))
GROUP BY g;
```
Result before this PR:
```
org.apache.spark.SparkException: Job aborted due to stage failure: ...
scala.MatchError: org.apache.spark.unsafe.types.BinaryView@15dc1861 (of
class org.apache.spark.unsafe.types.BinaryView)
at
org.apache.spark.sql.catalyst.expressions.InterpretedHashFunction.hash(hash.scala:713)
```
A `GROUP BY` on a geometry key also fails when the hash aggregate spills to
the sort-based fallback. This failure occurs also with one partition:
```
org.codehaus.commons.compiler.CompileException: ... A method named "compare"
is not declared ...
at
org.apache.spark.sql.catalyst.expressions.codegen.GenerateOrdering$.create(GenerateOrdering.scala:192)
at
org.apache.spark.sql.execution.UnsafeKVExternalSorter.<init>(UnsafeKVExternalSorter.java:84)
```
`CollationAggregationSuite` (SPARK-58847) expects `GROUP BY key, geometry`
to return a correct result. That test calls `repartition(1)` first. With one
partition, the plan has no hash shuffle, so the test does not find this problem.
### Does this PR introduce _any_ user-facing change?
Yes. Queries that failed with `scala.MatchError` or `INTERNAL_ERROR` now
return a correct result:
- `GROUP BY` on a GEOMETRY or GEOGRAPHY column
- `COUNT(DISTINCT ...)` and `SELECT DISTINCT` on these columns
- `DISTRIBUTE BY` on these columns
- `approx_count_distinct` on these columns
- `hash`, `xxhash64` and `hive-hash` on these values
Two values are in the same group when their physical bytes are equal. This
is the same equality that the hash aggregate already uses for grouping keys.
### How was this patch tested?
New tests:
- `HashExpressionsSuite`: "SPARK-61081: GEOMETRY and GEOGRAPHY hash their
physical bytes". The test compares `Murmur3Hash`, `XxHash64` and `HiveHash`
with expected values. It uses a `BinaryView` that points into the middle of a
larger array, so the test also checks the offset and the length.
`checkEvaluation` runs the interpreted path and the codegen path.
- `GeometryDataFrameSuite` and `GeographyDataFrameSuite`: "SPARK-61081:
GROUP BY, DISTINCT and hashing on a ... column". The input has four partitions,
so each query does a hash shuffle on the geo column. The test runs `GROUP BY`,
`COUNT(DISTINCT)`, `DISTINCT`, `approx_count_distinct`, `DISTRIBUTE BY`, `hash`
and `xxhash64`. Then it runs the `GROUP BY` again with
`spark.sql.TungstenAggregate.testFallbackStartsAt=1,2`, which forces the spill.
Without the fix, the three new tests fail with the errors above. With the
fix, all tests in these three suites pass.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 5.5)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]