Thanks Huaxin for starting this. I remember bumping into some of these
challenges when discussing check constraints.

+1 to Peter's volatility enum, with Andrei's rule that readers treat
unknown values as VOLATILE.

> I don't think that the current determinist/non-deterministic split is a
blocker to the table function work (check constraint, derived column). The
current Iceberg deterministic should be enough.

The spec already asks for more than the flag promises. A check expression
"must be pure. Its result must depend only on the values of the fields it
references in the row" [1]. The next paragraph requires only that a UDF in
the check declares `deterministic` as true, and the UDF spec defines that
flag as "Whether the function is deterministic" [2]. Spark and Trino treat
current_timestamp() as deterministic, so a UDF that calls it can set the
flag and still break the purity rule. I think the check rule should require
IMMUTABLE instead of `deterministic`.

Iceberg's definition of IMMUTABLE needs three parts that PostgreSQL's
doesn't. An Iceberg UDF can have a different body for each engine, and
engines like Spark, Trino, and DataFusion run it in parallel across many
machines.

1. Every representation returns the same result for the same arguments.
Huaxin's option 1 had this clause, and the enum should keep it.
2. The result depends only on the arguments. It must not depend on session
settings (time zone, locale, collation, or ANSI mode), other rows, row
order, how the engine splits up the work, or how many times the function
runs. For example, Spark's ANSI mode is a session setting [3], so the same
Spark body can fail on overflow in one session and return a value in
another. Even rand(42) in Spark gives different values depending on how the
input is split into tasks [4].
3. Behavior is preserved across definition versions. The check rule already
says this [1].

Representations will rarely disagree on purpose. They will disagree because
a function with the same name behaves differently in each engine. For
example, Spark and DataFusion can return different results for the same
regular expression [5]. An author who writes the same regex call in both
bodies can't see the difference by reading them, so the first part of the
definition is a promise that only testing can back.

> Should users be allowed to mark UDFs as Immutable/ Stable/ Volatile or
should for Immutable be reserved for iceberg/engine functions only?

Iceberg can't check that representations agree, so the only alternative to
trusting the creator is to disallow UDFs. Part of the declaration can be
checked, though. A UDF body is SQL [6], so anything that makes it
non-deterministic has to come from a function it calls, like now() or
rand(). An engine can look at the calls in the body for its own dialect.
Could the spec say an engine must treat a UDF as VOLATILE when that body
calls a STABLE or VOLATILE function? Then only agreement across dialects
rests on trust.

> 1/ The key rule is "computed again later", not the function itself.

I agree, and distributed engines also compute values again inside the
writing job. When a task fails, the engine runs it again, and the rerun
evaluates the expression again. If a random value decides which partition a
row lands in, the rerun can put the row somewhere else. If other tasks
already read the first attempt's output, the row can be written twice or
lost. Spark detects this and reruns everything downstream [7][8], but not
every engine does. So I'd require IMMUTABLE for a derived column used as a
partition source or in a primary key, even when it is materialized.

This also supports Szehon's point that STABLE has a narrow use. A STABLE
value stays the same across a distributed query only because the engine
computes it once up front and sends the result to every task. Spark does
this for the current time [9], and DataFusion does it for STABLE functions
called with constant arguments [10]. That helps the optimizer, but it says
nothing about what a later query computes, and that is what a stored
expression needs.

Thanks,
Matt

[1]
https://github.com/apache/iceberg/blob/9203eaff9eb97fef9d1f5a3ef4460e8453adaed9/format/spec.md?plain=1#L776-L778
[2]
https://github.com/apache/iceberg/blob/9203eaff9eb97fef9d1f5a3ef4460e8453adaed9/format/udf-spec.md?plain=1#L162
[3]
https://github.com/apache/spark/blob/cecbd5d846a2cdd891a4d9f0bda302b58b02092a/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala#L6314-L6323
[4]
https://github.com/apache/spark/blob/cecbd5d846a2cdd891a4d9f0bda302b58b02092a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/randomExpressions.scala#L59-L65
[5]
https://github.com/apache/datafusion-comet/blob/acddec4638e05f7362b502a4bc1f6ebc2cf953fb/docs/source/user-guide/latest/compatibility/regex.md?plain=1#L122-L143
[6]
https://github.com/apache/iceberg/blob/9203eaff9eb97fef9d1f5a3ef4460e8453adaed9/format/udf-spec.md?plain=1#L175-L178
[7]
https://github.com/apache/spark/blob/cecbd5d846a2cdd891a4d9f0bda302b58b02092a/core/src/main/scala/org/apache/spark/rdd/RDD.scala#L2276-L2290
[8]
https://github.com/apache/spark/blob/cecbd5d846a2cdd891a4d9f0bda302b58b02092a/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala#L6457-L6468
[9]
https://github.com/apache/spark/blob/cecbd5d846a2cdd891a4d9f0bda302b58b02092a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/finishAnalysis.scala#L117-L120
[10]
https://github.com/apache/datafusion/blob/4c7b30752c159a85c713453ad7041b9c29e4d647/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs#L611-L618

On Tue, Nov 17, 2026 at 2:16 PM Alex Cruise <[email protected]> wrote:

> Determinism per se is only one kind of effect/coeffect, and it's easy to
> get confused about it.
>
> For example, `now()` and `random()` are carefully documented by most
> engines to be evaluated at query start time, and the former might not
> strike many people as "nondeterministic" per se even though it makes
> queries non-repeatable even under an immutable dataset.
>
> `random()` is more intuitively viewed as "nondeterministic" but
> `random(seed=4)` is intuitively "deterministic" but, as with `now()`, when
> it's called, and how long it's memoized, matter to query correctness.
>
> More trivially, expressions that are pure (i.e. free of effects or
> coeffects) can be memoized forever, whereas an expression that only carries
> a "reads a field from a record in a table" coeffect can be memoized just as
> safely, but for a shorter timespan.
>
> -0xe1a
>
> On Tue, Nov 17, 2026 at 9:44 AM Alex Cruise <[email protected]> wrote:
>
>> IMO effects
>> <https://github.com/acruise/_meta/blob/f344af6cf3502329b2afdedff764b0ace9648d02/types/src/effects.rs#L15-L72>
>> and coeffects
>> <https://github.com/acruise/_meta/blob/f344af6cf3502329b2afdedff764b0ace9648d02/types/src/coeffects.rs#L15-L56>
>> are the most fundamental approach to this class of problem.
>>
>> Both are declared by leaf expressions, and combine "rootward" mostly
>> using set semantics, except certain combinations of the same type can be
>> coalesced (if their parameter space is lattice-ish?)
>>
>> Please don't pay _too_ much attention to the specific implementation I've
>> linked above, it's intended to be illustrative at this stage and not
>> updated since I started interacting with Iceberg. :)
>>
>> -0xe1a
>>
>> On Tue, Nov 17, 2026 at 9:05 AM Marco Kroll <
>> [email protected]> wrote:
>>
>>> Hi Huaxin, Peter, Andrei & Szehon,
>>>
>>> I agree that proper annotation of UDFs would be helpful and I too like
>>> Peter's idea of using a volatility enum.
>>>
>>> I checked and Huaxin is right, the following features would be affected
>>> by this:
>>> - Check Constraints [2]
>>> - Stats-only [3]
>>> - Derived Columns [4]
>>>
>>> It does not affect Default Values [1] as it explicitly allows
>>> non-deterministic UDFs
>>> Regarding Delta, I've too been looking into this topic. The Delta
>>> Protocol does not prohibit UDFs in expressions, but its reference
>>> implementation does not support it, except for a corner case, IDENTITY
>>> columns, which under the hood are generated columns with a
>>> non-deterministic function.
>>>
>>> To add to the discussion:
>>> - A third option would be to clearly define the term 'deterministic' for
>>> iceberg. Whorever, I guess this will be difficult now that various engines
>>> support it and might have different takes on it.
>>> - Should users be allowed to mark UDFs as Immutable/ Stable/ Volatile or
>>> should for Immutable be reserved for iceberg/engine functions only?
>>>
>>> Thanks for opening up this discussion
>>> Marco
>>>
>>> [1]:
>>> https://github.com/apache/iceberg/blob/master/format/spec.md#default-values
>>> [2]:
>>> https://github.com/apache/iceberg/blob/main/format/spec.md#constraints
>>> [3]:
>>> https://github.com/apache/iceberg/blob/main/format/spec.md#stats-only-fields
>>> [4]: https://github.com/apache/iceberg/issues/15923
>>>
>>>
>>> On Tue, Nov 17, 2026 at 2:55 PM Szehon Ho <[email protected]>
>>> wrote:
>>>
>>>> Hi Peter/Huaxin,
>>>>
>>>> Thanks for starting this.
>>>>
>>>> First in the current state, I think it's up to the engines to enforce
>>>> 'deterministic' as Iceberg can do little.
>>>>
>>>> About Spark's problem with its definition of 'deterministic, I indeed
>>>> saw this this while looking at support for derived column (aka generated
>>>> columnl).  I am thinking to fix this on the Spark side but haven't had the
>>>> chance to organize it yet.   As you note, Spark 'deterministic' is
>>>> unfortunately double-meaning and I find it somewhat a soft blocker for
>>>> clean Spark support for derived column.  As you stated, we can't rely
>>>> on the flag as it seems a combination of 'stable' and 'immutable', and
>>>> derived column should only be the latter (like Postgres).  The other
>>>> option was, to just whitelist known deterministic catalyst Expression in
>>>> Iceberg-Spark until we have the proper 'immutable/stable' split.
>>>>
>>>> Options
>>>>
>>>> 1. Add a new optional flag to the UDF spec for the strong requirement
>>>>> (name TBD, default false). When it's true, the function returns the same
>>>>> result for the same input in any engine, session, or environment, and all
>>>>> its SQL bodies return the same result. Check constraints, stats-only
>>>>> fields, and derived columns would require this new flag instead of
>>>>> `deterministic`. This is similar to PostgreSQL and Oracle: UDFs are
>>>>> allowed, and we trust the person who creates them.
>>>>> 2. Do what Delta does: only allow functions defined in the Iceberg
>>>>> spec, and no UDFs.
>>>>
>>>>
>>>> On option 2, I am not sure if Delta spec really prohibits UDF's,
>>>> Delta-Spark does but its implementation detail and can probably be
>>>> relaxed.  So I'd say it's not that relevant to Iceberg UDF spec (as Delta
>>>> never had UDF spec, only Delta-Spark knows about Spark UDF's).
>>>>
>>>> On option 1 (taking Peter's proposal), I do support adding Immutable/
>>>> Stable/ Volatile to Iceberg. I think it is always to be clear. But two
>>>> thoughts:
>>>>
>>>> 1. The stable category has a bit narrow of a use case, and may not be
>>>> always applicable for derived column.  It's more for allowing the same
>>>> function call to be collapsed into one (see Redshift documentation:
>>>> https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_FUNCTION.html)
>>>> 2. I don't think that the current determinist/non-deterministic split
>>>> is a blocker to the table function work (check constraint, derived
>>>> column).  The current Iceberg deterministic should be enough.
>>>>
>>>> So while it is useful and I support it, I do hope it's useful to more
>>>> engines, as the deprecation will inevitably bring confusion.
>>>>
>>>> Thanks
>>>> Szehon
>>>>
>>>>
>>>> On Tue, Nov 17, 2026 at 11:57 AM Andrei Tserakhau via dev <
>>>> [email protected]> wrote:
>>>>
>>>>> Thanks Huaxin, I think this question is a bit overdue.
>>>>>
>>>>> There are multiple places where this matters: derived columns, check
>>>>> constraints and stats-only fields all hit the same question, so
>>>>> answering
>>>>> it here is important.
>>>>>
>>>>> I think determinism is a must wherever a value gets computed again
>>>>> later. But forbidding UDFs closes too much. It just pushes people back to
>>>>> engine-specific table properties.
>>>>>
>>>>> +1 to Peter's enum idea, with some more thoughts:
>>>>>
>>>>> 1/ The key rule is "computed again later", not the function itself.
>>>>> Check validation, virtual columns and stats-only expressions need to be
>>>>> IMMUTABLE. A value computed once and stored as the source of truth can be
>>>>> VOLATILE: defaults, materialized derived columns. They are very useful 
>>>>> with
>>>>> AI functions like embed(body, 'my-awesome-model'). They can be
>>>>> non-deterministic, but they can't be virtual.
>>>>>
>>>>> 2/ IMMUTABLE means no hidden inputs. Session time zone, locale or
>>>>> collation version must be explicit arguments. That also covers built-ins:
>>>>> wrap them in a UDF declared IMMUTABLE.
>>>>>
>>>>> 3/ deterministic=true is IMO closer to STABLE, not IMMUTABLE. Readers
>>>>> should treat unknown values as VOLATILE.
>>>>>
>>>>> 4/ Allowing UDFs shouldn’t mean every engine has to support them. An
>>>>> engine that can’t evaluate a stored expression ignores it on read (falls
>>>>> back to a scan) and refuses to write, same as any unsupported feature.
>>>>>
>>>>> best,
>>>>> Andrei
>>>>>
>>>>> On Tue, Nov 17, 2026 at 9:26 AM Péter Váry <
>>>>> [email protected]> wrote:
>>>>>
>>>>>> Thanks Huaxin for putting this together.
>>>>>>
>>>>>> I would like to suggest another option: *add an optional *
>>>>>> *volatility** enum* with three values: VOLATILE, STABLE, and
>>>>>> IMMUTABLE, with semantics based on PostgreSQL's volatility categories 
>>>>>> [1].
>>>>>> We could deprecate the existing *deterministic* flag and define its
>>>>>> relationship with volatility during a transition period.
>>>>>>
>>>>>> Pros:
>>>>>>
>>>>>>    - Covers both the existing weaker notion of determinism and the
>>>>>>    stronger guarantee needed here.
>>>>>>    - Provides a clear, established distinction without introducing
>>>>>>    another overlapping boolean.
>>>>>>    - Can be used for other cases where STABLE is sufficient.
>>>>>>
>>>>>> Cons:
>>>>>>
>>>>>>    - Slightly more complex than a boolean.
>>>>>>    - Adding enum values in the future may have forward-compatibility
>>>>>>    implications.
>>>>>>
>>>>>> I would prefer this option because it provides a clean distinction
>>>>>> between the different guarantees and is more expressive than adding 
>>>>>> another
>>>>>> boolean flag.
>>>>>>
>>>>>> Thanks,
>>>>>> Peter
>>>>>>
>>>>>> [1] PostgreSQL Function Volatility Categories:
>>>>>> https://www.postgresql.org/docs/current/xfunc-volatility.html
>>>>>>
>>>>>> huaxin gao <[email protected]> ezt írta (időpont: 2026. nov.
>>>>>> 17., K, 6:11):
>>>>>>
>>>>>>> Hi everyone,
>>>>>>>
>>>>>>> I want to start a discussion about what "deterministic" means for
>>>>>>> functions in stored expressions, and what the UDF spec should say about 
>>>>>>> it.
>>>>>>>
>>>>>>> Why this matters
>>>>>>>
>>>>>>> Some features compute a value when data is written, and compute it
>>>>>>> again later, maybe in a different engine:
>>>>>>>
>>>>>>>    - Check constraints (v4): a UDF used in a check must set
>>>>>>>    `deterministic` to true.
>>>>>>>    - Stats-only fields in the index spec (
>>>>>>>    https://github.com/apache/iceberg/pull/16961): the expression
>>>>>>>    must be deterministic. The writer uses the value to sort index 
>>>>>>> entries. The
>>>>>>>    reader computes it again to find the right region file.
>>>>>>>    - Derived (generated) columns: the value is computed from other
>>>>>>>    columns when data is written. Different engines may write rows to 
>>>>>>> the same
>>>>>>>    table, so they must compute the same value for the same input.
>>>>>>>
>>>>>>> If two engines get different results for the same input, a check can
>>>>>>> pass in one engine and fail in another. An index lookup can also miss 
>>>>>>> rows.
>>>>>>> For a primary key lookup, a missed row means a duplicate row is written.
>>>>>>>
>>>>>>> What the UDF spec says today
>>>>>>>
>>>>>>>    - `deterministic` is a boolean on each definition version. The
>>>>>>>    spec doesn't say what it means: the same result within one query, 
>>>>>>> within
>>>>>>>    one session, or always.
>>>>>>>    - "Deterministic" can be a weak requirement or a strong
>>>>>>>    requirement. The weak one is a hint for the engine to optimize, for 
>>>>>>> example
>>>>>>>    to cache a result. The strong one is "the same input always gives 
>>>>>>> the same
>>>>>>>    output". The `deterministic` flag in the UDF spec is meant as the 
>>>>>>> weak one.
>>>>>>>    - A UDF can have one SQL body per engine. Nothing says these
>>>>>>>    bodies must return the same result. This came up in the UDF spec PR, 
>>>>>>> and
>>>>>>>    the decision was to follow the view spec and not add a rule:
>>>>>>>    https://github.com/apache/iceberg/pull/14117#discussion_r2771641415
>>>>>>>    .
>>>>>>>
>>>>>>>
>>>>>>> What other systems do
>>>>>>>
>>>>>>>    - "Deterministic" has two meanings in practice. The SQL
>>>>>>>    standard, Oracle, Db2, MySQL, Snowflake (IMMUTABLE), BigQuery 
>>>>>>> (JavaScript
>>>>>>>    UDFs) and PostgreSQL (IMMUTABLE) mean "always the same result for 
>>>>>>> the same
>>>>>>>    input". Spark and Trino use a weaker meaning: both treat
>>>>>>>    current_timestamp() as deterministic, because its value is fixed 
>>>>>>> within one
>>>>>>>    query. This is close to PostgreSQL's STABLE.
>>>>>>>    - Session settings can change results. PostgreSQL says a
>>>>>>>    function that depends on the time zone should be STABLE, not 
>>>>>>> IMMUTABLE.
>>>>>>>    Oracle warns that changing NLS settings in a session can give wrong 
>>>>>>> results
>>>>>>>    from a function-based index. SQL Server requires fixed SET options 
>>>>>>> for
>>>>>>>    indexes on computed columns. Db2 doesn't allow functions that depend 
>>>>>>> on
>>>>>>>    special registers in generated columns.
>>>>>>>    - In almost all systems, the flag is a promise from the person
>>>>>>>    who creates the function. The system doesn't check it. If the flag is
>>>>>>>    wrong, results can be wrong.
>>>>>>>    - For stored data, many systems don't allow UDFs at all, even
>>>>>>>    ones marked deterministic: Delta generated columns and CHECK 
>>>>>>> constraints,
>>>>>>>    Snowflake virtual columns and materialized views, BigQuery 
>>>>>>> materialized
>>>>>>>    views, MySQL generated columns and check constraints, Db2 
>>>>>>> expression-based
>>>>>>>    indexes, and Oracle check constraints. Some systems do allow UDFs:
>>>>>>>    PostgreSQL in indexes and stored generated columns, if the UDF is 
>>>>>>> marked
>>>>>>>    IMMUTABLE; Oracle in function-based indexes and virtual columns, if 
>>>>>>> it is
>>>>>>>    marked DETERMINISTIC; and SQL Server in indexed computed columns, if 
>>>>>>> SQL
>>>>>>>    Server decides the function is deterministic.
>>>>>>>    - The databases above each run one engine, so they don't need
>>>>>>>    multiple engines to agree. Delta is also used by many engines. The 
>>>>>>> Delta
>>>>>>>    Lake docs say generated column expressions can't use user-defined
>>>>>>>    functions, and the Databricks docs say the same for CHECK 
>>>>>>> constraints.
>>>>>>>
>>>>>>>
>>>>>>> Options
>>>>>>> 1. Add a new optional flag to the UDF spec for the strong
>>>>>>> requirement (name TBD, default false). When it's true, the function 
>>>>>>> returns
>>>>>>> the same result for the same input in any engine, session, or 
>>>>>>> environment,
>>>>>>> and all its SQL bodies return the same result. Check constraints,
>>>>>>> stats-only fields, and derived columns would require this new flag 
>>>>>>> instead
>>>>>>> of `deterministic`. This is similar to PostgreSQL and Oracle: UDFs are
>>>>>>> allowed, and we trust the person who creates them.
>>>>>>> 2. Do what Delta does: only allow functions defined in the Iceberg
>>>>>>> spec, and no UDFs.
>>>>>>>
>>>>>>> Option 2 is simpler and safer, but it's too limiting. For example,
>>>>>>> sorting an index by a Hilbert curve value needs a function that Iceberg
>>>>>>> doesn't have, and we want to keep the list of Iceberg functions small.
>>>>>>>
>>>>>>> Open questions
>>>>>>>
>>>>>>> 1. What should the new flag be called? `immutable` matches
>>>>>>> PostgreSQL and Snowflake, but people may confuse it with immutable view 
>>>>>>> and
>>>>>>> UDF versions.
>>>>>>> 2. What about functions that are not UDFs? Stored expressions can
>>>>>>> also call SQL standard functions and engine built-in functions, and the 
>>>>>>> new
>>>>>>> flag doesn't cover them. For example, lower() can depend on locale. 
>>>>>>> Should
>>>>>>> we only allow Iceberg functions and UDFs with the new flag? Engines can
>>>>>>> wrap their own functions in UDFs.
>>>>>>> 3. Should the new flag also mean `deterministic` is true?
>>>>>>>
>>>>>>> If people agree with adding a new flag in UDF spec, I can open a PR
>>>>>>> for this.
>>>>>>>
>>>>>>> Thanks,
>>>>>>> Huaxin
>>>>>>>
>>>>>>

Reply via email to