Skip to content

Conversation

lnkuiper
Copy link
Contributor

This PR implements the schema parameter for read_parquet, which allows us to read a Parquet file as if it has the supplied schema. Field IDs are required. For example:

COPY (SELECT 42::INTEGER i) TO 'integers.parquet' (FIELD_IDS {i: 0});
SELECT *
FROM read_parquet('integers.parquet', schema=map {
                    0: {name: 'renamed_i', type: 'BIGINT', default_value: NULL},
                    1: {name: 'new_column', type: 'UTINYINT', default_value: 43}
                  });
-- ┌───────────┬────────────┐
-- │ renamed_i │ new_column │
-- │   int64   │   uint8    │
-- ├───────────┼────────────┤
-- │        42 │         43 │
-- └───────────┴────────────┘

Columns are identified by field id and can be added, deleted, reordered, renamed, and cast to a different type.

This parameter cannot be combined with union_by_name=true, and for now, it also cannot be combined with hive partitioning. Nested types are also not (yet) supported.

@lnkuiper lnkuiper requested a review from samansmink September 27, 2023 08:12
Copy link
Contributor

@samansmink samansmink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool! I added 2 minor comments, but in general it looks good!

@Mytherin Mytherin changed the base branch from main to feature September 27, 2023 09:09
@github-actions github-actions bot marked this pull request as draft September 27, 2023 10:33
@lnkuiper
Copy link
Contributor Author

Thanks for the feedback! I've added the tests, and disabled auto-detection of hive partitioning when the schema parameter is used

@lnkuiper lnkuiper marked this pull request as ready for review September 27, 2023 10:50
@samansmink
Copy link
Contributor

LGTM!

@samansmink
Copy link
Contributor

samansmink commented Sep 29, 2023

woops, can I still undo declaring LGTM? I have started experimenting with this feature in the iceberg extension and ran into the following test failure:

query I
SELECT count(*)
FROM read_parquet('__TEST_DIR__/integers.parquet', schema=map {
                    0: {name: 'renamed_i', type: 'BIGINT', default_value: NULL},
                    1: {name: 'new_column', type: 'UTINYINT', default_value: 43}
                  })
----
2

fails with

Actual result:
INTERNAL Error: Attempted to access index 1 within vector of size 1

edit: i think the solution is quite simple, we can have a look together @lnkuiper
edit2: i think I have a fix in https://github.com/samansmink/duckdb/tree/parquet-schema-fix which i branched off this PR

@github-actions github-actions bot marked this pull request as draft October 4, 2023 07:25
@lnkuiper
Copy link
Contributor Author

lnkuiper commented Oct 4, 2023

Thanks for the feedback @Tishj and for finding the bug @samansmink ! I have updated the PR and pulled Sam's fix into it.

@lnkuiper lnkuiper marked this pull request as ready for review October 4, 2023 07:29
@lnkuiper
Copy link
Contributor Author

lnkuiper commented Oct 4, 2023

I think this is ready to go!

@Mytherin Mytherin changed the base branch from feature to main October 5, 2023 16:59
@Mytherin Mytherin changed the base branch from main to feature October 5, 2023 16:59
@Mytherin Mytherin merged commit c3aa759 into duckdb:feature Oct 5, 2023
@Mytherin
Copy link
Collaborator

Mytherin commented Oct 5, 2023

Thanks!

@lnkuiper lnkuiper deleted the parquet_schema branch November 24, 2023 13:36
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request Dec 11, 2023
Merge pull request duckdb/duckdb#9164 from Mause/feature/jdbc-uuid-param
Merge pull request duckdb/duckdb#9185 from pdet/adbc_07
Merge pull request duckdb/duckdb#9126 from Maxxen/parquet-kv-metadata
Merge pull request duckdb/duckdb#9123 from lnkuiper/parquet_schema
Merge pull request duckdb/duckdb#9086 from lnkuiper/json_inconsistent_structure
Merge pull request duckdb/duckdb#8977 from Tishj/python_readcsv_multi_v2
Merge pull request duckdb/duckdb#9279 from hawkfish/nsdate-cast
Merge pull request duckdb/duckdb#8851 from taniabogatsch/binary_lambdas
Merge pull request duckdb/duckdb#8983 from Maxxen/types/fixedsizelist
Merge pull request duckdb/duckdb#9318 from Maxxen/fix-unused
Merge pull request duckdb/duckdb#9220 from hawkfish/exclude
Merge pull request duckdb/duckdb#9230 from Maxxen/json-plan-serialization
Merge pull request duckdb/duckdb#9011 from Tmonster/add_create_statement_support_to_fuzzer
Merge pull request duckdb/duckdb#9400 from Maxxen/array-fixes
Merge pull request duckdb/duckdb#8741 from Tishj/python_import_cache_upgrade
Merge fixes
Merge pull request duckdb/duckdb#9395 from taniabogatsch/lambda-performance
Merge pull request duckdb/duckdb#9427 from Tishj/python_table_support_replacement_scan
Merge pull request duckdb/duckdb#9516 from carlopi/fixformat
Merge pull request duckdb/duckdb#9485 from Maxxen/fix-parquet-serialization
Merge pull request duckdb/duckdb#9388 from chrisiou/issue217
Merge pull request duckdb/duckdb#9565 from Maxxen/fix-array-vector-sizes
Merge pull request duckdb/duckdb#9583 from carlopi/feature
Merge pull request duckdb/duckdb#8907 from cryoEncryp/new-list-functions
Merge pull request duckdb/duckdb#8642 from Virgiel/capi-streaming-arrow
Merge pull request duckdb/duckdb#8658 from Tishj/pytype_optional
Merge pull request duckdb/duckdb#9040 from Light-City/feature/set_mg
Mytherin added a commit that referenced this pull request Jan 15, 2025
…bal column (#15446)

This PR essentially moves the specialized code that was already in the
parquet extension for matching on `field_id`, added by
<#9123>, into the MultiFileReader.

It also makes it possible to map a local (per-file) column name to a
different global name.

To do this we bundle the type+name into a struct
(`MultiFileReaderColumnDefinition`), where we can also bundle additional
metadata like default values, and the parquet field_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants