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

Table Options formatted like Field option #22

Closed
dang1507 opened this issue Feb 4, 2024 · 5 comments
Closed

Table Options formatted like Field option #22

dang1507 opened this issue Feb 4, 2024 · 5 comments

Comments

@dang1507
Copy link

dang1507 commented Feb 4, 2024

I found an oddity in the formatting when using BIGQUERY syntax. When creating tables with metadata, the PARTITION BY and CLUSTER BY come before the OPTIONS(<TABLE OPTIONS>). The formatter does not add a new line as it should because this syntax mirrors field options.

Actual

CREATE OR REPLACE TABLE `project.DATASET.MYNEWTABLE` (
    MYNUMBER INT64 NOT NULL DEFAULT 0 OPTIONS(DESCRIPTION = "my favorite number"),
    MYSTRING STRING NOT NULL DEFAULT "hello world" OPTIONS(DESCRIPTION = "my favorite string"),
    MYDOUBLE DOUBLE (10, 2) NOT NULL DEFAULT 0.0 OPTIONS(DESCRIPTION = "my favorite double with precision"),
    MYFLOAT FLOAT64 NOT NULL DEFAULT 0.0 OPTIONS(DESCRIPTION = "my favorite float"),
    MYBIGINT BIGINT OPTIONS(DESCRIPTION = "my favorite big number"),
    MYDATE DATE OPTIONS(DESCRIPTION = "my favorite date"),
    PRIMARY KEY (MYNUMBER, MYDATE) NOT ENFORCED,
    FOREIGN KEY (MYDOUBLE) REFERENCES `project.DATASET.MYDOUBLETABLE` (MYDOUBLE) NOT ENFORCED,
    FOREIGN KEY (MYFLOAT) REFERENCES `project.DATASET.ROOT` (BEERFLOAT) NOT ENFORCED
)
PARTITION BY
    MYNUMBER RANGE_BUCKET(0, 100000)
CLUSTER BY
    MYSTRING,
    MYNUMBER OPTIONS(  -- I am not a field option. I am a table option and should be on a new line
        DESCRIPTION = "test format fun",
        LABELS = [("format", "test")],
        REQUIRE_PARTITION = TRUE,
        EXPIRATION_TIMESTAMP = CURRENT_TIMESTAMP + INTERVAL 1 DAY
    ) AS

SELECT
    *
FROM
    `project.DATASET.FAKE_TABLE`
;

Corrected

CREATE OR REPLACE TABLE `project.DATASET.MYNEWTABLE` (
    MYNUMBER INT64 NOT NULL DEFAULT 0 OPTIONS(DESCRIPTION = "my favorite number"),
    MYSTRING STRING NOT NULL DEFAULT "hello world" OPTIONS(DESCRIPTION = "my favorite string"),
    MYDOUBLE DOUBLE (10, 2) NOT NULL DEFAULT 0.0 OPTIONS(DESCRIPTION = "my favorite double with precision"),
    MYFLOAT FLOAT64 NOT NULL DEFAULT 0.0 OPTIONS(DESCRIPTION = "my favorite float"),
    MYBIGINT BIGINT OPTIONS(DESCRIPTION = "my favorite big number"),
    MYDATE DATE OPTIONS(DESCRIPTION = "my favorite date"),
    PRIMARY KEY (MYNUMBER, MYDATE) NOT ENFORCED,
    FOREIGN KEY (MYDOUBLE) REFERENCES `project.DATASET.MYDOUBLETABLE` (MYDOUBLE),
    FOREIGN KEY (MYFLOAT) REFERENCES `project.DATASET.ROOT` (BEERFLOAT)
)
PARTITION BY
    MYNUMBER RANGE_BUCKET(0, 100000)
CLUSTER BY
    MYSTRING,
    MYNUMBER 
OPTIONS( -- I belong here 
        DESCRIPTION = "test format fun",
        LABELS = [("format", "test")],
        REQUIRE_PARTITION = TRUE,
        EXPIRATION_TIMESTAMP = CURRENT_TIMESTAMP + INTERVAL 1 DAY
) AS

SELECT
    *
FROM `project.DATASET.FAKE_TABLE`
;
@nene
Copy link
Owner

nene commented Feb 4, 2024

It seems that you are not using prettier-plugin-sql-cst.

Because when I attempt to format this code I get a parse error. Mainly because of the PRIMARY KEY and FOREIGN KEY syntax, which was been added to BigQuery after I finished implementing support of it.

I suspect you're instead using either sql-formatter or prettier-plugin-sql (which uses sql-formatter under the hood).

I can only say that fixing the OPTIONS() formatting will likely not happen in sql-formatter because that formatter does not understand much about the context. So it has to either always format OPTIONS() with newline before it or always without the newline.

@nene
Copy link
Owner

nene commented Feb 4, 2024

Anyway, I'm closing this issue as it doesn't directly apply to prettier-plugin-sql-cst.

Thanks for reminding me about new syntax in BigQuery. I've opened a separate issue to fix that: nene/sql-parser-cst#69

As you are using BigQuery, I do encourage you to give a try to this plugin though as BigQuery is one of the dialects that has the best support. For example it does format the above OPTIONS() correctly. Though for now it does crash for various other parts of the BigQuery syntax that's not yet supported.

you can try it out in the playground

@nene nene closed this as not planned Won't fix, can't repro, duplicate, stale Feb 4, 2024
@nene
Copy link
Owner

nene commented Feb 5, 2024

BTW, @dang1507 is the DOUBLE data type actually supported by BigQuery? I don't see it mentioned in the documentation. I think you intended to use DECIMAL as DOUBLE (10, 2) doesn't really make sense to me.

Also, the syntax you used for PARTITION BY:

PARTITION BY
    MYNUMBER RANGE_BUCKET(0, 100000)

Is this actually supported by BigQuery? It doesn't look like valid syntax to me.

@dang1507
Copy link
Author

dang1507 commented Feb 5, 2024 via email

@nene
Copy link
Owner

nene commented Feb 5, 2024

Thanks.

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