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 相关文章推荐
Centos7.7 64位利用本地完整安装包安装lnmp/lamp套件教程
Mar 09 Servers
Nginx配置并兼容HTTP实现代码解析
Mar 31 Servers
nginx简单配置多个server的方法
Mar 31 Servers
Nginx配置80端口访问8080及项目名地址方法解析
Mar 31 Servers
Nginx工作原理和优化总结。
Apr 02 Servers
制作能在nginx和IIS中使用的ssl证书
Jun 21 Servers
docker-compose部署Yapi的方法
Apr 08 Servers
Windows Server 2012 修改远程默认端口3389的方法
Apr 28 Servers
使用Nginx的访问日志统计PV与UV
May 06 Servers
搭建zabbix监控以及邮件报警的超级详细教学
Jul 15 Servers
Windows server 2016服务器基本设置
Aug 14 Servers
zabbix如何添加监控主机和自定义监控项
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
通用PHP动态生成静态HTML网页的代码
2010/03/04 PHP
php文章内容分页并生成相应的htm静态页面代码
2010/06/07 PHP
PHP使用DES进行加密与解密的方法详解
2013/06/06 PHP
基于PHP输出缓存(output_buffering)的深入理解
2013/06/13 PHP
浅谈php使用curl模拟多线程发送请求
2019/03/08 PHP
php使用pecl方式安装扩展操作示例
2019/08/12 PHP
深入分析js中的constructor和prototype
2012/04/07 Javascript
javascript中window.event事件用法详解
2012/12/11 Javascript
jQuery异步加载数据并添加事件示例
2014/08/24 Javascript
jQuery将多条数据插入模态框的示例代码
2014/09/25 Javascript
jQuery的图片滑块焦点图插件整理推荐
2014/12/07 Javascript
一波JavaScript日期判断脚本分享
2016/03/06 Javascript
Javascript从数组中随机取出不同元素的两种方法
2016/09/22 Javascript
getElementById().innerHTML与getElementById().value的区别
2016/10/27 Javascript
js实现滑动到页面底部自动加载更多功能
2017/02/15 Javascript
vue-router中scrollBehavior的巧妙用法
2018/07/09 Javascript
vue(2.x,3.0)配置跨域代理
2019/11/27 Javascript
[01:00]DOTA2 store: Collection of Artisan's Wonders
2015/08/12 DOTA
[51:00]Secret vs VGJ.S 2018国际邀请赛淘汰赛BO3 第一场 8.24
2018/08/25 DOTA
[56:17]NB vs Infamous 2019国际邀请赛淘汰赛 败者组 BO3 第三场 8.22
2019/09/05 DOTA
全面了解python字符串和字典
2016/07/07 Python
Django 添加静态文件的两种实现方法(必看篇)
2017/07/14 Python
Python 在字符串中加入变量的实例讲解
2018/05/02 Python
python 请求服务器的实现代码(http请求和https请求)
2018/05/25 Python
python清除字符串前后空格函数的方法
2018/10/21 Python
Python Request爬取seo.chinaz.com百度权重网站的查询结果过程解析
2019/08/13 Python
django中嵌套的try-except实例
2020/05/21 Python
PyQt5如何将.ui文件转换为.py文件的实例代码
2020/05/26 Python
pandas to_excel 添加颜色操作
2020/07/14 Python
欧尚俄罗斯网上超市:Auchan俄罗斯
2018/05/03 全球购物
Currentbody美国/加拿大:美容仪专家
2020/03/09 全球购物
数控技术与应用毕业生自荐信
2013/09/24 职场文书
教师党员公开承诺书
2014/03/25 职场文书
先进班集体申报材料
2014/12/26 职场文书
材料员岗位职责范本
2015/04/11 职场文书
python如何读取和存储dict()与.json格式文件
2022/06/25 Python