Apache Calcite 实现方言转换的代码


Posted in Servers onApril 24, 2021

定义

Calcite能够通过解析Sql为SqlNode,再将SqlNode转化为特定数据库的方言的形式实现Sql的统一。

实现

在Calcite中实现方言转换的主要类是SqlDialect基类,其具体的变量含义如下:

public class SqlDialect {

BUILT_IN_OPERATORS_LIST: 支持的内置定义函数或者运算符(例如:abs and..)

// 列 表的标识符
String identifierQuoteString:    标识符的开始符号
String identifierEndQuoteString: 标识符的结束符号
String identifierEscapedQuote: (暂时没有弄明白这个在做什么,像是字符串中的转义符?)

// 常量的标识符
String literalQuoteString:    常量的开始符号
String literalEndQuoteString: 常量的结束符号
String literalEscapedQuote:(暂时没有弄明白这个在做什么,像是字符串中的转义符?)

DatabaseProduct databaseProduct: 所属的数据库产品
NullCollation nullCollation: 在进行排序查询式,空值的返回顺序
RelDataTypeSystem dataTypeSystem: 数据类型

// 和解析相关
Casing unquotedCasing: 大小写转换
Casing quotedCasing: 大小写转换
boolean caseSensitive: 是否大小写敏感(列名 表明 函数名等)
}
// 方法区(不同的数据源根据细节的不同实现自定义的复写方法)
allowsAs
configureParser
configureParser
containsNonAscii
create
defaultNullDirection
emptyContext
emulateJoinTypeForCrossJoin
emulateNullDirection
emulateNullDirectionWithIsNull
getCalendarPolicy
getCastSpec
getConformance
getDatabaseProduct
getNullCollation
getProduct
getQuotedCasing
getQuoting
getSingleRowTableName
getTypeSystem
getUnquotedCasing
hasImplicitTableAlias
identifierNeedsQuote
isCaseSensitive
quoteIdentifier
quoteIdentifier
quoteIdentifier
quoteStringLiteral
quoteStringLiteral
quoteStringLiteralUnicode
quoteTimestampLiteral
requiresAliasForFromItems
rewriteSingleValueExpr
supportsAggregateFunction
supportsAliasedValues
supportsCharSet
supportsDataType
supportsFunction
supportsGroupByWithCube
supportsGroupByWithRollup
supportsImplicitTypeCoercion
supportsNestedAggregations
supportsOffsetFetch
supportsWindowFunctions
unparseCall
unparseDateTimeLiteral
unparseFetchUsingAnsi
unparseFetchUsingLimit
unparseLimit
unparseOffset
unparseOffsetFetch
unparseSqlDatetimeArithmetic
unparseSqlIntervalLiteral
unparseSqlIntervalQualifier
unparseTopN
unquoteStringLiteral

使用方式Demo

/** Returns SqlNode for type in "cast(column as type)", which might be
  * different between databases by type name, precision etc.
  *
  * <p>If this method returns null, the cast will be omitted. In the default
  * implementation, this is the case for the NULL type, and therefore
  * {@code CAST(NULL AS <nulltype>)} is rendered as {@code NULL}. */
  public SqlNode getCastSpec(RelDataType type)

  这个方法就可以根据具体的数据源的数据类型进行转换,例如:

  @Override public SqlNode getCastSpec(RelDataType type) {
    switch (type.getSqlTypeName()) {
    case VARCHAR:
      // MySQL doesn't have a VARCHAR type, only CHAR.
      int vcMaxPrecision = this.getTypeSystem().getMaxPrecision(SqlTypeName.CHAR);
      int precision = type.getPrecision();
      if (vcMaxPrecision > 0 && precision > vcMaxPrecision) {
        precision = vcMaxPrecision;
      }
      return new SqlDataTypeSpec(
          new SqlBasicTypeNameSpec(SqlTypeName.CHAR, precision, SqlParserPos.ZERO),
          SqlParserPos.ZERO);
    }
    return super.getCastSpec(type);
  }

  就可以经Sql中的Cast语句Cast为特定的类型:

  final String query = "select cast(\"product_id\" as varchar(50)), \"product_id\" "
       + "from \"product\" ";
   final String expected = "SELECT CAST(`product_id` AS CHAR(50)), `product_id`\n"
       + "FROM `foodmart`.`product`";
// 解析过的SqlNode
sqlNode.toSqlString(CalciteSqlDialect.DEFAULT).getSql();

到此这篇关于Apache Calcite 实现方言转换的代码的文章就介绍到这了,更多相关Apache Calcite方言转换内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Servers 相关文章推荐
解决Nginx 配置 proxy_pass 后 返回404问题
Mar 31 Servers
Nginx + consul + upsync 完成动态负载均衡的方法详解
Mar 31 Servers
Nginx服务器如何设置url链接
Mar 31 Servers
Nginx本地目录映射实现代码实例
Mar 31 Servers
nginx安装以及配置的详细过程记录
Sep 15 Servers
Windows下用Nginx配置https服务器及反向代理的问题
Sep 25 Servers
Nginx图片服务器配置之后图片访问404的问题解决
Mar 21 Servers
Windows Server 2012 R2 磁盘分区教程
Apr 29 Servers
ubuntu下常用apt命令介绍
Jun 05 Servers
Windows Server 修改远程桌面端口的实现
Jun 25 Servers
Tomcat安装使用及部署Web项目的3种方法汇总
Aug 14 Servers
apache基于端口创建虚拟主机的示例
Apr 24 #Servers
Nginx进程管理和重载原理详解
详解Apache SkyWalking 告警配置指南
Apr 22 #Servers
apache基于端口创建虚拟主机的示例
Apr 22 #Servers
Nginx使用X-Accel-Redirect实现静态文件下载的统计、鉴权、防盗链、限速等
Apr 04 #Servers
Nginx工作原理和优化总结。
利用Nginx代理如何解决前端跨域问题详析
Apr 02 #Servers
You might like
一个简单的MySQL数据浏览器
2006/10/09 PHP
不用数据库的多用户文件自由上传投票系统(3)
2006/10/09 PHP
让你同时上传 1000 个文件 (二)
2006/10/09 PHP
一篇有意思的技术文章php介绍篇
2010/10/26 PHP
php使用标签替换的方式生成静态页面
2015/05/21 PHP
php文件上传 你真的掌握了吗
2016/11/28 PHP
ThinkPHP框架使用redirect实现页面重定向的方法实例分析
2018/04/12 PHP
简单实用的PHP文本缓存类实例
2019/03/22 PHP
javascript 写类方式之七
2009/07/05 Javascript
解决js数据包含加号+通过ajax传到后台时出现连接错误
2013/08/01 Javascript
那些精彩的JavaScript代码片段
2017/01/12 Javascript
ng2学习笔记之bootstrap中的component使用教程
2017/03/09 Javascript
JavaScript异步上传图片文件的实例代码
2017/07/04 Javascript
angular6的table组件开发的实现示例
2018/12/26 Javascript
微信小程序实现页面浮动导航
2019/01/28 Javascript
vue项目中在外部js文件中直接调用vue实例的方法比如说this
2019/04/28 Javascript
vue.js路由mode配置之去掉url上默认的#方法
2019/11/01 Javascript
js实现录音上传功能
2019/11/22 Javascript
[03:59]第二届DOTA2亚洲邀请赛选手传记-VGJ.rOtk
2017/04/03 DOTA
Python中用Descriptor实现类级属性(Property)详解
2014/09/18 Python
Python运算符重载用法实例分析
2015/06/01 Python
python轻松查到删除自己的微信好友
2016/01/10 Python
对sklearn的使用之数据集的拆分与训练详解(python3.6)
2018/12/14 Python
Python列表对象实现原理详解
2019/07/01 Python
Python环境管理virtualenv&amp;virtualenvwrapper的配置详解
2020/07/01 Python
Python高并发解决方案实现过程详解
2020/07/31 Python
Python解析m3u8拼接下载mp4视频文件的示例代码
2021/03/03 Python
船餐厅和泰晤士河餐饮游轮:Bateaux London
2018/03/19 全球购物
奶茶店创业计划书
2014/08/14 职场文书
终止劳动合同协议书
2014/10/05 职场文书
营运督导岗位职责
2015/04/10 职场文书
幼师必备:幼儿园期末教师评语50条
2019/11/01 职场文书
Springboot配置suffix指定mvc视图的后缀方法
2021/07/03 Java/Android
spring cloud 配置中心native配置方式
2021/09/25 Java/Android
Redis之RedisTemplate配置方式(序列和反序列化)
2022/03/13 Redis
win10频率超出范围怎么办?win10老显示超出工作频率范围的解决方法
2022/07/07 数码科技