Skip to content

Commit 472e17b

Browse files
authored
Add support for decimal values without leading digits (#794)
Encountered issue while handling decimal values without leading digits (for example .00457). Lexer was breaking, updated number token regex to support the similar cases and added a corresponding test case. Sample SQL query: `SELECT employee_id FROM employees WHERE salary > .456 * 1000000 AND bonus < .0000239 * salary;` ``` An Unexpected Error Occurred Parse error at token: 456 at line 1 column 51 Unexpected NUMBER token: {"type":"NUMBER","raw":"456","text":"456","start":50} ```
2 parents 2ad4028 + a8435b5 commit 472e17b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/lexer/Tokenizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default class Tokenizer {
5151
{
5252
type: TokenType.NUMBER,
5353
regex:
54-
/(?:0x[0-9a-fA-F]+|0b[01]+|(?:-\s*)?[0-9]+(?:\.[0-9]*)?(?:[eE][-+]?[0-9]+(?:\.[0-9]+)?)?)(?![\w\p{Alphabetic}])/uy,
54+
/(?: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,
5555
},
5656
// RESERVED_PHRASE is matched before all other keyword tokens
5757
// to e.g. prioritize matching "TIMESTAMP WITH TIME ZONE" phrase over "WITH" clause.

test/behavesLikeSqlFormatter.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,19 @@ export default function behavesLikeSqlFormatter(format: FormatFn) {
279279
tbl;
280280
`);
281281
});
282+
283+
it('supports decimal values without leading digits', () => {
284+
const result = format(`
285+
SELECT employee_id FROM employees WHERE salary > .456 * 1000000 AND bonus < .0000239 * salary;
286+
`);
287+
expect(result).toBe(dedent`
288+
SELECT
289+
employee_id
290+
FROM
291+
employees
292+
WHERE
293+
salary > .456 * 1000000
294+
AND bonus < .0000239 * salary;
295+
`);
296+
});
282297
}

0 commit comments

Comments
 (0)