From 927cbddd9f5cb5694230619f40eda6907dba0d1f Mon Sep 17 00:00:00 2001 From: gabotechs Date: Mon, 25 Mar 2024 21:49:05 +0100 Subject: [PATCH 1/2] Add failing test that extracts locations from a select statement with a table alias --- src/syntax/select.spec.ts | 46 ++++++++++++++++++++++++++++++++++++++- src/syntax/spec-utils.ts | 6 ++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/syntax/select.spec.ts b/src/syntax/select.spec.ts index c7fab3d..1766470 100644 --- a/src/syntax/select.spec.ts +++ b/src/syntax/select.spec.ts @@ -1,6 +1,6 @@ import 'mocha'; import 'chai'; -import { checkSelect, checkInvalid, columns, ref, star, tbl, name, qname, checkStatement, int, binary } from './spec-utils'; +import { checkSelect, checkSelectLoc, checkInvalid, columns, ref, star, tbl, name, qname, checkStatement, int, binary } from './spec-utils'; import { JoinType, SelectStatement } from './ast'; describe('Select statements', () => { @@ -1060,4 +1060,48 @@ describe('Select statements', () => { type: 'skip locked', } }); + + checkSelectLoc('select * from test as t', { + _location: { start: 0, end: 23 }, + type: 'select', + from: [{ + _location: { start: 14, end: 23 }, + type: 'table', + name: { + _location: { start: 14, end: 23 }, + name: 'test', + alias: 't', + } + }], + columns: [{ + _location: { start: 7, end: 8 }, + expr: { + _location: { start: 7, end: 8 }, + type: 'ref', + name: '*', + } + }] + }); + + checkSelectLoc('select * from test t', { + _location: { start: 0, end: 20 }, + type: 'select', + from: [{ + _location: { start: 14, end: 20 }, + type: 'table', + name: { + _location: { start: 14, end: 20 }, + name: 'test', + alias: 't', + }, + }], + columns: [{ + _location: { start: 7, end: 8 }, + expr: { + _location: { start: 7, end: 8 }, + type: 'ref', + name: '*', + }, + }] + }); }); diff --git a/src/syntax/spec-utils.ts b/src/syntax/spec-utils.ts index 4dc800a..4221f3b 100644 --- a/src/syntax/spec-utils.ts +++ b/src/syntax/spec-utils.ts @@ -13,6 +13,10 @@ export function checkSelect(value: string | string[], expected: SelectStatement) checkTree(value, expected, (p, m) => m.statement(p)); } +export function checkSelectLoc(value: string | string[], expected: SelectStatement) { + checkTree(value, expected, (p, m) => m.statement(p), undefined, true); +} + export function checkCreateSequence(value: string | string[], expected: CreateSequenceStatement) { checkTree(value, expected, (p, m) => m.statement(p)); } @@ -330,4 +334,4 @@ export function tbl(nm: string): FromTable { } export function numeric(value: number): ExprNumeric { return { type: 'numeric', value }; -} \ No newline at end of file +} From 72b61fbd05f85c6413d341830724cd1c4fa8f932 Mon Sep 17 00:00:00 2001 From: gabotechs Date: Mon, 25 Mar 2024 21:49:44 +0100 Subject: [PATCH 2/2] Fix the location tracking by using `last` instead of `unwrap` on ident_aliased --- src/syntax/base.ne | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/syntax/base.ne b/src/syntax/base.ne index 68c50fa..d291855 100644 --- a/src/syntax/base.ne +++ b/src/syntax/base.ne @@ -312,7 +312,7 @@ data_type_date # === Table ref (ex: [db.]mytable [as X] ) # [AS x] or just [x] -ident_aliased -> (%kw_as ident {% last %}) | ident {% unwrap %} +ident_aliased -> (%kw_as ident {% last %}) | ident {% last %} table_ref -> qualified_name {% unwrap %}