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

WeChat Mini Programs Achieve Seamless Scrolling

This article example shares the specific code for...

MySQL Workbench download and use tutorial detailed explanation

1. Download MySQL Workbench Workbench is a graphi...

js canvas implements verification code and obtains verification code function

This article example shares the specific code of ...

Detailed steps to deploy lnmp under Docker

Table of contents Pull a centos image Generate ng...

HTTP header information interpretation and analysis (detailed summary)

HTTP Header Explanation 1. Accept: Tells the web s...

Example code for implementing the "plus sign" effect with CSS

To achieve the plus sign effect shown below: To a...

How to add links to FLASH in HTML and make it compatible with all major browsers

Look at the code first Copy code The code is as fo...

Web page printing thin line table + page printing ultimate strategy

When I was printing for a client recently, he aske...

React High-Order Component HOC Usage Summary

One sentence to introduce HOC What is a higher-or...

The difference between br and br/ in HTML

answer from stackflow: Simply <br> is suffic...

How to modify the "Browse" button of the html form to upload files

Copy code The code is as follows: <!DOCTYPE HT...

Two ways to configure Vue global methods

Table of contents 1. Introduction 2. The first me...

A brief discussion on the use of React.FC and React.Component

Table of contents 1. React.FC<> 2. class xx...

A brief summary of vue keep-alive

1. Function Mainly used to preserve component sta...