Skip to content

Commit 9f14225

Browse files
committed
bugfix: support records containing non-DoltgresType values
1 parent 79152d5 commit 9f14225

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

server/plpgsql/interpreter_logic.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,9 @@ func convertRowsToRecords(schema sql.Schema, rows []sql.Row) [][]pgtypes.RecordV
300300
t := schema[i].Type
301301
doltgresType, ok := t.(*pgtypes.DoltgresType)
302302
if !ok {
303-
panic("expected Doltgres type")
303+
// non-Doltgres types are still used in analysis, but we only support disk serialization
304+
// for Doltgres types, so we must convert the GMS type to the nearest Doltgres type here.
305+
doltgresType = pgtypes.FromGmsType(t)
304306
}
305307

306308
record[i] = pgtypes.RecordValue{

testing/go/create_function_plpgsql_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,56 @@ $$ LANGUAGE plpgsql;`},
654654
},
655655
},
656656
},
657+
{
658+
Name: "RETURNS TABLE with join query",
659+
SetUpScript: []string{
660+
`CREATE TABLE customers (
661+
id INT PRIMARY KEY,
662+
name TEXT
663+
);`,
664+
`CREATE TABLE orders (
665+
id SERIAL PRIMARY KEY,
666+
customer_id INT,
667+
amount INT
668+
);`,
669+
`INSERT INTO customers VALUES (1, 'John'), (2, 'Jane');`,
670+
`INSERT INTO orders VALUES (1, 1, 100), (2, 2, 10);`,
671+
`CREATE OR REPLACE FUNCTION func2(n INT) RETURNS TABLE (c_id INT, c_name TEXT, c_total_spent INT)
672+
LANGUAGE plpgsql
673+
AS $$
674+
BEGIN
675+
RETURN QUERY
676+
SELECT c.id,
677+
c.name,
678+
SUM(o.amount) AS total_spent
679+
FROM customers c
680+
JOIN orders o ON o.customer_id = c.id
681+
GROUP BY c.id, c.name
682+
HAVING SUM(o.amount) >= n
683+
;
684+
END;
685+
$$;`,
686+
},
687+
Assertions: []ScriptTestAssertion{
688+
{
689+
Query: "SELECT func2(1);",
690+
Expected: []sql.Row{
691+
{"(1,John,100)"},
692+
{"(2,Jane,10)"},
693+
},
694+
},
695+
{
696+
Query: "SELECT func2(11);",
697+
Expected: []sql.Row{
698+
{"(1,John,100)"},
699+
},
700+
},
701+
{
702+
Query: "SELECT func2(111);",
703+
Expected: []sql.Row{},
704+
},
705+
},
706+
},
657707
{
658708
Name: "RETURNS SETOF with composite param",
659709
SetUpScript: []string{

0 commit comments

Comments
 (0)