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

[Bug] formatting nested select with having #337

Closed bogdan closed 8 months ago

bogdan commented 8 months ago

The following query can not be formatted:

SELECT count(*) FROM (SELECT * FROM employees  GROUP BY company_id HAVING count(company_id) = 1)

Error:

Uncaught exception: Use of uninitialized value in pattern match (m//) at /usr/local/Cellar/pgformatter/5.5/libexec/lib/pgFormatter/Beautify.pm line 968, <STDIN> line 1.
 at /usr/local/bin/pg_format line 19, <STDIN> line 1.
    main::__ANON__("Use of uninitialized value in pattern match (m//) at /usr/loc"...) called at /usr/local/Cellar/pgformatter/5.5/libexec/lib/pgFormatter/Beautify.pm line 968
    pgFormatter::Beautify::beautify(pgFormatter::Beautify=HASH(0x7f9052960a88)) called at /usr/local/Cellar/pgformatter/5.5/libexec/lib/pgFormatter/CLI.pm line 170
    pgFormatter::CLI::beautify(pgFormatter::CLI=HASH(0x7f905380b828)) called at /usr/local/Cellar/pgformatter/5.5/libexec/lib/pgFormatter/CLI.pm line 78
    pgFormatter::CLI::run(pgFormatter::CLI=HASH(0x7f905380b828)) called at /usr/local/bin/pg_format line 49

Reproduce script:

echo "SELECT count(*) FROM (SELECT * FROM employees  GROUP BY company_id HAVING count(company_id) = 1)" | pg_format
darold commented 8 months ago

Please use latest development code, this have been fixed:

$ echo "SELECT count(*) FROM (SELECT * FROM employees  GROUP BY company_id HAVING count(company_id) = 1)" | perl pg_format
SELECT
    count(*)
FROM (
    SELECT
        *
    FROM
        employees
    GROUP BY
        company_id
    HAVING
        count(company_id) = 1)