cockroachdb / cockroachdb/cockroach

sql: inlined UDFs do not report error CONTEXT

Open
#172,500 0 comments 0 reactions 1 assignee Claimed by @michae2 View on GitHub
A-sql-plpgsql A-sql-routine branch-release-26.3 C-bug docs-known-limitation O-qa T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

When UDF bodies are inlined into their calling statements, they lose the ability to report error CONTEXT. Here's an example:

```sql
-- with a call to random() the UDF is not inlined, and is able to add CONTEXT to errors
CREATE FUNCTION f_not_inlined(n INT) RETURNS INT AS $$
BEGIN
IF n < 0 THEN
RETURN 0;
END IF;
RETURN 1 / n + (random() * 0)::INT;
END
$$ LANGUAGE PLpgSQL;
SELECT f_not_inlined(0);
-- ERROR: division by zero
-- SQLSTATE: 22012
-- CONTEXT: PL/pgSQL function f_not_inlined(bigint) line 2 at IF

-- without random() the UDF is inlined into the calling SELECT and cannot add CONTEXT to errors
CREATE FUNCTION f_inlined(n INT) RETURNS INT AS $$
BEGIN
IF n < 0 THEN
RETURN 0;
END IF;
RETURN 1 / n;
END
$$ LANGUAGE PLpgSQL;
SELECT f_inlined(0);
-- ERROR: division by zero
-- SQLSTATE: 22012
```

Jira issue: CRDB-65744

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.