Skip to content

Commit

Permalink
feat: support built-in sql snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
jialan committed Jan 3, 2025
1 parent c1b619c commit 631a479
Show file tree
Hide file tree
Showing 16 changed files with 1,071 additions and 14 deletions.
22 changes: 19 additions & 3 deletions src/baseSQLWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BasicSQL } from 'dt-sql-parser/dist/parser/common/basicSQL';
import { worker } from './fillers/monaco-editor-core';
import { Suggestions, ParseError, EntityContext } from 'dt-sql-parser';
import { Position } from './fillers/monaco-editor-core';
import { SemanticContext } from 'dt-sql-parser/dist/parser/common/types';

export interface ICreateData {
languageId: string;
Expand Down Expand Up @@ -45,17 +46,32 @@ export abstract class BaseSQLWorker {
async doCompletionWithEntities(
code: string,
position: Position
): Promise<[Suggestions | null, EntityContext[] | null]> {
): Promise<{
suggestions: Suggestions | null;
allEntities: EntityContext[] | null;
context: SemanticContext | null;
}> {
code = code || this.getTextDocument();
if (code) {
const suggestions = this.parser.getSuggestionAtCaretPosition(code, position);
let allEntities = null;
if (suggestions?.syntax?.length) {
allEntities = this.parser.getAllEntities(code, position);
}
return Promise.resolve([suggestions, allEntities]);
const semanticContext = this.parser.getSemanticContextAtCaretPosition(code, position);

return Promise.resolve({
suggestions,
allEntities,
context: semanticContext
});
}
return Promise.resolve([null, null]);

return Promise.resolve({
suggestions: null,
allEntities: null,
context: null
});
}

async getAllEntities(code: string, position?: Position): Promise<EntityContext[] | null> {
Expand Down
15 changes: 12 additions & 3 deletions src/languageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { debounce } from './common/utils';
import { BaseSQLWorker } from './baseSQLWorker';
import type { ParseError } from 'dt-sql-parser';
import type { LanguageServiceDefaults } from './monaco.contribution';
import type { CompletionSnippet, LanguageServiceDefaults } from './monaco.contribution';

export interface WorkerAccessor<T extends BaseSQLWorker> {
(...uris: Uri[]): Promise<T>;
Expand Down Expand Up @@ -159,13 +159,22 @@ export class CompletionAdapter<T extends BaseSQLWorker>
}
return worker.doCompletionWithEntities(code, position);
})
.then(([suggestions, allEntities]) => {
.then(({ suggestions, allEntities, context: semanticContext }) => {
let snippets: CompletionSnippet[] = [];
if (semanticContext?.isStatementBeginning) {
snippets = this._defaults.completionSnippets.map((item) => ({
...item,
insertText: typeof item.body === 'string' ? item.body : item.body.join('\n')
}));
}

return this._defaults.completionService(
model,
position,
context,
suggestions,
allEntities
allEntities,
snippets
);
})
.then((completions) => {
Expand Down
115 changes: 115 additions & 0 deletions src/languages/flink/flink.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import type { CompletionSnippetOption } from 'src/monaco.contribution';

export const flinkSnippets: CompletionSnippetOption[] = [
{
label: 'create-source-table',
prefix: 'CREATE-SOURCE-TABLE',
body: [
'CREATE TABLE ${1:source_table} (',
'\tid STRING,',
'\tval BIGINT,',
'\tts TIMESTAMP(3),',
"\tWATERMARK FOR ts AS ts - INTERVAL '5' SECOND",
') WITH (',
"\t'connector' = 'kafka',",
"\t'topic' = 'input_topic',",
"\t'properties.bootstrap.servers' = 'localhost:9092',",
"\t'format' = 'json',",
"\t'scan.startup.mode' = 'earliest-offset'",
');'
]
},
{
label: 'create-sink-table',
prefix: 'CREATE-SINK-TABLE',
body: [
'CREATE TABLE ${1:sink_table} (',
'\tid STRING,',
'\tcnt BIGINT',
') WITH (',
"\t'connector' = 'jdbc',",
"\t'url' = 'jdbc:mysql://localhost:3306/test',",
"\t'table-name' = 'output_table',",
"\t'username' = 'root',",
"\t'password' = 'password'",
');'
]
},
{
label: 'tumble-window',
prefix: 'TUMBLE-WINDOW',
body: [
'SELECT',
'\twindow_start,',
'\twindow_end,',
'\tSUM(price) as total_price',
'FROM',
'\tTABLE(TUMBLE(TABLE table_name2,',
'\tDESCRIPTOR(create_time),',
"\tINTERVAL '1' MINUTE))",
'GROUP BY',
'\twindow_start,',
'\twindow_end;'
]
},
{
label: 'hop-window',
prefix: 'HOP-WINDOW',
body: [
'SELECT',
'\twindow_start,',
'\twindow_end,',
'\tSUM(price) as total_price',
'FROM',
'\tTABLE(HOP(TABLE table_name2,',
'\tDESCRIPTOR(create_time),',
"\tINTERVAL '30' SECONDS,",
"\tINTERVAL '1' MINUTE))",
'GROUP BY',
'\twindow_start,',
'\twindow_end;'
]
},
{
label: 'comulate-window',
prefix: 'CUMULATE-WINDOW',
body: [
'SELECT',
'\twindow_start,',
'\twindow_end,',
'\tSUM(price) as total_price',
'FROM',
'\tTABLE(CUMULATE(TABLE table_name2,',
'\tDESCRIPTOR(create_time),',
"\tINTERVAL '30' SECONDS,",
"\tINTERVAL '1' MINUTE))",
'GROUP BY',
'\twindow_start,',
'\twindow_end;'
]
},
{
label: 'session-window',
prefix: 'SESSION-WINDOW',
body: [
'SELECT',
"\tSESSION_START(create_time, INTERVAL '10' SECOND) AS session_beg,",
"\tSESSION_ROWTIME(create_time, INTERVAL '10' SECOND) AS session_end,",
'\tSUM(price) AS total_price',
'FROM',
'\ttable_name',
'GROUP BY',
"\tSESSION(create_time, INTERVAL '10' SECOND);"
]
},
{
label: 'insert-into-select',
prefix: 'INSERT-INTO-SELECT',
body: [
'INSERT INTO ${1:table_name}',
'SELECT ${2:column1}, ${3:column2}',
'FROM ${4:source_table}',
'WHERE ${5:conditions};\n$6'
]
}
];
144 changes: 144 additions & 0 deletions src/languages/hive/hive.snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import type { CompletionSnippetOption } from 'src/monaco.contribution';

export const hiveSnippets: CompletionSnippetOption[] = [
{
label: 'select',
prefix: 'SELECT',
body: ['SELECT ${2:column1}, ${3:column2} FROM ${1:table_name};\n$4']
},
{
label: 'select-join',
prefix: 'SELECT-JOIN',
body: [
'SELECT ${8:column1} FROM ${1:table_name} ${2:t1}',
'${3:LEFT} JOIN ${4:table2} ${5:t2} ON ${2:t1}.${6:column1} = ${5:t2}.${7:column2};\n$9'
]
},
{
label: 'select-order-by',
prefix: 'SELECT-ORDERBY',
body: [
'SELECT ${2:column1}, ${3:column2} FROM ${1:table_name} ORDER BY ${4:column1} ${5:desc};\n$6'
]
},
{
label: 'insert',
prefix: 'INSERT-INTO',
body: [
'INSERT INTO ${1:table_name} (${2:column1}, ${3:column2}) VALUES (${4:value1}, ${5:value2});\n$6'
]
},
{
label: 'insert-into-select',
prefix: 'INSERT-INTO-SELECT',
body: [
'INSERT INTO TABLE ${1:table_name}',
'SELECT ${3:column1}, ${4:column2}',
'FROM ${2:source_table}',
'WHERE ${5:conditions};\n$6'
]
},
{
label: 'insert-overwrite-table',
prefix: 'INSERT-OVERWRITE-TABLE',
body: [
'INSERT OVERWRITE TABLE ${1:table_name}',
'SELECT ${3:column1}, ${4:column2}',
'FROM ${2:source_table}',
'WHERE ${5:conditions};\n$6'
]
},
{
label: 'update',
prefix: 'UPDATE',
body: [
'UPDATE ${1:table_name}',
'SET ${2:column1} = ${3:value1}',
'WHERE ${4:column2} = ${5:value2};\n$6'
]
},
{
label: 'delete',
prefix: 'DELETE',
body: ['DELETE FROM ${1:table_name}', 'WHERE ${2:column1} = ${3:value1};\n$4']
},
{
label: 'create-table',
prefix: 'CREATE-TABLE',
body: [
'CREATE TABLE IF NOT EXISTS ${1:table_name} (',
'\t${2:column1} ${3:STRING},',
'\t${4:column2} ${5:STRING}',
')',
"COMMENT '${6:table_comment}'",
'ROW FORMAT ${7:DELIMITED}',
"FIELDS TERMINATED BY '${8:\\t}'",
'STORED AS ${9:PARQUET};\n$10'
]
},
{
label: 'create-table-as-select',
prefix: 'CREATE-TABLE-AS-SELECT',
body: [
'CREATE TABLE IF NOT EXISTS ${1:table_name}',
'AS',
'SELECT ${2:column1}, ${3:column2}',
'FROM ${4:source_table}',
'WHERE ${5:conditions};\n$6'
]
},
{
label: 'create-partition-table',
prefix: 'CREATE-PARTITION-TABLE',
body: [
'CREATE TABLE IF NOT EXISTS ${1:table_name} (',
'\t${2:column1} ${3:STRING},',
'\t${4:column2} ${5:STRING}',
')',
"COMMENT '${6:table_comment}'",
'PARTITIONED BY (${7:part_column_name} STRING)',
'ROW FORMAT ${8:DELIMITED}',
"FIELDS TERMINATED BY '${9:\\t}'",
'STORED AS ${10:PARQUET};\n$11'
]
},
{
label: 'create-bucket-table',
prefix: 'CREATE-BUCKET-TABLE',
body: [
'CREATE TABLE IF NOT EXISTS ${1:table_name} (',
'\t${2:column1} ${3:STRING},',
'\t${4:column2} ${5:STRING}',
')',
"COMMENT '${6:table_comment}'",
'PARTITIONED BY (${7:part_column_name} STRING)',
'CLUSTERED BY (${8:bucket_column_name}) INTO ${9:1} BUCKETS',
'ROW FORMAT ${10:DELIMITED}',
"FIELDS TERMINATED BY '${11:\\t}'",
'STORED AS ${12:PARQUET};\n$13'
]
},
{
label: 'alter-table-partition',
prefix: 'ALTER-TABLE-PARTITION',
body: [
"ALTER TABLE ${1:table_name} ${2:ADD} PARTITION (${3:part_column}='${4:part_value}');\n$5"
]
},
{
label: 'alter-table-properties',
prefix: 'ALTER-TABLE-PROPERTIES',
body: [
"ALTER TABLE ${1:table_name} SET TBLPROPERTIES ('${2:property_name}'='${3:property_value}');\n$4"
]
},
{
label: 'alter-table-columns',
prefix: 'ALTER-TABLE-COLUMNS',
body: [
'ALTER TABLE ${1:table_name} ADD COLUMNS (',
"\t${2:column_name} ${3:STRING} COMMENT '${4:desc}'",
');\n$5'
]
}
];
Loading

0 comments on commit 631a479

Please sign in to comment.