cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

sql: support UDFs with named args, strictness, and volatility

Open mgartner opened this issue 3 years ago • 4 comments
trafficstars

sql: UDF with empty result should evaluate to NULL

If the last statement in a UDF returns no rows, the UDF will evaluate to NULL. Prior to this commit the evaluation of the UDF would panic.

Release note: None

sql: support UDFs with named arguments

UDFs with named arguments can now be evaluated.

During query planning, statements in the function body are built with a scope that includes the named arguments for the function as columns. This allows references to arguments to be resolved as variables.

During evaluation, the input expressions are first evaluated into datums. When a plan is built for each statement in the UDF, the argument columns in the expression are replaced with the input datums before the expression is optimized.

Note that anonymous arguments and integer references to arguments (e.g., $1) are not yet supported.

Also, the formatting of UDFExprs has been improved to show argument columns and input expressions.

Release note: None

sql: do not evaluate strict UDFs if any input values are NULL

A UDF can have one of two behaviors when it is invoked with NULL inputs:

  1. If the UDF is CALLED ON NULL INPUT (the default) then the function is evaluated regardless of whether or not any of the input values are NULL.
  2. If the UDF RETURNS NULL ON NULL INPUT or is STRICT then the function is not evaluated if any of the input values are NULL. Instead, the function directly results in NULL.

This commit implements these two behaviors.

In the future, we can add a normalization rule that folds a strict UDF if any of its inputs are constant NULL values.

Release note: None

sql: make mutations visible to volatile UDFs

The volatility of a UDF affects the visibility of mutations made by the statement calling the function. A volatile function will see these mutations. Also, statements within a volatile function's body will see changes made by previous statements the function body (note that this is left untested in this commit because we do not currently support mutations within UDF bodies). In contrast, a stable, immutable, or leakproof function will see a snapshot of the data as of the start of the statement calling the function.

Release note: None

mgartner avatar Aug 08 '22 23:08 mgartner

This change is Reviewable

cockroach-teamcity avatar Aug 08 '22 23:08 cockroach-teamcity

TFTRs!

bors r+

mgartner avatar Aug 11 '22 00:08 mgartner

Build failed (retrying...):

craig[bot] avatar Aug 11 '22 04:08 craig[bot]

Build failed:

craig[bot] avatar Aug 11 '22 09:08 craig[bot]

bors r+

mgartner avatar Aug 11 '22 14:08 mgartner

Build failed (retrying...):

craig[bot] avatar Aug 11 '22 16:08 craig[bot]

bors r-

mgartner avatar Aug 11 '22 16:08 mgartner

Canceled.

craig[bot] avatar Aug 11 '22 16:08 craig[bot]

bors r+

mgartner avatar Aug 11 '22 21:08 mgartner

bors r+

mgartner avatar Aug 11 '22 23:08 mgartner

Already running a review

craig[bot] avatar Aug 11 '22 23:08 craig[bot]

bors r+

mgartner avatar Aug 11 '22 23:08 mgartner

Already running a review

craig[bot] avatar Aug 11 '22 23:08 craig[bot]

Build failed (retrying...):

craig[bot] avatar Aug 12 '22 04:08 craig[bot]

Build succeeded:

craig[bot] avatar Aug 12 '22 07:08 craig[bot]