In this post we are going to cover querying a stage directly in Snowflake.
Not loading it. Querying it. @STAGE/path is a FROM target, the same way a table is, and $1, $2 and $3 are its columns. Which means you can look at a file properly before you commit to a table shape for it.
We will start with six rows out of two files and no table anywhere in the query, and build it up step by step. The first thing you will type at a stage is the one thing it refuses, so we will get that out of the way early.
Every query below was run on a real Snowflake account, and every result you see is the output it gave back.
Episode 124 of the Snowflake SQL: Zero to Hero series on TechBrothersIT.
- Let's put four files on a stage
- Let's read two of them with no table anywhere
- What happens when we write SELECT *?
- So what is $1, exactly?
- What happens if we ask for a column the file has not got?
- Now the column you always wish you had
- Three more metadata columns, and one that is not real
- Let's pick just one of the files with PATTERN
- And here is what a PATTERN typo costs
- Now the JSON file
- Can we alias it, join it, and put it in a CTE?
- Can we build a view over a stage?
- Three refusals worth recognising
- One last thing: the two byte counts that disagreed
- And let's tidy up
Let's put four files on a stage
We need a stage, a CSV format and a JSON format. All three came up in the last two posts.
-- A stage and two file formats to work with.
USE DATABASE DEMO_DB;
USE SCHEMA PUBLIC;
CREATE OR REPLACE STAGE STAGE_T124;
CREATE OR REPLACE FILE FORMAT CSV_T124
TYPE = CSV
FIELD_OPTIONALLY_ENCLOSED_BY = '"';
CREATE OR REPLACE FILE FORMAT JSON_T124 TYPE = JSON;Now four files. Two CSVs with three rows each, one short file with a row missing a column, and some JSON. All four go up with the compression turned off, so the names stay as they are.
PUT 'file://C:/your/folder/t124_a.csv' @STAGE_T124/many/ AUTO_COMPRESS = FALSE;| source | target | source_size | target_size | source_compression | target_compression | status | message |
|---|---|---|---|---|---|---|---|
| t124_a.csv | t124_a.csv | 51 | 64 | NONE | NONE | UPLOADED |
PUT 'file://C:/your/folder/t124_b.csv' @STAGE_T124/many/ AUTO_COMPRESS = FALSE;| source | target | source_size | target_size | source_compression | target_compression | status | message |
|---|---|---|---|---|---|---|---|
| t124_b.csv | t124_b.csv | 56 | 64 | NONE | NONE | UPLOADED |
PUT 'file://C:/your/folder/t124_short.csv' @STAGE_T124/short/ AUTO_COMPRESS = FALSE;| source | target | source_size | target_size | source_compression | target_compression | status | message |
|---|---|---|---|---|---|---|---|
| t124_short.csv | t124_short.csv | 11 | 16 | NONE | NONE | UPLOADED |
PUT 'file://C:/your/folder/t124_events.json' @STAGE_T124/json/ AUTO_COMPRESS = FALSE;| source | target | source_size | target_size | source_compression | target_compression | status | message |
|---|---|---|---|---|---|---|---|
| t124_events.json | t124_events.json | 275 | 288 | NONE | NONE | UPLOADED |
The local paths say C:/your/folder here, which is what the shared demo script says too. Point them at a folder of your own.
And here is the stage.
LIST @STAGE_T124/;| name | size | md5 | last_modified |
|---|---|---|---|
| stage_t124/json/t124_events.json | 288 | a4b2827a9b746057f022c6247ce70e4f | Wed, 30 Sep 2026 16:36:38 GMT |
| stage_t124/many/t124_a.csv | 64 | 075c1946d8c76598b291a4fe135d7cdd | Wed, 30 Sep 2026 16:36:37 GMT |
| stage_t124/many/t124_b.csv | 64 | b493d7dc524cf8e498a77f2e9aa2b2b6 | Wed, 30 Sep 2026 16:36:38 GMT |
| stage_t124/short/t124_short.csv | 16 | 1066f6ff798324c3c1dd5f3beaf4a26d | Wed, 30 Sep 2026 16:36:38 GMT |
Four files, under three prefixes. Keep an eye on the size column, because the last section of this post is about nothing else.
Let's read two of them with no table anywhere
A CSV file has no column names, so you ask for its columns by position.
SELECT $1 AS ID, $2 AS CITY, $3 AS AMOUNT
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124')
ORDER BY $1;| ID | CITY | AMOUNT |
|---|---|---|
| 1 | Toronto | 100.00 |
| 2 | Boston | 250.50 |
| 3 | Zurich | 75.25 |
| 4 | London | 310.00 |
| 5 | Toronto | 45.00 |
| 6 | Nowhereville | 12.00 |
Six rows, across two files, and there is no table in that query at all. The many/ prefix holds both CSVs and the read picked up both of them.
Now let's take the FILE_FORMAT away entirely and see what happens.
SELECT $1 AS COLUMN_1, $2 AS COLUMN_2, $3 AS COLUMN_3
FROM @STAGE_T124/many/
ORDER BY $1;| COLUMN_1 | COLUMN_2 | COLUMN_3 |
|---|---|---|
| 1 | Toronto | 100.00 |
| 2 | Boston | 250.50 |
| 3 | Zurich | 75.25 |
| 4 | London | 310.00 |
| 5 | Toronto | 45.00 |
| 6 | Nowhereville | 12.00 |
The same six rows. That is the stage's own format doing the work, which is the thing DESCRIBE STAGE showed us two posts ago.
What happens when we write SELECT *?
This is the one everybody types first.
SELECT * FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124');001080 (42601): SQL compilation error: error line 1 at position 0 SELECT with no columns
SELECT with no columns. And that is exactly right, when you think about what we are asking for. A file has no column names, so there is nothing for the * to expand into.
Naming a column rather than a position says the same thing in different words.
SELECT ID FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124');000904 (42000): SQL compilation error: error line 1 at position 7 invalid identifier 'ID'
invalid identifier 'ID'. The file does not know what ID means. We gave that name to $1 in the query above, and the name lived only for that one result.
There is an answer to this one further down the post, and it surprised me when I found it. Have a guess before you get there.
So what is $1, exactly?
Rather than assume, let's ask. SYSTEM$TYPEOF reports the data type of an expression.
SELECT SYSTEM$TYPEOF($1) AS TYPE_OF_DOLLAR_1,
SYSTEM$TYPEOF($3) AS TYPE_OF_DOLLAR_3
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124')
LIMIT 1;| TYPE_OF_DOLLAR_1 | TYPE_OF_DOLLAR_3 |
|---|---|
| VARCHAR[LOB] | VARCHAR[LOB] |
VARCHAR[LOB] for both of them. And $3 is the column holding 100.00.
So every column off a stage read is text until you cast it, including the ones that look like numbers. If you ORDER BY $1 on a file with ten rows, you will get them in text order and wonder what happened.
What happens if we ask for a column the file has not got?
Our two CSVs have three columns each. Let's ask for a fourth.
SELECT $1 AS ID, $4 AS THERE_IS_NO_FOUR
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124')
ORDER BY $1;| ID | THERE_IS_NO_FOUR |
|---|---|
| 1 | NULL |
| 2 | NULL |
| 3 | NULL |
| 4 | NULL |
| 5 | NULL |
| 6 | NULL |
No error. Six rows, with a column of nulls on the end.
Let's count that rather than read it off the screen.
SELECT COUNT(*) AS ROWS_READ,
COUNT($4) AS ROWS_WHERE_FOUR_IS_NOT_NULL,
SUM(IFF($4 IS NULL, 1, 0)) AS ROWS_WHERE_FOUR_IS_NULL
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124');| ROWS_READ | ROWS_WHERE_FOUR_IS_NOT_NULL | ROWS_WHERE_FOUR_IS_NULL |
|---|---|---|
| 6 | 0 | 6 |
Six rows read, none of them with a value in $4, and all six null. So a column that is not in the file comes back as a null, not as a complaint.
A row that is too short does the same thing. The short/ prefix holds one row with only two fields in it.
SELECT $1 AS ID, $2 AS CITY, $3 AS AMOUNT
FROM @STAGE_T124/short/ (FILE_FORMAT => 'CSV_T124');| ID | CITY | AMOUNT |
|---|---|---|
| 9 | onlytwo | NULL |
SELECT COUNT(*) AS ROWS_READ,
SUM(IFF($3 IS NULL, 1, 0)) AS ROWS_WITH_NO_THIRD_COLUMN
FROM @STAGE_T124/short/ (FILE_FORMAT => 'CSV_T124');| ROWS_READ | ROWS_WITH_NO_THIRD_COLUMN |
|---|---|
| 1 | 1 |
One row, and the missing third column is a real null.
ERROR_ON_COLUMN_COUNT_MISMATCH is a COPY setting. It is on by default and it does nothing at all to a read. So a stage read will never tell you that a file is the wrong shape. It will hand you nulls and let you find out later.
Now the column you always wish you had
This is the real reason to read a stage rather than load it. Which file did this row come from?
SELECT METADATA$FILENAME AS WHICH_FILE,
METADATA$FILE_ROW_NUMBER AS WHICH_ROW,
$1 AS ID
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124')
ORDER BY WHICH_FILE, WHICH_ROW;| WHICH_FILE | WHICH_ROW | ID |
|---|---|---|
| many/t124_a.csv | 1 | 1 |
| many/t124_a.csv | 2 | 2 |
| many/t124_a.csv | 3 | 3 |
| many/t124_b.csv | 1 | 4 |
| many/t124_b.csv | 2 | 5 |
| many/t124_b.csv | 3 | 6 |
Two things worth noticing in that grid.
First, the file name. It is many/t124_a.csv: the prefix and the file, but not the stage name. The unload path two posts ago did carry the stage name, lower-cased. A read does not.
Second, the row number restarts. One, two, three in the first file and one, two, three in the second, rather than running one to six across both.
Let's prove that second one rather than eyeball it.
SELECT METADATA$FILENAME AS WHICH_FILE,
MIN(METADATA$FILE_ROW_NUMBER) AS LOWEST_ROW_NUMBER,
MAX(METADATA$FILE_ROW_NUMBER) AS HIGHEST_ROW_NUMBER
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124')
GROUP BY METADATA$FILENAME
ORDER BY WHICH_FILE;| WHICH_FILE | LOWEST_ROW_NUMBER | HIGHEST_ROW_NUMBER |
|---|---|---|
| many/t124_a.csv | 1 | 3 |
| many/t124_b.csv | 1 | 3 |
The lowest and highest row number is the same in both files. So METADATA$FILE_ROW_NUMBER is a position inside a file, not a position in the result.
And now group by the file name, which is the whole point of having it.
SELECT METADATA$FILENAME AS WHICH_FILE,
COUNT(*) AS ROWS_FROM_THAT_FILE,
SUM($3) AS TOTAL_FROM_THAT_FILE
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124')
GROUP BY METADATA$FILENAME
ORDER BY WHICH_FILE;| WHICH_FILE | ROWS_FROM_THAT_FILE | TOTAL_FROM_THAT_FILE |
|---|---|---|
| many/t124_a.csv | 3 | 425.75 |
| many/t124_b.csv | 3 | 367.0 |
Three rows from each file with its own total. Have a look at how those two totals are written: one carries two decimal places and the other does not. That is SUM() being handed a text column and working out a number from it, rather than a NUMBER(10,2) with a fixed shape. Cast $3 yourself if the formatting matters.
Three more metadata columns, and one that is not real
There are five of these columns in all. I had used two of them and was not certain the other three existed, so let's ask for them.
SELECT METADATA$FILE_CONTENT_KEY AS CONTENT_KEY,
METADATA$FILE_LAST_MODIFIED AS FILE_LAST_MODIFIED,
METADATA$START_SCAN_TIME AS START_SCAN_TIME
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124')
LIMIT 2;| CONTENT_KEY | FILE_LAST_MODIFIED | START_SCAN_TIME |
|---|---|---|
| 075c1946d8c76598b291a4fe135d7cdd | 2026-09-30 16:36:37 | 2026-09-30 09:36:39.831405-07:00 |
| 075c1946d8c76598b291a4fe135d7cdd | 2026-09-30 16:36:37 | 2026-09-30 09:36:39.831405-07:00 |
All three are real. The query ends in a LIMIT 2, so those two rows are both from the first file.
And look at CONTENT_KEY. That string is the same one the LIST grid at the top of this post prints as the file's md5.
START_SCAN_TIME is a timestamp, not a duration. Nothing on this page is timed and no performance claim is being made with it.
Now one that sounds like it ought to exist.
SELECT METADATA$FILE_SIZE AS HOW_BIG_IS_IT
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124');000904 (42000): SQL compilation error: error line 1 at position 7 invalid identifier 'METADATA$FILE_SIZE'
invalid identifier 'METADATA$FILE_SIZE'. There is no such column. The size of the file is in LIST, not in the read.
Let's pick just one of the files with PATTERN
PATTERN goes in the same brackets as the file format, and it is a regular expression matched against the file path.
SELECT METADATA$FILENAME AS WHICH_FILE, $1 AS ID, $2 AS CITY
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124', PATTERN => '.*t124_a.*')
ORDER BY $1;| WHICH_FILE | ID | CITY |
|---|---|---|
| many/t124_a.csv | 1 | Toronto |
| many/t124_a.csv | 2 | Boston |
| many/t124_a.csv | 3 | Zurich |
Three rows, all from the first file. The other file is still sitting in the same prefix and this read simply did not look at it.
And here is what a PATTERN typo costs
This is the part I would rather not have filmed, and it is the best thing in the post.
I wrote a pattern meant to pick up both files. Read it before you read the grid.
SELECT COUNT(*) AS ROWS_READ,
COUNT(DISTINCT METADATA$FILENAME) AS FILES_READ
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124', PATTERN => '.*t124_._csv.*');| ROWS_READ | FILES_READ |
|---|---|
| 0 | 0 |
Zero rows read, zero files read, and no error of any kind.
Look at the pattern again. It has an underscore in front of csv, and the files are called t124_a.csv. A dot, not an underscore. That pattern could never have matched anything, and Snowflake said nothing at all about it.
I did not notice until I read the grid. That is the danger, and it is mine.
It is not a one-off either. A pattern that matches nothing is just a zero.
SELECT COUNT(*) AS ROWS_READ
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124', PATTERN => '.*nothing_here.*');| ROWS_READ |
|---|
| 0 |
And so is a prefix that is not on the stage at all.
SELECT COUNT(*) AS ROWS_READ
FROM @STAGE_T124/definitely_not_a_prefix/ (FILE_FORMAT => 'CSV_T124');| ROWS_READ |
|---|
| 0 |
Which is worth sitting with for a second, because GET refused that exact question in the last post. The same mistake is a refusal going down and a zero coming back.
So does Snowflake check the pattern at all? Let's give it a broken one.
SELECT $1 AS ID
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124', PATTERN => '[unclosed');091003 (22000): Failure using stage area. Cause: [Invalid regular expression pattern '[unclosed'.]
Invalid regular expression pattern. So it is compiled. It just cannot tell you that it compiled fine and matched none of your files.
A stage read with a PATTERN has two indistinguishable outcomes: the files matched and had no rows, or the pattern was wrong. Both of them are zero and neither is an error. Put COUNT(DISTINCT METADATA$FILENAME) in the query while you are developing it, and you will see the difference immediately.
Now the JSON file
The fourth file on the stage is JSON, with three objects in it. One of them has a nested object and they all have an array.
SELECT $1 AS THE_WHOLE_OBJECT
FROM @STAGE_T124/json/ (FILE_FORMAT => 'JSON_T124');| THE_WHOLE_OBJECT |
|---|
| { "amount": 10.5, "event_id": 1, "kind": "click", "tags": [ "ui", "beta" ] } |
| { "amount": 0, "detail": { "campaign": "spring", "source": "email" }, "event_id": 2, "kind": "view", "tags": [ "ui" ] } |
| { "amount": 7.25, "event_id": 3, "kind": "click", "tags": [ "api", "beta", "mobile" ] } |
Three rows, and $1 is the whole object each time. For JSON, $1 is not a field. It is the document.
So let's pick the top-level fields out of it with a colon.
SELECT $1:event_id::INT AS EVENT_ID,
$1:kind::VARCHAR AS KIND,
$1:amount::NUMBER(10,2) AS AMOUNT
FROM @STAGE_T124/json/ (FILE_FORMAT => 'JSON_T124')
ORDER BY EVENT_ID;| EVENT_ID | KIND | AMOUNT |
|---|---|---|
| 1 | click | 10.50 |
| 2 | view | 0.00 |
| 3 | click | 7.25 |
And go deeper. A colon again for a nested object, and square brackets for an element of an array.
SELECT $1:event_id::INT AS EVENT_ID,
$1:detail:source::VARCHAR AS NESTED_SOURCE,
$1:tags[0]::VARCHAR AS FIRST_TAG,
ARRAY_SIZE($1:tags) AS HOW_MANY_TAGS
FROM @STAGE_T124/json/ (FILE_FORMAT => 'JSON_T124')
ORDER BY EVENT_ID;| EVENT_ID | NESTED_SOURCE | FIRST_TAG | HOW_MANY_TAGS |
|---|---|---|---|
| 1 | NULL | ui | 2 |
| 2 | ui | 1 | |
| 3 | NULL | api | 3 |
NESTED_SOURCE has a value on one row and nulls on the other two, because only one of the three objects has a detail in it. Asking for a field that is not there is a null, the same way asking for $4 was.
And the row number in a JSON file counts objects, not lines.
SELECT METADATA$FILE_ROW_NUMBER AS WHICH_ROW,
$1:event_id::INT AS EVENT_ID
FROM @STAGE_T124/json/ (FILE_FORMAT => 'JSON_T124')
ORDER BY WHICH_ROW;| WHICH_ROW | EVENT_ID |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
Three rows for three objects, even though the file is pretty-printed over many more lines than that.
LATERAL FLATTEN works on a staged file too.
SELECT s.$1:event_id::INT AS EVENT_ID,
f.VALUE::VARCHAR AS ONE_TAG
FROM @STAGE_T124/json/ (FILE_FORMAT => 'JSON_T124') s,
LATERAL FLATTEN(INPUT => s.$1:tags) f
ORDER BY EVENT_ID, ONE_TAG;| EVENT_ID | ONE_TAG |
|---|---|
| 1 | beta |
| 1 | ui |
| 2 | ui |
| 3 | api |
| 3 | beta |
| 3 | mobile |
Six rows out of three objects, one per tag. The arrays had two, one and three tags in them.
Can we alias it, join it, and put it in a CTE?
If @STAGE/path really is a table expression, then all the usual things should work. Let's check them one at a time. First a table alias.
SELECT s.$1 AS ID, s.$2 AS CITY
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124') s
ORDER BY s.$1;| ID | CITY |
|---|---|
| 1 | Toronto |
| 2 | Boston |
| 3 | Zurich |
| 4 | London |
| 5 | Toronto |
| 6 | Nowhereville |
s.$1 works, so the stage read is a thing that can be given a name.
Then a join to a real table, on the city.
SELECT s.$2 AS CITY_IN_THE_FILE,
COUNT(c.CUSTOMER_ID) AS MATCHING_CUSTOMERS
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124') s
LEFT JOIN CUSTOMERS c ON UPPER(c.CITY) = UPPER(s.$2)
GROUP BY s.$2
ORDER BY CITY_IN_THE_FILE;| CITY_IN_THE_FILE | MATCHING_CUSTOMERS |
|---|---|
| Boston | 1 |
| London | 1 |
| Nowhereville | 0 |
| Toronto | 2 |
| Zurich | 1 |
Five cities, with the number of customers each one matches. Toronto matches two, three of them match one each, and Nowhereville matches none. That is a file joined to a table with no load in between.
And a CTE, so we can cast the columns once and then use their names.
WITH FROM_THE_FILE AS (
SELECT $1::INT AS ID, $2::VARCHAR AS CITY, $3::NUMBER(10,2) AS AMOUNT
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124')
)
SELECT CITY AS CITY,
COUNT(*) AS HOW_MANY,
SUM(AMOUNT) AS TOTAL,
ROUND(AVG(AMOUNT), 2) AS AVERAGE
FROM FROM_THE_FILE
GROUP BY CITY
ORDER BY CITY;| CITY | HOW_MANY | TOTAL | AVERAGE |
|---|---|---|---|
| Boston | 1 | 250.50 | 250.50 |
| London | 1 | 310.00 | 310.00 |
| Nowhereville | 1 | 12.00 | 12.00 |
| Toronto | 2 | 145.00 | 72.50 |
| Zurich | 1 | 75.25 | 75.25 |
Five rows, one per city, with a count, a total and an average. Inside the CTE the columns were cast, so SUM and AVG are working on real numbers here.
Can we build a view over a stage?
This is the one I did not expect to work.
CREATE OR REPLACE VIEW V_STAGED_T124 AS
SELECT $1::INT AS ID, $2::VARCHAR AS CITY, $3::NUMBER(10,2) AS AMOUNT
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124');| status |
|---|
| View V_STAGED_T124 successfully created. |
Created. So let's select from it.
SELECT ID AS ID, CITY AS CITY, AMOUNT AS AMOUNT
FROM V_STAGED_T124
ORDER BY ID;| ID | CITY | AMOUNT |
|---|---|---|
| 1 | Toronto | 100.00 |
| 2 | Boston | 250.50 |
| 3 | Zurich | 75.25 |
| 4 | London | 310.00 |
| 5 | Toronto | 45.00 |
| 6 | Nowhereville | 12.00 |
Six rows, with real column names, and the FROM is a view name like any other. That is the answer to the refusal at the top of this post: you cannot SELECT * off a stage, but you can put the positions and the casts in a view once and then SELECT * off that for ever.
Then I asked the catalog what kind of view it was, and got this.
SELECT TABLE_NAME AS THE_NAME, TABLE_TYPE AS THE_TYPE, IS_SECURE AS IS_IT_SECURE
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_NAME = 'V_STAGED_T124';000904 (42000): SQL compilation error: error line 1 at position 31 invalid identifier 'TABLE_TYPE'
invalid identifier 'TABLE_TYPE'. INFORMATION_SCHEMA.VIEWS has no such column. I assumed it did because INFORMATION_SCHEMA.TABLES has one. That mistake is mine and it is left in.
Three refusals worth recognising
First, a stage that is not there.
SELECT $1 AS ID FROM @STAGE_T124_NOPE/many/ (FILE_FORMAT => 'CSV_T124');002003 (02000): SQL compilation error: Stage 'DEMO_DB.PUBLIC.STAGE_T124_NOPE' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have at least one privilege granted on STAGE DEMO_DB.PUBLIC.STAGE_T124_NOPE.
Then a file format that is not there.
SELECT $1 AS ID FROM @STAGE_T124/many/ (FILE_FORMAT => 'NO_SUCH_FORMAT_T124');002003 (02000): SQL compilation error: File format 'NO_SUCH_FORMAT_T124' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have at least one privilege granted on FILE FORMAT NO_SUCH_FORMAT_T124.
Both of them say does not exist or not authorized, which is the same sentence Snowflake uses when the object does exist and your role cannot see it.
And one more, to settle what a stage read is not. Can we write to one?
UPDATE @STAGE_T124/many/ SET $1 = 99;001003 (42000): SQL compilation error: syntax error line 1 at position 7 unexpected '@STAGE_T124/many/'.
A syntax error, on the stage name itself. @STAGE/path is a FROM target and nothing else. It is not somewhere UPDATE can point.
One last thing: the two byte counts that disagreed
The last post ended on something I said I could not explain. The stage reported a compressed file at 112 bytes and GET reported 102 for the same file. Both numbers were Snowflake's own.
This post has four more of those pairs in it, and they are all in grids above. Every PUT report gives the size of the real file in source_size and the size it ended up as in target_size, and the LIST grid agrees with target_size every time.
Scroll back up to the first section and read the two size columns off these four grids, one after the other, because the pattern only shows up when you see them together.
t124_short.csv, the smallest file we uploadedt124_a.csvandt124_b.csv, the two CSVst124_events.json, the biggest of the four
Every stage size is the next multiple of sixteen above the real size. A file of eleven bytes becomes sixteen. Fifty-one becomes sixty-four. Two hundred and seventy-five becomes two hundred and eighty-eight. Not roughly, and not on average. Exactly, every time.
And the pair the last post could not explain fits it too. 102 becomes 112, which is the next multiple of sixteen above it. So does every other pair this series has captured.
Proved: the stage size is the next multiple of sixteen above the real size. That is arithmetic on captured numbers and you can check it yourself on this page.
Likely: the reason is that an internal stage is encrypted, and a block cipher pads to a sixteen-byte boundary. I have no captured output that says so, so I am calling that likely rather than proved.
Either way, the practical version is short. LIST reports the size on the stage. GET reports the size of your file. They are not supposed to agree.
And let's tidy up
The view goes first, then the files, then everything else.
-- Put the schema back the way we found it.
DROP VIEW IF EXISTS V_STAGED_T124;
REMOVE @STAGE_T124/;
DROP STAGE IF EXISTS STAGE_T124;
DROP FILE FORMAT IF EXISTS CSV_T124;
DROP FILE FORMAT IF EXISTS JSON_T124;
-- This one never existed. It is here because the statement that would have
-- created it was refused on purpose, and a probe script should clean up after
-- the statements that failed as well as the ones that worked.
DROP STAGE IF EXISTS STAGE_T124_NOPE;DROP VIEW IF EXISTS V_STAGED_T124;| status |
|---|
| V_STAGED_T124 successfully dropped. |
REMOVE @STAGE_T124/;| name | result |
|---|---|
| stage_t124/many/t124_b.csv | removed |
| stage_t124/short/t124_short.csv | removed |
| stage_t124/json/t124_events.json | removed |
| stage_t124/many/t124_a.csv | removed |
Four files removed, which is all four we uploaded.
LIST @STAGE_T124/;| name | size | md5 | last_modified |
|---|
And the drop for the stage that never existed is worth showing, because of what it says.
DROP STAGE IF EXISTS STAGE_T124_NOPE;| status |
|---|
| Drop statement executed successfully (STAGE_T124_NOPE already dropped). |
Drop statement executed successfully (STAGE_T124_NOPE already dropped). With IF EXISTS, dropping something that was never there is a success with a sentence explaining itself.
SELECT CURRENT_DATABASE() AS THE_DATABASE,
CURRENT_SCHEMA() AS THE_SCHEMA,
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.VIEWS
WHERE RIGHT(TABLE_NAME, 5) = '_T124') AS PROBE_VIEWS_LEFT,
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.STAGES
WHERE STAGE_NAME LIKE '%_T124%') AS PROBE_STAGES_LEFT,
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.FILE_FORMATS
WHERE RIGHT(FILE_FORMAT_NAME, 5) = '_T124') AS PROBE_FORMATS_LEFT,
(SELECT COUNT(*) FROM CUSTOMERS) AS DEMO_DB_IS_READABLE;| THE_DATABASE | THE_SCHEMA | PROBE_VIEWS_LEFT | PROBE_STAGES_LEFT | PROBE_FORMATS_LEFT | DEMO_DB_IS_READABLE |
|---|---|---|---|---|---|
| DEMO_DB | PUBLIC | 0 | 0 | 0 | 10 |
LIST @~/t124/;| name | size | md5 | last_modified |
|---|
No probe views, no probe stages, no probe file formats, nothing of mine in the user stage, and the demo database still reads.
Key points
@STAGE/pathis aFROMtarget, and$1,$2and$3are its columns.- A read with no
FILE_FORMATnamed uses the stage's own format. SELECT *on a stage fails withSELECT with no columns, because a file has no column names to expand.$1isVARCHAR[LOB]. Every column off a stage is text until you cast it, including the numbers.- Asking for a column the file has not got gives you nulls, not an error. So does a row that is too short.
ERROR_ON_COLUMN_COUNT_MISMATCHis aCOPYsetting and does nothing to a read.METADATA$FILENAMEis the prefix and the file name, without the stage name.METADATA$FILE_ROW_NUMBERrestarts in every file.- There are five real
METADATA$columns.METADATA$FILE_SIZEis not one of them. - A
PATTERNthat matches nothing returns zero rows and no error, and so does a prefix that is not on the stage. - An invalid regular expression is caught, so the pattern is compiled. Snowflake just cannot tell you it matched no files.
- A stage read can be aliased, joined to a real table, put in a CTE and flattened.
- A
VIEWover a stage works, and gives you the column names back. - The size a stage reports is the next multiple of sixteen above the size of your file. That is why
LISTandGETdisagree.
Episode 125 — file format objects. Where every one of those parsing defaults lives, what DESCRIBE FILE FORMAT says about a format that names nothing, and the same CSV file read twice giving different data.
Every query and every result grid on this page was captured from a live Snowflake account (version 10.35.101) running the demo script for this episode. The local folder in the PUT statements is written as C:/your/folder, the same substitution the shared demo script makes; nothing else in any statement is changed and no error message is edited. The _T124 objects are created and dropped by the script, and the CUSTOMERS table in the join belongs to the sample data this series has used since Episode 1. The size rule in the last section is arithmetic on the captured byte counts above it; the explanation offered for it is labelled as likely rather than proved, because no capture on this page states it. Nothing on this page is timed, METADATA$START_SCAN_TIME is a timestamp rather than a duration, and no performance claims are made anywhere in this series. Stage query behaviour follows the Snowflake documentation on querying staged files.



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