Skip to content

Add support for DuckDB underscore numeric literals #891

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/languages/duckdb/duckdb.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ export const duckdb: DialectOptions = {
reservedFunctionNames: functions,
nestedBlockComments: true,
extraParens: ['[]', '{}'],
// Support underscore separators in numeric literals (e.g., 1_000_000)
numberRegex:
/(?:0x[0-9a-fA-F_]+|0b[01_]+|(?:-\s*)?(?:[0-9_]*\.[0-9_]+|[0-9_]+(?:\.[0-9_]*)?)(?:[eE][-+]?[0-9_]+(?:\.[0-9_]+)?)?)(?![\w\p{Alphabetic}])/uy,
stringTypes: [
'$$',
"''-qq",
Expand Down
1 change: 1 addition & 0 deletions src/lexer/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default class Tokenizer {
{
type: TokenType.NUMBER,
regex:
cfg.numberRegex ??
/(?:0x[0-9a-fA-F]+|0b[01]+|(?:-\s*)?(?:[0-9]*\.[0-9]+|[0-9]+(?:\.[0-9]*)?)(?:[eE][-+]?[0-9]+(?:\.[0-9]+)?)?)(?![\w\p{Alphabetic}])/uy,
},
// RESERVED_PHRASE is matched before all other keyword tokens
Expand Down
2 changes: 2 additions & 0 deletions src/lexer/TokenizerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export interface TokenizerOptions {
propertyAccessOperators?: string[];
// Enables PostgreSQL-specific OPERATOR(...) syntax
operatorKeyword?: boolean;
// Custom regex pattern for number tokens (defaults to standard SQL number pattern)
numberRegex?: RegExp;
// Allows custom modifications on the token array.
// Called after the whole input string has been split into tokens.
// The result of this will be the output of the tokenizer.
Expand Down
11 changes: 11 additions & 0 deletions test/duckdb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,15 @@ describe('DuckDBFormatter', () => {
1 IS NOT NULL;
`);
});

it('supports underscore separators in numeric literals', () => {
expect(format('SELECT 1_000_000, 3.14_159, 0x1A_2B_3C, 0b1010_0001, 1.5e+1_0;')).toBe(dedent`
SELECT
1_000_000,
3.14_159,
0x1A_2B_3C,
0b1010_0001,
1.5e+1_0;
`);
});
});