Skip to content

Views: if exists / if not exists for DDL #10831

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

Merged
merged 4 commits into from
Oct 29, 2024

Conversation

jepett0
Copy link
Collaborator

@jepett0 jepett0 commented Oct 24, 2024

Description

CREATE VIEW IF NOT EXISTS ...;
DROP VIEW IF EXISTS ...;

Introduce IF ... modifiers for the VIEW DDL to not produce errors if the object already exists / does not exist.

Context

We would like VIEW users to be able to batch create views with scripts (which should not produce errors during the execution) like the following one:

CREATE OR REPLACE VIEW first_view ...;
CREATE OR REPLACE VIEW second_view ...;
CREATE OR REPLACE VIEW third_view ...;
...

However, CREATE OR REPLACE VIEW is a bit harder to implement (it needs to drop the view and recreate it in one transaction + update the server side query cache ☆) than the simple alternative:

DROP VIEW IF EXISTS first_view;
CREATE VIEW first_view ...;

DROP VIEW IF EXISTS second_view;
CREATE VIEW second_view ...;
...

Ideally the server side query cache entry for the queries referencing the view-to-be-dropped should be deleted in the same transaction as the view. However, this feature is not easy to implement (see this PR for more info). I suppose the query cache update lag would be less of an issue for the scripts using DROP VIEW + CREATE VIEW rather than a single CREATE OR REPLACE VIEW, because the later is expected to be atomic.

This comment was marked as outdated.

This comment was marked as outdated.

@jepett0 jepett0 marked this pull request as ready for review October 24, 2024 12:57
@jepett0 jepett0 requested a review from a team as a code owner October 24, 2024 12:57
@jepett0 jepett0 requested review from ijon, dorooleg and UgnineSirdis and removed request for dorooleg October 24, 2024 14:22
better SQL unit tests
@jepett0 jepett0 requested a review from vitstn October 26, 2024 12:00

This comment was marked as outdated.

This comment was marked as outdated.

);
ExecuteQuery(session, dropQuery);
// an attempt to drop an already deleted view does not produce an error
ExecuteQuery(session, dropQuery);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need more tests to the following cases:

  • DROP VIEW query must fail if there is no such view
  • DROP VIEW query must fail if we are attempting to delete view, but it is not a view (topic, table etc)
  • DROP VIEW IF EXISTS query must fail if we are attempting to delete view, but it is not a view (topic, table etc)

All these cases should work also for ExecuteQuery calls (not only for ExecuteSchemeQuery) - they are newer API

Also the same cases, but for CREATE type

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added all 3 DROP VIEW tests you mentioned.

As for the CREATE VIEW, the test that checks that the CREATE VIEW statement does in fact create a view has already existed, so I haven't added it. However, I have added the other two tests for the CREATE VIEW statement.

more tests
Copy link

github-actions bot commented Oct 29, 2024

2024-10-29 11:06:41 UTC Pre-commit check linux-x86_64-relwithdebinfo for 7463682 has started.
2024-10-29 11:06:52 UTC Artifacts will be uploaded here
2024-10-29 11:10:06 UTC ya make is running...
🟢 2024-10-29 12:14:12 UTC Tests successful.

Test history | Ya make output | Test bloat

TESTS PASSED ERRORS FAILED SKIPPED MUTED?
76222 62172 0 0 13952 98

🟢 2024-10-29 12:21:15 UTC Build successful.
🟡 2024-10-29 12:21:34 UTC ydbd size 2.8 GiB changed* by +169.4 KiB, which is >= 100.0 KiB vs main: Warning

ydbd size dash main: 8acfed4 merge: 7463682 diff diff %
ydbd size 3 033 991 864 Bytes 3 034 165 320 Bytes +169.4 KiB +0.006%
ydbd stripped size 480 683 032 Bytes 480 706 168 Bytes +22.6 KiB +0.005%

*please be aware that the difference is based on comparing your commit and the last completed build from the post-commit, check comparation

Copy link

github-actions bot commented Oct 29, 2024

2024-10-29 11:06:51 UTC Pre-commit check linux-x86_64-release-asan for 7463682 has started.
2024-10-29 11:07:03 UTC Artifacts will be uploaded here
2024-10-29 11:10:12 UTC ya make is running...
🟡 2024-10-29 12:29:10 UTC Some tests failed, follow the links below. This fail is not in blocking policy yet

Test history | Ya make output | Test bloat

TESTS PASSED ERRORS FAILED SKIPPED MUTED?
12041 11945 0 29 34 33

🟢 2024-10-29 12:30:09 UTC Build successful.
🟡 2024-10-29 12:30:39 UTC ydbd size 5.7 GiB changed* by +202.4 KiB, which is >= 100.0 KiB vs main: Warning

ydbd size dash main: 8acfed4 merge: 7463682 diff diff %
ydbd size 6 139 835 928 Bytes 6 140 043 184 Bytes +202.4 KiB +0.003%
ydbd stripped size 1 532 105 648 Bytes 1 532 155 344 Bytes +48.5 KiB +0.003%

*please be aware that the difference is based on comparing your commit and the last completed build from the post-commit, check comparation

@jepett0 jepett0 requested a review from UgnineSirdis October 29, 2024 11:07
@jepett0 jepett0 merged commit 7c45196 into ydb-platform:main Oct 29, 2024
10 checks passed
jepett0 added a commit to jepett0/ydb that referenced this pull request Oct 30, 2024
This was referenced Nov 1, 2024
robot-piglet pushed a commit that referenced this pull request Nov 12, 2024
#11152 - Allow to choose normal or aggreation PG function

#11075 - Handle invalid base

#11068 - Allow more postgis functions

#10831 - Views: if exists / if not exists for DDL
commit_hash:0ebf35e45ac6de147c9000440ca25237db061d2e
maximyurchuk pushed a commit that referenced this pull request Nov 12, 2024
#11152 - Allow to choose normal or aggreation PG function

#11075 - Handle invalid base

#11068 - Allow more postgis functions

#10831 - Views: if exists / if not exists for DDL
commit_hash:0ebf35e45ac6de147c9000440ca25237db061d2e
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.

views: IF EXISTS / IF NOT EXISTS to help with scripting view dropping / creation
3 participants