Skip to content

Commit

Permalink
docs: add docs for semantic context
Browse files Browse the repository at this point in the history
  • Loading branch information
JackWang032 committed Jan 3, 2025
1 parent fcdfc8a commit 7c11ea2
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
49 changes: 49 additions & 0 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,55 @@ console.log(sqlSlices)

行列号信息不是必传的,如果传了行列号信息,那么收集到的实体中,如果实体位于对应行列号所在的语句下,那么实体的所属的语句对象上会带有 `isContainCaret` 标识,这在与自动补全功能结合时,可以帮助你快速筛选出需要的实体信息。


### 获取语义上下文信息
调用 SQL 实例上的 `getSemanticContextAtCaretPosition` 方法,传入 sql 文本和指定位置的行列号, 例如:
```typescript
import { HiveSQL } from 'dt-sql-parser';
const hive = new HiveSQL();
const sql = 'SELECT * FROM tb;';
const pos = { lineNumber: 1, column: 18 }; // 'tb;' 的后面
const semanticContext = hive.getSemanticContextAtCaretPosition(sql, pos);
console.log(semanticContext);
```

*输出*

```typescript
/*
{
isStatementBeginning: true,
}
*/
```

目前能收集到的语义上下文信息如下,如果有更多的需求,欢迎提[issue](https://github.com/DTStack/dt-sql-parser/issues)
- `isStatementBeginning` 当前输入位置是否为一条语句的开头

默认情况下,`isStatementBeginning` 的收集策略为`SqlSplitStrategy.STRICT`

有两种可选策略:
- `SqlSplitStrategy.STRICT` 严格策略, 仅以语句分隔符`;`作为上一条语句结束的标识
- `SqlSplitStrategy.LOOSE` 宽松策略, 以语法解析树为基础分割SQL

两种策略的差异:
如输入SQL
```sql
CREATE TABLE tb (id INT)
SELECT
```
CREATE语句后未添加分号,那么当获取SELECT后的语义上下文时,
`SqlSplitStrategy.STRICT`策略下`isStatementBeginning``false`, 因为CREATE语句未以分号结尾,那么会被认为这条语句尚未结束;
`SqlSplitStrategy.LOOSE`策略下`isStatementBeginning``true`, 因为语法解析树中这条SQL被拆分成了CREATE独立语句与SELECT独立语句。

可以通过第三个`options`参数设置策略:
```typescript
hive.getSemanticContextAtCaretPosition(sql, pos, { splitSqlStrategy: SqlSplitStrategy.LOOSE });
```

### 其他 API

- `createLexer` 创建一个 Antlr4 Lexer 实例并返回;
Expand Down
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,57 @@ Call the `getAllEntities` method on the SQL instance, and pass in the sql text a
Position is not required, if the position is passed, then in the collected entities, if the entity is located under the statement where the corresponding position is located, then the statement object to which the entity belongs will be marked with `isContainCaret`, which can help you quickly filter out the required entities when combined with the code completion function.
### Get semantic context information
Call the `getSemanticContextAtCaretPosition` method on the SQL instance, passing in the sql text and the line and column numbers at the specified position, for example:
```typescript
import { HiveSQL } from 'dt-sql-parser';

const hive = new HiveSQL();
const sql = 'SELECT * FROM tb;';
const pos = { lineNumber: 1, column: 18 }; // after 'tb;'
const semanticContext = hive.getSemanticContextAtCaretPosition(sql, pos);

console.log(semanticContext);
```
*output*
```typescript
/*
{
isStatementBeginning: true,
}
*/
```
Currently, the semantic context information that can be collected is as follows. If there are more requirements, please submit an [issue](https://github.com/DTStack/dt-sql-parser/issues).
- `isStatementBeginning` Whether the current input position is the beginning of a statement
The **default strategy** for `isStatementBeginning` is `SqlSplitStrategy.STRICT`
There are two optional strategies:
- `SqlSplitStrategy.STRICT` Strict strategy, only the statement delimiter `;` is used as the identifier for the end of the previous statement
- `SqlSplitStrategy.LOOSE` Loose strategy, based on the syntax parsing tree to split SQL
The difference between the two strategies:
For example, if the input SQL is:
```sql
CREATE TABLE tb (id INT)

SELECT
```
In the `SqlSplitStrategy.STRICT` strategy, `isStatementBeginning` is `false`, because the CREATE statement is not terminated by a semicolon.
In the `SqlSplitStrategy.LOOSE` strategy, `isStatementBeginning` is `true`, because the syntax parsing tree splits the SQL into two independent statements: CREATE and SELECT.
You can set the strategy through the third `options` parameter:
```typescript
hive.getSemanticContextAtCaretPosition(sql, pos, { splitSqlStrategy: SqlSplitStrategy.LOOSE });
```
### Other API
- `createLexer` Create an instance of Antlr4 Lexer and return it;
Expand Down

0 comments on commit 7c11ea2

Please sign in to comment.