I agree with Matt's proposal. In my first email I said allowing only Iceberg functions was too limiting, because things like a Hilbert curve need functions Iceberg doesn't have. Matt's proposal answers that: those become Iceberg functions with exact semantics and test vectors. The cost is a longer function list, but I now think that's the right trade. Nobody can really promise that every dialect's body returns the same result. As Matt said, even with Spark as a reference to test against, matching Spark's results for a fixed set of functions in Comet took years, and differences still turn up. I saw this firsthand when I worked on Comet. A UDF author has much less to work with.
For check constraints, I prefer allowing only exactly specified functions, and I'd apply it to all check constraints, not only the ones planners rely on. Every engine must get the same result for the same row, both when writing and when planning. Otherwise a row that passes in one engine can fail in another, and the constraint status can't be trusted. So check constraints should follow the same rule as index keys and stats-only fields. I think derived columns that are computed again on read should follow the same rule. I'm happy to update the constraint spec once we agree. Thanks, Huaxin On Thu, Nov 19, 2026 at 10:11 AM Matt Butrovich via dev < [email protected]> wrote: > 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 > >>
