fix(postgres): cast schema placeholder to text in length() for CockroachDB

Closes #12722
This commit is contained in:
t8y2
2026-10-19 20:51:40 +08:00
parent b81f18e4f5
commit 050757bcaa
+9 -9
View File
@@ -8104,7 +8104,7 @@ const POSTGRES_COLUMNS_SQL: &str = "SELECT a.attname AS column_name, \
LIMIT 1 \
) dep ON TRUE \
LEFT JOIN pg_sequence pseq ON pseq.seqrelid = dep.objid \
WHERE a.attrelid = (CASE WHEN coalesce(length($1), 0) = 0 THEN quote_ident($2) ELSE quote_ident($1) || '.' || quote_ident($2) END)::regclass \
WHERE a.attrelid = (CASE WHEN coalesce(length($1::text), 0) = 0 THEN quote_ident($2) ELSE quote_ident($1) || '.' || quote_ident($2) END)::regclass \
AND a.attnum > 0 AND NOT a.attisdropped \
ORDER BY a.attnum";
@@ -8158,7 +8158,7 @@ const POSTGRES_COLUMNS_COMPAT_SQL: &str = "SELECT a.attname AS column_name, \
ORDER BY sequence_dep.objid \
LIMIT 1 \
) AND serial_seq.relkind = 'S' \
WHERE a.attrelid = (CASE WHEN coalesce(length($1), 0) = 0 THEN quote_ident($2) ELSE quote_ident($1) || '.' || quote_ident($2) END)::regclass \
WHERE a.attrelid = (CASE WHEN coalesce(length($1::text), 0) = 0 THEN quote_ident($2) ELSE quote_ident($1) || '.' || quote_ident($2) END)::regclass \
AND a.attnum > 0 AND NOT a.attisdropped \
ORDER BY a.attnum";
@@ -8186,7 +8186,7 @@ const POSTGRES_COLUMNS_INFORMATION_SCHEMA_SQL: &str = "SELECT c.column_name, \
CAST(c.character_maximum_length AS int) AS character_maximum_length, \
NULL::text AS enum_values \
FROM information_schema.columns c \
WHERE c.table_schema = (CASE WHEN coalesce(length($1), 0) = 0 THEN current_schema() ELSE $1 END) \
WHERE c.table_schema = (CASE WHEN coalesce(length($1::text), 0) = 0 THEN current_schema() ELSE $1 END) \
AND c.table_name = $2 \
ORDER BY c.ordinal_position";
@@ -9697,7 +9697,7 @@ const POSTGRES_INDEXES_SQL: &str = "SELECT i.relname AS index_name, \
LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum AND k.attnum > 0 \
LEFT JOIN pg_opclass oc ON oc.oid = k.class_oid \
LEFT JOIN pg_namespace opcns ON opcns.oid = oc.opcnamespace \
WHERE t.oid = (CASE WHEN coalesce(length($1), 0) = 0 THEN quote_ident($2) ELSE quote_ident($1) || '.' || quote_ident($2) END)::regclass \
WHERE t.oid = (CASE WHEN coalesce(length($1::text), 0) = 0 THEN quote_ident($2) ELSE quote_ident($1) || '.' || quote_ident($2) END)::regclass \
AND k.attnum >= 0 \
GROUP BY i.relname, i.oid, ix.indisunique, ix.indisvalid, ix.indisprimary, ix.indpred, ix.indrelid, am.amname, ix.indnkeyatts, ix.indkey \
ORDER BY i.relname";
@@ -9758,7 +9758,7 @@ const POSTGRES_INDEXES_COMPAT_SQL: &str = "SELECT i.relname AS index_name, \
JOIN pg_class i ON i.oid = ix.indexrelid \
JOIN pg_namespace n ON n.oid = t.relnamespace \
JOIN pg_am am ON am.oid = i.relam \
WHERE t.oid = (CASE WHEN coalesce(length($1), 0) = 0 THEN quote_ident($2) ELSE quote_ident($1) || '.' || quote_ident($2) END)::regclass \
WHERE t.oid = (CASE WHEN coalesce(length($1::text), 0) = 0 THEN quote_ident($2) ELSE quote_ident($1) || '.' || quote_ident($2) END)::regclass \
ORDER BY i.relname";
const POSTGRES_OWNERS_SQL: &str =
@@ -14400,18 +14400,18 @@ mod tests {
#[test]
fn postgres_single_relation_metadata_can_resolve_through_search_path() {
// GaussDB A mode folds `''` to NULL, so `$1 = ''` is never true there and the
// unqualified branch was skipped (issue #12198). `coalesce(length($1), 0) = 0`
// unqualified branch was skipped (issue #12198). `coalesce(length($1::text), 0) = 0`
// matches both NULL and the empty string on every PostgreSQL-compatible kernel.
for sql in [POSTGRES_COLUMNS_SQL, POSTGRES_COLUMNS_COMPAT_SQL] {
assert!(sql.contains("CASE WHEN coalesce(length($1), 0) = 0 THEN quote_ident($2)"), "{sql}");
assert!(sql.contains("CASE WHEN coalesce(length($1::text), 0) = 0 THEN quote_ident($2)"), "{sql}");
assert!(!sql.contains("information_schema.columns"));
}
for sql in [POSTGRES_INDEXES_SQL, POSTGRES_INDEXES_COMPAT_SQL] {
assert!(sql.contains("t.oid = (CASE WHEN coalesce(length($1), 0) = 0 THEN quote_ident($2)"), "{sql}");
assert!(sql.contains("t.oid = (CASE WHEN coalesce(length($1::text), 0) = 0 THEN quote_ident($2)"), "{sql}");
}
assert!(
POSTGRES_COLUMNS_INFORMATION_SCHEMA_SQL
.contains("CASE WHEN coalesce(length($1), 0) = 0 THEN current_schema() ELSE $1 END"),
.contains("CASE WHEN coalesce(length($1::text), 0) = 0 THEN current_schema() ELSE $1 END"),
"{POSTGRES_COLUMNS_INFORMATION_SCHEMA_SQL}"
);
}