Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

udf: unimplemented: SELECT usage inside a function definition (when using EXISTS) #88373

Closed
michae2 opened this issue Sep 21, 2022 · 3 comments
Closed
Assignees
Labels
A-sql-routine UDFs and Stored Procedures C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-qa T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)

Comments

@michae2
Copy link
Collaborator

michae2 commented Sep 21, 2022

Not sure if this is allowed, but when trying to use WHERE EXISTS (SELECT ...) inside a UDF I get an unimplemented: SELECT usage inside a function definition error. Creating functions with the same query, rewritten in different ways, does not err.

Here's the reproduction. It's kind of a silly semi-join query, finding the first value for which there exists value + 1 in the same table.

CREATE TABLE t (a) AS SELECT generate_series(0, 999);
EXPLAIN SELECT t1.a FROM t t1 JOIN t t2 ON (t2.a = t1.a + 1) ORDER BY t1.a LIMIT 1;
EXPLAIN SELECT t1.a FROM t t1, (SELECT * FROM t) t2 WHERE t2.a = t1.a + 1 ORDER BY t1.a LIMIT 1;
EXPLAIN SELECT t1.a FROM t t1 WHERE EXISTS (SELECT * FROM t t2 WHERE t2.a = t1.a + 1) ORDER BY t1.a LIMIT 1;
CREATE FUNCTION f1 () RETURNS INT LANGUAGE SQL AS $$ SELECT t1.a FROM t t1 JOIN t t2 ON (t2.a = t1.a + 1) ORDER BY t1.a LIMIT 1 $$;
CREATE FUNCTION f2 () RETURNS INT LANGUAGE SQL AS $$ SELECT t1.a FROM t t1, (SELECT * FROM t) t2 WHERE t2.a = t1.a + 1 ORDER BY t1.a LIMIT 1 $$;
CREATE FUNCTION f3 () RETURNS INT LANGUAGE SQL AS $$ SELECT t1.a FROM t t1 WHERE EXISTS (SELECT * FROM t t2 WHERE t2.a = t1.a + 1) ORDER BY t1.a LIMIT 1 $$;

All three versions of the query should be the same. Functions can be made with the first two, but not with the last:

demo@127.0.0.1:26257/defaultdb> CREATE FUNCTION f1 () RETURNS INT LANGUAGE SQL AS $$ SELECT t1.a FROM t t1 JOIN t t2 ON (t2.a = t1.a + 1) ORDER BY t1.a LIMIT 1 $$;
CREATE FUNCTION


Time: 36ms total (execution 36ms / network 0ms)

demo@127.0.0.1:26257/defaultdb> CREATE FUNCTION f2 () RETURNS INT LANGUAGE SQL AS $$ SELECT t1.a FROM t t1, (SELECT * FROM t) t2 WHERE t2.a = t1.a + 1 ORDER BY t1.a LIMIT 1 $$;
CREATE FUNCTION


Time: 29ms total (execution 28ms / network 0ms)

demo@127.0.0.1:26257/defaultdb> CREATE FUNCTION f3 () RETURNS INT LANGUAGE SQL AS $$ SELECT t1.a FROM t t1 WHERE EXISTS (SELECT * FROM t t2 WHERE t2.a = t1.a + 1) ORDER BY t1.a LIMIT 1 $$;
ERROR: unimplemented: SELECT usage inside a function definition
SQLSTATE: 0A000
HINT: You have attempted to use a feature that is not yet implemented.

Please check the public issue tracker to check whether this problem is
already tracked. If you cannot find it there, please report the error
with details by creating a new issue.

If you would rather not post publicly, please contact us directly
using the support form.

We appreciate your feedback.

I would expect the WHERE EXISTS (SELECT ...) version to also work.

This is on v22.2.0-alpha.3-421-g77c412dfef (tip of master).

Jira issue: CRDB-19789

@michae2 michae2 added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-qa T-sql-schema-deprecated Use T-sql-foundations instead T-sql-queries SQL Queries Team A-sql-routine UDFs and Stored Procedures labels Sep 21, 2022
@mgoddard
Copy link

mgoddard commented Jan 19, 2023

Would this also address the issue unimplemented: subquery usage inside a function definition? Here's an example from yesterday where I encountered this error message:

CREATE OR REPLACE FUNCTION map_countries (num_codes_agg STRING)
RETURNS STRING
IMMUTABLE LEAKPROOF LANGUAGE SQL AS
$$
  select array_to_string(array_agg(alph), '|')
  from num_to_alph
  where num in
  (
    select * from
    [select regexp_extract(regexp_split_to_table(num_codes_agg, '\|'), '(\d{4})') dig]
    where dig is not null
  )
$$;

@mgartner
Copy link
Collaborator

No, that is related to #87291. Can you share the schema of num_to_alpha in that issue with your example?

@mgartner
Copy link
Collaborator

mgartner commented Feb 1, 2023

This actually related to #87291, which is almost completed. On master, the original failure above no fails with ERROR: unimplemented: functions do not currently support * expressions, which will soon be fixed by #95710 (tracked by #90080).

@mgartner mgartner closed this as completed Feb 1, 2023
@exalate-issue-sync exalate-issue-sync bot removed the T-sql-queries SQL Queries Team label Feb 1, 2023
@exalate-issue-sync exalate-issue-sync bot added T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) and removed T-sql-schema-deprecated Use T-sql-foundations instead labels May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-routine UDFs and Stored Procedures C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-qa T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)
Projects
Archived in project
Development

No branches or pull requests

3 participants