The name of the stage where the file is located, as a string, in the form '@stage_name'.
relative_path
The path to the file on the stage specified by stage_name as a string.
file_url
A valid stage or scoped file URL as a string.
metadata
An OBJECT containing the required FILE attributes. A FILE must have CONTENT_TYPE, SIZE, ETAG, and LAST_MODIFIED fields.
It must also specify the file’s location in one of the following ways:
The following example demonstrates how to create FILE and store it in a table, then perform various operations using that
column, including saving and loading from Parquet, SnowPipe, datasets, materialized views, dynamic tables, and cloning
with time travel.
Creating a table with a FILE column:
CREATE OR REPLACETABLE sample_table (a INT, f FILENOT NULL);DESCRIBETABLE sample_table;INSERTINTO sample_table SELECT1,TO_FILE('@mystage','image.png');INSERTINTO sample_table SELECT1,TO_FILE('@mystage',relative_path)FROMDIRECTORY('@mystage');SELECT*FROM sample_table WHEREfl_get_file_type(f)='image';
To write a table containing a FILE column to a stage as a Parquet file and load it back:
-- Write to stage as ParquetCREATE OR REPLACESTAGE test_stage_parquet;CREATE OR REPLACEFILEFORMAT parquet_format
TYPE='PARQUET'USE_LOGICAL_TYPE=TRUE;COPYINTO@test_stage_parquet/file_copy.parquetFROM sample_table
FILE_FORMAT=(FORMAT_NAME= parquet_format)HEADER=TRUE->>SELECT"rows_unloaded"FROM $1;ALTERSTAGE test_stage_parquet SETDIRECTORY=(ENABLE=TRUE);ALTERSTAGE test_stage_parquet REFRESH;-- Read Parquet files back from stageSELECT*FROM@TEST_STAGE_PARQUET/file_copy.parquet_0_0_0.snappy.parquet(FILE_FORMAT=> parquet_format);SELECT*FROM@TEST_STAGE_PARQUET(PATTERN=>'.*.parquet',FILE_FORMAT=> parquet_format);
Create a dataset from a Parquet file:
CREATE OR REPLACEDATASET mydataset;ALTERDATASET mydataset ADDVERSION'v1'FROM(SELECT*FROM@TEST_STAGE_PARQUET/file_copy.parquet_0_0_0.snappy.parquet(FILE_FORMAT=> my_parquet_format))COMMENT='test dataset';
Copy Parquet files into a table:
CREATE OR REPLACETABLE t1_copy_parquet (a INT, f OBJECTNOT NULL);COPYINTO t1_copy_parquet
FROM@test_stage_parquet
FILE_FORMAT=(FORMAT_NAME= parquet_format)MATCH_BY_COLUMN_NAME=CASE_INSENSITIVE;SELECTFL_GET_STAGE(f),FL_GET_RELATIVE_PATH(f)FROM t1_copy_parquet;
Create a Snowpipe:
CREATE OR REPLACETABLE t1_copy_parquet_snowpipe (f OBJECTNOT NULL);CREATE OR REPLACEPIPE test_pipe ASCOPYINTO t1_copy_parquet_snowpipe
FROM@test_stage_parquet
FILE_FORMAT=(FORMAT_NAME= my_parquet_format)MATCH_BY_COLUMN_NAME=CASE_INSENSITIVE;ALTERPIPETEST_PIPEREFRESH;
Create a materialized view or a dynamic table from the table:
CREATE OR REPLACEMATERIALIZEDVIEWMVASSELECT*FROMSAMPLE_TABLE;CREATE OR REPLACEDYNAMICTABLE sample_dynamic_table
WAREHOUSE= my_warehouse
TARGET_LAG='60 minutes'ASSELECT f FROM sample_table;
Store files in an array in a table column:
CREATE OR REPLACETABLE files_array_table(filesARRAY);INSERTINTO files_array_table SELECTARRAY_CONSTRUCT(TO_FILE('@mystage','image.png'));CREATE OR REPLACETABLE files_array_table_copy(filesARRAY);INSERTINTO files_array_table_copy SELECTfiles[0]FROM files_array_table;
The following example shows attempts to GROUP BY, ORDER BY, and CLUSTER BY a FILE column, which is not supported
because FILE values cannot be compared.
SELECT f,count(*)FROM sample_table GROUP BY f;-- Expressions of type FILE cannot be used as GROUP BY keysSELECT*FROM sample_table ORDER by f;-- Expressions of type FILE cannot be used as ORDER BY keysCREATE OR REPLACETABLE cluster_to_file (a int,urlstring)CLUSTERBY(to_file(url));-- Unsupported type 'FILE' for clustering keys
This final example uses an incorrect stage name, specifically a slash at the end of the stage name.
Snowflake already adds a slash between the stage name and relative path, so this results in
two slashes, and the combined stage path does not specify any file.
SELECTTO_FILE('@mystage/','image.png');
Remote file '@mystage//image.png' was not found. There are several potential causes.
The file might not exist. The required credentials may be missing or invalid. If you
are running a copy command, please make sure files are not deleted when they are
being loaded or files are not being loaded into two different tables concurrently
with auto purge option.