Skip to content
This repository was archived by the owner on Jan 28, 2026. It is now read-only.

Postgres to duck db type remappings#23

Merged
avichalp merged 6 commits intomainfrom
avichalp/type-remapping
Dec 11, 2023
Merged

Postgres to duck db type remappings#23
avichalp merged 6 commits intomainfrom
avichalp/type-remapping

Conversation

@avichalp
Copy link
Contributor

@avichalp avichalp commented Nov 28, 2023

This pull request introduces type conversion between Postgres and DuckDB. Most of the Postgres data types are mapped to equivalent DuckDB but there are three exceptions. The following types are not supported in this PR but can be added later.

  • Custom enum types
  • Custom strust types
  • 2 or more dimensional arrays

@avichalp avichalp force-pushed the avichalp/type-remapping branch from 643282c to 8307bf2 Compare November 28, 2023 16:45
Signed-off-by: avichalp <hi@avichalp.me>
Signed-off-by: avichalp <hi@avichalp.me>
Signed-off-by: avichalp <hi@avichalp.me>
Signed-off-by: avichalp <hi@avichalp.me>
Signed-off-by: avichalp <hi@avichalp.me>
Signed-off-by: avichalp <hi@avichalp.me>
@avichalp avichalp force-pushed the avichalp/type-remapping branch from 8307bf2 to d1018fd Compare November 28, 2023 16:47
c.column_name,
pg_catalog.format_type(t.oid, NULL) AS full_data_type
FROM information_schema.columns AS c
JOIN pg_catalog.pg_type AS t ON c.udt_name = t.typname
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Enhances the inspect query to get metadata for columns of type ARRAY.

Screenshot 2023-11-29 at 01 31 04

// pgToDDBType maps a PG type to a duckdb type.
func (dbm *DBManager) pgToDDBType(typ string) (duckdbType, error) {
// handle character(N), character varying(N), numeric(N, M)
if strings.HasSuffix(typ, ")") {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Handle edge cases and finds appropriate type from typeConversionMap

Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: use strings.Cut, instead of strings.Split. example: https://go.dev/play/p/J4OwaJGJqyB

// typeConversionMap maps PG types to duckdb types.
// currently, only a subset of PG types are supported.
// Custom enum types, sturcts, multi-dimensional arrays are not supported.
var typeConversionMap = map[string]duckdbType{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

PG type is mapped to a DuckDB type and a transform function that will transform the column value from the WAL JSON into value that can be used in the Duck DB query.

@avichalp avichalp marked this pull request as ready for review November 29, 2023 07:43
Copy link
Contributor

@brunocalza brunocalza left a comment

Choose a reason for hiding this comment

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

Nice work! Looks like you exhaustively tested all types, really nice. Looks good to me! Could not find any problem, lmk if you need help with manual tests

@avichalp avichalp merged commit bc45387 into main Dec 11, 2023
@avichalp avichalp deleted the avichalp/type-remapping branch December 11, 2023 13:24
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants