Apache Calcite在SQL字符串中查找选定的列 [英] Apache Calcite to Find Selected Columns in an SQL String

查看:295
本文介绍了Apache Calcite在SQL字符串中查找选定的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用例,我想知道在SQL字符串中选择的列,例如,如果SQL像这样:
SELECT name, age*5 as intelligence FROM bla WHERE bla=bla
然后,在解析了上面的String之后,我只希望输出为:name, intelligence.
首先,可以通过方解石吗?
也欢迎任何其他选择.

I have a use case where I want to know the columns which have been selected in an SQL string.For instance, if the SQL is like this:
SELECT name, age*5 as intelligence FROM bla WHERE bla=bla
Then, after parsing the above String, I just want the output to be: name, intelligence.
Firstly, is it possible through Calcite?
Any other option is also welcome.

PS:在数据库上实际运行查询之前,我想知道这一点.

PS: I want to know this before actually running the query on database.

推荐答案

这对于方解石绝对可行.您首先需要创建SqlParser的实例并解析查询:

This is definitely doable with Calcite. You'll want to start by creating an instance of SqlParser and parsing the query:

SqlParser parser = SqlParser.create(query)
SqlNode parsed = parser.parseQuery()

从那里开始,实现

From there, you'll probably have the most success implementing the SqlVisitor interface. You'll want to first find a SqlSelect instance and then visit each expression being selected by calling visit on each element of getSelectList.

从那里开始,如何进行将取决于您要支持的表达式的复杂性.但是,递归访问所有SqlCall节点及其操作数,然后收集您看到的任何SqlIdentifier值,可能就足够了.

From there, how you proceed will depend on the complexity of expressions you want to support. However, it's probably sufficient to recursively visit all SqlCall nodes and their operands and then collect any SqlIdentifier values that you see.

这篇关于Apache Calcite在SQL字符串中查找选定的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆