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

[Bug]: completionService 中的 entities 未能获取到内容 #405

Closed
rick-chou opened this issue Mar 11, 2025 · 10 comments
Closed

[Bug]: completionService 中的 entities 未能获取到内容 #405

rick-chou opened this issue Mar 11, 2025 · 10 comments
Labels
bug Something isn't working

Comments

@rick-chou
Copy link

rick-chou commented Mar 11, 2025

Version

4.1.1

Which SQL?

Spark

SQL content

SELECT c FROM db1.customers AS c
    JOIN orders o ON c.id = o.customer_id
    LEFT JOIN log.deliveries d ON o.id = d.

JavaScript/TypeScript code

const completionService: CompletionService = function (
  model,
  position,
  completionContext,
  suggestions, // syntax context info at caretPosition
  entities // tables, columns in the syntax context of the editor text
) {
  return new Promise((resolve, reject) => {
    if (!suggestions) {
      return resolve([]);
    }

    const languageId = model.getLanguageId() as SQLType;
    const parser = new Parser[languageId]();
    const sqlFragments = parser.splitSQLByStatement(model.getValue());
    console.log("sqlFragments", sqlFragments); // --> []
    console.log("entities", entities); // --> []
  });
};

setupLanguageFeatures(LanguageIdEnum.SPARK, {
  completionItems: {
    enable: true,
    triggerCharacters: [" ", "."],
    completionService,
  },
});

What happened?

我想要实现 column 的代码提示
但是当输入例如 . 的时候 参数entities 就为空了
我需要在 entities 中获取 alias / table 等参数来查询可选的 columns

SELECT c FROM db1.customers AS c
    JOIN orders o ON c.id = o.customer_id
    LEFT JOIN log.deliveries d ON o.id = d.

Relevant log output

@JackWang032
Copy link
Collaborator

看上去是因为加的点导致 antlr4 的 errorStrategy 没有解析出期望的语法树
Image

@JackWang032 JackWang032 added the bug Something isn't working label Mar 11, 2025
@rick-chou
Copy link
Author

是的 但是在其他位置加 . 又能够正常解析

例如这里

SELECT c. FROM db1.customers AS c

@JackWang032
Copy link
Collaborator

是的 但是在其他位置加 . 又能够正常解析

例如这里

SELECT c. FROM db1.customers AS c

单token删除单token添加的错误恢复机制使你这段语句的解析树是完整的

Image

不过光依靠antlr4内置的错误恢复机制随机性太大,不同SQL有不同表现,我们在考虑针对"."的场景手动往后补上一个虚拟字符以使其能正常解析

@rick-chou
Copy link
Author

借个楼 顺便问一下有 api 可以获取光标所在位置 context 的 database / table 吗

例如

SELECT c FROM db1.customers AS c
    JOIN orders o ON c.id = o.customer_id
    LEFT JOIN log.deliveries d ON o.id = d.id

当光标在各自context的时候 能够正确拿到 database / table
@JackWang032 🙏🙏🙏

@JackWang032
Copy link
Collaborator

通过entityContextType过滤出 database / table 实体, 然后使用 entity.position 结合当前光标位置可以得出具体的信息

@liuxy0551
Copy link
Collaborator

Image

可以参考单测中 getAllEntities 的使用,过滤出需要的 EntityContextType,此时 entities 中是包含 position 的。

@rick-chou
Copy link
Author

当有 USE 语句的时候, suggestions.syntax 不同

有 USE 语句的时候

USE db;

SELECT * FROM db.
Image

没有 USE 语句的时候

SELECT * FROM db.
Image

导致很难有一种统一的方法去判断当前位置 应该是 table suggestion了
@JackWang032

@JackWang032
Copy link
Collaborator

没复现出问题

Image

@rick-chou
Copy link
Author

没复现出问题

Image

好像我也复现不出来了 🤣

@mumiao
Copy link
Collaborator

mumiao commented Mar 28, 2025

done

@mumiao mumiao closed this as completed Mar 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants