View a markdown version of this page

Table functions in DuckDB - Amazon Athena

Table functions in DuckDB

Use table functions to read files in Amazon S3 directly and to inspect the metadata of those files. You can also use them to list the databases, tables, columns, and functions that a query can use, and to generate rows of test data. A table function returns a set of rows. You call it in the FROM clause of a query, in place of a table name, as in SELECT * FROM read_parquet('s3://amzn-s3-demo-bucket/data/*.parquet'). You can filter, join, group, and sort its rows as you would the rows of a table.

The file readers, such as read_parquet, read_csv, read_json, and read_avro, return the data in files. The read_text and read_blob functions return the whole content of each file as one value. The file metadata functions, such as parquet_metadata, parquet_schema, and avro_metadata, return what a file records about itself in its header or footer. All of these functions take an Amazon S3 path that starts with s3://. Most of them also accept a path with a wildcard, such as *, or a list of paths.

These functions read Amazon S3 with the IAM permissions of the principal that runs the query, so the principal must have permission to read each path. The reads do not go through the AWS Glue Data Catalog, so AWS Glue and AWS Lake Formation table permissions do not apply to them. For more information about these permissions, see Reading files by Amazon S3 path.

You can also read a file by putting its quoted Amazon S3 path in place of a table name, as in FROM 's3://amzn-s3-demo-bucket/data/orders.parquet'. The path must end in .parquet, .csv, or .json, and the read works like a call to the matching reader function. It does not go through the AWS Glue Data Catalog. To set reader options, such as the file format, a header row, or the column types, call the reader function instead.

In DuckDB, a database in the AWS Glue Data Catalog is called a schema, and the catalog that holds it is called a database. The functions whose names start with duckdb_ take no arguments and list what the query can see. The duckdb_databases function lists catalogs, duckdb_schemas lists databases, duckdb_tables lists tables, duckdb_columns lists columns, and duckdb_functions lists functions.

A view cannot call a table function. For more information about what a view can read, see What a view can read.

The examples for these functions use the tables of the sales_db database and the files in their Amazon S3 locations. The sales_parquet table is a Hive table in Parquet format at s3://amzn-s3-demo-bucket/tables/sales_parquet/. It has six rows and the columns order_id, customer, region, amount, and order_date. The sales_csv table is a Hive table in CSV format that is partitioned by region, and sales_json is a Hive table in JSON format. The orders_iceberg table is an Iceberg table with two snapshots. Its Parquet data files and Avro metadata files are under s3://amzn-s3-demo-bucket/tables/orders_iceberg/.

The following table lists the table functions, with a link to the description of each one.

Function Description

avro_metadata

Returns the key-value metadata in the header of an Avro file, such as its schema and compression codec.

duckdb_columns

Lists the columns of the tables that a query can see, with their positions and data types.

duckdb_databases

Lists the catalogs that the engine has open for a query, which DuckDB calls databases.

duckdb_functions

Lists the functions in DuckDB, with their parameters and return types.

duckdb_schemas

Lists the databases that a query can see, which DuckDB calls schemas.

duckdb_tables

Lists the tables that a query can see, with their number of columns and a CREATE TABLE statement for each.

glob

Lists the files in Amazon S3 whose paths match a glob pattern. The utility functions topic describes it.

icu_calendar_names

Lists the names of the calendar systems that the engine's International Components for Unicode (ICU) library supports.

parquet_bloom_probe

Checks whether the Bloom filters of Parquet files rule out a value in a column, for each row group.

parquet_file_metadata

Returns the file-level metadata in the footers of Parquet files, such as the number of rows and row groups.

parquet_full_metadata

Returns all the footer metadata of each Parquet file in one row, as lists of structs.

parquet_kv_metadata

Returns the custom key-value metadata in the footers of Parquet files.

parquet_metadata

Returns the metadata of each column chunk of Parquet files, such as its statistics, compression, and size.

parquet_page_index

Returns the page index of Parquet files, with the location, row range, and statistics of each data page.

parquet_scan

Alias of read_parquet.

parquet_schema

Returns the schema in the footers of Parquet files, with one row for each schema element.

pragma_version

Returns the release line of open-source DuckDB that the engine is built from, as a table with one row.

read_avro

Reads Avro files from Amazon S3 and returns their records as rows.

read_blob

Reads files as binary data and returns one row for each file, with its name, content, size, and modification time.

read_csv

Reads CSV and other delimited text files from Amazon S3 and returns their rows as a table.

read_csv_auto

Alias of read_csv.

read_json

Reads JSON files from Amazon S3 and returns each JSON object as a row, with a column for each key.

read_json_auto

Alias of read_json.

read_json_objects

Reads JSON files from Amazon S3 and returns each top-level JSON value whole, as one JSON value per row.

read_json_objects_auto

Alias of read_json_objects.

read_ndjson

Reads newline-delimited JSON files from Amazon S3, with one object on each line, and returns a column for each key.

read_ndjson_auto

Alias of read_ndjson.

read_ndjson_objects

Reads newline-delimited JSON files from Amazon S3 and returns each line whole, as one JSON value per row.

read_parquet

Reads Apache Parquet files from Amazon S3 and returns their rows as a table.

read_text

Reads text files and returns one row for each file, with its name, content, size, and modification time.

repeat_row

Returns a table that repeats one row of values a given number of times.

sniff_csv

Detects the format of a CSV file and returns the options and column types that read_csv would use.