Skip to content

Commit

Permalink
Update keywords set
Browse files Browse the repository at this point in the history
  • Loading branch information
czgu committed Nov 19, 2024
1 parent 26d30a9 commit 70e1c6e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
2 changes: 2 additions & 0 deletions querybook/webapp/components/QueryEditor/QueryEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { acceptCompletion, startCompletion } from '@codemirror/autocomplete';
import { indentService } from '@codemirror/language';
import { EditorView } from '@codemirror/view';
import CodeMirror, { ReactCodeMirrorRef } from '@uiw/react-codemirror';
import clsx from 'clsx';
Expand Down Expand Up @@ -371,6 +372,7 @@ export const QueryEditor: React.FC<
searchExtension,
selectionExtension,
sqlCompleteExtension,
indentService.of((context, pos) => context.lineIndent(pos - 1)),
],
[
language,
Expand Down
44 changes: 35 additions & 9 deletions querybook/webapp/lib/codemirror/codemirror-language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,45 @@ import {
SQLite,
SQLDialect,
} from '@codemirror/lang-sql';
import { SettingsByLanguage } from 'lib/sql-helper/sql-setting';
import { getLanguageSetting } from 'lib/sql-helper/sql-setting';

function builtInTypeSplit(language: string, builtIn: string): [string, string] {
// source: https://github.com/codemirror/lang-sql/blob/main/src/tokens.ts
const SQLTypes =
'array binary bit boolean char character clob date decimal double float int integer interval large national nchar nclob numeric object precision real smallint time timestamp varchar varying ';
const SQLKeywords =
'absolute action add after all allocate alter and any are as asc assertion at authorization before begin between both breadth by call cascade cascaded case cast catalog check close collate collation column commit condition connect connection constraint constraints constructor continue corresponding count create cross cube current current_date current_default_transform_group current_transform_group_for_type current_path current_role current_time current_timestamp current_user cursor cycle data day deallocate declare default deferrable deferred delete depth deref desc describe descriptor deterministic diagnostics disconnect distinct do domain drop dynamic each else elseif end end-exec equals escape except exception exec execute exists exit external fetch first for foreign found from free full function general get global go goto grant group grouping handle having hold hour identity if immediate in indicator initially inner inout input insert intersect into is isolation join key language last lateral leading leave left level like limit local localtime localtimestamp locator loop map match method minute modifies module month names natural nesting new next no none not of old on only open option or order ordinality out outer output overlaps pad parameter partial path prepare preserve primary prior privileges procedure public read reads recursive redo ref references referencing relative release repeat resignal restrict result return returns revoke right role rollback rollup routine row rows savepoint schema scroll search second section select session session_user set sets signal similar size some space specific specifictype sql sqlexception sqlstate sqlwarning start state static system_user table temporary then timezone_hour timezone_minute to trailing transaction translation treat trigger under undo union unique unnest until update usage user using value values view when whenever where while with without work write year zone';
const SQLTypesSet = new Set(SQLTypes.split(' '));
const SQLKeywordsSet = new Set(SQLKeywords.split(' '));

function builtInTypeSplit(
language: string,
keywords: string,
builtIn: string
): [string, string] {
/**
* This is needed because codemirror 5 mixes type and builtin keywords together
* This is needed because codemirror 5 mixes type and builtin keywords together.
* In our case, we want:
* - keywords: standard SQL keywords, from SQLKeywords

Check failure on line 27 in querybook/webapp/lib/codemirror/codemirror-language.ts

View workflow job for this annotation

GitHub Actions / nodetests

There must be no indentation
* - builtin: functions, operators, etc
* - type: data types
*
* returns [builtIn, Type] deduped
* returns [builtIn, Type]
*/

const settings = SettingsByLanguage[language];
const settings = getLanguageSetting(language);
const keywordsSet = new Set(keywords.split(' '));
const typesSet = settings.type.union(SQLTypesSet);
const builtInSet = new Set(builtIn.split(' '));

const builtInWithoutType = Array.from(builtInSet.difference(settings.type));
const nonStandardKeywordSet = keywordsSet.difference(SQLKeywordsSet);
const nonStandardKeywordAndBuiltInSet = nonStandardKeywordSet
.union(builtInSet)
.difference(typesSet);

return [builtInWithoutType.join(' '), Array.from(settings.type).join(' ')];
return [
Array.from(nonStandardKeywordAndBuiltInSet).join(' '),
Array.from(typesSet).join(' '),
];
}

const trinoKeywords =
Expand All @@ -30,6 +54,7 @@ const trinoBuiltin =

const [processedTrinoBuiltin, processedTrinoType] = builtInTypeSplit(
'trino',
trinoKeywords,
trinoBuiltin
);

Expand All @@ -41,7 +66,7 @@ const TrinoSQL = SQLDialect.define({
unquotedBitLiterals: true,
hashComments: false,
spaceAfterDashes: false,
keywords: trinoKeywords,
keywords: SQLKeywords,
builtin: processedTrinoBuiltin,
types: processedTrinoType,
});
Expand All @@ -53,6 +78,7 @@ const sparkSQLBuiltin =

const [processedSparkSQLBuiltin, processedSparkSQLType] = builtInTypeSplit(
'sparksql',
sparkSQLKeywords,
sparkSQLBuiltin
);

Expand All @@ -62,7 +88,7 @@ const SparkSQL = SQLDialect.define({
unquotedBitLiterals: true,
hashComments: false,
spaceAfterDashes: false,
keywords: sparkSQLKeywords,
keywords: SQLKeywords,
types: processedSparkSQLType,
builtin: processedSparkSQLBuiltin,
operatorChars: '*/+-%<>!=~&|^',
Expand Down
4 changes: 2 additions & 2 deletions querybook/webapp/lib/sql-helper/sql-setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export interface ILanguageSetting {
quoteChars?: [quoteStart: string, quoteEnd: string];
}

export const SQL_KEYWORDS =
const SQL_KEYWORDS =
'alter and as asc between by count create delete desc distinct drop from group having in insert into is join like not on or order select set table union update values where limit';

export const SettingsByLanguage: Record<string, ILanguageSetting> = {
const SettingsByLanguage: Record<string, ILanguageSetting> = {
hive: {
keywords: new Set(
// 2.2.0
Expand Down

0 comments on commit 70e1c6e

Please sign in to comment.