From 9c28c2a8992434f670b618a1437c590469dd477e Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Sat, 27 Jun 2026 14:10:39 +0200 Subject: [PATCH 1/3] fix(pgsql-source): use CAST(col AS text) instead of col::text for Redshift (#1549) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- clojure/src/pgloader/ddl/citus.clj | 4 +++- clojure/src/pgloader/source/pgsql.clj | 2 +- src/sources/pgsql/pgsql.lisp | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/clojure/src/pgloader/ddl/citus.clj b/clojure/src/pgloader/ddl/citus.clj index b5ee70697..5192dbe3b 100644 --- a/clojure/src/pgloader/ddl/citus.clj +++ b/clojure/src/pgloader/ddl/citus.clj @@ -76,7 +76,9 @@ own-cols (mapv :column-name (:columns table-entry)) cast-text? (= :pgsql source-type) fmt-col (fn [tbl col] - (str tbl "." col (when cast-text? "::text"))) + (if cast-text? + (str "CAST(" tbl "." col " AS text)") + (str tbl "." col))) col-list (str/join ", " (concat [(fmt-col from-table-name dist-col-name)] (map #(fmt-col tname %) own-cols)))] diff --git a/clojure/src/pgloader/source/pgsql.clj b/clojure/src/pgloader/source/pgsql.clj index 616395464..6e94fb7ea 100644 --- a/clojure/src/pgloader/source/pgsql.clj +++ b/clojure/src/pgloader/source/pgsql.clj @@ -201,7 +201,7 @@ source-schema (or (:source-schema table-spec-entry) schema) base-sql (or citus-read-sql (let [col-names (mapv :column-name columns) - col-list (str/join ", " (map #(str "\"" % "\"") col-names))] + col-list (str/join ", " (map #(str "CAST(\"" % "\" AS text)") col-names))] (str "SELECT " col-list " FROM \"" source-schema "\".\"" table-name "\""))) sql (if (and ctid-lo ctid-hi) (str base-sql " " (first (ctid-where-sqlvec {:lo (str "'(" ctid-lo ",0)'::tid") diff --git a/src/sources/pgsql/pgsql.lisp b/src/sources/pgsql/pgsql.lisp index 684c2070d..2a84c51bf 100644 --- a/src/sources/pgsql/pgsql.lisp +++ b/src/sources/pgsql/pgsql.lisp @@ -40,7 +40,7 @@ (let* ((cols (mapcar #'column-name (fields pgsql))) (sql (format nil - "SELECT ~{~s::text~^, ~} FROM ~s.~s" + "SELECT ~{CAST(~s AS text)~^, ~} FROM ~s.~s" cols (schema-source-name (table-schema (source pgsql))) (table-source-name (source pgsql))))) From 8518d374f976eb176f82c1b6d8153ca328d1ef27 Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Sat, 27 Jun 2026 14:57:04 +0200 Subject: [PATCH 2/3] fix(citus): use CAST(col AS text) in backfill SELECT for Redshift source 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. --- src/utils/citus.lisp | 2 +- test/citus/README.md | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/utils/citus.lisp b/src/utils/citus.lisp index b846cdb03..017d4d988 100644 --- a/src/utils/citus.lisp +++ b/src/utils/citus.lisp @@ -368,7 +368,7 @@ (format-citus-join-clause source-table (table-citus-rule target-table)))) (format nil - "SELECT ~{~a::text~^, ~} FROM ~s.~s ~a" + "SELECT ~{CAST(~a AS text)~^, ~} FROM ~s.~s ~a" cols (schema-source-name (table-schema source-table)) (table-source-name source-table) diff --git a/test/citus/README.md b/test/citus/README.md index 499ecd66f..47c621983 100644 --- a/test/citus/README.md +++ b/test/citus/README.md @@ -28,15 +28,15 @@ model, and finally the data is backfilled automatically in the target table thanks to generating queries like the following: ~~~ -SELECT "campaigns".company_id::text, - "impressions".id::text, - "impressions".ad_id::text, - "impressions".seen_at::text, - "impressions".site_url::text, - "impressions".cost_per_impression_usd::text, - "impressions".user_ip::text, - "impressions".user_data::text - FROM "public"."impressions" +SELECT CAST(campaigns.company_id AS text), + CAST(impressions.id AS text), + CAST(impressions.ad_id AS text), + CAST(impressions.seen_at AS text), + CAST(impressions.site_url AS text), + CAST(impressions.cost_per_impression_usd AS text), + CAST(impressions.user_ip AS text), + CAST(impressions.user_data AS text) + FROM "public"."impressions" JOIN "public"."ads" ON impressions.ad_id = ads.id JOIN "public"."campaigns" ON ads.campaign_id = campaigns.id ~~~ From 4fb02247bf12156f60b9cbe93cc45ecf40e1f81b Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Sat, 27 Jun 2026 15:17:16 +0200 Subject: [PATCH 3/3] fix(test): update citus-test assertion from ::text to CAST(... AS text) 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. --- clojure/Makefile | 1 + clojure/test/pgloader/ddl/citus_test.clj | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/clojure/Makefile b/clojure/Makefile index 2bc77252b..9c917c587 100644 --- a/clojure/Makefile +++ b/clojure/Makefile @@ -49,6 +49,7 @@ test-unit: -n pgloader.cast-test \ -n pgloader.batch-test \ -n pgloader.ddl-test \ + -n pgloader.ddl.citus-test \ -n pgloader.load-file.parser-test \ -n pgloader.transforms-test \ -n pgloader.pg-service-test \ diff --git a/clojure/test/pgloader/ddl/citus_test.clj b/clojure/test/pgloader/ddl/citus_test.clj index dc8671669..5220c9035 100644 --- a/clojure/test/pgloader/ddl/citus_test.clj +++ b/clojure/test/pgloader/ddl/citus_test.clj @@ -88,14 +88,16 @@ (citus/augment-catalog broken-catalog rules)))))) (deftest test-augment-catalog-pg-source-type - (testing "PG source type adds ::text casts to the read SQL" + (testing "PG source type wraps columns in CAST(... AS text) in the read SQL" (let [rules [{:type :distributed-from :table "splits" :using "biller_id" :from ["receivable_accounts"]}] aug (citus/augment-catalog sample-catalog rules :source-type :pgsql) entry (first (filter #(= "splits" (:table-name %)) aug)) sql (:citus-read-sql entry)] (is (string? sql)) - (is (re-find #"::text" sql))))) + (is (re-find #"CAST\(" sql)) + (is (re-find #"AS text\)" sql)) + (is (not (re-find #"::text" sql)))))) (deftest test-augment-catalog-from-chain (testing "Two-hop FROM chain traverses correctly"