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

Detailed explanation of the new features of ES9: Async iteration

Table of contents Asynchronous traversal Asynchro...

Using shadowsocks to build a LAN transparent gateway

Table of contents Install and configure dnsmasq I...

Three ways to implement animation in CSS3

This is a test of the interviewee's basic kno...

Talk about nextTick in Vue

When the data changes, the DOM view is not update...

Summary of using MySQL online DDL gh-ost

background: As a DBA, most of the DDL changes of ...

In-depth explanation of MySQL learning engine, explain and permissions

engine Introduction Innodb engine The Innodb engi...

5 things to note when writing React components using hooks

Table of contents 01. Use useState when render is...

Vite+Electron to quickly build VUE3 desktop applications

Table of contents 1. Introduction 2. Create a Vit...

Explain TypeScript mapped types and better literal type inference

Table of contents Overview Using mapped types to ...

The webpage cannot be opened because the div element lacks a closing tag

At first I thought it was a speed issue, so I late...

Detailed tutorial on deploying Apollo custom environment with docker-compose

Table of contents What is the Apollo Configuratio...

JS implements jQuery's append function

Table of contents Show Me The Code Test the effec...

Detailed explanation on how to install MySQL database on Alibaba Cloud Server

Preface Since I needed to install Zookeeper durin...

Introduction to Spark and comparison with Hadoop

Table of contents 1. Spark vs. Hadoop 1.1 Disadva...