Skip to content
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
12 changes: 12 additions & 0 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,18 @@ console.log(sqlSlices)
[ 'CATALOG', 'FUNCTION', 'TEMPORARY', 'VIEW', 'DATABASE', 'TABLE' ]
*/
```

可以通过可选的 `keywordFilter` 移除不需要的关键字候选项。返回 `true` 保留关键字,返回 `false` 移除关键字。

```typescript
const sql = 'SELECT * FROM tb ';
const pos = { lineNumber: 1, column: sql.length + 1 };
const excludedKeywords = new Set(['WHERE', 'ORDER BY']);
const keywords = flink.getSuggestionAtCaretPosition(sql, pos, {
keywordFilter: (keyword) => !excludedKeywords.has(keyword),
})?.keywords;
```

+ **获取语法相关自动补全信息**
```typescript
import { FlinkSQL } from 'dt-sql-parser';
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,18 @@ Call the `getAllEntities` method on the SQL instance, pass the SQL content and t
[ 'CATALOG', 'FUNCTION', 'TEMPORARY', 'VIEW', 'DATABASE', 'TABLE' ]
*/
```

Use the optional `keywordFilter` to remove unwanted keyword candidates. Return `true` to keep a keyword and `false` to remove it.

```typescript
const sql = 'SELECT * FROM tb ';
const pos = { lineNumber: 1, column: sql.length + 1 };
const excludedKeywords = new Set(['WHERE', 'ORDER BY']);
const keywords = flink.getSuggestionAtCaretPosition(sql, pos, {
keywordFilter: (keyword) => !excludedKeywords.has(keyword),
})?.keywords;
```

+ **Obtaining information related to grammar completion**
```javascript
import { FlinkSQL } from 'dt-sql-parser';
Expand Down
15 changes: 8 additions & 7 deletions src/grammar/mysql/MySqlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ orderByClause
;

orderByExpression
: expression order=(KW_ASC | KW_DESC)?
: (expression | columnNamePathAllowEmpty) order=(KW_ASC | KW_DESC)?
;

tableSources
Expand Down Expand Up @@ -1258,15 +1258,15 @@ groupByClause
;

havingClause
: KW_HAVING havingExpr=expression
: KW_HAVING (havingExpr=expression | columnNamePathAllowEmpty)
;

windowClause
: KW_WINDOW windowName KW_AS '(' windowSpec ')' (',' windowName KW_AS '(' windowSpec ')')*
;

groupByItem
: expression order=(KW_ASC | KW_DESC)?
: (expression | columnNamePathAllowEmpty) order=(KW_ASC | KW_DESC)?
;

limitClause
Expand Down Expand Up @@ -2431,13 +2431,14 @@ columnName
;

columnNamePath
: uid (dottedId dottedId?)?
| .? dottedId dottedId?
: uid (dottedId dottedId?)? # columnNamePath_default
| .? dottedId dottedId? # columnNamePath_dotted
| uid DOT {this.shouldMatchEmpty()}? emptyColumn # columnNamePath_dot_empty
;

columnNamePathAllowEmpty
: {this.shouldMatchEmpty()}? emptyColumn
| uid (dottedId dottedId?)?
: columnNamePath
| {this.shouldMatchEmpty()}? emptyColumn
;

tableSpaceNameCreate
Expand Down
19 changes: 16 additions & 3 deletions src/grammar/postgresql/PostgreSqlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ options {
superClass=SQLParserBase;
}

@parser::members {
isFollowedByInto(): boolean {
return this.tokenStream.LA(1) === PostgreSqlParser.KW_INTO;
}

isFollowedByEnd(): boolean {
const la = this.tokenStream.LA(1);
return la === PostgreSqlParser.SEMI || la === PostgreSqlParser.EOF;
}
}

@header {
import { SQLParserBase } from '../SQLParserBase';
}
Expand Down Expand Up @@ -2629,7 +2640,8 @@ optIndirection
;

targetList
: targetEl (COMMA targetEl)*
: {this.isFollowedByInto() || this.isFollowedByEnd()}?
| targetEl (COMMA targetEl)*
;

targetEl
Expand Down Expand Up @@ -2735,7 +2747,8 @@ columnName
;

columnNamePath
: colId optIndirection
: colId optIndirection # columnNamePath_default
| colId DOT {this.shouldMatchEmpty()}? emptyColumn # columnNamePath_dot_empty
;

columnNameCreate
Expand Down Expand Up @@ -3628,5 +3641,5 @@ anyIdentifier
;

sqlExpression
: targetList? intoClause? fromClause? whereClause? groupClause? havingClause? windowClause?
: targetList intoClause? fromClause? whereClause? groupClause? havingClause? windowClause?
;
21 changes: 20 additions & 1 deletion src/grammar/trino/TrinoSql.g4
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ options {
superClass=SQLParserBase;
}

@parser::members {
notEntityCollecting(): boolean {
return !this.entityCollecting;
}
}

@header {
import { SQLParserBase } from '../SQLParserBase';
}
Expand Down Expand Up @@ -380,10 +386,19 @@ joinType
;

joinCriteria
: KW_ON booleanExpression
: KW_ON (joinColumnEquality | {this.notEntityCollecting()}? booleanExpression)
| KW_USING '(' identifier (',' identifier)* ')'
;

joinColumnEquality
: left=joinColumnReference EQ right=joinColumnReference
;

joinColumnReference
: identifier DOT {this.entityCollecting}? emptyColumn
| columnName
;

sampledRelation
: patternRecognition (KW_TABLESAMPLE sampleType '(' percentage=expression ')')?
;
Expand Down Expand Up @@ -1039,6 +1054,10 @@ columnRef
| {this.shouldMatchEmpty()}?
;

emptyColumn
:
;

columnName
: qualifiedName
;
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ export { EntityContextType } from './parser/common/types';

export { StmtContextType } from './parser/common/entityCollector';

export type { CaretPosition, Suggestions, SyntaxSuggestion } from './parser/common/types';
export type {
CaretPosition,
SuggestionOptions,
Suggestions,
SyntaxSuggestion,
} from './parser/common/types';

export type { WordRange, TextSlice } from './parser/common/textAndWord';

Expand Down
2 changes: 1 addition & 1 deletion src/lib/mysql/MySqlParser.interp

Large diffs are not rendered by default.

Loading
Loading