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