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服务器添加Systemd自定义服务过程解析
Mar 31 Servers
Nginx 502 Bad Gateway错误原因及解决方案
Mar 31 Servers
nginx基于域名,端口,不同IP的虚拟主机设置的实现
Mar 31 Servers
fastdfs+nginx集群搭建的实现
Mar 31 Servers
Nginx如何配置Http、Https、WS、WSS的方法步骤
May 11 Servers
Apache Hudi的多版本清理服务彻底讲解
Mar 31 Servers
Vertica集成Apache Hudi重磅使用指南
Mar 31 Servers
Nginx配置根据url参数重定向
Apr 11 Servers
阿里云服务器部署RabbitMQ集群的详细教程
Jun 01 Servers
解决Git推送错误non-fast-forward的方法
Jun 25 Servers
kubernetes集群搭建Zabbix监控平台的详细过程
Jul 07 Servers
本地搭建minio文件服务器(使用bat脚本启动)的方法
Jul 15 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
《五等分的花嫁》漫画完结!2020年10月第2期TV动画制作组换血!
2020/03/06 日漫
Discuz 6.0+ 批量注册用户名
2009/09/13 PHP
php IP转换整形(ip2long)的详解
2013/06/06 PHP
Symfony核心类概述
2016/03/17 PHP
收集的网上用的ajax之chat.js文件
2007/04/08 Javascript
js直接编辑当前cookie的脚本
2008/09/14 Javascript
JavaScript 字符串与数组转换函数[不用split与join]
2009/12/13 Javascript
js冒泡法和数组转换成字符串示例代码
2013/08/14 Javascript
JavaScript中的原型和继承详解(图文)
2014/07/18 Javascript
Node.js中使用mongoskin操作mongoDB实例
2014/09/28 Javascript
JavaScript实现更改网页背景与字体颜色的方法
2015/02/02 Javascript
HTML5游戏引擎LTweenLite实现的超帅动画效果(附demo源码下载)
2016/01/26 Javascript
AngularJS入门教程之链接与图片模板详解
2016/08/19 Javascript
jQuery弹出窗口打开链接的实现代码
2016/12/24 Javascript
javascript中BOM基础知识总结
2017/02/14 Javascript
微信小程序支付及退款流程详解
2017/11/30 Javascript
vue 实现左右拖拽元素并且不超过他的父元素的宽度
2018/11/30 Javascript
微信小程序实现搜索功能并跳转搜索结果页面
2019/05/18 Javascript
TypeScript类型声明书写详解
2019/08/28 Javascript
整理 node-sass 安装失败的原因及解决办法(小结)
2020/02/19 Javascript
详解Python发送邮件实例
2016/01/10 Python
Python进阶学习之特殊方法实例详析
2017/12/01 Python
Python封装原理与实现方法详解
2018/08/28 Python
pycharm解决关闭flask后依旧可以访问服务的问题
2020/04/03 Python
jupyter notebook 使用过程中python莫名崩溃的原因及解决方式
2020/04/10 Python
Pycharm配置autopep8实现流程解析
2020/11/28 Python
解析HTML5的存储功能和web SQL的相关操作方法
2016/02/19 HTML / CSS
精致的手工皮鞋:Shoe Embassy
2019/11/08 全球购物
淘宝网店营销策划书
2014/01/11 职场文书
幼儿园开学寄语
2014/04/03 职场文书
企业承诺书格式
2014/05/21 职场文书
校园演讲稿汇总
2014/05/21 职场文书
餐饮店长岗位职责
2015/04/14 职场文书
借款民事起诉状范文
2015/05/19 职场文书
springboot利用redis、Redisson处理并发问题的操作
2021/06/18 Java/Android
vue报错function () { [native code] },无法出现我们想要的内容 Unknown custom element
2022/04/11 Vue.js