13.6. Reading Storage Data #
- 13.6.1. Reading Parquet Files (
read_parquet)- 13.6.2. Reading CSV Files (
read_csv)- 13.6.3. Reading JSON Files (
read_json)- 13.6.4. Reading Iceberg Tables (
iceberg_scan)- 13.6.5. Reading Iceberg Table Metadata (
iceberg_metadata)- 13.6.6. Reading Iceberg Table Snapshots (
iceberg_snapshots)- 13.6.7. Reading Delta Datasets (
delta_scan) - 13.6.2. Reading CSV Files (
This section describes how to read data from storages.
13.6.1. Reading Parquet Files (read_parquet) #
Required privileges: Superuser or the Postgres Pro AXE administrator.
For more information about access management, refer to Chapter 12.
Execute the following command on the Postgres Pro AXE server:
SELECT * FROM read_parquet('path', optional_parameters);
Where:
path: The path to the storage directory with the Parquet files that must be read.You can specify a value in one of the following formats:
a path to a single Parquet file, such as
'file.parquet'a glob pattern, such as
'data/*.parquet'an array of paths, such as
['file1.parquet', 'file2.parquet']
optional_parameters: The optional parameters, specified in theparameter := valueformat.Available parameters:
binary_as_string: Specifies whether to return binary columns as strings.Possible values:
truefalse
Default value:
false.encryption_config: The Parquet encryption configuration, specified as aSTRUCTvalue.filename: Specifies whether to return thefilenamecolumn with file paths. Since DuckDB v1.3.0, this column is added automatically.Possible values:
truefalse
Default value:
false.file_row_number: Specifies whether to return thefile_row_numbercolumn with row numbers of Parquet files.Possible values:
truefalse
Default value:
false.hive_partitioning: Specifies whether the directory is associated with a partitioned analytical table and whether partition columns are returned.Possible values:
truefalse
Default value: Detected automatically based on the directory structure.
For more information about Hive partitioning, refer to Chapter 23.
hive_types: The data types of partition columns, specified as aSTRUCTvalue.If the
hive_types_autocastparameter is set totrue, theDATE,TIMESTAMP, andBIGINTdata types are detected automatically.Default value:
''.For more information about Hive partitioning, refer to Chapter 23.
hive_types_autocast: Specifies whether to detect theDATE,TIMESTAMP, andBIGINTdata types of partition columns automatically.Possible values:
truefalse
You can specify data types of partition columns manually using the
hive_typesparameter.Default value:
true.union_by_name: Specifies whether columns from different files are aligned by name instead of by position.Possible values:
truefalse
If this parameter is set to
true, memory consumption increases.Default value:
false.schema: The custom schema for reading Parquet files, specified as aMAPvalue.
Postgres Pro AXE will return rows of Parquet files.
Example 13.6. Reading Parquet Files
SELECT * FROM read_parquet('file.parquet', binary_as_string := true);
id | name | age
----+---------+-----
1 | Alice | 30
2 | Bob | 25
3 | Charlie | 35
You can select specific columns by assigning an alias to a procedure, such as r, and using the r[' syntax: column_name']
SELECT r['id'], r['name'] FROM read_parquet('file.parquet', binary_as_string := true) r WHERE r['age'] = 25;
id | name ----+------ 2 | Bob
13.6.2. Reading CSV Files (read_csv) #
Required privileges: Superuser or the Postgres Pro AXE administrator.
For more information about access management, refer to Chapter 12.
Execute the following command on the Postgres Pro AXE server:
SELECT * FROM read_csv('path', optional_parameters);
Where:
path: The path to the storage directory with the CSV files that must be read.You can specify a value in one of the following formats:
a path to a single CSV file, such as
'file.csv'a glob pattern, such as
'data/*.csv'an array of paths, such as
['file1.csv', 'file2.csv']
optional_parameters: The optional parameters, specified in theparameter := valueformat.Available parameters:
all_varchar: Specifies whether to skip automatic detection of column data types and return all columns asVARCHAR.Possible values:
truefalse
Default value:
false.allow_quoted_nulls: Specifies whether to convert quoted values toNULLvalues.Possible values:
truefalse
Default value:
true.auto_detect: Specifies whether to automatically detect CSV parameters, such as column data types.Possible values:
truefalse
Default value:
true.auto_type_candidates: The data types to match against when automatically detecting column data types, specified as an array. If no type matches, theVARCHARtype is used.buffer_size: The size of read buffers, in bytes. It must be large enough to store four lines.Default value: 16 ×
max_line_size.comment: The character used to add comments. Lines starting with this character are ignored.Default value:
''.compression: The file compression method.Possible values:
auto_detectnonegzipzstd
Default value:
auto_detect— the compression method is detected automatically using file extensions.dateformat: The format used when parsing dates.Default value:
''.decimal_separator: The decimal separator character for numeric values.Default value:
..delim/sep: The separator character for columns. It can be up to four bytes.Default value:
,.escape: The character used to escape quotes within quoted values.Default value:
".encoding: The file encoding method.Possible values:
utf-8utf-16latin-1
Default value:
utf-8.filename: Specifies whether to return thefilenamecolumn with file paths. Since DuckDB v1.3.0, this column is added automatically.Possible values:
truefalse
Default value:
false.force_not_null: The columns for whichNULLmatching is disabled, so theirNULLvalues are returned as strings.Default value:
[]—NULLmatching is enabled for all columns.header: Specifies whether the first line of each CSV file contains column names.Possible values:
truefalse
Default value:
false.hive_partitioning: Specifies whether the directory is associated with a partitioned analytical table and whether partition columns are returned.Possible values:
truefalse
Default value: Detected automatically based on the directory structure.
For more information about Hive partitioning, refer to Chapter 23.
ignore_errors: Specifies whether to ignore parsing errors.Possible values:
truefalse
Default value:
false.max_line_size/maximum_line_size: The maximum line size, in bytes.Default value:
2000000.names/column_names: The column names used when CSV files do not contain headers, specified as an array.Default value:
''— column names are detected automatically.new_line: The character for new lines.Possible values:
\r\n\r\n
Default value:
''.normalize_names: Specifies whether to normalize column names by removing non-alphanumeric characters. Column names that match SQL reserved keywords are prefixed with an underscore (_).Possible values:
truefalse
Default value:
false.null_padding: Specifies whether to fill the missing columns withNULLvalues when a line has fewer columns than expected.Possible values:
truefalse
Default value:
false.nullstr/null: The strings to return asNULLvalues, specified as an array.Default value:
''.parallel: Specifies whether to use the parallel CSV reader.Possible values:
truefalse
Default value:
true.quote: The character used to quote values.Default value:
".rejects_scan: The name of the temporary table where information about file-level parsing errors is stored.Default value:
reject_scans.rejects_table: The name of the temporary table where information about line-level parsing errors is stored.Default value:
reject_errors.rejects_limit: The maximum number of line-level parsing errors per file to store.Default value:
0— all line-level parsing errors are stored.sample_size: The number of lines sampled within each CSV file for automatic detection of CSV parameters.Default value:
20480.skip: The number of lines to skip at the start of each CSV file.Default value:
0.store_rejects: Specifies whether to skip lines with parsing errors and store information about them in temporary tables specified by therejects_scanandrejects_tableparameters.Possible values:
truefalse
Default value:
false.strict_mode: Specifies whether to enforce the strictness level of the CSV reader.Possible values:
truefalse
If this parameter is set to
false, the CSV reader attempts to process structurally incorrect files, which may produce unexpected results.Default value:
true.thousands: The character used as the thousands separator in numeric values. It must differ from thedecimal_separatorvalue.Default value:
''.timestampformat: The format used when parsing timestamps.Default value:
''.types/dtypes/column_types: The column data types to use instead of the automatically detected ones.You can specify a value in one of the following formats:
an array of type names matching the column order, such as
['INTEGER', 'VARCHAR']a
STRUCTvalue with column names as keys, such as{'col1': 'INTEGER'}
Default value:
''— column types are detected automatically.union_by_name: Specifies whether columns from different files are aligned by name instead of by position.Possible values:
truefalse
If this parameter is set to
true, memory consumption increases.Default value:
false.
Postgres Pro AXE will return rows of CSV files.
Example 13.7. Reading CSV Files
Contents of file.csv:
1;Alice;30;2020-01-15 2;Bob;25;2021-03-22 3;Charlie;35;2019-07-01
SELECT * FROM read_csv('file.csv',
delim := ';',
header := false,
names := ARRAY['id', 'name', 'age', 'hired'],
types := ARRAY['INTEGER', 'VARCHAR', 'INTEGER', 'DATE']);
id | name | age | hired
----+---------+-----+------------
1 | Alice | 30 | 2020-01-15
2 | Bob | 25 | 2021-03-22
3 | Charlie | 35 | 2019-07-01
You can select specific columns by assigning an alias to a procedure, such as r, and using the r[' syntax: column_name']
SELECT r['id'], r['name'] FROM read_csv('file.csv',
delim := ';',
header := false,
names := ARRAY['id', 'name', 'age', 'hired'],
types := ARRAY['INTEGER', 'VARCHAR', 'INTEGER', 'DATE']) r
WHERE r['age'] = 25;
id | name ----+------ 2 | Bob
13.6.3. Reading JSON Files (read_json) #
Required privileges: Superuser or the Postgres Pro AXE administrator.
For more information about access management, refer to Chapter 12.
Execute the following command on the Postgres Pro AXE server:
SELECT * FROM read_json('path', optional_parameters);
Where:
path: The path to the storage directory with the JSON files that must be read.You can specify a value in one of the following formats:
a path to a single JSON file, such as
'file.json'a glob pattern, such as
'data/*.json'an array of paths, such as
['file1.json', 'file2.json']
optional_parameters: The optional parameters, specified in theparameter := valueformat.Available parameters:
compression: The file compression method.Possible values:
auto_detectnonegzipzstd
Default value:
auto_detect— the compression method is detected automatically using file extensions.filename: Specifies whether to return thefilenamecolumn with file paths. Since DuckDB v1.3.0, this column is added automatically.Possible values:
truefalse
Default value:
false.format: The JSON format.Possible values:
autounstructurednewline_delimitedarray
Default value:
array.hive_partitioning: Specifies whether the directory is associated with a partitioned analytical table and whether partition columns are returned.Possible values:
truefalse
Default value: Detected automatically based on the directory structure.
For more information about Hive partitioning, refer to Chapter 23.
ignore_errors: Specifies whether to ignore parsing errors.Possible values:
truefalse
This parameter is applicable only if the
formatparameter is set tonewline_delimited.Default value:
false.maximum_sample_files: The maximum number of JSON files sampled for automatic detection of column names and data types.You can specify the number of JSON objects sampled within each file manually using the
sample_sizeparameter.Default value:
32.maximum_object_size: The maximum size of a JSON object, in bytes.Default value:
16777216.auto_detect: Specifies whether to automatically detect column names and data types.Possible values:
truefalse
Default value:
true.dateformat: The format used when parsing dates.Default value:
iso.maximum_depth: The maximum nesting depth for automatic detection of column names and data types.Default value:
-1— no nesting depth limit.records: Specifies whether to return JSON objects as individual columns.Possible values:
autotruefalse
If this parameter is set to
false, each JSON object is returned as aSTRUCTvalue.Default value:
auto.sample_size: The number of JSON objects sampled within each file for automatic detection of column names and data types.You can specify the number of sampled JSON files manually using the
maximum_sample_filesparameter.To scan JSON files entirely, set this parameter to
-1.Default value:
20480.timestampformat: The format used when parsing timestamps.Default value:
iso.union_by_name: Specifies whether columns from different files are aligned by name instead of by position.Possible values:
truefalse
If this parameter is set to
true, memory consumption increases.Default value:
false.map_inference_threshold: The threshold at which automatic detection of column names and data types assigns theMAPtype instead of theSTRUCTtype to a JSON object.To disable automatic assignment of the
MAPtype, set this parameter to-1.Default value:
200.field_appearance_threshold: The threshold that determines whether the JSON reader assigns theMAPor theSTRUCTdata type to a JSON object.For each field, the JSON reader calculates how frequently the field appears relative to the number of objects in the sample. If the average of these ratios across all fields of an object is below this threshold, the reader defaults to assigning the
MAPdata type to this object.Default value:
0.1.
Postgres Pro AXE will return rows of JSON files.
Example 13.8. Reading JSON Files
Contents of file.json:
[
{"id": 1, "name": "Alice", "age": 30, "hired": "2020-01-15"},
{"id": 2, "name": "Bob", "age": 25, "hired": "2021-03-22"},
{"id": 3, "name": "Charlie", "age": 35, "hired": "2019-07-01"}
]
SELECT * FROM read_json('file.json');
id | name | age | hired ----+---------+-----+------------ 1 | Alice | 30 | 2020-01-15 2 | Bob | 25 | 2021-03-22 3 | Charlie | 35 | 2019-07-01
You can select specific columns by assigning an alias to a procedure, such as r, and using the r[' syntax: column_name']
SELECT r['id']::INTEGER AS id,
r['name']::VARCHAR AS name,
r['age']::INTEGER AS age,
r['hired']::DATE AS hired
FROM read_json('file.json') AS r
WHERE r['age']::INTEGER = 25;
id | name | age | hired ----+------+-----+------------ 2 | Bob | 25 | 2021-03-22
13.6.4. Reading Iceberg Tables (iceberg_scan) #
Before reading Iceberg tables, install the Iceberg extension.
Required privileges: Superuser or the Postgres Pro AXE administrator.
For more information about access management, refer to Chapter 12.
Execute the following command on the Postgres Pro AXE server:
SELECT * FROM iceberg_scan('path', optional_parameters);
Where:
path: The path to the storage directory with the Iceberg table that must be read.optional_parameters: The optional parameters, specified in theparameter := valueformat.Available parameters:
allow_moved_paths: Specifies whether to read Iceberg tables that are moved to a different directory.Possible values:
truefalse
Default value:
false.metadata_compression_codec: The compression codec to use when reading metadata files.Default value:
''— no compression codec is used.snapshot_from_id: The ID of the snapshot associated with the Iceberg table.Default value:
NULL— the latest snapshot is used.snapshot_from_timestamp: The timestamp of the snapshot associated with the Iceberg table.Default value:
NULL— the latest snapshot is used.version: The version of the Iceberg table metadata file.Default value:
'?'— the version is determined automatically.version_name_format: The format string that determines how theversionvalue is converted to the Iceberg table metadata file name.Default value:
'v%s%s.metadata.json,%s%s.metadata.json'.
Postgres Pro AXE will return rows of the Iceberg table.
Example 13.9. Reading an Iceberg Table
SELECT * FROM iceberg_scan('data/iceberg/table');
id | name | age
----+---------+-----
1 | Alice | 30
2 | Bob | 25
3 | Charlie | 35
You can select specific columns by assigning an alias to a procedure, such as r, and using the r[' syntax: column_name']
SELECT r['id'], r['name'] FROM iceberg_scan('data/iceberg/table') r WHERE r['age'] = 25;
id | name ----+------ 2 | Bob
13.6.5. Reading Iceberg Table Metadata (iceberg_metadata) #
Before reading Iceberg table metadata, install the Iceberg extension.
Required privileges: Superuser or the Postgres Pro AXE administrator.
For more information about access management, refer to Chapter 12.
Execute the following command on the Postgres Pro AXE server:
SELECT * FROM iceberg_metadata('path', optional_parameters);
Where:
path: The path to the storage directory with the Iceberg table from which metadata must be read.optional_parameters: The optional parameters, specified in theparameter := valueformat.Available parameters:
allow_moved_paths: Specifies whether to read Iceberg tables that are moved to a different directory.Possible values:
truefalse
Default value:
false.metadata_compression_codec: The compression codec to use when reading metadata files.Default value:
''— no compression codec is used.snapshot_from_id: The ID of the snapshot associated with the Iceberg table.Default value:
NULL— the latest snapshot is used.snapshot_from_timestamp: The timestamp of the snapshot associated with the Iceberg table.Default value:
NULL— the latest snapshot is used.version: The version of the Iceberg table metadata file.Default value:
'?'— the version is determined automatically.version_name_format: The format string that determines how theversionvalue is converted to the Iceberg table metadata file name.Default value:
'v%s%s.metadata.json,%s%s.metadata.json'.
Postgres Pro AXE will return rows with the metadata of the Iceberg table.
Example 13.10. Reading Iceberg Table Metadata
SELECT * FROM iceberg_metadata('data/iceberg/table');
13.6.6. Reading Iceberg Table Snapshots (iceberg_snapshots) #
Before reading Iceberg table snapshots, install the Iceberg extension.
Required privileges: Superuser or the Postgres Pro AXE administrator.
For more information about access management, refer to Chapter 12.
Execute the following command on the Postgres Pro AXE server:
SELECT * FROM iceberg_snapshots('path', optional_parameters);
Where:
path: The path to the storage directory with the Iceberg table from which snapshots must be read.optional_parameters: The optional parameters, specified in theparameter := valueformat.Available parameters:
metadata_compression_codec: The compression codec to use when reading metadata files.Default value:
''— no compression codec is used.version: The version of the Iceberg table metadata file.Default value:
'?'— the version is determined automatically.version_name_format: The format string that determines how theversionvalue is converted to the Iceberg table metadata file name.Default value:
'v%s%s.metadata.json,%s%s.metadata.json'.
Postgres Pro AXE will return rows with snapshots of the Iceberg table.
Example 13.11. Reading Iceberg Table Snapshots
SELECT * FROM iceberg_snapshots('data/iceberg/table');
13.6.7. Reading Delta Datasets (delta_scan) #
Before reading Delta datasets, install the Delta extension.
Required privileges: Superuser or the Postgres Pro AXE administrator.
For more information about access management, refer to Chapter 12.
Execute the following command on the Postgres Pro AXE server:
SELECT * FROM delta_scan('path', optional_parameters);
Where:
path: The path to the storage directory with the Delta dataset that must be read.optional_parameters: The optional parameters, specified in theparameter := valueformat.Available parameters:
delta_file_number: Specifies whether to return thedelta_file_numbercolumn with the index of the physical Parquet file for each row.Possible values:
truefalse
Default value:
false.binary_as_string: Specifies whether to return binary columns as strings.Possible values:
truefalse
Default value:
false.filename: Specifies whether to return thefilenamecolumn with file paths. Since DuckDB v1.3.0, this column is added automatically.Possible values:
truefalse
Default value:
false.file_row_number: Specifies whether to return thefile_row_numbercolumn with row numbers of Parquet files.Possible values:
truefalse
Default value:
false.hive_partitioning: Specifies whether the directory is associated with a partitioned analytical table and whether partition columns are returned.Possible values:
truefalse
Default value: Detected automatically based on the directory structure.
For more information about Hive partitioning, refer to Chapter 23.
hive_types: The data types of partition columns, specified as aSTRUCTvalue.If the
hive_types_autocastparameter is set totrue, theDATE,TIMESTAMP, andBIGINTdata types are detected automatically.Default value:
''.For more information about Hive partitioning, refer to Chapter 23.
hive_types_autocast: Specifies whether to detect theDATE,TIMESTAMP, andBIGINTdata types of partition columns automatically.Possible values:
truefalse
You can specify data types of partition columns manually using the
hive_typesparameter.Default value:
true.union_by_name: Specifies whether columns from different files are aligned by name instead of by position.Possible values:
truefalse
If this parameter is set to
true, memory consumption increases.Default value:
false.schema: The custom schema for reading Parquet files, specified as aMAPvalue.
Postgres Pro AXE will return rows of the Delta dataset.
Example 13.12. Reading a Delta Dataset
SELECT * FROM delta_scan('data/delta/dataset');
id | name | age
----+---------+-----
1 | Alice | 30
2 | Bob | 25
3 | Charlie | 35
You can select specific columns by assigning an alias to a procedure, such as r, and using the r[' syntax: column_name']
SELECT r['id'], r['name'] FROM delta_scan('data/delta/dataset') r WHERE r['age'] = 25;
id | name ----+------ 2 | Bob