Skip to content

Conversation

JelteF
Copy link
Collaborator

@JelteF JelteF commented Feb 11, 2025

In DuckDB the canonical name for an unlimited size text column is VARCHAR1, in Postgres this is TEXT2. In DuckDB TEXT is simply an alias for VARCHAR type, and there's no way to know what was provided by the user. In Postgres these types are actually distinct, although behave exactly the same for unlimited length. Basically everyone uses TEXT instead of VARCHAR.

Currently we convert the DuckDB type to a Postgres VARCHAR. In many cases this doesn't really matter, because pretty much all clients handle VARCHAR and TEXT the same too. There are a few places where this leaks through though: DDL coming from a query. For example if you do a CTAS with a DuckDB query the resulting table columns will be of type character varying instead of text3. Similarly when creating a MotherDuck table each also character varying will be displayed as the type instead of text. In both cases that looks pretty strange to a Postgres user, and overly long. So this starts using text as the PG equivalent type for the DuckDB VARCHAR type.

@@ -1,4 +1,4 @@
CREATE TABLE t(a INT, b VARCHAR);
CREATE TABLE t(a INT, b TEXT);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There seems to be some bug when creating materialized views. Without changing this type we'd get an error like this:

+ERROR:  SELECT rule's target entry 2 has different type from column "b"
+DETAIL:  SELECT target entry has type character varying, but column has type text.

That shouldn't happen, the type should be detected as text everywhere.

@JelteF JelteF added this to the 0.4.0 milestone Feb 11, 2025
@JelteF JelteF force-pushed the text-instead-of-varchar branch 2 times, most recently from 26b7ea7 to 7b53385 Compare April 16, 2025 13:18
@JelteF JelteF changed the base branch from main to fix-type-issues-with-ctas-and-matview April 16, 2025 13:19
JelteF added a commit that referenced this pull request Apr 16, 2025
DuckDB and Postgres can return different types for the same queries.
This can confuse Postgres in various ways when these queries are
involved in CTAS or materialized views. This fixes a bunch of issues
related to that and adds appropriate regression tests.

This includes a fix for the materialized view issue that was found in
#583
Base automatically changed from fix-type-issues-with-ctas-and-matview to main April 16, 2025 14:42
@JelteF JelteF force-pushed the text-instead-of-varchar branch 2 times, most recently from f25062e to 4f24c16 Compare April 17, 2025 07:40
@JelteF JelteF added the enhancement New feature or request label Apr 17, 2025
@JelteF JelteF force-pushed the text-instead-of-varchar branch from 4f24c16 to b666739 Compare April 17, 2025 13:17
In DuckDB the canonical name for an unlimited size text column is
`VARCHAR`[1], in Postgres this is `TEXT`[2]. In DuckDB `TEXT` is simply
an alias for `VARCHAR` type, and there's no way to know what was
provided by the user. In Postgres these types are actually distinct,
although behave exactly the same for unlimited length. Basically
everyone uses `TEXT` instead of `VARCHAR`.

Currently we convert the DuckDB type to a Postgres `VARCHAR`. In many
cases this doesn't really matter, because pretty much all clients handle
VARCHAR and TEXT the same too. There's one place where this leaks
through though: DDL coming from a query. For example if you do a CTAS
with a DuckDB query the resulting table columns will be of type
`character varying` instead of `text`[3].

[1]: https://duckdb.org/docs/sql/data_types/text.html
[2]: https://www.postgresql.org/docs/current/datatype-character.html
[3]: #556 (comment)
@JelteF JelteF force-pushed the text-instead-of-varchar branch from b666739 to f0fff52 Compare April 17, 2025 13:27
@JelteF JelteF marked this pull request as ready for review April 17, 2025 13:28
@JelteF JelteF requested a review from Y-- April 17, 2025 13:28
@JelteF JelteF enabled auto-merge (squash) April 17, 2025 13:32
@JelteF JelteF merged commit b5b24d2 into main Apr 17, 2025
6 checks passed
@JelteF JelteF deleted the text-instead-of-varchar branch April 17, 2025 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants