Skip to content

Feat: upgrade trino to 450 #323

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 6 commits into from
Jul 4, 2024
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
930 changes: 667 additions & 263 deletions src/grammar/trino/TrinoSql.g4

Large diffs are not rendered by default.

210 changes: 189 additions & 21 deletions src/lib/trino/TrinoSql.interp

Large diffs are not rendered by default.

1,160 changes: 643 additions & 517 deletions src/lib/trino/TrinoSql.tokens

Large diffs are not rendered by default.

227 changes: 210 additions & 17 deletions src/lib/trino/TrinoSqlLexer.interp

Large diffs are not rendered by default.

1,158 changes: 642 additions & 516 deletions src/lib/trino/TrinoSqlLexer.tokens

Large diffs are not rendered by default.

2,871 changes: 1,609 additions & 1,262 deletions src/lib/trino/TrinoSqlLexer.ts

Large diffs are not rendered by default.

1,511 changes: 1,311 additions & 200 deletions src/lib/trino/TrinoSqlListener.ts

Large diffs are not rendered by default.

35,122 changes: 22,264 additions & 12,858 deletions src/lib/trino/TrinoSqlParser.ts

Large diffs are not rendered by default.

830 changes: 761 additions & 69 deletions src/lib/trino/TrinoSqlVisitor.ts

Large diffs are not rendered by default.

29 changes: 19 additions & 10 deletions src/parser/trino/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ export class TrinoSQL extends BasicSQL<TrinoSqlLexer, ProgramContext, TrinoSqlPa
}

protected preferredRules: Set<number> = new Set([
TrinoSqlParser.RULE_catalogName,
TrinoSqlParser.RULE_catalogRef,
TrinoSqlParser.RULE_catalogNameCreate,
TrinoSqlParser.RULE_schemaName,
TrinoSqlParser.RULE_schemaRef,
TrinoSqlParser.RULE_schemaNameCreate,
TrinoSqlParser.RULE_tableName,
TrinoSqlParser.RULE_tableRef,
TrinoSqlParser.RULE_tableNameCreate,
TrinoSqlParser.RULE_viewName,
TrinoSqlParser.RULE_viewRef,
TrinoSqlParser.RULE_viewNameCreate,
TrinoSqlParser.RULE_functionName,
TrinoSqlParser.RULE_columnName,
TrinoSqlParser.RULE_functionNameCreate,
TrinoSqlParser.RULE_columnRef,
TrinoSqlParser.RULE_columnNameCreate,
]);

Expand All @@ -60,27 +61,31 @@ export class TrinoSQL extends BasicSQL<TrinoSqlLexer, ProgramContext, TrinoSqlPa

let syntaxContextType: EntityContextType | StmtContextType | undefined = void 0;
switch (ruleType) {
case TrinoSqlParser.RULE_catalogName: {
case TrinoSqlParser.RULE_catalogRef: {
syntaxContextType = EntityContextType.CATALOG;
break;
}
case TrinoSqlParser.RULE_schemaName: {
case TrinoSqlParser.RULE_catalogNameCreate: {
syntaxContextType = EntityContextType.CATALOG_CREATE;
break;
}
case TrinoSqlParser.RULE_schemaRef: {
syntaxContextType = EntityContextType.DATABASE;
break;
}
case TrinoSqlParser.RULE_schemaNameCreate: {
syntaxContextType = EntityContextType.DATABASE_CREATE;
break;
}
case TrinoSqlParser.RULE_tableName: {
case TrinoSqlParser.RULE_tableRef: {
syntaxContextType = EntityContextType.TABLE;
break;
}
case TrinoSqlParser.RULE_tableNameCreate: {
syntaxContextType = EntityContextType.TABLE_CREATE;
break;
}
case TrinoSqlParser.RULE_viewName: {
case TrinoSqlParser.RULE_viewRef: {
syntaxContextType = EntityContextType.VIEW;
break;
}
Expand All @@ -92,11 +97,15 @@ export class TrinoSQL extends BasicSQL<TrinoSqlLexer, ProgramContext, TrinoSqlPa
syntaxContextType = EntityContextType.FUNCTION;
break;
}
case TrinoSqlParser.RULE_functionNameCreate: {
syntaxContextType = EntityContextType.FUNCTION_CREATE;
break;
}
case TrinoSqlParser.RULE_columnNameCreate: {
syntaxContextType = EntityContextType.COLUMN_CREATE;
break;
}
case TrinoSqlParser.RULE_columnName: {
case TrinoSqlParser.RULE_columnRef: {
syntaxContextType = EntityContextType.COLUMN;
break;
}
Expand Down
24 changes: 17 additions & 7 deletions src/parser/trino/trinoEntityCollector.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,53 @@
import type {
CatalogNameCreateContext,
CatalogRefContext,
ColumnNameCreateContext,
CreateMaterializedViewContext,
CreateSchemaContext,
CreateTableAsSelectContext,
CreateTableContext,
CreateViewContext,
InsertIntoContext,
QueryStatementContext,
SchemaNameContext,
SchemaNameCreateContext,
SchemaRefContext,
SingleStatementContext,
TableNameContext,
TableNameCreateContext,
ViewNameContext,
TableRefContext,
ViewNameCreateContext,
ViewRefContext,
QueryStatementContext,
} from '../../lib/trino/TrinoSqlParser';
import type { TrinoSqlListener } from '../../lib/trino/TrinoSqlListener';
import { EntityContextType } from '../common/types';
import { StmtContextType, EntityCollector } from '../common/entityCollector';

export class TrinoEntityCollector extends EntityCollector implements TrinoSqlListener {
/** ====== Entity Begin */
exitSchemaName(ctx: SchemaNameContext) {
exitCatalogRef(ctx: CatalogRefContext) {
this.pushEntity(ctx, EntityContextType.CATALOG);
}

exitCatalogNameCreate(ctx: CatalogNameCreateContext) {
this.pushEntity(ctx, EntityContextType.CATALOG_CREATE);
}

exitSchemaRef(ctx: SchemaRefContext) {
this.pushEntity(ctx, EntityContextType.DATABASE);
}

exitSchemaNameCreate(ctx: SchemaNameCreateContext) {
this.pushEntity(ctx, EntityContextType.DATABASE_CREATE);
}

exitTableName(ctx: TableNameContext) {
exitTableRef(ctx: TableRefContext) {
this.pushEntity(ctx, EntityContextType.TABLE);
}

exitTableNameCreate(ctx: TableNameCreateContext) {
this.pushEntity(ctx, EntityContextType.TABLE_CREATE);
}

exitViewName(ctx: ViewNameContext) {
exitViewRef(ctx: ViewRefContext) {
this.pushEntity(ctx, EntityContextType.VIEW);
}

Expand Down
4 changes: 3 additions & 1 deletion test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export const readSQL = (dirname: string, fileName: string) => {
tmp += char;

const isMulti =
tmp.includes('EXECUTE STATEMENT SET') || tmp.includes('BEGIN STATEMENT SET;');
tmp.includes('EXECUTE STATEMENT SET') ||
tmp.includes('BEGIN STATEMENT SET;') ||
tmp.includes('BEGIN');

if (!isMulti) {
// 非批量的先简单按照分号切割
Expand Down
8 changes: 8 additions & 0 deletions test/parser/trino/suggestion/fixtures/syntaxSuggestion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ INSERT INTO tb (id, );
SELECT * FROM tb ORDER BY ;

SELECT * FROM tb GROUP BY ;

CREATE CATALOG ;

DROP CATALOG cat ;

CREATE FUNCTION example.default. ;

DROP FUNCTION ;
70 changes: 70 additions & 0 deletions test/parser/trino/suggestion/syntaxSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,74 @@ describe('Trino SQL Syntax Suggestion', () => {
expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
});

test('Create Catalog', () => {
const pos: CaretPosition = {
lineNumber: 39,
column: 16,
};
const syntaxes = trino.getSuggestionAtCaretPosition(
commentOtherLine(syntaxSql, pos.lineNumber),
pos
)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === EntityContextType.CATALOG_CREATE
);

expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
});
test('Drop Catalog', () => {
const pos: CaretPosition = {
lineNumber: 41,
column: 17,
};
const syntaxes = trino.getSuggestionAtCaretPosition(
commentOtherLine(syntaxSql, pos.lineNumber),
pos
)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === EntityContextType.CATALOG
);

expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['cat']);
});
test('Create Function', () => {
const pos: CaretPosition = {
lineNumber: 43,
column: 33,
};
const syntaxes = trino.getSuggestionAtCaretPosition(
commentOtherLine(syntaxSql, pos.lineNumber),
pos
)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === EntityContextType.FUNCTION_CREATE
);

expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([
'example',
'.',
'default',
'.',
]);
});
test('Drop Function', () => {
const pos: CaretPosition = {
lineNumber: 45,
column: 15,
};
const syntaxes = trino.getSuggestionAtCaretPosition(
commentOtherLine(syntaxSql, pos.lineNumber),
pos
)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === EntityContextType.FUNCTION
);

expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
});
});
20 changes: 16 additions & 4 deletions test/parser/trino/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import path from 'path';
import { TrinoSQL } from 'src/parser/trino';
import { CaretPosition } from 'src/parser/common/types';
import { CaretPosition, EntityContextType } from 'src/parser/common/types';
import { commentOtherLine } from 'test/helper';

const tokenSql = fs.readFileSync(path.join(__dirname, 'fixtures', 'tokenSuggestion.sql'), 'utf-8');
Expand Down Expand Up @@ -34,11 +34,13 @@ describe('Trino SQL Token Suggestion', () => {

expect(suggestion).toMatchUnorderedArrary([
'ROLE',
'VIEW',
'FUNCTION',
'OR',
'VIEW',
'MATERIALIZED',
'TABLE',
'SCHEMA',
'CATALOG',
]);
});

Expand Down Expand Up @@ -76,9 +78,17 @@ describe('Trino SQL Token Suggestion', () => {
const suggestion = trino.getSuggestionAtCaretPosition(
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
);

expect(suggestion?.keywords?.includes('INPUT')).toBeTruthy();
expect(suggestion?.keywords?.includes('OUTPUT')).toBeTruthy();

expect(suggestion).toMatchUnorderedArrary(['OUTPUT', 'INPUT']);
expect(
suggestion?.syntax?.find((item) => item.syntaxContextType === EntityContextType.TABLE)
).not.toBeUndefined();
expect(
suggestion?.syntax?.find((item) => item.syntaxContextType === EntityContextType.VIEW)
).not.toBeUndefined();
});

test('After DROP', () => {
Expand All @@ -93,10 +103,12 @@ describe('Trino SQL Token Suggestion', () => {

expect(suggestion).toMatchUnorderedArrary([
'ROLE',
'FUNCTION',
'VIEW',
'MATERIALIZED',
'TABLE',
'SCHEMA',
'CATALOG',
]);
});

Expand Down
12 changes: 12 additions & 0 deletions test/parser/trino/syntax/createStatement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const features = {
role: readSQL(__dirname, 'create_role.sql'),
tableAsSelect: readSQL(__dirname, 'create_table_as_select.sql'),
materializedView: readSQL(__dirname, 'create_materialized_view.sql'),
catalog: readSQL(__dirname, 'create_catalog.sql'),
function: readSQL(__dirname, 'create_function.sql'),
};

describe('TrinoSQL Create Statements Syntax Tests', () => {
Expand Down Expand Up @@ -43,4 +45,14 @@ describe('TrinoSQL Create Statements Syntax Tests', () => {
expect(trino.validate(sql).length).toBe(0);
});
});
features.catalog.forEach((sql) => {
it(sql, () => {
expect(trino.validate(sql).length).toBe(0);
});
});
features.function.forEach((sql) => {
it(sql, () => {
expect(trino.validate(sql).length).toBe(0);
});
});
});
12 changes: 12 additions & 0 deletions test/parser/trino/syntax/dropStatement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const features = {
role: readSQL(__dirname, 'drop_role.sql'),
column: readSQL(__dirname, 'drop_column.sql'),
materializedView: readSQL(__dirname, 'drop_materialized_view.sql'),
catalog: readSQL(__dirname, 'drop_catalog.sql'),
function: readSQL(__dirname, 'drop_function.sql'),
};

describe('TrinoSQL Drop Statements Syntax Tests', () => {
Expand Down Expand Up @@ -43,4 +45,14 @@ describe('TrinoSQL Drop Statements Syntax Tests', () => {
expect(trino.validate(sql).length).toBe(0);
});
});
features.catalog.forEach((sql) => {
it(sql, () => {
expect(trino.validate(sql).length).toBe(0);
});
});
features.function.forEach((sql) => {
it(sql, () => {
expect(trino.validate(sql).length).toBe(0);
});
});
});
2 changes: 2 additions & 0 deletions test/parser/trino/syntax/fixtures/alter_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ ALTER TABLE users DROP COLUMN zip;
ALTER TABLE IF EXISTS users DROP COLUMN IF EXISTS zip;
ALTER TABLE users RENAME COLUMN id TO user_id;
ALTER TABLE IF EXISTS users RENAME column IF EXISTS id to user_id;
ALTER TABLE users ALTER COLUMN id SET DATA TYPE bigint;
ALTER TABLE users ALTER COLUMN id DROP NOT NULL;
ALTER TABLE people SET AUTHORIZATION alice;
ALTER TABLE people SET AUTHORIZATION ROLE PUBLIC;
ALTER TABLE people SET PROPERTIES x = 'y';
Expand Down
2 changes: 2 additions & 0 deletions test/parser/trino/syntax/fixtures/comment.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
COMMENT ON TABLE users IS 'master table';

COMMENT ON VIEW users IS 'master view';

COMMENT ON COLUMN users.name IS 'full name';

SHOW COMMENT ON COLUMN column1;
Expand Down
13 changes: 13 additions & 0 deletions test/parser/trino/syntax/fixtures/create_catalog.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE CATALOG tpch USING tpch;

CREATE CATALOG brain USING memory
WITH ("memory.max-data-per-node" = '128MB');


CREATE CATALOG example USING postgresql
WITH (
"connection-url" = 'jdbc:pg:localhost:5432',
"connection-user" = '${ENV:POSTGRES_USER}',
"connection-password" = '${ENV:POSTGRES_PASSWORD}',
"case-insensitive-name-matching" = 'true'
);
8 changes: 8 additions & 0 deletions test/parser/trino/syntax/fixtures/create_function.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE FUNCTION example.default.meaning_of_life()
RETURNS bigint
BEGIN
RETURN 42;
END;


CREATE FUNCTION meaning_of_life() RETURNS bigint RETURN 42;
Loading
Loading