Skip to content

Conversation

@apstndb
Copy link

@apstndb apstndb commented Feb 6, 2025

This PR implements CREATE [OR REPLACE] VIEW suppoort.

TODO:

  • Ensure ordering when VIEWs have dependencies.
    • May be needed to consider recreate.

Related Issue

@apstndb apstndb force-pushed the feature/support-views branch from 32366e4 to 3ea7384 Compare February 6, 2025 09:17
@apstndb apstndb mentioned this pull request Feb 6, 2025
@nielm nielm self-requested a review February 6, 2025 09:32
@nielm nielm self-assigned this Feb 6, 2025
@nielm
Copy link
Collaborator

nielm commented Feb 6, 2025

Thank you for this - I will look into it in the next couple of days...

As for ordering, views depend on tables and indexes, so drop the views before dropping indexes, and create the view after creating the tables and indexes.

A modified view would have to be implemented as a drop / recreate, as a modified view may be due to table cols being dropped and table cols being created....

@apstndb
Copy link
Author

apstndb commented Feb 6, 2025

Thank you for quick replying.
I am doubting that there are edge cases when views depends on other views.

@nielm
Copy link
Collaborator

nielm commented Feb 6, 2025

Ah, good point.

I deal with this in tables by keeping the creation order of the tables from the original DDL and ensure that tables are created in the same order (and deleted in the reverse order).

IIRC I use a linkedhashmap for storing the table objects so they the insertion order of the keys (table names) is preserved, so this can be used to ensure that creation / deletion order of the views are preserved.

@apstndb
Copy link
Author

apstndb commented Feb 6, 2025

Original(final depends on preprocess)

CREATE OR REPLACE VIEW preprocess SQL SECURITY INVOKER AS SELECT 1 AS n;
CREATE OR REPLACE VIEW final SQL SECURITY INVOKER AS SELECT v.n FROM preprocess AS v;

New(preprocess only modified)

CREATE OR REPLACE VIEW preprocess SQL SECURITY INVOKER AS SELECT 1 AS n, "foo" AS s;
CREATE OR REPLACE VIEW final SQL SECURITY INVOKER AS SELECT v.n FROM preprocess AS v;

Expected output

Ideally, output should be this order because final depends on preprocess so DROP VIEW preprocess can't be executed before DROP VIEW final.

DROP VIEW final;
DROP VIEW preprocess;
CREATE OR REPLACE VIEW preprocess SQL SECURITY INVOKER AS SELECT 1 AS n, "foo" AS s;
CREATE OR REPLACE VIEW final SQL SECURITY INVOKER AS SELECT v.n FROM preprocess AS v;

But it is hard to detect final is needed to be re-create because view_definition is not parsed.
It may be possible workaround to re-create all views, even if itself is not updated(and it is not wanted).

Note: In this case, CREATE OR REPLACE VIEW preprocess is valid, but CREATE OR REPLACE is not possible when dependencies are re-created.

Note

I expects extractTableNamesFromStatement of ZetaSQL can be used to extract dependencies in view_definition.
https://github.com/google/zetasql/blob/a516c6b26d183efc4f56293256bba92e243b7a61/java/com/google/zetasql/Analyzer.java#L152-L162

But ZetaSQL JNI binding contains native binary and it seems that there is Apple Silicon compatibility problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants