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:
CREATEORREPLACETABLE sample_table (a INT, f FILENOTNULL);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 ParquetCREATEORREPLACESTAGE test_stage_parquet;CREATEORREPLACEFILEFORMAT 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);
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 GROUPBY f;-- Expressions of type FILE cannot be used as GROUP BY keysSELECT*FROM sample_table ORDERby f;-- Expressions of type FILE cannot be used as ORDER BY keysCREATEORREPLACETABLE 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 requiredcredentials may be missing or invalid.If you
are running a copycommand, please make sure files are not deleted when they are
being loaded orfiles are not being loaded into two different tables concurrently
withautopurgeoption.