try_to_file function

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime 18 LTS and above

Important

This feature is in Beta.

Returns a FILE value that references the file at path, or NULL if the file doesn't exist or is inaccessible.

Syntax

try_to_file(path)

Arguments

  • path: A STRING expression 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, or NULL if the file doesn't exist or is inaccessible.

This function is the error-tolerant version of to_file function. To gather file metadata, Azure Databricks makes a remote call to the file's storage location. If the file doesn't exist or is inaccessible, this function returns NULL instead of raising an error.

Common error conditions

  • TO_FILE_ILLEGAL_USAGE.NO_PATH_PROVIDED

For more information, see Error conditions in Azure Databricks.

Examples

To create a FILE reference from a path in a volume:

SELECT try_to_file('/Volumes/my_catalog/my_schema/my_volume/report.pdf');

If the path doesn't exist or is inaccessible, try_to_file returns NULL:

SELECT try_to_file('/Volumes/my_catalog/my_schema/my_volume/ghost.csv');
NULL