Snowflake SQL Tutorial 124 - My PATTERN Matched Nothing And Snowflake Said Nothing

Snowflake SQL · Episode 124

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

We need a stage, a CSV format and a JSON format. All three came up in the last two posts.

Setup
-- 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.

Query
PUT 'file://C:/your/folder/t124_a.csv' @STAGE_T124/many/ AUTO_COMPRESS = FALSE;
Result1 row · 8 columns · scroll →
Scroll sideways for the status column.
sourcetargetsource_sizetarget_sizesource_compressiontarget_compressionstatusmessage
t124_a.csvt124_a.csv5164NONENONEUPLOADED
Query
PUT 'file://C:/your/folder/t124_b.csv' @STAGE_T124/many/ AUTO_COMPRESS = FALSE;
Result1 row · 8 columns · scroll →
Scroll sideways for the status column.
sourcetargetsource_sizetarget_sizesource_compressiontarget_compressionstatusmessage
t124_b.csvt124_b.csv5664NONENONEUPLOADED
Query
PUT 'file://C:/your/folder/t124_short.csv' @STAGE_T124/short/ AUTO_COMPRESS = FALSE;
Result1 row · 8 columns · scroll →
Scroll sideways for the status column.
sourcetargetsource_sizetarget_sizesource_compressiontarget_compressionstatusmessage
t124_short.csvt124_short.csv1116NONENONEUPLOADED
Query
PUT 'file://C:/your/folder/t124_events.json' @STAGE_T124/json/ AUTO_COMPRESS = FALSE;
Result1 row · 8 columns · scroll →
Scroll sideways for the status column.
sourcetargetsource_sizetarget_sizesource_compressiontarget_compressionstatusmessage
t124_events.jsont124_events.json275288NONENONEUPLOADED

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.

Query
LIST @STAGE_T124/;
Result4 rows · 4 columns
namesizemd5last_modified
stage_t124/json/t124_events.json288a4b2827a9b746057f022c6247ce70e4fWed, 30 Sep 2026 16:36:38 GMT
stage_t124/many/t124_a.csv64075c1946d8c76598b291a4fe135d7cddWed, 30 Sep 2026 16:36:37 GMT
stage_t124/many/t124_b.csv64b493d7dc524cf8e498a77f2e9aa2b2b6Wed, 30 Sep 2026 16:36:38 GMT
stage_t124/short/t124_short.csv161066f6ff798324c3c1dd5f3beaf4a26dWed, 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.

Query
SELECT $1 AS ID, $2 AS CITY, $3 AS AMOUNT
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124')
ORDER BY $1;
Result6 rows · 3 columns
IDCITYAMOUNT
1Toronto100.00
2Boston250.50
3Zurich75.25
4London310.00
5Toronto45.00
6Nowhereville12.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.

Query
SELECT $1 AS COLUMN_1, $2 AS COLUMN_2, $3 AS COLUMN_3
FROM @STAGE_T124/many/
ORDER BY $1;
Result6 rows · 3 columns
COLUMN_1COLUMN_2COLUMN_3
1Toronto100.00
2Boston250.50
3Zurich75.25
4London310.00
5Toronto45.00
6Nowhereville12.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.

Refused
SELECT * FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124');
Query failed — Snowflake returned an error
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.

Refused
SELECT ID FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124');
Query failed — Snowflake returned an error
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.

Hold that thought

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.

Query
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;
Result1 row · 2 columns
TYPE_OF_DOLLAR_1TYPE_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.

Query
SELECT $1 AS ID, $4 AS THERE_IS_NO_FOUR
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124')
ORDER BY $1;
Result6 rows · 2 columns
IDTHERE_IS_NO_FOUR
1NULL
2NULL
3NULL
4NULL
5NULL
6NULL

No error. Six rows, with a column of nulls on the end.

Let's count that rather than read it off the screen.

Query
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');
Result1 row · 3 columns
ROWS_READROWS_WHERE_FOUR_IS_NOT_NULLROWS_WHERE_FOUR_IS_NULL
606

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.

Query
SELECT $1 AS ID, $2 AS CITY, $3 AS AMOUNT
FROM @STAGE_T124/short/ (FILE_FORMAT => 'CSV_T124');
Result1 row · 3 columns
IDCITYAMOUNT
9onlytwoNULL
Query
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');
Result1 row · 2 columns
ROWS_READROWS_WITH_NO_THIRD_COLUMN
11

One row, and the missing third column is a real null.

The setting that does not help you here

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?

Query
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;
Result6 rows · 3 columns
WHICH_FILEWHICH_ROWID
many/t124_a.csv11
many/t124_a.csv22
many/t124_a.csv33
many/t124_b.csv14
many/t124_b.csv25
many/t124_b.csv36

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.

Query
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;
Result2 rows · 3 columns
WHICH_FILELOWEST_ROW_NUMBERHIGHEST_ROW_NUMBER
many/t124_a.csv13
many/t124_b.csv13

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.

Query
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;
Result2 rows · 3 columns
WHICH_FILEROWS_FROM_THAT_FILETOTAL_FROM_THAT_FILE
many/t124_a.csv3425.75
many/t124_b.csv3367.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.

Query
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;
Result2 rows · 3 columns
CONTENT_KEYFILE_LAST_MODIFIEDSTART_SCAN_TIME
075c1946d8c76598b291a4fe135d7cdd2026-09-30 16:36:372026-09-30 09:36:39.831405-07:00
075c1946d8c76598b291a4fe135d7cdd2026-09-30 16:36:372026-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.

Refused
SELECT METADATA$FILE_SIZE AS HOW_BIG_IS_IT
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124');
Query failed — Snowflake returned an error
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.

Query
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;
Result3 rows · 3 columns
WHICH_FILEIDCITY
many/t124_a.csv1Toronto
many/t124_a.csv2Boston
many/t124_a.csv3Zurich

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.

Query
SELECT COUNT(*)                            AS ROWS_READ,
       COUNT(DISTINCT METADATA$FILENAME)    AS FILES_READ
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124', PATTERN => '.*t124_._csv.*');
Result1 row · 2 columns
ROWS_READFILES_READ
00

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.

Query
SELECT COUNT(*) AS ROWS_READ
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124', PATTERN => '.*nothing_here.*');
Result1 row · 1 column
ROWS_READ
0

And so is a prefix that is not on the stage at all.

Query
SELECT COUNT(*) AS ROWS_READ
FROM @STAGE_T124/definitely_not_a_prefix/ (FILE_FORMAT => 'CSV_T124');
Result1 row · 1 column
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.

Refused
SELECT $1 AS ID
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124', PATTERN => '[unclosed');
Query failed — Snowflake returned an error
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.

What to do about it

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.

Query
SELECT $1 AS THE_WHOLE_OBJECT
FROM @STAGE_T124/json/ (FILE_FORMAT => 'JSON_T124');
Result3 rows · 1 column
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.

Query
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;
Result3 rows · 3 columns
EVENT_IDKINDAMOUNT
1click10.50
2view0.00
3click7.25

And go deeper. A colon again for a nested object, and square brackets for an element of an array.

Query
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;
Result3 rows · 4 columns
EVENT_IDNESTED_SOURCEFIRST_TAGHOW_MANY_TAGS
1NULLui2
2emailui1
3NULLapi3

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.

Query
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;
Result3 rows · 2 columns
WHICH_ROWEVENT_ID
11
22
33

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.

Query
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;
Result6 rows · 2 columns
EVENT_IDONE_TAG
1beta
1ui
2ui
3api
3beta
3mobile

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.

Query
SELECT s.$1 AS ID, s.$2 AS CITY
FROM @STAGE_T124/many/ (FILE_FORMAT => 'CSV_T124') s
ORDER BY s.$1;
Result6 rows · 2 columns
IDCITY
1Toronto
2Boston
3Zurich
4London
5Toronto
6Nowhereville

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.

Query
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;
Result5 rows · 2 columns
CITY_IN_THE_FILEMATCHING_CUSTOMERS
Boston1
London1
Nowhereville0
Toronto2
Zurich1

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.

Query
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;
Result5 rows · 4 columns
CITYHOW_MANYTOTALAVERAGE
Boston1250.50250.50
London1310.00310.00
Nowhereville112.0012.00
Toronto2145.0072.50
Zurich175.2575.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.

Query
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');
Result1 row · 1 column
status
View V_STAGED_T124 successfully created.

Created. So let's select from it.

Query
SELECT ID AS ID, CITY AS CITY, AMOUNT AS AMOUNT
FROM V_STAGED_T124
ORDER BY ID;
Result6 rows · 3 columns
IDCITYAMOUNT
1Toronto100.00
2Boston250.50
3Zurich75.25
4London310.00
5Toronto45.00
6Nowhereville12.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.

Refused
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';
Query failed — Snowflake returned an error
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.

Refused
SELECT $1 AS ID FROM @STAGE_T124_NOPE/many/ (FILE_FORMAT => 'CSV_T124');
Query failed — Snowflake returned an error
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.

Refused
SELECT $1 AS ID FROM @STAGE_T124/many/ (FILE_FORMAT => 'NO_SUCH_FORMAT_T124');
Query failed — Snowflake returned an error
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?

Refused
UPDATE @STAGE_T124/many/ SET $1 = 99;
Query failed — Snowflake returned an error
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 uploaded
  • t124_a.csv and t124_b.csv, the two CSVs
  • t124_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.

Which half of this I can stand behind

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.

Cleanup
-- 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;
Query
DROP VIEW IF EXISTS V_STAGED_T124;
Result1 row · 1 column
status
V_STAGED_T124 successfully dropped.
Query
REMOVE @STAGE_T124/;
Result4 rows · 2 columns
nameresult
stage_t124/many/t124_b.csvremoved
stage_t124/short/t124_short.csvremoved
stage_t124/json/t124_events.jsonremoved
stage_t124/many/t124_a.csvremoved

Four files removed, which is all four we uploaded.

Query
LIST @STAGE_T124/;
Result0 rows · 4 columns
namesizemd5last_modified

And the drop for the stage that never existed is worth showing, because of what it says.

Query
DROP STAGE IF EXISTS STAGE_T124_NOPE;
Result1 row · 1 column
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.

Query
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;
Result1 row · 6 columns · scroll →
THE_DATABASETHE_SCHEMAPROBE_VIEWS_LEFTPROBE_STAGES_LEFTPROBE_FORMATS_LEFTDEMO_DB_IS_READABLE
DEMO_DBPUBLIC00010
Query
LIST @~/t124/;
Result0 rows · 4 columns
namesizemd5last_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/path is a FROM target, and $1, $2 and $3 are its columns.
  • A read with no FILE_FORMAT named uses the stage's own format.
  • SELECT * on a stage fails with SELECT with no columns, because a file has no column names to expand.
  • $1 is VARCHAR[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_MISMATCH is a COPY setting and does nothing to a read.
  • METADATA$FILENAME is the prefix and the file name, without the stage name.
  • METADATA$FILE_ROW_NUMBER restarts in every file.
  • There are five real METADATA$ columns. METADATA$FILE_SIZE is not one of them.
  • A PATTERN that 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 VIEW over 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 LIST and GET disagree.
Next in this series

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.