pglast icon indicating copy to clipboard operation
pglast copied to clipboard

Unknown variable errors with plpgsql composite types

Open pauldzy opened this issue 5 months ago • 3 comments

Hi Lele, great project,

When parsing a plpgsql function with a composite type, the parse fails with an unknown variable error.

Example:

  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);

In this case I'd like to parse the function code:

from pglast import parse_plpgsql

str_sql = """
    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;
""";

root = parse_plpgsql(str_sql);

This returns

ParseError: "p_out.sumattribute" is not a known variable

I assume the code does not currently support cross-referencing the attributes of the type when variables are checked? Is there a way to avoid the check and just accept that p_out.sumattribute is valid?

Thanks! Paul

pauldzy avatar Sep 17 '24 00:09 pauldzy