ganeshashree opened a new pull request, #60547:
URL: https://github.com/apache/spark/pull/60547

   ### What changes were proposed in this pull request?
   
   Add a built-in `farm_fingerprint(expr)` function. It returns the FarmHash 
Fingerprint64 of a `STRING` or `BINARY` value as a `BIGINT`, the same as 
`FARM_FINGERPRINT` in BigQuery / GoogleSQL.
   
   ```sql
   > SELECT farm_fingerprint('Spark');
    -2743780590904673122
   > SELECT farm_fingerprint(concat('1', 'foo', 'true'));
    -1541654101129638711   -- same as BigQuery
   ```
   
   - Available in SQL, Scala (`functions.farm_fingerprint`), and PySpark 
(classic and Connect).
   - Built on Guava's `Hashing.farmHashFingerprint64()`, which Spark already 
depends on, so there is no new dependency.
   - Follows the `crc32` / `xxh3_64` pattern: one argument, implicitly cast to 
`BINARY`, so strings are hashed as their UTF-8 bytes. `NULL` in gives `NULL` 
out, and codegen is supported.
   - Collation is ignored: a collated string is hashed from its bytes, the same 
as `md5`, `crc32`, and `xxh3_64`.
   
   ### Why are the changes needed?
   
   `FARM_FINGERPRINT` is widely used in BigQuery for sampling, bucketing, 
surrogate keys, and dedup, for example `MOD(ABS(FARM_FINGERPRINT(id)), 10) < 
1`. Spark has no equivalent. `hash()` and `xxhash64()` return different values, 
so migrated queries silently change results, and Spark can't match fingerprints 
already stored by BigQuery.
   
   Fingerprint64 is also guaranteed not to change across versions or platforms, 
so its values are safe to persist.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, a new `farm_fingerprint` SQL function plus Scala and PySpark APIs. 
Existing behavior doesn't change.
   
   ### How was this patch tested?
   
   - `HashExpressionsSuite`: matches three published BigQuery 
`FARM_FINGERPRINT` outputs. It also pins values for every Fingerprint64 length 
branch and boundary (0 to 128 bytes), binary and multi-byte UTF-8 input, and 
`NULL`, and checks that interpreted and codegen results agree.
   - `DataFrameFunctionsSuite`: Scala API and SQL, string vs. its UTF-8 bytes, 
non-ASCII text, and collated input.
   - SQL golden tests in `misc-functions.sql`, including rejection of `INT` and 
`ARRAY` arguments. Regenerated `sql-expression-schema.md`.
   - Spark Connect plan golden files (`PlanGenerationTestSuite`, 
`ProtoToParsedPlanTestSuite`).
   - PySpark doctest and `test_farm_fingerprint`.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)
   
   This pull request and its description were written by Isaac.
   


-- 
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]

Reply via email to