cockroachdb / cockroachdb/cockroach

sql shell lexical error: unterminated string

Open
#120,057 2 comments 0 reactions 0 assignees View on GitHub
C-bug O-community T-sql-foundations
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

**Describe the problem**

The cloud sql shell produces an error when a function is defined using the `$$` syntax.

**To Reproduce**

Following some examples from our [docs](https://www.cockroachlabs.com/docs/stable/create-function?_gl=1*1nr8v2k*_ga*MTg1MDQ4ODg5LjE3MDQwNzc1MTc.*_ga_WPTKE58Z5D*MTcwOTgyNjMyNi4xNTguMS4xNzA5ODI2Mzc1LjAuMC4w#create-a-function-that-modifies-a-table), we observe that the following function:

```
CREATE OR REPLACE FUNCTION update_code(
code_name VARCHAR,
new_rules JSONB
)
RETURNS promo_codes AS $$
UPDATE promo_codes SET rules = new_rules
WHERE code = code_name
RETURNING *;
$$ LANGUAGE SQL;
```

On 23.2.1 using `cockroach demo` works as expected:
image

On 23.2.1 using the cloud sql shell produces a lexical error:
image

This is a bug reported on our [community forum](https://forum.cockroachlabs.com/t/create-function-lexical-error-unterminated-string/6087) by a user when they tried testing out [creating a function using a loop](https://forum.cockroachlabs.com/t/create-function-lexical-error-unterminated-string/6087):

```
CREATE FUNCTION fib(n int) RETURNS INT AS $$
DECLARE
tmp INT;
a INT := 0;
b INT := 1;
i INT := 2;
BEGIN
IF n < 0 THEN
RAISE EXCEPTION 'n must be non-negative';
END IF;
IF n = 0 THEN RETURN 0; END IF;
IF n = 1 THEN RETURN 1; END IF;
LOOP
IF i > n THEN EXIT; END IF;
tmp := a + b;
a := b;
b := tmp;
i := i + 1;
END LOOP;
RETURN b;
END
$$ LANGUAGE PLpgSQL;
```

Jira issue: CRDB-36454

Epic CRDB-23743

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.