Skip to content

Commit

Permalink
Return TEXT instead of VARCHAR columns
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
JelteF committed Feb 11, 2025
1 parent f884d32 commit ee3e580
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/pgduckdb_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ GetPostgresArrayDuckDBType(const duckdb::LogicalType &type) {
case duckdb::LogicalTypeId::UINTEGER:
return INT8ARRAYOID;
case duckdb::LogicalTypeId::VARCHAR:
return type.IsJSONType() ? JSONARRAYOID : VARCHARARRAYOID;
return type.IsJSONType() ? JSONARRAYOID : TEXTARRAYOID;
case duckdb::LogicalTypeId::DATE:
return DATEARRAYOID;
case duckdb::LogicalTypeId::TIMESTAMP:
Expand Down Expand Up @@ -1064,7 +1064,7 @@ GetPostgresDuckDBType(const duckdb::LogicalType &type) {
case duckdb::LogicalTypeId::UINTEGER:
return INT8OID;
case duckdb::LogicalTypeId::VARCHAR:
return type.IsJSONType() ? JSONOID : VARCHAROID;
return type.IsJSONType() ? JSONOID : TEXTOID;
case duckdb::LogicalTypeId::DATE:
return DATEOID;
case duckdb::LogicalTypeId::TIMESTAMP:
Expand Down
2 changes: 1 addition & 1 deletion test/regression/expected/materialized_view.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE t(a INT, b VARCHAR);
CREATE TABLE t(a INT, b TEXT);
INSERT INTO t SELECT g % 100, MD5(g::VARCHAR) FROM generate_series(1,1000) g;
SELECT COUNT(*) FROM t WHERE a % 10 = 0;
count
Expand Down
2 changes: 1 addition & 1 deletion test/regression/sql/materialized_view.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE t(a INT, b VARCHAR);
CREATE TABLE t(a INT, b TEXT);
INSERT INTO t SELECT g % 100, MD5(g::VARCHAR) FROM generate_series(1,1000) g;
SELECT COUNT(*) FROM t WHERE a % 10 = 0;
CREATE MATERIALIZED VIEW tv AS SELECT * FROM t WHERE a % 10 = 0;
Expand Down

0 comments on commit ee3e580

Please sign in to comment.