sql-formatter-org / sql-formatter

A whitespace formatter for different query languages
https://sql-formatter-org.github.io/sql-formatter/
MIT License
2.23k stars 392 forks source link

Feature Request: Support for Duckdb #730

Open PMassicotte opened 2 months ago

PMassicotte commented 2 months ago

duckdb is getting a lot of attention as a fast in-process analytical database. I found out that duckdb is not currently supported. I am currently using spark option, but it is not working every time.

Are there any plans to add it?

nene commented 2 months ago

Thanks for the feature request.

I'm open for a pull request, but I'm not really interested in implementing it by myself. Luckily for you, adding a support for a new dialect to sql-formatter is not that hard. You don't even need to fork sql-formatter to do so, you can just provide it a custom dialect definition.

Well, admittedly there's not that much of documentation there, but just copying an existing dialect implementation from sql-formatter source code and modifying it slightly should be pretty simple.

You can also look at the wiki, which documents syntax details of all the currently supported SQL dialects.

PMassicotte commented 2 months ago

Great, I will have a look at it!

PMassicotte commented 2 months ago

Just for reference:

select * from duckdb_keywords();
select * from duckdb_functions();
hughcameron commented 2 months ago

I've set up a PR for this.

It's based on the duckdb words:

COPY (
    SELECT DISTINCT upper(function_name) AS function_name
    FROM duckdb_functions()
    WHERE function_name SIMILAR TO '^[a-z].*'
    ORDER BY function_name
) TO 'duckdb_functions.txt' WITH (sep ',', header FALSE);

COPY (
    SELECT upper(keyword_name)
    FROM duckdb_keywords()
    ORDER BY keyword_name
) TO 'duckdb_keywords.txt' WITH (sep ',', header FALSE);

COPY (
    SELECT DISTINCT upper(logical_type)
    FROM duckdb_types()
    ORDER BY logical_type
) TO 'duckdb_types.txt' WITH (sep ',', header FALSE);

I've also leveraged the postgres tests, most are passing. I'm not clear on how to resolve the failing tests - it would be great to get some help.