广告位联系
返回顶部
分享到

Apache Calcite 实现方言转换的代码

linux 来源:转载 作者:秩名 发布时间:2021-06-08 11:01:31 人浏览
摘要

定义 Calcite能够通过解析Sql为SqlNode,再将SqlNode转化为特定数据库的方言的形式实现Sql的统一。 实现 在Calcite中实现方言转换的主要类是SqlDialect基类,其具体的变量含义如下: public class SqlDialect {BUILT_IN_OPERATORS_LIST: 支持的内置定义函数或

定义

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();


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/it_dx/article/details/115904782
相关文章
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计