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

sql: Support view queries with star expansions #10028

Closed
a-robinson opened this issue Oct 17, 2016 · 24 comments · Fixed by #97515
Closed

sql: Support view queries with star expansions #10028

a-robinson opened this issue Oct 17, 2016 · 24 comments · Fixed by #97515
Assignees
Labels
A-sql-optimizer SQL logical planning and optimizations. A-sql-pgcompat Semantic compatibility with PostgreSQL A-sql-semantics A-tools-graphile Issues relating to graphile compatibility C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) E-starter Might be suitable for a starter project for new employees or team members. T-sql-queries SQL Queries Team

Comments

@a-robinson
Copy link
Contributor

a-robinson commented Oct 17, 2016

Copied over from #9921 (comment)

We currently don't expand out star selectors before persisting a new view's query. If a dependent table has columns added to it, that can break later queries from the view depending on how that dependent table was used.

It's kind of a fluke, but a simple star select works just fine as columns are added because the view descriptor only lists the original columns as its ResultColumns:

root@:26257> create table star (a int primary key, b int);
CREATE TABLE
root@:26257> create view v as select * from star;
CREATE VIEW
root@:26257> insert into star values (1, 1), (2, 2);
INSERT 2
root@:26257> select * from star;
+---+---+
| a | b |
+---+---+
| 1 | 1 |
| 2 | 2 |
+---+---+
(2 rows)
root@:26257> SELECT * FROM v;
+---+---+
| a | b |
+---+---+
| 1 | 1 |
| 2 | 2 |
+---+---+
(2 rows)
root@:26257> alter table star add column c int;
ALTER TABLE
root@:26257> SELECT * FROM star;
+---+---+------+
| a | b |  c   |
+---+---+------+
| 1 | 1 | NULL |
| 2 | 2 | NULL |
+---+---+------+
(2 rows)
root@:26257> SELECT * FROM v;
+---+---+
| a | b |
+---+---+
| 1 | 1 |
| 2 | 2 |
+---+---+
(2 rows)

On the other hand, if the * isn't at the end of the query, it can break the view as columns are added:

root@:26257> create table t (a int primary key, b int);
CREATE TABLE
root@:26257> insert into t values (1, 1), (2, 2);
INSERT 2
root@:26257> CREATE VIEW tv AS SELECT t1.*, t2.b AS t2b FROM t AS t1 JOIN t AS t2 ON t1.a = t2.a;
CREATE VIEW
root@:26257> select * from tv;
+---+---+-----+
| a | b | t2b |
+---+---+-----+
| 1 | 1 |   1 |
| 2 | 2 |   2 |
+---+---+-----+
(2 rows)
root@:26257> ALTER TABLE t ADD COLUMN c INT;
ALTER TABLE
root@:26257> SELECT t1.*, t2.b AS t2b FROM t AS t1 JOIN t AS t2 ON t1.a = t2.a;
+---+---+------+-----+
| a | b |  c   | t2b |
+---+---+------+-----+
| 1 | 1 | NULL |   1 |
| 2 | 2 | NULL |   2 |
+---+---+------+-----+
(2 rows)
root@:26257> SELECT * FROM tv;
+---+---+------+
| a | b | t2b  |
+---+---+------+
| 1 | 1 | NULL |
| 2 | 2 | NULL |
+---+---+------+
(2 rows)

The logic for expanding stars appears to be a little broader in scope than the logic for normalizing table names, so I'm not sure if there's as well-contained of a fix for it as for #9921 (beyond moving to a semantic representation, of course).

Is this the sort of thing that we should block the rollout of views for? Is it ok to leave as a known issue? Is there a fix that could reasonably be put in place for it?

@knz @dt @nvanbenschoten

Epic CRDB-2410

Jira issue: CRDB-6144

@a-robinson
Copy link
Contributor Author

As briefly discussed in the SQL meeting yesterday, we should "release" views but should either mark the problem as a known issue or not allow creating views with a star-expansion in them.

To avoid potential backwards-compatibility issues down the road, it seems much safer to prevent creation of views with a star-expansion, even if they kind of work now for simple cases, because it could be tough to migrate them properly down the road. I'll send out a PR for that soon if no one disagrees.

@a-robinson a-robinson self-assigned this Oct 19, 2016
@knz knz added A-sql-semantics C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. S-3-ux-surprise Issue leaves users wondering whether CRDB is behaving properly. Likely to hurt reputation/adoption. labels Oct 27, 2016
@a-robinson a-robinson changed the title sql: Views with complex queries that include a star expansion can break if the underlying table is modified sql: Support view queries with star expansions Oct 28, 2016
@a-robinson a-robinson added C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) and removed C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. labels Oct 28, 2016
@a-robinson
Copy link
Contributor Author

I'm not going to be working on this for a while since it'll depend on some sort of intermediate representation being in place. Unassigning in the meantime.

@a-robinson a-robinson removed their assignment Oct 31, 2016
@petermattis petermattis added this to the Later milestone Feb 23, 2017
@knz knz self-assigned this May 2, 2017
@knz knz added A-sql-optimizer SQL logical planning and optimizations. and removed A-bulkio-schema-changes labels Jul 23, 2018
@knz knz added A-sql-pgcompat Semantic compatibility with PostgreSQL and removed S-3-ux-surprise Issue leaves users wondering whether CRDB is behaving properly. Likely to hurt reputation/adoption. labels Oct 2, 2018
@petermattis petermattis removed this from the Later milestone Oct 5, 2018
knz added a commit to knz/cockroach that referenced this issue Oct 5, 2018
knz added a commit to knz/cockroach that referenced this issue Oct 7, 2018
@martinrode
Copy link

I want to +1 this.

It is painful to have to unfold broader tables in expressions.

@rytaft
Copy link
Collaborator

rytaft commented Apr 27, 2021

cc @kevin-v-ngo and @awoods187 for visibility into possible future feature work

@jlinder jlinder added the T-sql-queries SQL Queries Team label Jun 16, 2021
@vy-ton vy-ton added the A-tools-graphile Issues relating to graphile compatibility label Aug 30, 2021
@ajwerner
Copy link
Contributor

For anybody who finds themselves here, have a look at #10083 (comment) which has some nice context.

@rafiss rafiss added the E-starter Might be suitable for a starter project for new employees or team members. label Oct 13, 2021
@simplenotezy
Copy link

simplenotezy commented Jul 11, 2022

Not sure why, but when we're trying to create a view like so:

CREATE OR REPLACE VIEW public. "match" AS
SELECT
	swipe.swiper_id AS user_id,
	swipe.swiped_id AS match_id,
	swipe.swiper_cell AS user_cell,
	swipe.swiped_cell AS match_cell
FROM
	swipe
	JOIN active_users u1 ON u1.id = swipe.swiper_id
	JOIN active_users u2 ON u2.id = swipe.swiped_id
WHERE
	swipe.swiper_like
	AND swipe.swiped_like
	AND swipe.deleted_at IS NULL;

We are getting the same error:

Query 1 ERROR: ERROR:  unimplemented: views do not currently support * expressions
HINT:  You have attempted to use a feature that is not yet implemented.
See: https://go.crdb.dev/issue-v/10028/v22.1

Not sure why, since we're not using SELECT *

Update

Turns out the error was triggered because of the ON statement was pointing to a non-existing column (id - should have been user_id)

@rytaft
Copy link
Collaborator

rytaft commented Jul 11, 2022

Thanks, @simplenotezy -- I've opened #84178 to track this new issue.

@chengxiong-ruan
Copy link
Contributor

Thanks for tracking with this issue. I think I'm running into the same problem when tracking column dependencies for UDFs :) glad I ended up here.

@vy-ton
Copy link
Contributor

vy-ton commented Aug 3, 2022

@chengxiong-ruan will any of the planned UDF work help lift this restriction?

@ajwerner
Copy link
Contributor

@rharding6373 can we apply #95710 to fix this issue in exactly the same way?

@knz
Copy link
Contributor

knz commented Feb 22, 2023

@rharding6373 can we apply #95710 to fix this issue in exactly the same way?

I have investigated this further today. No we cannot do it easily.

On the one hand, removing the condition on inViewDef makes the CREATE VIEW statement succeed and derive an appropriate number of columns for the view schema.

On the other hand, the moment we do ALTER ADD COLUMN on the table underlying the view, the columns don't align and we get an execution panic.

So handling views properly here means we need to rewrite the * in the SQL stored in the view descriptor, or use a different (binary) encoding of the AST.

I am going to check next whether the UDFs behave properly in that case too.

@ajwerner
Copy link
Contributor

I do not understand. Postgres eagerly binds a * to the set of columns which exist at the time the view was defined. The view is not affected by adding a column to the underlying table. We ought to do what postgres does and not what was initially described.

@ajwerner
Copy link
Contributor

I am going to check next whether the UDFs behave properly in that case too.

The story for UDFs is more interesting. Postgres has two approaches to UDFs. The "legacy" textual definition, we sometimes called late-bound (AS $$ ... $$), and the modern early-bound format BEGIN ATOMIC; ...; END. Cockroach, for better or for worse, chose to always early-bound UDF bodies, even when using the legacy syntax.

I assume what we care about is supporting the RETURNS TABLE syntax (note that SETOF <tablename> returns a single record value). In the late-bound type, with a statically stated table return type, changing the shape of the table behind the * will break the UDF. In the early-bound format, the star is expanded eagerly like in views and thus will continue to work and will not be affected by the added column.

@Lewenhaupt
Copy link

Lewenhaupt commented Jun 5, 2023

According to this thread this should be closed but it appears to still be an issue? Just tried adding a simple view Joining on one other table and selecting main_table.* and it gives me the same error as above and links to this issue.

Tried again with the same "star" table as in the original question and that does not work in my serverless cluster.

@rytaft
Copy link
Collaborator

rytaft commented Jun 5, 2023

@Lewenhaupt this issue is only fixed in CockroachDB v23.1.x. Serverless clusters have not yet been upgraded to v23.1.x, but the plan is for this to happen in the next couple of weeks.

@mgartner mgartner moved this to Done in SQL Queries Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-optimizer SQL logical planning and optimizations. A-sql-pgcompat Semantic compatibility with PostgreSQL A-sql-semantics A-tools-graphile Issues relating to graphile compatibility C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) E-starter Might be suitable for a starter project for new employees or team members. T-sql-queries SQL Queries Team
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.