View a markdown version of this page

File listing functions - Amazon Athena

File listing functions

Use this function to list the files in Amazon S3 whose paths match a glob pattern, for example to check which files a pattern matches before you read them.

For a description of the sample tables and files that the examples on this page use, see Table functions in DuckDB.

glob table function

Returns one row for each file whose path matches pattern, with the full path of the file in the file column. Use it to see which files a pattern matches before you read them with a function such as read_parquet or read_csv. You can also use it to find the partition folders under a table location.

In the pattern, * matches any characters within one part of the path but not /, and ** matches any number of folder levels. The wildcard ? matches one character, and [abc] matches one character from the set. Pass a list of patterns to list the files that match any of them. If no file matches, the function returns no rows.

To test whether a string matches a pattern, use the GLOB operator instead. For more information about the operator, see GLOB operator.

Syntax

glob(pattern) glob(pattern_list)

Arguments

The glob table function takes the following arguments:

pattern

An Amazon S3 path that can contain wildcards, as a VARCHAR value, for example 's3://amzn-s3-demo-bucket/tables/sales_csv/*/*'.

pattern_list

A list of such paths, as a VARCHAR[] value.

Return type

A table with one row per matching file and one column, file (VARCHAR), that holds the full Amazon S3 path of the file.

Examples

The following SQL query lists the partition folders of a partitioned table location. It extracts the folder name from each file path.

SELECT DISTINCT regexp_extract(file, 'region=[a-z]+') AS partition FROM glob( 's3://amzn-s3-demo-bucket/tables/sales_csv/*/*' ) ORDER BY partition

The query returns the following results.

partition

region=east

region=north

region=west

The following SQL query uses ** to match the files at any folder level under the table location.

SELECT DISTINCT regexp_extract(file, 'region=[a-z]+') AS partition FROM glob( 's3://amzn-s3-demo-bucket/tables/sales_csv/**' ) ORDER BY partition

The query returns the following results.

partition

region=east

region=north

region=west

The following SQL query passes a list of two patterns and returns the partitions whose files match either one.

SELECT DISTINCT regexp_extract(file, 'region=[a-z]+') AS partition FROM glob([ 's3://amzn-s3-demo-bucket/tables/sales_csv/region=east/*', 's3://amzn-s3-demo-bucket/tables/sales_csv/region=west/*' ]) ORDER BY partition

The query returns the following results.

partition

region=east

region=west

The following SQL query uses a pattern that matches no file, because the table has no region=south partition. The function returns no rows, so the count is 0.

SELECT count(*) AS file_count FROM glob( 's3://amzn-s3-demo-bucket/tables/sales_csv/region=south/*' )

The query returns the following result.

file_count

0

Considerations

A partitioned table stores its files in folders under the table location, such as region=east/. A pattern that ends in /* directly after the table location matches only files in the location itself. To include the files in the partition folders, use /*/* or /**.