sqlparse icon indicating copy to clipboard operation
sqlparse copied to clipboard

Using `sqlformat --reindent` produces erroneous output.

Open TomEubank opened this issue 7 years ago • 3 comments

Given file example.sql, containing the following SQL statements:

create table if not exists "tSession" (
    "id" integer primary key  autoincrement  not null  unique,
    "start_time" integer unique,
    "end_time" integer,
    "date" text
    );

create table if not exists "tProcess" (
    "id" integer primary key  autoincrement  not null  unique,
    "name" text,
    "start_time" integer,
    "end_time" integer,
    "file_id" integer,
    "file_start_line" integer,
    "file_end_line" integer,
    "line_cnt" integer
    );

Omitting the --reindent option from the sqlformat command, generates output which is identical to the input.

However, using the --reindent option produces output that is inconsistently and counter-intuitively formatted. Identical results are produced when the --indent_width and/or --wrap_after options are provided. I.e., all of the following commands generate identical output.

    $ sqlformat --reindent example.sql
    $ sqlformat -r --indent_width=4 example.sql
    $ sqlformat --reindent --wrap_after 79 example.sql
    $ sqlformat -r --indent_width 4 --wrap_after=79 example.sql

In the example.out file (piped from the above commands), the 2nd and 3rd lines of the first SQL statement are indented 89 and 110 spaces respectively.

    create table if not exists "tSession" ( "id" integer primary key autoincrement not null unique,
    (89 spaces) "start_time" integer unique,
    (110 spaces) "end_time" integer, "date" text );

In the output generated for the second SQL statement, the 2nd line is indented 89 spaces, whereafter no further wrapping or indenting occurs, and the 3rd line extends to column 236.

    create table if not exists "tProcess" ( "id" integer primary key autoincrement not null unique,
    (89 spaces) "name" text, "start_time" integer, "end_time" integer, "file_id" integer, "session_id" integer, "file_start_line" integer, "file_end_line" integer, "session_start_line" integer, "session_end_line" integer, "line_cnt" integer );

Details

sqlformat version 0.2.4, installed 2017-10-04 19:54, using:

    pip3 install --user sqlparse

python3 version 3.5.3 (default, Sep 7 2017, 16:23:57) [GCC 6.3.0 20170406]

proc/version_signature: Ubuntu 4.10.0-35.39-generic 4.10.17

proc/version: Linux version 4.10.0-35-generic (buildd@lcy01-16) (gcc version 6.3.0 20170406 (Ubuntu 6.3.0-12ubuntu2) ) #39-Ubuntu SMP Wed Sep 13 07:46:59 UTC 2017

Attached

  • example.sql.txt : Input file describe above
  • example.out.txt : output from `sqlformat -r example.sql
  • example_compare.sh.txt : script used to generate and compare output from sqlformat.
  • example_imports.txt: a list of the non-sqlparse modules being used by python. (This may be overkill, but it shows that no out-of-version modules are being imported.)

example.out.txt example.sql.txt example_compare.sh.txt example_imports.txt

TomEubank avatar Oct 05 '17 04:10 TomEubank

Any Progress on this? This is the default sql formatter for https://github.com/sbdchd/neoformat and I think some VIM users would be happy, if the output for CREATE TABLE statements would be in a better shape :)

tmerse avatar Apr 09 '18 10:04 tmerse

Is there any possible workaround?

willchao612 avatar Aug 30 '21 03:08 willchao612

Found a workaround here as I commented in another issue.

willchao612 avatar Aug 30 '21 09:08 willchao612