Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown variable errors with plpgsql composite types #156

Closed
pauldzy opened this issue Sep 17, 2024 · 4 comments
Closed

Unknown variable errors with plpgsql composite types #156

pauldzy opened this issue Sep 17, 2024 · 4 comments

Comments

@pauldzy
Copy link

pauldzy commented Sep 17, 2024

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

@lelit
Copy link
Owner

lelit commented Sep 17, 2024 via email

@lelit
Copy link
Owner

lelit commented Sep 21, 2024

Hi! Re-reading your report I see that it is clearly unrelated to #154, and indeed the following C program

#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;
}

fails even with svenklemm's branch.

So I'm afraid there's little I can do on the pglast side. Maybe you could post the question on the libpg_query issues?

@pauldzy
Copy link
Author

pauldzy commented Sep 22, 2024

Thanks for your help, I passed the problem over to libpg_query as pganalyze/libpg_query#260
Looking at the history of such issues its might be useful to tag these errors more clearly as coming from libpg_query.
Cheers!

lelit added a commit that referenced this issue Oct 29, 2024
Latest commits (in particular pganalyze/libpg_query@9a6fb9e)
there fix issue #156, but they didn't release an official version yet.
lelit added a commit that referenced this issue Oct 29, 2024
@lelit
Copy link
Owner

lelit commented Oct 29, 2024

This is fixed in just released v6.8.

@lelit lelit closed this as completed Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants