Skip to content
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

feat: move AggregateFunctionsSuggestion to a separate interface, refactor general parser create tests #92

Merged
merged 15 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
8 changes: 5 additions & 3 deletions src/parsing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ export interface ParseResult {
onlyTables?: boolean;
};
suggestColumns?: ColumnSuggestion;
suggestAggregateFunctions?: {
tables: Table[],
};
suggestAggregateFunctions?: AggregateFunctionsSuggestion;
suggestAnalyticFunctions?: unknown;
suggestColRefKeywords?: unknown;
suggestColumnAliases?: ColumnAliasSuggestion[];
Expand Down Expand Up @@ -87,6 +85,10 @@ export type StatementPart =
qualified: boolean,
};

export interface AggregateFunctionsSuggestion {
tables: Table[],
}

export interface ColumnSuggestion {
source?: string;
tables: Table[];
Expand Down
2 changes: 1 addition & 1 deletion src/parsing/parsers/clickhouse/jison/structure.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"../../generic/jison/alter/alter_table.jison",
"../../generic/jison/alter/alter_view.jison",
"../../generic/jison/create/create_common.jison",
"../../generic/jison/create/create_database.jison",
"../../generic/jison/create/create_database_or_schema.jison",
"../../generic/jison/create/create_role.jison",
"create/create_table.jison",
"../../generic/jison/create/create_view.jison",
Expand Down
20 changes: 0 additions & 20 deletions src/parsing/parsers/generic/jison/create/create_common.test.json

This file was deleted.

14 changes: 14 additions & 0 deletions src/parsing/parsers/generic/jison/create/create_common.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
KeywordSuggestion,
parseGenericSql,
} from '../../../../index';
import {expect, test} from '@jest/globals';

test('should suggest CREATE', () => {
const parseResult = parseGenericSql('', '');

expect(parseResult.errors).toBeUndefined();

const suggestion: KeywordSuggestion = { value: 'CREATE', weight: -1 };
expect(parseResult.suggestKeywords).toContainEqual(suggestion);
})
47 changes: 0 additions & 47 deletions src/parsing/parsers/generic/jison/create/create_database.test.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
KeywordSuggestion,
parseGenericSql, parseGenericSqlWithoutCursor, StatementPart,
} from '../../../../index';
import {expect, test} from '@jest/globals';

// TODO: add separate DatabaseOrSchema tests:
// - 'something [IF NOT EXITS]'
// - 'something IF [NOT EXISTS]'
// - 'something IF NOT [exists]'
// - 'something [IF NOT EXISTS] something2'
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the todos are fine, I'll fix them later


test('should suggest creating DATABASE and SCHEMA', () => {
const parseResult = parseGenericSql('CREATE ', '');

expect(parseResult.errors).toBeUndefined();

const suggestions: KeywordSuggestion[] = [
{ value: 'DATABASE', weight: -1 },
{ value: 'SCHEMA', weight: -1 },
];
expect(parseResult.suggestKeywords).toEqual(expect.arrayContaining(suggestions))
})

// TODO: remove duplicates, because databaseOrSchema should be tested separately
test('should suggest IF NOT EXISTS for database creation', () => {
const parseResult = parseGenericSql('CREATE DATABASE ', '');

expect(parseResult.errors).toBeUndefined();

const suggestions: KeywordSuggestion[] = [
{ value: 'IF NOT EXISTS', weight: -1 },
];
expect(parseResult.suggestKeywords).toEqual(expect.arrayContaining(suggestions))
})

test('should suggest IF NOT EXISTS for schema creation', () => {
const parseResult = parseGenericSql('CREATE SCHEMA ', '');

expect(parseResult.errors).toBeUndefined();

const suggestions: KeywordSuggestion[] = [
{ value: 'IF NOT EXISTS', weight: -1 },
];
expect(parseResult.suggestKeywords).toEqual(expect.arrayContaining(suggestions))
})

// TODO: remove duplicates, because databaseOrSchema should be tested separately
test('should not report errors on full CREATE DATABASE statement', () => {
const parseResult = parseGenericSqlWithoutCursor('CREATE DATABASE test_database;');
expect(parseResult.errors).toBeUndefined();
})

test('should not report errors on full CREATE SCHEMA statement and fill locations', () => {
const parseResult = parseGenericSqlWithoutCursor('CREATE SCHEMA test_schema;');

expect(parseResult.errors).toBeUndefined();

const statementParts: StatementPart[] = [
{
type: 'statement',
location: {
first_column: 1,
first_line: 1,
last_column: 26,
last_line: 1
},
}
];
expect(parseResult.locations).toEqual(statementParts);
})
21 changes: 0 additions & 21 deletions src/parsing/parsers/generic/jison/create/create_role.test.json

This file was deleted.

35 changes: 35 additions & 0 deletions src/parsing/parsers/generic/jison/create/create_role.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
KeywordSuggestion,
parseGenericSql, parseGenericSqlWithoutCursor, StatementPart,
} from '../../../../index';
import {expect, test} from '@jest/globals';

test('should suggest creating ROLE', () => {
const parseResult = parseGenericSql('CREATE ', '');

expect(parseResult.errors).toBeUndefined();

const suggestions: KeywordSuggestion[] = [
{ value: 'ROLE', weight: -1 },
];
expect(parseResult.suggestKeywords).toEqual(expect.arrayContaining(suggestions))
})

test('should not report errors on full CREATE ROLE statement and fill locations', () => {
const parseResult = parseGenericSqlWithoutCursor('CREATE ROLE test_role;');

expect(parseResult.errors).toBeUndefined();

const statementParts: StatementPart[] = [
{
type: 'statement',
location: {
first_column: 1,
first_line: 1,
last_column: 22,
last_line: 1
},
}
];
expect(parseResult.locations).toEqual(statementParts);
})
65 changes: 0 additions & 65 deletions src/parsing/parsers/generic/jison/create/create_table.test.json

This file was deleted.

82 changes: 82 additions & 0 deletions src/parsing/parsers/generic/jison/create/create_table.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import {
KeywordSuggestion,
parseGenericSql, parseGenericSqlWithoutCursor, StatementPart,
} from '../../../../index';
import {expect, test} from '@jest/globals';

test('should suggest creating TABLE', () => {
const parseResult = parseGenericSql('CREATE ', '');

expect(parseResult.errors).toBeUndefined();

const suggestions: KeywordSuggestion[] = [
{ value: 'TABLE', weight: -1 },
];
expect(parseResult.suggestKeywords).toEqual(expect.arrayContaining(suggestions))
})

test('should suggest IF NOT EXISTS', () => {
const parseResult = parseGenericSql('CREATE TABLE ', '');

expect(parseResult.errors).toBeUndefined();

const suggestions: KeywordSuggestion[] = [
{ value: 'IF NOT EXISTS', weight: -1 },
];
expect(parseResult.suggestKeywords).toEqual(expect.arrayContaining(suggestions))
})

test('should suggest data types', () => {
const parseResult = parseGenericSql('CREATE TABLE food (id ', '');

// TODO: fix unhandled error
// expect(parseResult.errors).toBeUndefined();

const suggestions: KeywordSuggestion[] = [
{ value: 'BIGINT', weight: -1 },
{ value: 'BOOLEAN', weight: -1 },
{ value: 'CHAR', weight: -1 },
{ value: 'DECIMAL', weight: -1 },
{ value: 'DOUBLE', weight: -1 },
{ value: 'FLOAT', weight: -1 },
{ value: 'INT', weight: -1 },
];
expect(parseResult.suggestKeywords).toEqual(expect.arrayContaining(suggestions))
})

test('should suggest data types when some types are already written', () => {
const parseResult = parseGenericSql('CREATE TABLE food (id INT, age FLOAT, bar ', '');

// TODO: fix unhandled error
// expect(parseResult.errors).toBeUndefined();

const suggestions: KeywordSuggestion[] = [
{ value: 'BIGINT', weight: -1 },
{ value: 'BOOLEAN', weight: -1 },
{ value: 'CHAR', weight: -1 },
{ value: 'DECIMAL', weight: -1 },
{ value: 'DOUBLE', weight: -1 },
{ value: 'FLOAT', weight: -1 },
{ value: 'INT', weight: -1 },
];
expect(parseResult.suggestKeywords).toEqual(expect.arrayContaining(suggestions))
})

test('should not report errors on full CREATE TABLE statement', () => {
const parseResult = parseGenericSqlWithoutCursor('CREATE TABLE test_table (id INT, age FLOAT);');

expect(parseResult.errors).toBeUndefined();

const statementParts: StatementPart[] = [
{
type: 'statement',
location: {
first_column: 1,
first_line: 1,
last_column: 44,
last_line: 1
},
}
];
expect(parseResult.locations).toEqual(statementParts);
})
Loading
Loading