Skip to content

Commit 24b6a2d

Browse files
Merge pull request #7 from deepnote/orom/support-parsing-database-entities-with-hyphens
Add support for parsing database entities including hyphens
2 parents 947fb3f + 82b651f commit 24b6a2d

File tree

5 files changed

+51
-7
lines changed

5 files changed

+51
-7
lines changed

packages/server/src/complete/utils/getLastToken.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Gets the last token from the given string considering that tokens can contain dots.
1+
// Gets the last token from the given string considering that tokens can contain dots and dashes.
22
export function getLastToken(sql: string): string {
3-
const match = sql.match(/^(?:.|\s)*[^A-z0-9\\.:'"](.*?)$/)
3+
const match = sql.match(/^(?:.|\s)*[^A-z0-9\\.\-:'"](.*?)$/)
44
if (match) {
55
let prevToken = ''
66
let currentToken = match[1]

packages/server/test/complete.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,3 +999,47 @@ describe('DROP statement', () => {
999999
expect(result.candidates[0].label).toEqual('TABLE1')
10001000
})
10011001
})
1002+
1003+
const SIMPLE_NESTED_SCHEMA_WITH_HYPHEN = {
1004+
tables: [
1005+
{
1006+
catalog: 'catalog-3',
1007+
database: 'schema3',
1008+
tableName: 'table3',
1009+
columns: [{ columnName: 'abc', description: 'def' }],
1010+
},
1011+
],
1012+
functions: [],
1013+
}
1014+
1015+
describe('Fully qualified table names with dash', () => {
1016+
test('complete catalog name', () => {
1017+
const result = complete(
1018+
'SELECT * FROM catalog-3.sch',
1019+
{ line: 0, column: 26 },
1020+
SIMPLE_NESTED_SCHEMA_WITH_HYPHEN
1021+
)
1022+
expect(result.candidates.length).toEqual(1)
1023+
const expected = [expect.objectContaining({ label: 'schema3' })]
1024+
expect(result.candidates).toEqual(expect.arrayContaining(expected))
1025+
})
1026+
test('complete table name', () => {
1027+
const result = complete(
1028+
'SELECT * FROM catalog-3.schema3.tab',
1029+
{ line: 0, column: 34 },
1030+
SIMPLE_NESTED_SCHEMA_WITH_HYPHEN
1031+
)
1032+
expect(result.candidates.length).toEqual(1)
1033+
const expected = [expect.objectContaining({ label: 'table3' })]
1034+
expect(result.candidates).toEqual(expect.arrayContaining(expected))
1035+
})
1036+
test('complete table name on dot', () => {
1037+
const result = complete(
1038+
'SELECT * FROM catalog-3.schema3.',
1039+
{ line: 0, column: 32 },
1040+
SIMPLE_NESTED_SCHEMA_WITH_HYPHEN
1041+
)
1042+
const expected = [expect.objectContaining({ label: 'table3' })]
1043+
expect(result.candidates).toEqual(expect.arrayContaining(expected))
1044+
})
1045+
})

packages/sql-parser/base/fromClauseParser.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/sql-parser/base/parser.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/sql-parser/parser.pegjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ ident_name
843843

844844
ident_start = [A-Za-z_]
845845

846-
ident_part = [A-Za-z0-9_]
846+
ident_part = [A-Za-z0-9_-]
847847

848848
// to support column name like `cf1:name` in hbase
849849
// Allow square brackets and quote to support nested columns with subscripts for example `books['title'].chapters[12].paragraphs`

0 commit comments

Comments
 (0)