Calcite 语法拓展
2023-04-18 14:55:16 时间
下面我们以一个具体例子来说怎么做SQL语法拓展,比如我们需要支持语法
USE FUNCTION func_name [ WITH (name = '', version = '') ]
1. 创建maven项目
2. 复制模板文件
从Flink源码flink-table/flink-sql-parser/src/main/codegen文件夹copy到自己项目的src/main目录下
3. 拓展SQL语法
主要分为下面三步
3.1 在 codegen/includes/parserImpls.ftl 定义解析use function 的规则,包含了方法名及对应的规则。
/**
* USE FUNCTION func_name [ WITH (name = '', version = '') ]
*/
SqlCall SqlUseFunction() :
{
SqlParserPos pos;
SqlIdentifier funcName;
SqlNodeList funcProps = null;
}
{
<USE>
{
// Token位置
pos = getPos();
}
<FUNCTION>
funcName = CompoundIdentifier()
[ <WITH> funcProps = TableProperties() ]
{
return new SqlUseFunction(pos, funcName, funcProps);
}
}
3.2 拓展 SqlCall
规则匹配成功返回一个SqlUseFunction节点,作为解析树中的SqlNode。
public class SqlUseFunction extends SqlCall {
private static final SqlSpecialOperator OPERATOR = new SqlSpecialOperator("USE FUNCTION",
SqlKind.OTHER_FUNCTION);
private final SqlIdentifier funcName;
private final SqlNodeList funcProps;
/**
* SqlUseFunction constructor.
*
* @param pos sql define location
* @param funcName function name
* @param funcProps function property
* */
public SqlUseFunction(SqlParserPos pos, SqlIdentifier funcName, SqlNodeList funcProps) {
super(pos);
this.funcName = funcName;
this.funcProps = funcProps;
}
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.keyword("USE FUNCTION");
funcName.unparse(writer, leftPrec, rightPrec);
if (funcProps != null) {
writer.keyword("WITH");
SqlWriter.Frame frame = writer.startList("(", ")");
for (SqlNode c : funcProps) {
writer.sep(",");
c.unparse(writer, 0, 0);
}
writer.endList(frame);
}
}
@Override
public SqlOperator getOperator() {
return OPERATOR;
}
@Override
public List<SqlNode> getOperandList() {
return ImmutableNullableList.of(funcName, funcProps);
}
}
3.3 将添加的规则,加入配置文件 codegen/data/Parser.tdd
- imports增加SqlUseFunction类。
- statementParserMethods增加定义的规则方法。在Parser中使用新规则。
imports: [
"org.apache.flink.sql.extended.SqlUseFunction"
]
# List of methods for parsing custom SQL statements.
# Return type of method implementation should be 'SqlNode'.
# Example: SqlShowDatabases(), SqlShowTables().
statementParserMethods: [
"SqlUseFunction()"
]
相关文章
- 【技术种草】cdn+轻量服务器+hugo=让博客“云原生”一下
- CLB运维&运营最佳实践 ---访问日志大洞察
- vnc方式登陆服务器
- 轻松学排序算法:眼睛直观感受几种常用排序算法
- 十二个经典的大数据项目
- 为什么使用 CDN 内容分发网络?
- 大数据——大数据默认端口号列表
- Weld 1.1.5.Final,JSR-299 的框架
- JavaFX 2012:彻底开源
- 提升as3程序性能的十大要点
- 通过凸面几何学进行独立于边际的在线多类学习
- 利用行动影响的规律性和部分已知的模型进行离线强化学习
- ModelLight:基于模型的交通信号控制的元强化学习
- 浅谈Visual Source Safe项目分支
- 基于先验知识的递归卡尔曼滤波的代理人联合状态和输入估计
- 结合网络结构和非线性恢复来提高声誉评估的性能
- 最佳实践丨云开发CloudBase多环境管理实践
- TimeVAE:用于生成多变量时间序列的变异自动编码器
- 具有线性阈值激活的神经网络:结构和算法
- 内网渗透之横向移动 -- 从域外向域内进行密码喷洒攻击