Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails value insertion if type unsupported by duckdb #471

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

dentiny
Copy link

@dentiny dentiny commented Dec 3, 2024

I would like to followup on issue: #467

I think current implementation is not ideal:

  • If user creates a table with duckdb unsupported numeric type, we silently fallback to double type
  • Later write operation doesn't always fail, based on the input numeric value, which adds more confusion to the users

Here I propose two improvements:

  • method-1: No silent fallback on type conversion "failure"; so we print a warning message with elog(WARNING, ...), and get users aware of the fallback behavior;
  • method-2: Instead of falling back to double type, we fallback to user-defined type, since it's not supported by duckdb (system) anyway.

In this PR, I selected method-2, but I'm open to other feedbacks and suggestions.
The goal here is to disallow silent fallback and surprising write behavior.

Edit:
Followup on feedback #471 (comment), I fail the table creation if column type is not supported by duckdb.

src/pgduckdb_types.cpp Outdated Show resolved Hide resolved
@dentiny dentiny force-pushed the hjiang/out-of-scale-type-user branch from f245d2d to 76681ba Compare December 3, 2024 08:40
@JelteF
Copy link
Collaborator

JelteF commented Dec 3, 2024

Thank you for the contribution.

The fact that we silently fall back to double has been bothering me too (see #309). I think it would be nice to make this a configurable setting that should probably be off by default, called something like duckdb.use_double_for_large_numerics.

However, afaict in the case you're describing this is actually not what is happening. In this case NUMERIC is converted to DECIMAL(18, 3), not double.

> CREATE TEMP TABLE t (large_number NUMERIC) USING duckdb;
CREATE TABLE
> select * from duckdb.raw_query($$ describe pg_temp.main.t $$);
NOTICE:  00000: result: column_name column_type null    key default extra
VARCHAR VARCHAR VARCHAR VARCHAR VARCHAR VARCHAR
[ Rows: 1]
large_number    DECIMAL(18,3)   YES NULL    NULL    NULL

@JelteF
Copy link
Collaborator

JelteF commented Dec 3, 2024

To be clear, I think it's probably reasonable to disallow specifying plain NUMERIC as the type for duckdb tables and require specifying a width and scale. But I don't think the current code actually has that effect.

@dentiny
Copy link
Author

dentiny commented Dec 4, 2024

Both suggestion makes sense to me, thank you! I will make changes these days.

@dentiny dentiny marked this pull request as draft December 4, 2024 11:27
@dentiny dentiny changed the title Make out-of-scale postgres user defined duckdb type [WIP] Make out-of-scale postgres user defined duckdb type Dec 4, 2024
@dentiny dentiny force-pushed the hjiang/out-of-scale-type-user branch 3 times, most recently from f08e29f to 0e97387 Compare December 5, 2024 12:11
@dentiny
Copy link
Author

dentiny commented Dec 5, 2024

Hi @JelteF , I tried to implemented your suggestion, which fails to create table if column type is not supported by duckdb.
I verified by existing SQL test statements with make check.

@dentiny dentiny changed the title [WIP] Make out-of-scale postgres user defined duckdb type Fails table creation if column type unsupported by duckdb Dec 5, 2024
@dentiny dentiny marked this pull request as ready for review December 5, 2024 12:16
src/pgduckdb_ddl.cpp Show resolved Hide resolved
@@ -253,6 +292,10 @@ DECLARE_PG_FUNCTION(duckdb_create_table_trigger) {
elog(ERROR, "Expected single table to be created, but found %" PRIu64, static_cast<uint64_t>(SPI_processed));
}

// Check whether new table creation is supported in duckdb.
// TODO(hjiang): Same type validation should be applied to other DDL as well.
ValidateRelationCreation(SPI_tuptable->tupdesc);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: prob move this check to pgduckdb_get_tabledef(), where other lockdowns (e.g., generated columns, identity columns) are handled

Copy link
Author

Choose a reason for hiding this comment

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

I think it makes sense, my concern is, other DDL statements (i.e. ALTER TABLE) require the same validation logic.
I would prefer to put the validation logic in the same file.

Copy link
Author

@dentiny dentiny Dec 6, 2024

Choose a reason for hiding this comment

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

where other lockdowns (e.g., generated columns, identity columns) are handled

I fully agree to place all validation logic into one function / one place.
My personal preference is to move all logic into DDL file, since other validations (i.e. generated columns, as you mentioned) should also apply to other DDL statements.

Would like to know how pg_duckdb's owner think about it. :)

@dentiny dentiny force-pushed the hjiang/out-of-scale-type-user branch 2 times, most recently from 50e9d3a to 331bd4e Compare December 6, 2024 11:01
@dentiny dentiny requested a review from dpxcc December 6, 2024 17:05
@dentiny
Copy link
Author

dentiny commented Dec 16, 2024

Hi @JelteF , I'm wondering if you could take another look when you have some time? Thank you!
Let me know if there's anything else I should change.

The fact that we silently fall back to double has been bothering me too (see #309). I think it would be nice to make this a configurable setting that should probably be off by default, called something like duckdb.use_double_for_large_numerics.

On this proposal, I will address it in another followup PR.

@dentiny dentiny marked this pull request as draft December 16, 2024 10:34
@dentiny
Copy link
Author

dentiny commented Dec 16, 2024

There's branch conflict and unit test failure, let me resolve them before re-requesting reviews.

@dentiny dentiny changed the title Fails table creation if column type unsupported by duckdb [WIP] Fails table creation if column type unsupported by duckdb Dec 16, 2024
@dentiny dentiny force-pushed the hjiang/out-of-scale-type-user branch from 331bd4e to d29be83 Compare December 21, 2024 09:01
@dentiny dentiny marked this pull request as ready for review December 21, 2024 10:42
@dentiny dentiny changed the title [WIP] Fails table creation if column type unsupported by duckdb Fails table creation if column type unsupported by duckdb Dec 21, 2024
@dentiny dentiny changed the title Fails table creation if column type unsupported by duckdb Fails value insertion if type unsupported by duckdb Dec 21, 2024
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.

3 participants