darold / pgFormatter

A PostgreSQL SQL syntax beautifier that can work as a console program or as a CGI. On-line demo site at http://sqlformat.darold.net/
PostgreSQL License
1.69k stars 101 forks source link

Use of uninitialized value in pattern match in Beautify.pm line 968 #307

Closed Profpatsch closed 1 year ago

Profpatsch commented 1 year ago

When trying to format the following query:

SELECT id, usergroups, entryval FROM foo.collections c WHERE (NULL::text IS NULL OR c.id = NULL::text) AND c.collection = 'foo'::text AND (FALSE::bool OR c.usergroups::text[] && ARRAY['620a6d408474d60001000000', 'public']::text[]) AND ( SELECT coalesce(bool_and(l.value = r.value), TRUE) FROM jsonb_each(c.entryval) l JOIN jsonb_each('{"organization":"620a6d408474d60001000000"}'::jsonb) r ON l.key = r.key)

with pg_format 5.5., I get:

Uncaught exception: Use of uninitialized value in pattern match (m//) at /nix/store/dlsxkvrlvq7y98l9476df39crz2i5cn6-perl5.36.0-pgformatter-5.5/bin/lib/pgFormatter/Beautify.pm line 968, <STDIN> line 1.
 at /nix/store/dlsxkvrlvq7y98l9476df39crz2i5cn6-perl5.36.0-pgformatter-5.5/bin/pg_format line 19, <STDIN> line 1.
    main::__ANON__("Use of uninitialized value in pattern match (m//) at /nix/sto"...) called at /nix/store/dlsxkvrlvq7y98l9476df39crz2i5cn6-perl5.36.0-pgformatter-5.5/bin/lib/pgFormatter/Beautify.pm line 968
    pgFormatter::Beautify::beautify(pgFormatter::Beautify=HASH(0x223a5c8)) called at /nix/store/dlsxkvrlvq7y98l9476df39crz2i5cn6-perl5.36.0-pgformatter-5.5/bin/lib/pgFormatter/CLI.pm line 170
    pgFormatter::CLI::beautify(pgFormatter::CLI=HASH(0x1940da0)) called at /nix/store/dlsxkvrlvq7y98l9476df39crz2i5cn6-perl5.36.0-pgformatter-5.5/bin/lib/pgFormatter/CLI.pm line 78
    pgFormatter::CLI::run(pgFormatter::CLI=HASH(0x1940da0)) called at /nix/store/dlsxkvrlvq7y98l9476df39crz2i5cn6-perl5.36.0-pgformatter-5.5/bin/pg_format line 49

vs pg_format 5.3:

SELECT
    id,
    usergroups,
    entryval
FROM
    foo.collections c
WHERE (NULL::text IS NULL
    OR c.id = NULL::text)
AND c.collection = 'foo'::text
AND (FALSE::bool
    OR c.usergroups::text[] && ARRAY['620a6d408474d60001000000', 'public']::text[])
AND (
    SELECT
        coalesce(bool_and(l.value = r.value), TRUE)
    FROM
        jsonb_each(c.entryval) l
        JOIN jsonb_each('{"organization":"620a6d408474d60001000000"}'::jsonb) r ON l.key = r.key)
gilles-migops commented 1 year ago

Commit c2622c4 fixes this issue.

Profpatsch commented 1 year ago

That works for me, thanks!

Profpatsch commented 1 year ago

cc @marsam maybe we want to include this into the current nixpkgs release.

sam-gronblom-rj commented 1 year ago

Is this fix already released? I think I am running into this problem at the moment.

darold commented 1 year ago

Not release yet, you must install the development code to fix this issue for the moment.