Skip to content

Add support for parsing database entities including hyphens #7

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

Merged
Merged
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
4 changes: 2 additions & 2 deletions packages/server/src/complete/utils/getLastToken.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Gets the last token from the given string considering that tokens can contain dots.
// Gets the last token from the given string considering that tokens can contain dots and dashes.
export function getLastToken(sql: string): string {
const match = sql.match(/^(?:.|\s)*[^A-z0-9\\.:'"](.*?)$/)
const match = sql.match(/^(?:.|\s)*[^A-z0-9\\.\-:'"](.*?)$/)
if (match) {
let prevToken = ''
let currentToken = match[1]
Expand Down
44 changes: 44 additions & 0 deletions packages/server/test/complete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -999,3 +999,47 @@ describe('DROP statement', () => {
expect(result.candidates[0].label).toEqual('TABLE1')
})
})

const SIMPLE_NESTED_SCHEMA_WITH_HYPHEN = {
tables: [
{
catalog: 'catalog-3',
database: 'schema3',
tableName: 'table3',
columns: [{ columnName: 'abc', description: 'def' }],
},
],
functions: [],
}

describe('Fully qualified table names with dash', () => {
test('complete catalog name', () => {
const result = complete(
'SELECT * FROM catalog-3.sch',
{ line: 0, column: 26 },
SIMPLE_NESTED_SCHEMA_WITH_HYPHEN
)
expect(result.candidates.length).toEqual(1)
const expected = [expect.objectContaining({ label: 'schema3' })]
expect(result.candidates).toEqual(expect.arrayContaining(expected))
})
test('complete table name', () => {
const result = complete(
'SELECT * FROM catalog-3.schema3.tab',
{ line: 0, column: 34 },
SIMPLE_NESTED_SCHEMA_WITH_HYPHEN
)
expect(result.candidates.length).toEqual(1)
const expected = [expect.objectContaining({ label: 'table3' })]
expect(result.candidates).toEqual(expect.arrayContaining(expected))
})
test('complete table name on dot', () => {
const result = complete(
'SELECT * FROM catalog-3.schema3.',
{ line: 0, column: 32 },
SIMPLE_NESTED_SCHEMA_WITH_HYPHEN
)
const expected = [expect.objectContaining({ label: 'table3' })]
expect(result.candidates).toEqual(expect.arrayContaining(expected))
})
})
4 changes: 2 additions & 2 deletions packages/sql-parser/base/fromClauseParser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/sql-parser/base/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/sql-parser/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ ident_name

ident_start = [A-Za-z_]

ident_part = [A-Za-z0-9_]
ident_part = [A-Za-z0-9_-]

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