Skip to content

Commit 0e14fbd

Browse files
committed
fix: recursively process all expressions in COALESCE arguments
The COALESCE expression handler was only processing SubLink (nested SELECT) arguments, but not other expression types like TypeCast nodes. This caused queries with '[]'::jsonb inside COALESCE to fail with "Type jsonb does not exist" errors, since the ::jsonb->::json remapping never occurred. Changed COALESCE handler to recursively process all argument types using remappedExpressions(), matching the pattern used for function calls and boolean expressions. This ensures type casts and other expressions are properly remapped regardless of nesting context. Added test coverage for COALESCE with jsonb type casts.
1 parent a56c8ce commit 0e14fbd

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

‎src/server/query_handler_test.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,16 @@ func TestHandleQuery(t *testing.T) {
258258
"types": {uint32ToString(pgtype.TextOID)},
259259
"values": {"bemidb"},
260260
},
261+
"SELECT COALESCE(NULL, '[]'::jsonb) AS json_value": {
262+
"description": {"json_value"},
263+
"types": {uint32ToString(pgtype.JSONOID)},
264+
"values": {"[]"},
265+
},
266+
"SELECT jsonb_array_length(COALESCE('[]'::jsonb, '{}'::jsonb)) AS length": {
267+
"description": {"length"},
268+
"types": {uint32ToString(pgtype.Int8OID)},
269+
"values": {"0"},
270+
},
261271
"SELECT * FROM pg_catalog.pg_stat_gssapi": {
262272
"description": {"pid", "gss_authenticated", "principal", "encrypted", "credentials_delegated"},
263273
"types": {uint32ToString(pgtype.Int4OID), uint32ToString(pgtype.BoolOID), uint32ToString(pgtype.TextOID), uint32ToString(pgtype.BoolOID), uint32ToString(pgtype.BoolOID)},

‎src/server/query_remapper.go‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,9 @@ func (remapper *QueryRemapper) remappedExpressions(node *pgQuery.Node, remappedC
354354
// COALESCE(value1, value2, ...)
355355
coalesceExpr := node.GetCoalesceExpr()
356356
if coalesceExpr != nil {
357-
for _, arg := range coalesceExpr.Args {
358-
if arg.GetSubLink() != nil {
359-
// Nested SELECT
360-
subSelect := arg.GetSubLink().Subselect.GetSelectStmt()
361-
remapper.remapSelectStatement(subSelect, permissions, indentLevel+1) // recursion
357+
for i, arg := range coalesceExpr.Args {
358+
if arg != nil {
359+
coalesceExpr.Args[i] = remapper.remappedExpressions(arg, remappedColumnRefs, permissions, indentLevel+1) // self-recursion
362360
}
363361
}
364362
}

0 commit comments

Comments
 (0)