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

MySql 8.0.16-win64 Installation Tutorial

1. Unzip the downloaded file as shown below . 2. ...

Vue.js Textbox with Dropdown component

A Textbox with Dropdown allows users to select an...

JavaScript to achieve simple drag effect

This article shares the specific code of JavaScri...

Vue improves page response speed through lazy loading

Table of contents Overview What is lazy loading? ...

Summary of how to use bootstrap Table

This article shares with you how to use bootstrap...

React implements double slider cross sliding

This article shares the specific code for React t...

The w3c organization gives style recommendations for html4

This is the style recommendation given by the W3C ...

Improvements to the web server to improve website performance

<br />In the first section of this series, w...

Complete step record of vue encapsulation TabBar component

Table of contents Implementation ideas: Step 1: C...

Build a severe weather real-time warning system with Node.JS

Table of contents Preface: Step 1: Find the free ...

The difference and reasons between the MySQL query conditions not in and in

Write a SQL first SELECT DISTINCT from_id FROM co...

Docker Basics

Preface: Docker is an open source application con...

Fixed table width table-layout: fixed

In order to make the table fill the screen (the re...