Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/flink auto complete #115

Merged
merged 10 commits into from
Jun 9, 2023
Merged

Conversation

HaydenOrz
Copy link
Collaborator

@HaydenOrz HaydenOrz commented Jun 2, 2023

简介

Flink SQL 支持自动补全 #13

主要变更

  • 新增 tablePathtablePathCreatedatabasePathdatabasePathCreatecatalogPath 规则,替代 uid
  • 分号匹配由 sqlStatements 移到 sqlStatement 规则下,方便做代码切割
  • 安装 antlr4-c3 依赖
  • 优化 basicParser 类内部的ts 类型
  • 重构 basicParser 使其在连续 parse 相同的 input 时,只会创建一次 parser,降低性能开销
  • basicParser 支持自动补全,并新增对应的抽象成员和抽象方法
  • Flink SQL 实现自动补全相关抽象方法,支持自动补全
  • 添加 FLink SQL 自动补全相关测试 case
  • 添加 splitSQL 功能,新增 FlinkSQLSplitListener
  • 优化 FlinkSQL 自动补全的性能,在 c3 运行前,先切分sql,找到 caretPosition 所在的 sql 语句,c3 在收集候选项时,只会分析某一个语句对应的 ParserRuleContext

自动补全功能

自动补全功能主要依靠 antlr4-c3 实现,使用 antlr4-c3 有两个关键点

  • caretTokenIndex -- 需要补全的具体位置对应的Token索引
  • context -- 通过antlr4 构建的 ParserTee 的某个节点

caretTokenIndex

通常来说需要补全的位置(下文中统称“插入位置”)一般用行和列来表示,所以需要把行和列转化为 Token 索引,主体实现思路是先获取到所有的token,然后寻找出插入位置对应的行和列被哪一个token包含

context

参考 antlr4-c3 文档 https://github.com/mike-lischke/antlr4-c3#constraining-the-search-space
出于性能问题考虑,我们需要找到一个子树,来减少 antlr4-c3 的解析/查找时间,这里采取的方案是先将 sql 按语句切分,并找到插入位置位于哪一个语句中, antlr4-c3 将根据这个语句对应的解析树来解析/查找
但是还需要考虑另一个问题,如果输入的 sql 文本中有语法错误,那么从语法错误的位置直到sql文本的末尾尾,都将无法构建为 ruleContext,如下图所示:

image

此时,采取的策略是,把 从最后一条语句开始(包括最后一条语句)直到sql文本末尾 对应的所有文本,重新解析,形成一颗新的解析树,antlr4-c3 将根据这个新的解析树来解析/查找。
另外,即使语法错误出现在 sql 文本的开头,并且 sql 文本较大,也不会太影响性能,因为此时 antlr4 的解析结果都是如上图中的散状结构,并未形成完整的树结构。

@HaydenOrz HaydenOrz added the enhancement Enhance existing feature label Jun 2, 2023
@HaydenOrz HaydenOrz requested a review from wewoor June 2, 2023 08:56
@HaydenOrz HaydenOrz self-assigned this Jun 2, 2023
@HaydenOrz HaydenOrz force-pushed the feat/flink_auto_complete branch 3 times, most recently from c8edbc1 to a996ec8 Compare June 7, 2023 01:59
@melin
Copy link

melin commented Jun 8, 2023

不同功能建议拆分为多个pr

@HaydenOrz HaydenOrz marked this pull request as ready for review June 8, 2023 09:02
@HaydenOrz HaydenOrz force-pushed the feat/flink_auto_complete branch 2 times, most recently from 267f6e0 to e69e204 Compare June 8, 2023 11:15
Copy link
Contributor

@wewoor wewoor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@wewoor wewoor merged commit 1b02ff5 into DTStack:main Jun 9, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhance existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants