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 比 Apache 更牛逼
Mar 31 Servers
Linux安装Nginx步骤详解
Mar 31 Servers
Windows下使用Nginx+Tomcat做负载均衡的完整步骤
Mar 31 Servers
Apache Hudi的多版本清理服务彻底讲解
Mar 31 Servers
Tomcat用户管理的优化配置详解
Mar 31 Servers
nginx搭建NFS网络文件系统
Apr 14 Servers
Windows Server 2012 修改远程默认端口3389的方法
Apr 28 Servers
nginx 配置缓存
May 11 Servers
阿里云服务器Ubuntu 20.04上安装Odoo 15
May 20 Servers
Nginx开源可视化配置工具NginxConfig使用教程
Jun 21 Servers
nginx静态资源的服务器配置方法
Jul 07 Servers
Nginx使用ngx_http_upstream_module实现负载均衡功能示例
Aug 05 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
人族 Terran 基本策略
2020/03/14 星际争霸
php学习笔记(三)操作符与控制结构
2011/08/06 PHP
php实现批量下载百度云盘文件例子分享
2014/04/10 PHP
一个PHP实现的轻量级简单爬虫
2015/07/08 PHP
textContent在Firefox下与innerText等效的属性
2007/05/12 Javascript
js将long日期格式转换为标准日期格式实现思路
2013/04/07 Javascript
javascript查找字符串中出现最多的字符和次数的小例子
2013/10/29 Javascript
jquery自定义右键菜单、全选、不连续选择
2016/03/01 Javascript
JavaScript学习小结之使用canvas画“哆啦A梦”时钟
2016/07/24 Javascript
jQuery多文件异步上传带进度条实例代码
2016/08/16 Javascript
AngularJS 视图详解及示例代码
2016/08/17 Javascript
JavaScript 函数模式详解及示例
2016/09/07 Javascript
Vue.js每天必学之内部响应式原理探究
2016/09/07 Javascript
Vue仿手机qq的实例代码(demo)
2017/09/08 Javascript
详解Vue中watch对象内属性的方法
2019/02/01 Javascript
详解一个小实例理解js原型和继承
2019/04/24 Javascript
js的Object.assign用法示例分析
2020/03/05 Javascript
[01:32]寻找你心中的那团火 DOTA2 TI9火焰传递活动今日开启
2019/05/16 DOTA
Bottle框架中的装饰器类和描述符应用详解
2017/10/28 Python
python2与python3共存问题的解决方法
2018/09/18 Python
在Mac上删除自己安装的Python方法
2018/10/29 Python
Python gevent协程切换实现详解
2020/09/14 Python
python爬虫爬取网页数据并解析数据
2020/09/18 Python
CSS3中引入多种自定义字体font-face
2020/06/12 HTML / CSS
html5 制作地图当前定位箭头的方法示例
2020/01/10 HTML / CSS
美国校园市场:OCM
2017/06/08 全球购物
新加坡交友网站:be2新加坡
2019/04/10 全球购物
联想C++笔试题
2012/06/13 面试题
美容院店长岗位职责
2014/04/08 职场文书
花坛标语大全
2014/06/30 职场文书
学生党员一帮一活动总结
2014/07/08 职场文书
党员三严三实对照检查材料
2014/10/13 职场文书
部队个人年终总结
2015/03/02 职场文书
网吧管理制度范本
2015/08/05 职场文书
初中班长竞选稿
2015/11/20 职场文书
MySQL和Oracle批量插入SQL的通用写法示例
2021/11/17 MySQL