Skip to content
Draft
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
49 changes: 49 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,55 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [4.4.0](https://github.com/DTStack/dt-sql-parser/compare/v4.4.0-beta.0...v4.4.0) (2025-11-26)

## [4.4.0-beta.0](https://github.com/DTStack/dt-sql-parser/compare/v4.3.1...v4.4.0-beta.0) (2025-11-18)


### Features

* mark as entityCollecting in getAllEntities context to allow empty column ([ba6fbbb](https://github.com/DTStack/dt-sql-parser/commit/ba6fbbbe90bee79a7bba22350c3d50d6c9205b0d))
* remove noReserved keywords in completions ([cf1957e](https://github.com/DTStack/dt-sql-parser/commit/cf1957e66896fbcb587f6038242e8f174b7045ff))


### Bug Fixes

* [#438](https://github.com/DTStack/dt-sql-parser/issues/438) syntaxContextType not duplicate ([4705620](https://github.com/DTStack/dt-sql-parser/commit/47056209150ef6aaade7c612700b7a289c279208))
* **flink:** [#442](https://github.com/DTStack/dt-sql-parser/issues/442) fix flink's insert values() can't support function problem ([98ab7d4](https://github.com/DTStack/dt-sql-parser/commit/98ab7d459b189b7ef07cc1bf820524594a0cc78c))
* **postgresql:** [#432](https://github.com/DTStack/dt-sql-parser/issues/432) remove error rule ([3684ae7](https://github.com/DTStack/dt-sql-parser/commit/3684ae71e92cefe4bac18174b757e162d3f89457))

### [4.3.1](https://github.com/DTStack/dt-sql-parser/compare/v4.3.0...v4.3.1) (2025-06-05)


### Bug Fixes

* **common:** [#424](https://github.com/DTStack/dt-sql-parser/issues/424) allTokens slice when caretTokenIndex use tokenIndexOffset ([#426](https://github.com/DTStack/dt-sql-parser/issues/426)) ([616dc11](https://github.com/DTStack/dt-sql-parser/commit/616dc1126d742bcdd902752ddaedab9b352a8927))

## [4.3.0](https://github.com/DTStack/dt-sql-parser/compare/v4.2.0...v4.3.0) (2025-05-16)


### Features

* [#410](https://github.com/DTStack/dt-sql-parser/issues/410) optimize processCandidates tokenIndexOffset ([#411](https://github.com/DTStack/dt-sql-parser/issues/411)) ([5fccaa1](https://github.com/DTStack/dt-sql-parser/commit/5fccaa1ac6724419e3f4799b4e4f8fcdba8c049d))
* **all sql:** add all sql expression column ([#358](https://github.com/DTStack/dt-sql-parser/issues/358)) ([26219b8](https://github.com/DTStack/dt-sql-parser/commit/26219b8eb2086f560670d2a72469f1f6cb77fb44))
* collect entity's attribute([#333](https://github.com/DTStack/dt-sql-parser/issues/333)) ([ab60b14](https://github.com/DTStack/dt-sql-parser/commit/ab60b144bcdb74383c3f0064531282d89bf46f75))
* complete after error syntax ([#334](https://github.com/DTStack/dt-sql-parser/issues/334)) ([99b01e5](https://github.com/DTStack/dt-sql-parser/commit/99b01e5310597dc373c3d1589c1dcca2094c22c2))
* **flinksql:** collect comment, type attribute for entity ([#319](https://github.com/DTStack/dt-sql-parser/issues/319)) ([1f1dd19](https://github.com/DTStack/dt-sql-parser/commit/1f1dd19ff1ef3b062ffbb99f60e8539b41ad1401)), closes [#305](https://github.com/DTStack/dt-sql-parser/issues/305)
* improve errorListener msg ([#281](https://github.com/DTStack/dt-sql-parser/issues/281)) ([ff49c91](https://github.com/DTStack/dt-sql-parser/commit/ff49c91623ae05f389bb0d9205bc485acbf7e0fb))
* **merge conflict:** merge main solve conflict ([f17f19f](https://github.com/DTStack/dt-sql-parser/commit/f17f19f57a546aad9b9c8440ad6cfb4721f93f5c))
* support semantic context of isNewStatement ([#361](https://github.com/DTStack/dt-sql-parser/issues/361)) ([042477d](https://github.com/DTStack/dt-sql-parser/commit/042477d363e7a3f7cdb7adb0a49625346506b4c5))
* unify variables in lexer ([#366](https://github.com/DTStack/dt-sql-parser/issues/366)) ([07ff5dc](https://github.com/DTStack/dt-sql-parser/commit/07ff5dc5a1455efda434f71536b4fef38e45b5f0))


### Bug Fixes

* [#362](https://github.com/DTStack/dt-sql-parser/issues/362) set hiveVar value ([#369](https://github.com/DTStack/dt-sql-parser/issues/369)) ([ba9e3d6](https://github.com/DTStack/dt-sql-parser/commit/ba9e3d67cd43ccf07059e369cad7157931723221))
* [#371](https://github.com/DTStack/dt-sql-parser/issues/371) export EntityContext types ([#372](https://github.com/DTStack/dt-sql-parser/issues/372)) ([c0a2854](https://github.com/DTStack/dt-sql-parser/commit/c0a28546a0dfa3b42e7a94e81db8b833a62d3f9e))
* **flink:** fix flinksql syntax error about ROW and function using ([#383](https://github.com/DTStack/dt-sql-parser/issues/383)) ([188d42d](https://github.com/DTStack/dt-sql-parser/commit/188d42da72ad81d5e1f96c453e9e8238692cdb9f))
* **flink:** resolve conflicts ([ec65d4e](https://github.com/DTStack/dt-sql-parser/commit/ec65d4e21ff0a4ca8e2997b42ccc887b3bba763b))
* minimum collect candidates boundary to fix parse performance ([#378](https://github.com/DTStack/dt-sql-parser/issues/378)) ([64ed7e4](https://github.com/DTStack/dt-sql-parser/commit/64ed7e4cd30ed12238390d113f202d4122ef9d73))
* spell check ([#337](https://github.com/DTStack/dt-sql-parser/issues/337)) ([fb55c99](https://github.com/DTStack/dt-sql-parser/commit/fb55c999542a11b197d33b35c25bd5801eff6fa7))

## [4.2.0](https://github.com/DTStack/dt-sql-parser/compare/v4.1.1...v4.2.0) (2025-05-09)


Expand Down
175 changes: 171 additions & 4 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ console.log(sqlSlices)
{
entityContextType: 'table',
text: 'tb',
declareType: 0,
isAccessible: true,
position: {
line: 1,
startIndex: 14,
Expand All @@ -346,16 +348,181 @@ console.log(sqlSlices)
},
relatedEntities: null,
columns: null,
isAlias: false,
origin: null,
alias: null
}
_alias: null,
_comment: null
},
{
entityContextType: 'queryResult',
text: '*',
declareType: undefined,
isAccessible: null,
position: {
line: 1,
startIndex: 7,
endIndex: 7,
startColumn: 8,
endColumn: 9
},
belongStmt: {
stmtContextType: 'selectStmt',
position: [Object],
rootStmt: [Object],
parentStmt: [Object],
isContainCaret: true
},
relatedEntities: [
// relate to table entity
],
columns: [
// relate to `*` column entity
],
_alias: null,
_comment: null,
},
]
*/
```

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

在子查询嵌套的情况下,`isContainCaret` 可能不足以筛选出需要的实体,例如对于SQL: `SELECT id FROM t1 LEFT JOIN (SELECT id, name FROM t2) AS t3 ON t1.id = t3.id`, 当我们光标处在内部查询`t3`派生表内时, 期望提供`t2`表下的字段补全, 但由于`t1`与`t2`的`isContainCaret`都为`true`, 无法更细节的区分出可用的表实体。

所以, 针对`entityContextType`为`table`的实体类型, 收集到的实体上会带有`isAccessible`标识, 用于表示该实体是否可访问。`isAccessible`内部利用作用域深度来判断, 当实体的语句作用域深度与光标所在语句的作用域深度相同且`isContainCaret`为`true`时, 则认为该实体可访问(当然这种判断方法并非绝对,但能排除大多数无关实体)。

#### 实体额外信息说明

**别名(Alias)信息**

当实体具有别名时,会在实体对象中包含 `_alias` 字段:
- `_alias`: 别名的详细信息,包含文本内容和位置信息

```typescript
// 示例:SELECT u.name FROM users AS u
{
entityContextType: 'table',
text: 'users',
_alias: { // 表的别名信息
text: 'u',
startIndex: 29,
endIndex: 29,
startColumn: 30,
endColumn: 31,
line: 1
}
}

// 示例:SELECT name AS username FROM users
{
entityContextType: 'column',
text: 'name',
_alias: { // 列的别名信息
text: 'username',
startIndex: 15,
endIndex: 22,
startColumn: 16,
endColumn: 24,
line: 1
}
}
```

**声明类型(DeclareType)**

`declareType` 字段用于标识实体的声明方式,不同类型的实体有不同的声明类型:

**表实体的声明类型(TableDeclareType):**
- `LITERAL`:字面量表名,如 `SELECT * FROM users`
- `EXPRESSION`:表达式定义的表,如子查询 `SELECT * FROM (SELECT * FROM users) AS t`

**列实体的声明类型(ColumnDeclareType):**
- `LITERAL`:字面量列名,如 `SELECT id, name FROM users`
- `ALL`:通配符语法,如 `SELECT users.* FROM users`
- `EXPRESSION`:复杂表达式,如子查询、CASE语句、函数调用等

```typescript
// 示例:不同 declareType 的示例
// 1. 字面量列
{
entityContextType: 'column',
text: 'name',
declareType: ColumnDeclareType.LITERAL,
}

// 2. 通配符列
{
entityContextType: 'column',
text: 'users.*',
declareType: ColumnDeclareType.ALL,
}

// 3. 表达式列
{
entityContextType: 'column',
text: 'CASE WHEN age > 18 THEN "adult" ELSE "minor" END',
declareType: ColumnDeclareType.EXPRESSION,
}
```


**其他元信息字段**

**注释信息(Comment)**
- `_comment`:实体的注释信息,主要用于 CREATE 语句中的列注释或表注释

```typescript
// 示例:CREATE TABLE users (id INT COMMENT 'USERID', name VARCHAR(50) COMMENT 'USERNAME')
{
entityContextType: 'column',
text: 'id',
_comment: {
text: "'USERID'",
startIndex: 35,
endIndex: 42,
startColumn: 36,
endColumn: 44,
line: 1
},
_colType: {
text: 'INT',
startIndex: 23,
endIndex: 42,
startColumn: 24,
endColumn: 44,
line: 1
}
}
```

**列类型信息(Column Type)**
- `_colType`:列的数据类型信息,仅用于建表语句中的列实体,包含类型名称和位置信息

```typescript
// 示例:CREATE TABLE users (name VARCHAR(50) NOT NULL)
{
entityContextType: 'columnCreate',
text: 'name',
_colType: {
text: 'VARCHAR(50)',
startIndex: 25,
endIndex: 35,
startColumn: 26,
endColumn: 37,
line: 1
}
}
```

**关联信息字段**
- `relatedEntities`:与当前实体相关的其他实体列表,例如查询结果实体关联的表实体
- `columns`:包含的字段列表

一个简单的实体关联实例:

```sql
CREATE TABLE tb1 AS SELECT id FROM tb2;
```

![relation-image](./docs/images/relation.png)

### 获取语义上下文信息
调用 SQL 实例上的 `getSemanticContextAtCaretPosition` 方法,传入 sql 文本和指定位置的行列号, 例如:
Expand Down
Loading
Loading