Skip to content

fix(pgsql-source): use CAST(col AS text) instead of col::text for Redshift#1746

Merged
dimitri merged 3 commits into
mainfrom
fix/redshift-cast-syntax
Jun 27, 2026
Merged

fix(pgsql-source): use CAST(col AS text) instead of col::text for Redshift#1746
dimitri merged 3 commits into
mainfrom
fix/redshift-cast-syntax

Conversation

@dimitri

@dimitri dimitri commented Jun 27, 2026

Copy link
Copy Markdown
Owner

Closes #1549.

Problem

When using Redshift as the data source for a pgsql-to-pgsql migration, pgloader generates a SELECT that casts every column to text using PostgreSQL's :: shorthand:

SELECT "boolean_column"::text, ... FROM "schema"."table"

Redshift raises:

Database error 42846: cannot cast type boolean to character varying

The :: cast operator is a PostgreSQL extension that Redshift only partially supports — boolean→text is not allowed via :: in Redshift. The ANSI SQL CAST(expr AS type) syntax works for all types on both engines.

Affected code paths

There are four call-sites, all now fixed:

File Context Before After
src/sources/pgsql/pgsql.lisp v3 normal table SELECT ~s::text CAST(~s AS text)
src/utils/citus.lisp v3 Citus backfill SELECT ~a::text CAST(~a AS text)
clojure/src/pgloader/source/pgsql.clj v4 normal table SELECT plain "col" CAST("col" AS text)
clojure/src/pgloader/ddl/citus.clj v4 Citus backfill SELECT tbl.col::text CAST(tbl.col AS text)

The Citus backfill SELECT is generated against the source database. In a Redshift→Citus migration (Redshift as source, Citus-distributed PostgreSQL as target), pgloader must JOIN source tables to retrieve the distribution key — that JOIN query runs on Redshift, so it also needs CAST syntax. The test/citus/README.md example is updated to reflect the generated SQL.

Testing

  • Regular PostgreSQL: CAST(col AS text) and col::text are semantically identical; existing regression tests cover this path.
  • Citus backfill: test/citus/ documents the generated SQL; the updated README shows the new form.
  • Redshift: no CI environment available; the change is a mechanical substitution of two equivalent syntaxes.

dimitri added 3 commits June 27, 2026 14:10
…shift (#1549)

Redshift does not support the col::text cast operator for all type
combinations — in particular 'cannot cast type boolean to character
varying' is raised for any boolean column when reading from a Redshift
source.

The ANSI SQL CAST(expression AS type) syntax is supported by both
standard PostgreSQL and Redshift, so replace the PostgreSQL-specific
::text shorthand with it everywhere a source SELECT casts columns to text:

  v3 src/sources/pgsql/pgsql.lisp
    SELECT col::text → SELECT CAST(col AS text)

  v4 clojure/src/pgloader/source/pgsql.clj
    SELECT "col" → SELECT CAST("col" AS text)
    (v4 previously relied on JDBC .getObject()+str for implicit conversion;
    explicit casting is safer and makes Redshift compatibility explicit)

  v4 clojure/src/pgloader/ddl/citus.clj
    tbl.col::text → CAST(tbl.col AS text)
    (Citus backfill SELECT, pgsql-source path)
…rce compatibility

The Citus backfill SELECT is generated against the SOURCE database.
When the source is Redshift, col::text fails for boolean and other types
that Redshift does not support via the :: operator.

Using CAST(col AS text) instead is semantically identical on PostgreSQL
and Redshift both supports it for all types.

Update the test/citus/README.md example to reflect the new SQL.
The test-augment-catalog-pg-source-type test was asserting that the
generated Citus backfill SQL contained ::text, which is no longer true
after switching to CAST(col AS text) for Redshift compatibility.

Update the assertion and description to match the new syntax, and add
pgloader.ddl.citus-test to the local Makefile test-unit target so it
matches what CI runs.
@dimitri dimitri merged commit f36a121 into main Jun 27, 2026
37 of 38 checks passed
@dimitri dimitri deleted the fix/redshift-cast-syntax branch June 27, 2026 14:05
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.

Casting errors when using Redshift as a data source

1 participant