In this post we are going to cover views in Snowflake.
A view is not a table that updates itself. It is a query with a name. You give Snowflake a SELECT, Snowflake saves it, and every time somebody reads the view that SELECT runs again.
Almost everything surprising about a view follows from that one sentence, so we will start with the simplest one and keep asking what it is really holding. The part that catches most people out is what SELECT * does inside a view, and it is not going stale.
Every query below was run on a real Snowflake account, and every result you see is the output it gave back.
Episode 108 of the Snowflake SQL: Zero to Hero series on TechBrothersIT.
- Let's create some sample data
- Now let's put a view over it
- Let's ask the catalog what each of those two objects is
- So what happens when the base table changes?
- Can we write through a view?
- Now let's write one with SELECT * and ask what Snowflake stored
- Now let's add a column to the table underneath
- Let's fix it the only way there is
- Now let's do it again with a named-column view beside it
- Can the catalog tell us which one is broken?
- Does it break the other way round too?
- Now let's comment and rename a view
- What does SET SECURE actually hide?
- Now let's build a view on another view
- What happens when we drop the base table?
- One last thing: what the catalog shows at the end
Let's create some sample data
One ordinary table. Six sales across three regions.
-- Sample data for this post.
USE DATABASE DEMO_DB;
USE SCHEMA PUBLIC;
CREATE OR REPLACE TABLE SALES_T108 (
SALE_ID INT,
REGION VARCHAR(20),
AMOUNT NUMBER(10,2)
);
INSERT INTO SALES_T108 (SALE_ID, REGION, AMOUNT)
SELECT 1, 'north', 100.00
UNION ALL SELECT 2, 'north', 250.00
UNION ALL SELECT 3, 'south', 75.50
UNION ALL SELECT 4, 'south', 310.00
UNION ALL SELECT 5, 'east', 40.00
UNION ALL SELECT 6, 'east', 60.00;SELECT SALE_ID, REGION, AMOUNT FROM SALES_T108 ORDER BY SALE_ID;| SALE_ID | REGION | AMOUNT |
|---|---|---|
| 1 | north | 100.00 |
| 2 | north | 250.00 |
| 3 | south | 75.50 |
| 4 | south | 310.00 |
| 5 | east | 40.00 |
| 6 | east | 60.00 |
Nothing clever here. This is the table everything else in the post reads from.
Now let's put a view over it
A view is the word VIEW, a name, and a query. Let's save the totals per region.
CREATE OR REPLACE VIEW REGION_TOTALS_T108 AS
SELECT REGION,
COUNT(*) AS SALES,
SUM(AMOUNT) AS TOTAL
FROM SALES_T108
GROUP BY REGION;| status |
|---|
| View REGION_TOTALS_T108 successfully created. |
Now let's read it exactly as though it were a table.
SELECT REGION, SALES, TOTAL FROM REGION_TOTALS_T108 ORDER BY REGION;| REGION | SALES | TOTAL |
|---|---|---|
| east | 2 | 100.00 |
| north | 2 | 350.00 |
| south | 2 | 385.50 |
Three rows, one per region, which is exactly what the query inside the view says. Reading a view looks like reading a table, and that is the point of one.
Let's ask the catalog what each of those two objects is
Both of them appear in INFORMATION_SCHEMA.TABLES, so we can ask about them side by side.
SELECT TABLE_NAME, TABLE_TYPE, ROW_COUNT, BYTES
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC'
AND TABLE_NAME IN ('SALES_T108', 'REGION_TOTALS_T108')
ORDER BY TABLE_NAME;| TABLE_NAME | TABLE_TYPE | ROW_COUNT | BYTES |
|---|---|---|---|
| REGION_TOTALS_T108 | VIEW | NULL | NULL |
| SALES_T108 | BASE TABLE | 6 | 1024 |
The table is a BASE TABLE with a row count and a size in bytes. The view is a VIEW, and its row count and its bytes are both NULL.
Those are real NULLs, which the grid above shows differently from an empty cell. They are not zero either. There is no answer to give, because a view does not hold rows and does not occupy bytes.
That is the whole idea, written down in the catalog.
So what happens when the base table changes?
Let's add one more sale to the table. A big one, in the north.
INSERT INTO SALES_T108 (SALE_ID, REGION, AMOUNT) SELECT 7, 'north', 1000.00;| number of rows inserted |
|---|
| 1 |
Now let's ask the view again. We are not going to refresh anything, or rebuild anything, or wait.
SELECT REGION, SALES, TOTAL FROM REGION_TOTALS_T108 ORDER BY REGION;| REGION | SALES | TOTAL |
|---|---|---|
| east | 2 | 100.00 |
| north | 3 | 1350.00 |
| south | 2 | 385.50 |
North has a third sale now and a much bigger total. The other two regions are untouched.
There is nothing to refresh, because there is nothing held. The query ran again and the table it reads had changed.
Can we write through a view?
Let's try inserting a row into the view.
INSERT INTO REGION_TOTALS_T108 (REGION, SALES, TOTAL) SELECT 'west', 1, 10.00;001759 (42601): SQL compilation error: INSERT statement's target must be a table
001759, and the message is blunt: an INSERT statement's target must be a table.
That view aggregates though, so it would be fair to say it is the GROUP BY being refused rather than the view. Let's take that excuse away and build the simplest view there is. One table, a WHERE clause, no grouping at all.
CREATE OR REPLACE VIEW BIG_SALES_T108 AS
SELECT SALE_ID, REGION, AMOUNT
FROM SALES_T108
WHERE AMOUNT >= 100;| status |
|---|
| View BIG_SALES_T108 successfully created. |
SELECT SALE_ID, REGION, AMOUNT FROM BIG_SALES_T108 ORDER BY SALE_ID;| SALE_ID | REGION | AMOUNT |
|---|---|---|
| 1 | north | 100.00 |
| 2 | north | 250.00 |
| 4 | south | 310.00 |
| 7 | north | 1000.00 |
Four rows, and every one of them is a row of the base table. There is nothing in this view for an insert to be confused by.
INSERT INTO BIG_SALES_T108 (SALE_ID, REGION, AMOUNT) SELECT 8, 'west', 500.00;001759 (42601): SQL compilation error: INSERT statement's target must be a table
The same refusal and the same error code. And an update?
UPDATE BIG_SALES_T108 SET AMOUNT = 0 WHERE SALE_ID = 1;001759 (42601): SQL compilation error: UPDATE statement's target must be a table
Refused as well. Views in Snowflake are read only. All of them, not just the complicated ones.
Now let's write one with SELECT * and ask what Snowflake stored
This is the part of the post that matters, and it starts with something that looks harmless.
CREATE OR REPLACE VIEW EVERYTHING_T108 AS
SELECT * FROM SALES_T108;| status |
|---|
| View EVERYTHING_T108 successfully created. |
SELECT * FROM EVERYTHING_T108 ORDER BY SALE_ID;| SALE_ID | REGION | AMOUNT |
|---|---|---|
| 1 | north | 100.00 |
| 2 | north | 250.00 |
| 3 | south | 75.50 |
| 4 | south | 310.00 |
| 5 | east | 40.00 |
| 6 | east | 60.00 |
| 7 | north | 1000.00 |
Every row and every column of the table. Exactly what you would expect.
Now let's ask Snowflake what it actually saved when we created that view.
SELECT GET_DDL('VIEW', 'EVERYTHING_T108') AS THE_STORED_DEFINITION;| THE_STORED_DEFINITION |
|---|
| create or replace view EVERYTHING_T108( SALE_ID, REGION, AMOUNT ) as SELECT * FROM SALES_T108; |
Read that carefully. There is no star in it. The stored definition names the three columns, and the SELECT * is still in the body underneath them.
Snowflake expanded the star the moment the view was saved, and wrote the column list into the object. It is frozen there.
SELECT ORDINAL_POSITION, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'EVERYTHING_T108'
ORDER BY ORDINAL_POSITION;| ORDINAL_POSITION | COLUMN_NAME |
|---|---|
| 1 | SALE_ID |
| 2 | REGION |
| 3 | AMOUNT |
And the catalog agrees. The view declares three columns, by name, in order.
Now let's add a column to the table underneath
So the view declares three columns. Let's give the table a fourth.
ALTER TABLE SALES_T108 ADD COLUMN CHANNEL VARCHAR(10) DEFAULT 'retail';| status |
|---|
| Statement executed successfully. |
SELECT SALE_ID, REGION, AMOUNT, CHANNEL FROM SALES_T108 ORDER BY SALE_ID;| SALE_ID | REGION | AMOUNT | CHANNEL |
|---|---|---|---|
| 1 | north | 100.00 | retail |
| 2 | north | 250.00 | retail |
| 3 | south | 75.50 | retail |
| 4 | south | 310.00 | retail |
| 5 | east | 40.00 | retail |
| 6 | east | 60.00 | retail |
| 7 | north | 1000.00 | retail |
The table has its new column and every existing row has the default in it. That part went fine.
SELECT (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'SALES_T108') AS COLUMNS_IN_THE_TABLE,
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'EVERYTHING_T108') AS COLUMNS_IN_THE_VIEW;| COLUMNS_IN_THE_TABLE | COLUMNS_IN_THE_VIEW |
|---|---|
| 4 | 3 |
Four columns in the table, three in the view. Now let's ask the view for the new column.
SELECT SALE_ID, CHANNEL FROM EVERYTHING_T108 ORDER BY SALE_ID;002057 (42601): SQL compilation error: View definition for 'DEMO_DB.PUBLIC.EVERYTHING_T108' declared 3 column(s), but view query produces 4 column(s).
002057. Read the message, because it is not the one I expected: “declared 3 column(s), but view query produces 4 column(s)”.
It is not telling us the column is unknown. It is telling us the view no longer agrees with itself. The declaration says one thing and the body says another, and Snowflake will not guess which one we meant.
Not stale. Refused.
Let's fix it the only way there is
There is no repair command for this. The fix is to save the view again, which expands the star again, over the table as it is now.
CREATE OR REPLACE VIEW EVERYTHING_T108 AS
SELECT * FROM SALES_T108;| status |
|---|
| View EVERYTHING_T108 successfully created. |
SELECT * FROM EVERYTHING_T108 ORDER BY SALE_ID;| SALE_ID | REGION | AMOUNT | CHANNEL |
|---|---|---|---|
| 1 | north | 100.00 | retail |
| 2 | north | 250.00 | retail |
| 3 | south | 75.50 | retail |
| 4 | south | 310.00 | retail |
| 5 | east | 40.00 | retail |
| 6 | east | 60.00 | retail |
| 7 | north | 1000.00 | retail |
Four columns, and it works. Exactly the same statement we wrote the first time, with a different result frozen into it.
Which is worth sitting with for a second. The text of your view definition does not tell you what the view declares. The day you last ran it does.
Now let's do it again with a named-column view beside it
The refusal above is the finding of this episode, and the first run of the demo nearly lost it. I expected the view to go quietly stale rather than refuse, so the query went in unmarked, and the capture harness of that evening stored nothing for an unmarked failure. The harness was fixed the same night, and a follow-up file asked the question again with the right marker and added the comparison the first file never made. That is why the objects below carry a B on the end of their names.
Two views over one table. The first written SELECT *, the lazy one.
-- The follow-up run, on names of its own.
CREATE OR REPLACE TABLE SALES_T108B (
SALE_ID INT,
REGION VARCHAR(20),
AMOUNT NUMBER(10,2)
);
CREATE OR REPLACE VIEW STAR_VIEW_T108B AS
SELECT * FROM SALES_T108B;INSERT INTO SALES_T108B (SALE_ID, REGION, AMOUNT) SELECT 1, 'north', 100.00
UNION ALL SELECT 2, 'north', 250.00
UNION ALL SELECT 3, 'south', 75.50
UNION ALL SELECT 4, 'east', 40.00;| number of rows inserted |
|---|
| 4 |
And the second naming its three columns, which is the only difference between them.
CREATE OR REPLACE VIEW NAMED_VIEW_T108B AS
SELECT SALE_ID, REGION, AMOUNT FROM SALES_T108B;| status |
|---|
| View NAMED_VIEW_T108B successfully created. |
SELECT * FROM STAR_VIEW_T108B ORDER BY SALE_ID;| SALE_ID | REGION | AMOUNT |
|---|---|---|
| 1 | north | 100.00 |
| 2 | north | 250.00 |
| 3 | south | 75.50 |
| 4 | east | 40.00 |
SELECT SALE_ID, REGION, AMOUNT FROM NAMED_VIEW_T108B ORDER BY SALE_ID;| SALE_ID | REGION | AMOUNT |
|---|---|---|
| 1 | north | 100.00 |
| 2 | north | 250.00 |
| 3 | south | 75.50 |
| 4 | east | 40.00 |
Identical results. You could not tell them apart from the output.
SELECT (SELECT COUNT(*) FROM STAR_VIEW_T108B) AS ROWS_FROM_THE_STAR_VIEW,
(SELECT COUNT(*) FROM NAMED_VIEW_T108B) AS ROWS_FROM_THE_NAMED_VIEW;| ROWS_FROM_THE_STAR_VIEW | ROWS_FROM_THE_NAMED_VIEW |
|---|---|
| 4 | 4 |
Let's ask what Snowflake stored for the star one.
SELECT GET_DDL('VIEW', 'STAR_VIEW_T108B') AS THE_STORED_DEFINITION;| THE_STORED_DEFINITION |
|---|
| create or replace view STAR_VIEW_T108B( SALE_ID, REGION, AMOUNT ) as SELECT * FROM SALES_T108B; |
| ORDINAL_POSITION | COLUMN_NAME |
|---|---|
| 1 | SALE_ID |
| 2 | REGION |
| 3 | AMOUNT |
The same thing as before. The star is gone and three column names are written into the object.
Now let's add the fourth column to this table too.
ALTER TABLE SALES_T108B ADD COLUMN CHANNEL VARCHAR(10) DEFAULT 'retail';SELECT (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'SALES_T108B') AS COLUMNS_IN_THE_TABLE,
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'STAR_VIEW_T108B') AS COLUMNS_THE_VIEW_DECLARES;| COLUMNS_IN_THE_TABLE | COLUMNS_THE_VIEW_DECLARES |
|---|---|
| 4 | 3 |
Four in the table, three declared by the view. Now let's ask the star view for everything.
SELECT * FROM STAR_VIEW_T108B ORDER BY SALE_ID;002057 (42601): SQL compilation error: View definition for 'DEMO_DB.PUBLIC.STAR_VIEW_T108B' declared 3 column(s), but view query produces 4 column(s).
Refused, same code, same sentence. And it is worse than it looks. Let's ask it for only the three columns it does declare.
SELECT SALE_ID, REGION, AMOUNT FROM STAR_VIEW_T108B ORDER BY SALE_ID;002057 (42601): SQL compilation error: View definition for 'DEMO_DB.PUBLIC.STAR_VIEW_T108B' declared 3 column(s), but view query produces 4 column(s).
The same error. So let's not ask for any columns at all and just count the rows.
SELECT COUNT(*) AS ROWS_PRESENT FROM STAR_VIEW_T108B;002057 (42601): SQL compilation error: View definition for 'DEMO_DB.PUBLIC.STAR_VIEW_T108B' declared 3 column(s), but view query produces 4 column(s).
The same error again. The view is unusable entirely, for every query you can put to it, because a column was added to a table it does not even mention by name.
And the one that spelled its columns out?
SELECT SALE_ID, REGION, AMOUNT FROM NAMED_VIEW_T108B ORDER BY SALE_ID;| SALE_ID | REGION | AMOUNT |
|---|---|---|
| 1 | north | 100.00 |
| 2 | north | 250.00 |
| 3 | south | 75.50 |
| 4 | east | 40.00 |
Four rows, completely fine. The new column is not in its definition, it was never in its definition, and nothing about it changed.
SELECT (SELECT COUNT(*) FROM NAMED_VIEW_T108B) AS ROWS_FROM_THE_NAMED_VIEW,
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'STAR_VIEW_T108B')
AS STAR_VIEW_STILL_IN_CATALOG,
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'NAMED_VIEW_T108B')
AS NAMED_VIEW_STILL_IN_CATALOG;| ROWS_FROM_THE_NAMED_VIEW | STAR_VIEW_STILL_IN_CATALOG | NAMED_VIEW_STILL_IN_CATALOG |
|---|---|---|
| 4 | 1 | 1 |
One keyword was the whole difference. And both views are still listed in the catalog.
Can the catalog tell us which one is broken?
That last grid counted both views as present, which is already the uncomfortable part. Let's try to ask the catalog the question directly.
SELECT TABLE_NAME, TABLE_TYPE, IS_SECURE
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC' AND RIGHT(TABLE_NAME, 6) = '_T108B'
ORDER BY TABLE_NAME;000904 (42000): SQL compilation error: error line 1 at position 19 invalid identifier 'TABLE_TYPE'
That one is my mistake rather than Snowflake's. INFORMATION_SCHEMA.VIEWS has no TABLE_TYPE column, so the query never ran. I am leaving it on the page because the error is useful: if you go looking for TABLE_TYPE, it lives in INFORMATION_SCHEMA.TABLES, not in the views view.
The question still has an answer, and the grid above the refusal is where it is. Both views counted as present, one of them answers every query and the other answers none, and there is no column anywhere in the catalog saying which is which.
A broken view looks exactly like a working one until somebody reads it. Nothing lists it as invalid, nothing warns you when you change the table, and the ALTER TABLE that broke it succeeded quietly.
Does it break the other way round too?
Let's save the star view again, so it declares four columns, and then take a column away instead of adding one.
CREATE OR REPLACE VIEW STAR_VIEW_T108B AS
SELECT * FROM SALES_T108B;| status |
|---|
| View STAR_VIEW_T108B successfully created. |
SELECT * FROM STAR_VIEW_T108B ORDER BY SALE_ID;| SALE_ID | REGION | AMOUNT | CHANNEL |
|---|---|---|---|
| 1 | north | 100.00 | retail |
| 2 | north | 250.00 | retail |
| 3 | south | 75.50 | retail |
| 4 | east | 40.00 | retail |
Working again, with four columns declared. Now let's drop that column from the table.
ALTER TABLE SALES_T108B DROP COLUMN CHANNEL;| status |
|---|
| Statement executed successfully. |
SELECT * FROM STAR_VIEW_T108B ORDER BY SALE_ID;002057 (42601): SQL compilation error: View definition for 'DEMO_DB.PUBLIC.STAR_VIEW_T108B' declared 4 column(s), but view query produces 3 column(s).
Refused, and read the numbers in the message: declared four, produces three. The same complaint with the two numbers the other way round.
So either direction breaks it. Adding a column breaks it and removing one breaks it, and in both cases the view is the thing that stops working while the table is perfectly happy.
SELECT SALE_ID, REGION, AMOUNT FROM NAMED_VIEW_T108B ORDER BY SALE_ID;| SALE_ID | REGION | AMOUNT |
|---|---|---|
| 1 | north | 100.00 |
| 2 | north | 250.00 |
| 3 | south | 75.50 |
| 4 | east | 40.00 |
The named view carried on without a murmur, again. It never mentioned that column, so dropping it was none of its business.
SELECT * in a worksheet is fine, and we said so back in Episode 1. SELECT * inside anything you save is a different thing, because the star is resolved once and kept.
Name your columns in a view. It is more typing on the day you write it and it is the difference between a view that ignores a schema change and a view that refuses every query until somebody rebuilds it.
Now let's comment and rename a view
A view takes a comment and a rename the same way a table does. Let's comment the totals view.
ALTER VIEW REGION_TOTALS_T108 SET COMMENT = 'totals per region - episode 108';| status |
|---|
| Statement executed successfully. |
SELECT TABLE_NAME, COMMENT, IS_SECURE
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'REGION_TOTALS_T108';| TABLE_NAME | COMMENT | IS_SECURE |
|---|---|---|
| REGION_TOTALS_T108 | totals per region - episode 108 | NO |
The comment is recorded, and there is an IS_SECURE column next to it reading NO. Hold on to that column, we are coming back to it.
Now let's rename the view.
ALTER VIEW REGION_TOTALS_T108 RENAME TO TOTALS_BY_REGION_T108;| status |
|---|
| Statement executed successfully. |
SELECT REGION, TOTAL FROM REGION_TOTALS_T108 ORDER BY REGION;002003 (42S02): SQL compilation error: Object 'REGION_TOTALS_T108' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have at least one privilege granted on TABLE REGION_TOTALS_T108.
The old name is gone, with the same 002003 a dropped table gives. A rename is not a copy.
SELECT REGION, SALES, TOTAL FROM TOTALS_BY_REGION_T108 ORDER BY REGION;| REGION | SALES | TOTAL |
|---|---|---|
| east | 2 | 100.00 |
| north | 3 | 1350.00 |
| south | 2 | 385.50 |
And the new name gives the same three rows.
SELECT TABLE_NAME, COMMENT
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'TOTALS_BY_REGION_T108';| TABLE_NAME | COMMENT |
|---|---|
| TOTALS_BY_REGION_T108 | totals per region - episode 108 |
The comment came with it. Comments follow the object, not the name.
What does SET SECURE actually hide?
This is the part I did not know the answer to. First, let's read the definition of an ordinary view out of the catalog.
SELECT TABLE_NAME, VIEW_DEFINITION
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'BIG_SALES_T108';| TABLE_NAME | VIEW_DEFINITION |
|---|---|
| BIG_SALES_T108 | CREATE OR REPLACE VIEW BIG_SALES_T108 AS SELECT SALE_ID, REGION, AMOUNT FROM SALES_T108 WHERE AMOUNT >= 100 |
There it is, the whole SELECT statement in plain text. Anybody who can see the catalog can read it.
Now let's make it secure.
ALTER VIEW BIG_SALES_T108 SET SECURE;| status |
|---|
| Statement executed successfully. |
SELECT TABLE_NAME, IS_SECURE
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC'
AND TABLE_NAME IN ('BIG_SALES_T108', 'TOTALS_BY_REGION_T108')
ORDER BY TABLE_NAME;| TABLE_NAME | IS_SECURE |
|---|---|
| BIG_SALES_T108 | YES |
| TOTALS_BY_REGION_T108 | NO |
The flag flipped to YES for the one we changed and the other view is still NO. So the statement did something. The question is what.
SELECT TABLE_NAME, VIEW_DEFINITION
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'BIG_SALES_T108';| TABLE_NAME | VIEW_DEFINITION |
|---|---|
| BIG_SALES_T108 | CREATE OR REPLACE VIEW BIG_SALES_T108 AS SELECT SALE_ID, REGION, AMOUNT FROM SALES_T108 WHERE AMOUNT >= 100 |
Still completely readable. The whole definition, exactly as before.
SELECT GET_DDL('VIEW', 'BIG_SALES_T108') AS THE_STORED_DEFINITION;| THE_STORED_DEFINITION |
|---|
| create or replace secure view BIG_SALES_T108( SALE_ID, REGION, AMOUNT ) as SELECT SALE_ID, REGION, AMOUNT FROM SALES_T108 WHERE AMOUNT >= 100; |
And GET_DDL prints the word secure in the statement, with the body right underneath it.
The reason is ownership. This session owns the view. SECURE is about what other roles can see, and the owner is not another role.
This is worth knowing before you assume a secure view is a black box. Run these two queries as yourself and the definition is right there, which makes it very easy to conclude that SET SECURE did nothing. To see what it does you have to ask from a role that does not own the view.
SELECT SALE_ID, REGION, AMOUNT FROM BIG_SALES_T108 ORDER BY SALE_ID;| SALE_ID | REGION | AMOUNT |
|---|---|---|
| 1 | north | 100.00 |
| 2 | north | 250.00 |
| 4 | south | 310.00 |
| 7 | north | 1000.00 |
Reading it still works, of course, with the same four rows.
ALTER VIEW BIG_SALES_T108 UNSET SECURE;| status |
|---|
| Statement executed successfully. |
SELECT TABLE_NAME, IS_SECURE
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'BIG_SALES_T108';| TABLE_NAME | IS_SECURE |
|---|---|
| BIG_SALES_T108 | NO |
And the flag goes back. It is one property, on and off.
Now let's build a view on another view
Views stack. Let's take the totals per region and put a second view on top of it that keeps only the top region.
CREATE OR REPLACE VIEW TOP_REGION_T108 AS
SELECT REGION, TOTAL
FROM TOTALS_BY_REGION_T108
QUALIFY ROW_NUMBER() OVER (ORDER BY TOTAL DESC) = 1;| status |
|---|
| View TOP_REGION_T108 successfully created. |
SELECT REGION, TOTAL FROM TOTALS_BY_REGION_T108 ORDER BY TOTAL DESC;| REGION | TOTAL |
|---|---|
| north | 1350.00 |
| south | 385.50 |
| east | 100.00 |
Those are the three regions in order, biggest total first.
SELECT REGION, TOTAL FROM TOP_REGION_T108;| REGION | TOTAL |
|---|---|
| north | 1350.00 |
North, with the biggest total. Two levels of query and one answer.
If you are reading this after watching the video, the narration calls the top region west at this point. That is wrong, and the grid above is why: there is no west row in the table yet. West turns up later in this post, when we recreate the table from scratch. The top region here is north.
Now let's drop the view in the middle.
DROP VIEW TOTALS_BY_REGION_T108;| status |
|---|
| TOTALS_BY_REGION_T108 successfully dropped. |
And ask the one built on top of it.
SELECT REGION, TOTAL FROM TOP_REGION_T108;002037 (42601): SQL compilation error: Failure during expansion of view 'TOP_REGION_T108': SQL compilation error: Object 'DEMO_DB.PUBLIC.TOTALS_BY_REGION_T108' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have at least one privilege granted on TABLE DEMO_DB.PUBLIC.TOTALS_BY_REGION_T108.
002037, failure during expansion of view. The message quotes the object it could not resolve, which is more help than most errors give you.
SELECT TABLE_NAME, TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'TOP_REGION_T108';| TABLE_NAME | TABLE_TYPE |
|---|---|
| TOP_REGION_T108 | VIEW |
And it is still in the catalog, still listed as a view. Broken, listed, and looking exactly like the others.
What happens when we drop the base table?
Something harsher. Let's drop the table everything reads from.
DROP TABLE SALES_T108;| status |
|---|
| SALES_T108 successfully dropped. |
SELECT TABLE_NAME, TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC'
AND TABLE_NAME IN ('SALES_T108', 'EVERYTHING_T108', 'BIG_SALES_T108')
ORDER BY TABLE_NAME;| TABLE_NAME | TABLE_TYPE |
|---|---|
| BIG_SALES_T108 | VIEW |
| EVERYTHING_T108 | VIEW |
The table is gone from that list and both views are still there. Neither of them noticed.
SELECT SALE_ID, REGION FROM BIG_SALES_T108 ORDER BY SALE_ID;002037 (42601): SQL compilation error: Failure during expansion of view 'BIG_SALES_T108': SQL compilation error: Object 'DEMO_DB.PUBLIC.SALES_T108' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have at least one privilege granted on TABLE DEMO_DB.PUBLIC.SALES_T108.
The same shape of error as the dropped view gave us: failure during expansion, naming the table it could not find.
Now let's put the table back. Same columns, two new rows, nothing else.
CREATE OR REPLACE TABLE SALES_T108 (
SALE_ID INT,
REGION VARCHAR(20),
AMOUNT NUMBER(10,2),
CHANNEL VARCHAR(10) DEFAULT 'retail'
);| status |
|---|
| Table SALES_T108 successfully created. |
INSERT INTO SALES_T108 (SALE_ID, REGION, AMOUNT) SELECT 10, 'west', 500.00
UNION ALL SELECT 11, 'west', 20.00;| number of rows inserted |
|---|
| 2 |
SELECT SALE_ID, REGION, AMOUNT FROM BIG_SALES_T108 ORDER BY SALE_ID;| SALE_ID | REGION | AMOUNT |
|---|---|---|
| 10 | west | 500.00 |
The view works again, on its own, and nobody rebuilt it. Only one of the two new rows comes back, because that view filters on the amount and the smaller row does not qualify.
SELECT * FROM EVERYTHING_T108 ORDER BY SALE_ID;| SALE_ID | REGION | AMOUNT | CHANNEL |
|---|---|---|---|
| 10 | west | 500.00 | retail |
| 11 | west | 20.00 | retail |
And the star view too, with four columns, because we saved it again earlier and the recreated table has the same four.
This is the sentence that explains the whole post. A view resolves its tables by name, every single time it runs. That is why it healed here, and it is why it broke in the first place.
One last thing: what the catalog shows at the end
Let's ask SHOW VIEWS what is in this schema before we tidy up.
SHOW VIEWS IN SCHEMA PUBLIC;| created_on | name | reserved | database_name | schema_name | owner | comment | text | is_secure | is_materialized | owner_role_type | change_tracking |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 2026-09-28 18:24:42.426000-07:00 | BIG_SALES_T108 | DEMO_DB | PUBLIC | ACCOUNTADMIN | CREATE OR REPLACE VIEW BIG_SALES_T108 AS SELECT SALE_ID, REGION, AMOUNT FROM SALES_T108 WHERE AMOUNT >= 100 | false | false | ROLE | OFF | ||
| 2026-09-28 18:24:46.695000-07:00 | EVERYTHING_T108 | DEMO_DB | PUBLIC | ACCOUNTADMIN | CREATE OR REPLACE VIEW EVERYTHING_T108 AS SELECT * FROM SALES_T108 | false | false | ROLE | OFF | ||
| 2026-09-28 18:24:51.847000-07:00 | TOP_REGION_T108 | DEMO_DB | PUBLIC | ACCOUNTADMIN | CREATE OR REPLACE VIEW TOP_REGION_T108 AS SELECT REGION, TOTAL FROM TOTALS_BY_REGION_T108 QUALIFY ROW_NUMBER() OVER (ORDER BY TOTAL DESC) = 1 | false | false | ROLE | OFF |
The whole definition of each view is in the text column, which is the quickest way to see what a view really is. Have a look at the comment cells too: they are empty, which the grid marks differently from a NULL. The one view we commented was renamed and then dropped, so it is not in this list.
SELECT COUNT(*) AS VIEWS_IN_THIS_SCHEMA_FROM_THIS_FILE
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC' AND RIGHT(TABLE_NAME, 5) = '_T108';| VIEWS_IN_THIS_SCHEMA_FROM_THIS_FILE |
|---|
| 3 |
Counted from INFORMATION_SCHEMA.VIEWS, which agrees with SHOW VIEWS about how many there are.
Finally, let's clean up after ourselves.
-- Put the schema back the way we found it.
DROP VIEW IF EXISTS TOP_REGION_T108;
DROP VIEW IF EXISTS EVERYTHING_T108;
DROP VIEW IF EXISTS BIG_SALES_T108;
DROP VIEW IF EXISTS NAMED_VIEW_T108B;
DROP VIEW IF EXISTS STAR_VIEW_T108B;
DROP TABLE IF EXISTS SALES_T108;
DROP TABLE IF EXISTS SALES_T108B;DROP VIEW IF EXISTS TOP_REGION_T108;| status |
|---|
| TOP_REGION_T108 successfully dropped. |
| status |
|---|
| EVERYTHING_T108 successfully dropped. |
| status |
|---|
| BIG_SALES_T108 successfully dropped. |
| status |
|---|
| NAMED_VIEW_T108B successfully dropped. |
DROP TABLE IF EXISTS SALES_T108;| status |
|---|
| SALES_T108 successfully dropped. |
SELECT CURRENT_DATABASE() AS THE_DATABASE,
CURRENT_SCHEMA() AS THE_SCHEMA,
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
WHERE RIGHT(TABLE_NAME, 5) = '_T108') AS PROBE_OBJECTS_LEFT,
(SELECT COUNT(*) FROM CUSTOMERS) AS DEMO_DB_IS_READABLE;| THE_DATABASE | THE_SCHEMA | PROBE_OBJECTS_LEFT | DEMO_DB_IS_READABLE |
|---|---|---|---|
| DEMO_DB | PUBLIC | 0 | 10 |
| THE_DATABASE | THE_SCHEMA | PROBE_OBJECTS_LEFT | DEMO_DB_IS_READABLE |
|---|---|---|---|
| DEMO_DB | PUBLIC | 0 | 10 |
Nothing left from either run, and the demo database still reads. Back where we started.
Key points
- A view is a saved query. The catalog gives it a
ROW_COUNTand aBYTESofNULL, because it holds nothing. - Change the base table and the view answers differently at once. There is nothing to refresh.
- You cannot
INSERTorUPDATEthrough a view, and that includes a single-table view with no aggregate. Both get001759. SELECT *inside a view is expanded when you save it.GET_DDLshows the column list and no star.- Add a column to the table and the star view does not go stale, it refuses — declared 3 column(s), but view query produces 4 column(s).
- It refuses every query, including the three columns it does declare and a bare
COUNT(*). - Drop a column instead and it breaks the same way with the numbers reversed: declared four, produces three.
- A view that named its columns survived both changes untouched.
- The broken view is still listed in the catalog and nothing says it is broken.
ALTER VIEWhandlesSET COMMENTandRENAME TO, and the comment follows the object through the rename.SET SECUREflipsIS_SECUREtoYESand does not hide the definition from the owner.GET_DDLprints the body right under the wordsecure.- Drop the view underneath a view, or the table underneath both, and you get
002037failure during expansion — while the views stay in the catalog looking healthy. - Put the table back and the views work again by themselves, because a view resolves its tables by name every time it runs.
Episode 109 — materialized views. The object that does store its results, and refuses most of the queries a view takes without complaint. The list of refusals is the specification.
Every query and every result grid on this page was captured from a live Snowflake account (version 10.34.101) running the demo script for this episode. The episode ran twice and the post says where it switches: the named-versus-star comparison is on SALES_T108B from a follow-up run, whose setup appears as plain code with no grid. One query on this page was written wrong and failed, and the text beside it says so rather than hiding it. The spoken narration names the wrong region in the stacked-view section, and the note there says which region the grid gives. Every count in the text is read off one of the grids on this page. Nothing is timed and no performance claims are made anywhere in this series. Behaviour follows the Snowflake CREATE VIEW documentation.



No comments:
Post a Comment
Note: Only a member of this blog may post a comment.