-
Notifications
You must be signed in to change notification settings - Fork 410
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
Add DuckDB Dialect Support #738
Open
hughcameron
wants to merge
24
commits into
sql-formatter-org:master
Choose a base branch
from
hughcameron:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
84552f1
Setup DuckDB dialect
hughcameron 9001e8c
Generate DuckDB words
hughcameron fc44804
Setup tests
hughcameron c51ccab
Add duckdb as dialect
hughcameron 97608df
Test updates
hughcameron 56059a3
Drop generation files
hughcameron 879eb5f
Add duckdb to docs
hughcameron 42abaf5
Reduce keyword list to reserved only.
hughcameron 57f272a
Extend Data Types List
hughcameron 5da1023
Drop INT from functions
hughcameron 826ee55
Adding clauses by @PMassicotte
hughcameron 7f30814
Restate reservedSetOperations
hughcameron eb7d9b1
Function Updates - Tests Success
hughcameron 925dd49
Run Yarn Pretty
hughcameron 08182dc
Small fixes
hughcameron 348eeac
pre-commit-hooks new line
hughcameron 159911f
Share source query on keywords & functions
hughcameron 0c6a830
Amend Set Operations
hughcameron 179be69
Drop square and cube root
hughcameron 8fb9a4f
Drop POSIX RegExp - not compatible
hughcameron 9a56b6d
Address Postrges DuckDB Differences
hughcameron b295cbd
Move Qualify to Reserved Clause
hughcameron 1aec041
Insert Supports By Position, Name
hughcameron 5f787f3
Attach/Detach
hughcameron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
import { DialectOptions } from '../../dialect.js'; | ||
import { expandPhrases } from '../../expandPhrases.js'; | ||
import { functions } from './duckdb.functions.js'; | ||
import { dataTypes, keywords } from './duckdb.keywords.js'; | ||
|
||
const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT] [EXCLUDE | REPLACE]']); | ||
|
||
const reservedClauses = expandPhrases([ | ||
// queries | ||
'WITH [RECURSIVE]', | ||
'FROM', | ||
'WHERE', | ||
'GROUP BY [ALL | DISTINCT]', | ||
'HAVING', | ||
'QUALIFY', | ||
'WINDOW', | ||
'PARTITION BY', | ||
'ORDER BY', | ||
'LIMIT', | ||
'OFFSET', | ||
// Data manipulation | ||
// - insert: | ||
'INSERT INTO', | ||
'VALUES', | ||
'DEFAULT VALUES', | ||
'BY [POSITION | NAME]', | ||
// - update: | ||
'SET', | ||
// other | ||
'RETURNING', | ||
]); | ||
|
||
const standardOnelineClauses = expandPhrases([ | ||
'CREATE [OR REPLACE] [TEMP | TEMPORARY] TABLE', | ||
]); | ||
|
||
const tabularOnelineClauses = expandPhrases([ | ||
// - create | ||
'CREATE [OR REPLACE] [TEMP | TEMPORARY] VIEW', | ||
// - update: | ||
'UPDATE', | ||
// - insert: | ||
'ON CONFLICT', | ||
// - delete: | ||
'DELETE FROM', | ||
// - drop table: | ||
'DROP TABLE [IF EXISTS]', | ||
// - alter table: | ||
'ALTER TABLE [IF EXISTS]', | ||
'ALTER TABLE ALL IN TABLESPACE', | ||
'RENAME [COLUMN]', | ||
'RENAME TO', | ||
'ADD [COLUMN] [IF NOT EXISTS]', | ||
'DROP [COLUMN] [IF EXISTS]', | ||
'DROP [PERSISTENT] SECRET', | ||
'DROP [SEQUENCE | MACRO | FUNCTION | INDEX | TYPE | SCHEMA] [IF EXISTS]', | ||
'DROP [VIEW] [IF EXISTS]', | ||
'ALTER [COLUMN]', | ||
'SET DATA TYPE', // for alter column | ||
'{SET | DROP} DEFAULT', // for alter column | ||
'{SET | DROP} NOT NULL', // for alter column | ||
// - truncate: | ||
'TRUNCATE [TABLE]', | ||
// other | ||
'ABORT', | ||
'ALTER VIEW', | ||
'ANALYZE', | ||
'BEGIN', | ||
'CALL', | ||
'CHECKPOINT', | ||
'COMMIT', | ||
'COPY', | ||
'CREATE FUNCTION', | ||
'CREATE INDEX', | ||
'CREATE MACRO', | ||
'CREATE SCHEMA', | ||
'CREATE SECRET', | ||
'CREATE SEQUENCE', | ||
'CREATE TYPE', | ||
'DEALLOCATE', | ||
'DECLARE', | ||
'EXECUTE', | ||
'EXPLAIN', | ||
'INSTALL', | ||
'LOAD', | ||
'PREPARE', | ||
'INSERT INTO', | ||
'SHOW', | ||
'BEGIN TRANSACTION', | ||
'UNLISTEN', | ||
'VACUUM [ANALYZE | FULL]', | ||
]); | ||
|
||
const reservedSetOperations = expandPhrases([ | ||
'UNION [ALL] BY NAME', | ||
'EXCEPT [ALL]', | ||
'INTERSECT [ALL]', | ||
]); | ||
|
||
const reservedJoins = expandPhrases([ | ||
hughcameron marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'ASOF {LEFT} JOIN', | ||
'JOIN', | ||
'NATURAL [INNER] JOIN', | ||
'NATURAL [LEFT] {ANTI | SEMI} JOIN', | ||
'NATURAL {LEFT | RIGHT | FULL} [OUTER] JOIN', | ||
'POSITIONAL JOIN', | ||
'[LEFT] {ANTI | SEMI} JOIN', | ||
'{INNER | CROSS} JOIN', | ||
'{LEFT | RIGHT | FULL} [OUTER] JOIN', | ||
]); | ||
|
||
const reservedPhrases = expandPhrases([ | ||
'PRIMARY KEY', | ||
'GENERATED ALWAYS', | ||
'ON {UPDATE | DELETE} [SET NULL | SET DEFAULT]', | ||
'{ROWS | RANGE | GROUPS} BETWEEN', | ||
'[TIMESTAMP | TIME] {WITH | WITHOUT} TIME ZONE', | ||
// comparison operator | ||
'IS [NOT] DISTINCT FROM', | ||
]); | ||
|
||
export const duckdb: DialectOptions = { | ||
name: 'duckdb', | ||
tokenizerOptions: { | ||
reservedSelect, | ||
reservedClauses: [...reservedClauses, ...standardOnelineClauses, ...tabularOnelineClauses], | ||
reservedSetOperations, | ||
reservedJoins, | ||
reservedPhrases, | ||
reservedKeywords: keywords, | ||
reservedDataTypes: dataTypes, | ||
reservedFunctionNames: functions, | ||
nestedBlockComments: true, | ||
extraParens: ['[]'], | ||
stringTypes: [ | ||
'$$', | ||
{ quote: "''-qq", prefixes: ['U&'] }, | ||
{ quote: "''-qq-bs", prefixes: ['E'], requirePrefix: true }, | ||
{ quote: "''-raw", prefixes: ['B', 'X'], requirePrefix: true }, | ||
], | ||
identTypes: [{ quote: '""-qq', prefixes: ['U&'] }], | ||
identChars: { rest: '$' }, | ||
paramTypes: { numbered: ['$'] }, | ||
operators: [ | ||
// Arithmetic | ||
'%', | ||
'^', | ||
'@', | ||
// Assignment | ||
':=', | ||
// Bitwise | ||
'&', | ||
'|', | ||
'~', | ||
'<<', | ||
'>>', | ||
// JSON | ||
'->', | ||
'->>', | ||
// Pattern matching | ||
'~~', | ||
'~~*', | ||
'!~~', | ||
'!~~*', | ||
'~', | ||
'!~', | ||
// String concatenation | ||
'||', | ||
// Text search | ||
'^@', | ||
], | ||
}, | ||
formatOptions: { | ||
alwaysDenseOperators: ['::', ':'], | ||
onelineClauses: [...standardOnelineClauses, ...tabularOnelineClauses], | ||
tabularOnelineClauses, | ||
}, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like
INSTALL
is the only name added to this list compared to PostgreSQL. I would suspect there are more differences by the statements supported by PostgreSQL and DuckDB.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @nene - you're correct. DuckDB uses the PostgreSQL parser. That's why DuckDB’s SQL dialect closely follows the conventions of the PostgreSQL dialect with only a few exceptions as listed here.
Many features are unsupported so I've removed unsupported elements in the formatter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @nene - let me know if it's OK to resolve this conversation.
I've also added some specific DuckDB features below.