Thanks everyone for the discussion in today's sync. Since my last message
I've looked at how often engines actually agree, and I'd like to narrow
what I proposed.

Both the current `deterministic` rule and an IMMUTABLE level in the enum
rest on the UDF author promising that every dialect's body returns the same
result. I don't think anyone can keep that promise. Engines disagree on
details the SQL spec leaves open, and nobody knows all of them.

For example, `array_contains(array(1, NULL), 2)` is NULL in Spark and false
in DataFusion and DuckDB. The SQL spec doesn't define `array_contains`,
both answers are defensible, and Spark kept its behavior on purpose [1][2].
Under Iceberg's null-safe comparisons [3], a check like
`array_contains(tags, 'pii') = false` rejects that row in Spark and accepts
it in DataFusion. An author who writes that call in both bodies has no
reason to suspect a difference. `ln(0)` is NULL in Spark and -Infinity in
DataFusion, and their `ln` results differ in the last bit on about 2% of
inputs [4][5][6]. Regex engines differ too [7]. Engines also sort floats
differently around NaN and -0.0, and I filed #19040 [8] for the spec side
of that.

This matches what we've seen on Comet. We've spent over two years making
Rust match Spark for a fixed set of functions, and we still keep a list of
known differences [7]. That's with a reference engine to test against and
fall back to. A UDF author has neither.

Here's what I'd propose now:

1. Stats-only fields and index ordering keys use only functions defined in
the Iceberg spec, each with exact semantics and test vectors, the way
`bucket` works today [13]. A Hilbert curve would become one of these
functions.
2. UDFs stay allowed for values that are computed once and stored, like
write defaults.
3. I still like Peter's volatility enum for UDFs, as long as unknown values
read as VOLATILE and the old `deterministic` flag never maps to IMMUTABLE.

That's close to Huaxin's option 2 for index keys and stats-only fields. It
means a longer list of Iceberg functions. But each one gets specified once,
instead of every pair of engines finding their differences through wrong
results.

Check constraints are the hard part, because the point is to let planners
trust them. The v4 rule already allows UDFs that declare `deterministic`
[9], and each engine runs the body for its own dialect. So a Spark writer
and a Trino writer can both mark a check as valid while running different
code [10]. SQL bodies make this hard to spot, because the differences hide
in built-ins like `array_contains`. Even one dialect isn't stable. Spark 4
turned ANSI mode on by default [11], so the same Spark body can fail on one
version and pass on another. And an engine with no body for its dialect
can't write to the table while the check is enforced [12].

If planners trust check statuses, every engine has to evaluate the check
the same way the writer did. That's the same requirement as index keys. So
I think we have to choose: allow only exactly-specified functions in checks
that planners trust, or say in the spec that planning on a UDF check can
return wrong results when bodies disagree. Today's rule does the second
without saying so. Which do people want?

Thanks,
Matt

[1] https://issues.apache.org/jira/browse/SPARK-55749
[2]
https://github.com/apache/datafusion/blob/13efc9c27d59b7fbc6503025042f14064c5fa0f7/datafusion/sqllogictest/test_files/array/array_has.slt#L22-L27
[3]
https://github.com/apache/iceberg/blob/2bd60a093a889a44a9e67033dba3abf4c36895f5/format/expressions-spec.md?plain=1#L140-L145
[4]
https://github.com/apache/spark/blob/cecbd5d846a2cdd891a4d9f0bda302b58b02092a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala#L783
[5]
https://github.com/apache/datafusion/blob/13efc9c27d59b7fbc6503025042f14064c5fa0f7/datafusion/functions/src/macros.rs#L293-L311
[6] https://github.com/apache/datafusion-spark/pull/334
[7]
https://github.com/apache/datafusion-comet/blob/d187e6823f9052e7331e8a810b08a1202debb91c/docs/source/user-guide/latest/compatibility/index.md?plain=1#L29-L30
[8] https://github.com/apache/iceberg/issues/19040
[9]
https://github.com/apache/iceberg/blob/2bd60a093a889a44a9e67033dba3abf4c36895f5/format/spec.md?plain=1#L787
[10]
https://github.com/apache/iceberg/blob/2bd60a093a889a44a9e67033dba3abf4c36895f5/format/spec.md?plain=1#L801-L802
[11]
https://github.com/apache/spark/blob/cecbd5d846a2cdd891a4d9f0bda302b58b02092a/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala#L6314-L6323
[12]
https://github.com/apache/iceberg/blob/2bd60a093a889a44a9e67033dba3abf4c36895f5/format/spec.md?plain=1#L773
[13]
https://github.com/apache/iceberg/blob/2bd60a093a889a44a9e67033dba3abf4c36895f5/format/spec.md?plain=1#L2018-L2019

>

Reply via email to