to_file function
Applies to: Databricks SQL
Databricks Runtime 18 LTS and above
Beta
This feature is in Beta.
Returns a FILE value that references the file at path.
Syntax
to_file(path)
Arguments
path: ASTRINGexpression specifying the path to a file. For example, a path in a Unity Catalog volume.
Returns
A FILE value that references the file at path.
To gather file metadata, Databricks makes a remote call to the file's storage location. If the file doesn't exist or is inaccessible, Databricks raises an error. To return NULL instead, use try_to_file function.
Common error conditions
TO_FILE_ERROR.FILE_NOT_EXISTSTO_FILE_AUTHORIZATION_ERROR.READ_UNAUTHORIZEDTO_FILE_ILLEGAL_USAGE.NO_PATH_PROVIDED
For more information, see Error conditions in Databricks.
Examples
To create a FILE reference from a path in a volume:
SQL
SELECT to_file('/Volumes/my_catalog/my_schema/my_volume/report.pdf');
If the path doesn't exist or the user lacks read permission, to_file raises an error:
SQL
SELECT to_file('/Volumes/my_catalog/my_schema/my_volume/ghost.csv');
Text
Error: TO_FILE_ERROR.FILE_NOT_EXISTS