rdtr opened a new pull request, #443:
URL: https://github.com/apache/datafusion-spark/pull/443
## Which issue does this PR close?
- Closes #204.
## Rationale for this change
Spark's `levenshtein(str1, str2[, threshold])` takes an optional `threshold`
(since Spark 3.5) and returns -1 when the distance is greater than it. Without
a Spark function here, `levenshtein` resolves to DataFusion's built-in, which
has no `threshold` argument and returns BIGINT for `LargeUtf8` where Spark
always returns INT.
## What changes are included in this PR?
This adds `levenshtein` in `function/string/levenshtein.rs`, ported from
Comet's `native/spark-expr/src/string_funcs/levenshtein.rs` as the issue
suggests: the full distance for two arguments and the banded distance up to the
threshold for three, both on code points with an ASCII fast path. A negative
threshold returns -1, and an `Integer.MAX_VALUE` threshold does not overflow.
Arguments go through `SparkInputTypes` (STRING, STRING[, INT]), so numbers are
cast to STRING and a BIGINT threshold to INT, and both strings are coerced to
one layout. `SparkInputTypes` takes a fixed argument count, so the function
holds one declaration per form and returns Spark's `WRONG_NUM_ARGS` with `[2,
3]` itself; it can move to `SparkInputTypes::optional` once #421 lands.
A NULL threshold returns NULL. Spark itself is inconsistent here: its
generated code returns NULL, while the interpreted `eval` unboxes NULL to 0
(`levenshtein('abc', 'abd', t)` with a NULL `t` returns -1 with
`spark.sql.codegen.factoryMode=NO_CODEGEN`, and in a `VALUES` relation that is
evaluated during optimization). This follows the generated code, which is the
normal path and matches `nullIntolerant`.
Earlier attempts at the threshold form were apache/datafusion#20487 and
apache/datafusion#20927.
## Are these changes tested?
`string/levenshtein.slt` replaces the commented-out queries with the cases
listed in the issue and more: NULL in each argument, empty strings, multi-byte
and supplementary characters, a combining sequence, thresholds of -1, 0, the
exact distance, distance - 1 and `Integer.MAX_VALUE`, implicit casts,
`LargeUtf8` / `Utf8View` inputs, column arguments including a column threshold
with NULLs, and the `WRONG_NUM_ARGS` / `UNEXPECTED_INPUT_TYPE` errors. Unit
tests check that the banded result equals the full distance (or -1) for every
threshold around the distance, and that the scratch buffers stay bounded.
Every expected value was checked against Spark 4.2.0 (`spark-sql`),
including the NULL-threshold behavior on the generated and interpreted paths.
The PySpark runner passes on the file; the one record with a NULL threshold
inside `VALUES` is marked `xfail spark` for the interpreted-eval difference
above. `cargo test -p datafusion-spark` and `cargo test -p
datafusion-spark-sqllogictest` pass in both modes.
--
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]