libpg_query
libpg_query copied to clipboard
Unknown variable errors parsing plpgsql functions referencing composite type attributes
Hi all,
When testing with pglast, it seems the library is unable to parse PLPgSQL functions returning a composite type attribute https://github.com/lelit/pglast/issues/156
Just to copy/paste over lele's answer from that thread, the following creates a simple type and function using that type
CREATE TYPE public.dz_sumthing
AS(sumattribute INTEGER);
CREATE FUNCTION public.dz_sumfunc(
IN p_in INTEGER
,OUT p_out public.dz_sumthing
)
AS $BODY$
DECLARE
BEGIN
p_out.sumattribute := p_in;
END;
$BODY$
LANGUAGE plpgsql;
SELECT * FROM public.dz_sumfunc(123);
When libpg_query attempts to parse the function, it fails with ParseError: "p_out.sumattribute" is not a known variable:
#include <pg_query.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
PgQueryPlpgsqlParseResult result;
result = pg_query_parse_plpgsql(" \
CREATE FUNCTION public.dz_sumfunc(\
IN p_in INTEGER\
,OUT p_out public.dz_sumthing\
)\
AS $BODY$\
DECLARE\
BEGIN\
p_out.sumattribute := p_in;\
END;\
$BODY$\
LANGUAGE plpgsql;");
if (result.error) {
printf("error: %s at %d\n", result.error->message, result.error->cursorpos);
} else {
printf("%s\n", result.plpgsql_funcs);
}
pg_query_free_plpgsql_parse_result(result);
// Optional, this ensures all memory is freed upon program exit (useful when running Valgrind)
pg_query_exit();
return 0;
}
I was interested if libpg_query supports composite types and/or if the problematic variable check could be bypassed.
Thanks