Apache Calcite code for dialect conversion

Apache Calcite code for dialect conversion

definition

Calcite can unify Sql by parsing Sql into SqlNode and then converting SqlNode into the dialect of a specific database.

accomplish

The main class for implementing dialect conversion in Calcite is the SqlDialect base class, and its specific variable meanings are as follows:

public class SqlDialect {

BUILT_IN_OPERATORS_LIST: Supported built-in functions or operators (for example: abs and..)

// List identifiers String identifierQuoteString: The start symbol of the identifier String identifierEndQuoteString: The end symbol of the identifier String identifierEscapedQuote: (I don't understand what this is doing yet, like an escape character in a string?)

// Constant identifier String literalQuoteString: constant start symbol String literalEndQuoteString: constant end symbol String literalEscapedQuote: (I don't understand what this is doing yet, like an escape character in a string?)

DatabaseProduct databaseProduct: the database product it belongs toNullCollation nullCollation: the order in which null values ​​are returned when performing a sort queryRelDataTypeSystem dataTypeSystem: data type // related to parsingCasing unquotedCasing: case conversionCasing quotedCasing: case conversionboolean caseSensitive: whether it is case sensitive (column name indicates function name, etc.)
}
// Method area (different data sources implement custom replication methods based on different details)
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

UsageDemo

/** 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)

  This method can be used to convert data according to the data type of the specific data source, for example:

  @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);
  }

  You can use the Cast statement in SQL to cast it to a specific type:

  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`";
// Parsed SqlNode
sqlNode.toSqlString(CalciteSqlDialect.DEFAULT).getSql();

This is the end of this article about the code for implementing dialect conversion in Apache Calcite. For more information about Apache Calcite dialect conversion, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vertica Integrated Apache Hudi Heavy Usage Guide
  • Detailed process of setting up Php+Apache operating environment on Alibaba Cloud Server
  • Detailed process of building and deploying Apache Pulsar cluster
  • Apache Calcite for SQL parsing (java code example)
  • Apache log4j2-RCE Vulnerability Reproduction and Repair Suggestions (CVE-2021-44228)
  • Apache Log4j2 reports a nuclear-level vulnerability and a quick fix
  • Apache Hudi's flexible payload mechanism hardcore analysis

<<:  Mobile terminal adaptation makes px automatically converted to rem

>>:  An article to understand the usage of typeof in js

Recommend

Tutorial on installing mongodb under linux

MongoDB is cross-platform and can be installed on...

Detailed explanation of MySQL cursor concepts and usage

This article uses examples to explain the concept...

Detailed explanation of docker compose usage

Table of contents Docker Compose usage scenarios ...

React's transition from Class to Hooks

Table of contents ReactHooks Preface WhyHooks? Fo...

Example of implementing circular progress bar in Vue

Data display has always been a demand that all wa...

How to install and modify the initial password of mysql5.7.18 under Centos7.3

This article shares with you the installation of ...

How to set list style attributes in CSS (just read this article)

List style properties There are 2 types of lists ...

WeChat Mini Program User Authorization Best Practices Guide

Preface When developing WeChat applets, you often...

Analysis and solution of a.getAttribute(href,2) problem in IE6/7

Brief description <br />In IE6 and 7, in a ...

Introduction to Semantic HTML Tags

In the past few years, DIV+CSS was very popular in...

How to set the width and height of html table cells

When making web pages, you often encounter the pr...

Related commands to completely uninstall nginx under ubuntu16.04

nginx Overview nginx is a free, open source, high...

Completely uninstall mysql. Personal test!

Cleanly uninstall MySQL. Personally tested, this ...

MySQL table type storage engine selection

Table of contents 1. View the storage engine of t...