Class SqlDialect
- java.lang.Object
-
- org.apache.calcite.sql.SqlDialect
-
- Direct Known Subclasses:
AccessSqlDialect
,AnsiSqlDialect
,BigQuerySqlDialect
,CalciteSqlDialect
,Db2SqlDialect
,DerbySqlDialect
,FirebirdSqlDialect
,H2SqlDialect
,HiveSqlDialect
,HsqldbSqlDialect
,InfobrightSqlDialect
,InformixSqlDialect
,IngresSqlDialect
,InterbaseSqlDialect
,JethroDataSqlDialect
,LucidDbSqlDialect
,MssqlSqlDialect
,MysqlSqlDialect
,NeoviewSqlDialect
,NetezzaSqlDialect
,OracleSqlDialect
,ParaccelSqlDialect
,PhoenixSqlDialect
,PostgresqlSqlDialect
,RedshiftSqlDialect
,SybaseSqlDialect
,TeradataSqlDialect
,VerticaSqlDialect
public class SqlDialect extends java.lang.Object
SqlDialect
encapsulates the differences between dialects of SQL.It is used by classes such as
SqlWriter
andSqlBuilder
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SqlDialect.CalendarPolicy
Whether this JDBC driver needs you to pass a Calendar object to methods such asResultSet.getTimestamp(int, java.util.Calendar)
.static interface
SqlDialect.Context
Information for creating a dialect.private static class
SqlDialect.ContextImpl
Implementation of Context.static class
SqlDialect.DatabaseProduct
Rough list of flavors of database.static class
SqlDialect.FakeUtil
A few utility functions copied from org.apache.calcite.util.Util.
-
Field Summary
Fields Modifier and Type Field Description protected static java.util.Set<SqlOperator>
BUILT_IN_OPERATORS_LIST
Built-in scalar functions and operators common for every dialect.static SqlDialect
CALCITE
Deprecated.UseCalciteSqlDialect.DEFAULT
instead.private SqlDialect.DatabaseProduct
databaseProduct
private RelDataTypeSystem
dataTypeSystem
static SqlDialect
DUMMY
Deprecated.UseAnsiSqlDialect.DEFAULT
instead.static SqlDialect.Context
EMPTY_CONTEXT
Empty context.private static char[]
HEXITS
private java.lang.String
identifierEndQuoteString
private java.lang.String
identifierEscapedQuote
private java.lang.String
identifierQuoteString
protected static org.slf4j.Logger
LOGGER
protected NullCollation
nullCollation
-
Constructor Summary
Constructors Constructor Description SqlDialect(SqlDialect.Context context)
Creates a SqlDialect.SqlDialect(SqlDialect.DatabaseProduct databaseProduct, java.lang.String databaseProductName, java.lang.String identifierQuoteString)
Deprecated.SqlDialect(SqlDialect.DatabaseProduct databaseProduct, java.lang.String databaseProductName, java.lang.String identifierQuoteString, NullCollation nullCollation)
Deprecated.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected boolean
allowsAs()
private static boolean
containsNonAscii(java.lang.String s)
Returns whether the string contains any characters outside the comfortable 7-bit ASCII range (32 through 127).static SqlDialect
create(java.sql.DatabaseMetaData databaseMetaData)
Deprecated.Replaced bySqlDialectFactory
RelFieldCollation.NullDirection
defaultNullDirection(RelFieldCollation.Direction direction)
Returns whether NULL values are sorted first or last, in this dialect, in an ORDER BY item of a given direction.protected static SqlDialect.Context
emptyContext()
Creates an empty context.SqlNode
emulateNullDirection(SqlNode node, boolean nullsFirst, boolean desc)
Returns the SqlNode for emulating the null direction for the given field ornull
if no emulation needs to be done.protected SqlNode
emulateNullDirectionWithIsNull(SqlNode node, boolean nullsFirst, boolean desc)
SqlDialect.CalendarPolicy
getCalendarPolicy()
SqlNode
getCastSpec(RelDataType type)
SqlDialect.DatabaseProduct
getDatabaseProduct()
Deprecated.To be removed without replacementNullCollation
getNullCollation()
Returns how NULL values are sorted if an ORDER BY item does not contain NULLS ASCENDING or NULLS DESCENDING.static SqlDialect.DatabaseProduct
getProduct(java.lang.String productName, java.lang.String productVersion)
Deprecated.RelDataTypeSystem
getTypeSystem()
Returns the type system implementation for this dialect.boolean
hasImplicitTableAlias()
Returns whether a qualified table in the FROM clause has an implicit alias which consists of just the table name.boolean
identifierNeedsToBeQuoted(java.lang.String val)
Returns whether a given identifier needs to be quoted.java.lang.String
quoteIdentifier(java.lang.String val)
Encloses an identifier in quotation marks appropriate for the current SQL dialect.java.lang.StringBuilder
quoteIdentifier(java.lang.StringBuilder buf, java.lang.String val)
Encloses an identifier in quotation marks appropriate for the current SQL dialect, writing the result to aStringBuilder
.java.lang.StringBuilder
quoteIdentifier(java.lang.StringBuilder buf, java.util.List<java.lang.String> identifiers)
Quotes a multi-part identifier.java.lang.String
quoteStringLiteral(java.lang.String val)
Converts a string into a string literal.void
quoteStringLiteralUnicode(java.lang.StringBuilder buf, java.lang.String val)
Converts a string into a unicode string literal.java.lang.String
quoteTimestampLiteral(java.sql.Timestamp timestamp)
Converts a timestamp to a SQL timestamp literal, e.g.protected boolean
requiresAliasForFromItems()
SqlNode
rewriteSingleValueExpr(SqlNode aggCall)
Rewrite SINGLE_VALUE into expression based on database variants E.g.boolean
supportsAggregateFunction(SqlKind kind)
boolean
supportsAliasedValues()
Returns whether the dialect supports VALUES in a sub-query with and an "AS t(column, ...)" values to define column names.boolean
supportsCharSet()
Returns whether the dialect supports character set names as part of a data type, for instanceVARCHAR(30) CHARACTER SET `ISO-8859-1`
.boolean
supportsFunction(SqlOperator operator, RelDataType type, java.util.List<RelDataType> paramTypes)
Returns whether this dialect supports a given function or operator.boolean
supportsNestedAggregations()
Returns whether the dialect supports nested aggregations, for instanceSELECT SUM(SUM(1))
.boolean
supportsOffsetFetch()
Deprecated.This method is no longer used.boolean
supportsWindowFunctions()
Returns whether this dialect supports window functions (OVER clause).void
unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec)
void
unparseDateTimeLiteral(SqlWriter writer, SqlAbstractDateTimeLiteral literal, int leftPrec, int rightPrec)
protected void
unparseFetchUsingAnsi(SqlWriter writer, SqlNode offset, SqlNode fetch)
Unparses offset/fetch using ANSI standard "OFFSET offset ROWS FETCH NEXT fetch ROWS ONLY" syntax.protected void
unparseFetchUsingLimit(SqlWriter writer, SqlNode offset, SqlNode fetch)
Unparses offset/fetch using "LIMIT fetch OFFSET offset" syntax.void
unparseOffsetFetch(SqlWriter writer, SqlNode offset, SqlNode fetch)
Converts an offset and fetch into SQL.void
unparseSqlDatetimeArithmetic(SqlWriter writer, SqlCall call, SqlKind sqlKind, int leftPrec, int rightPrec)
void
unparseSqlIntervalLiteral(SqlWriter writer, SqlIntervalLiteral literal, int leftPrec, int rightPrec)
Converts an interval literal to a SQL string.void
unparseSqlIntervalQualifier(SqlWriter writer, SqlIntervalQualifier qualifier, RelDataTypeSystem typeSystem)
Converts an interval qualifier to a SQL string.java.lang.String
unquoteStringLiteral(java.lang.String val)
Converts a string literal back into a string.
-
-
-
Field Detail
-
LOGGER
protected static final org.slf4j.Logger LOGGER
-
EMPTY_CONTEXT
public static final SqlDialect.Context EMPTY_CONTEXT
Empty context.
-
DUMMY
@Deprecated public static final SqlDialect DUMMY
Deprecated.UseAnsiSqlDialect.DEFAULT
instead.
-
CALCITE
@Deprecated public static final SqlDialect CALCITE
Deprecated.UseCalciteSqlDialect.DEFAULT
instead.
-
BUILT_IN_OPERATORS_LIST
protected static final java.util.Set<SqlOperator> BUILT_IN_OPERATORS_LIST
Built-in scalar functions and operators common for every dialect.
-
identifierQuoteString
private final java.lang.String identifierQuoteString
-
identifierEndQuoteString
private final java.lang.String identifierEndQuoteString
-
identifierEscapedQuote
private final java.lang.String identifierEscapedQuote
-
databaseProduct
private final SqlDialect.DatabaseProduct databaseProduct
-
nullCollation
protected final NullCollation nullCollation
-
dataTypeSystem
private final RelDataTypeSystem dataTypeSystem
-
HEXITS
private static final char[] HEXITS
-
-
Constructor Detail
-
SqlDialect
@Deprecated public SqlDialect(SqlDialect.DatabaseProduct databaseProduct, java.lang.String databaseProductName, java.lang.String identifierQuoteString)
Deprecated.
-
SqlDialect
@Deprecated public SqlDialect(SqlDialect.DatabaseProduct databaseProduct, java.lang.String databaseProductName, java.lang.String identifierQuoteString, NullCollation nullCollation)
Deprecated.Creates a SqlDialect.- Parameters:
databaseProduct
- Database product; may be UNKNOWN, never nulldatabaseProductName
- Database product name from JDBC driveridentifierQuoteString
- String to quote identifiers. Null if quoting is not supported. If "[", close quote is deemed to be "]".nullCollation
- Whether NULL values appear first or last
-
SqlDialect
public SqlDialect(SqlDialect.Context context)
Creates a SqlDialect.- Parameters:
context
- All the information necessary to create a dialect
-
-
Method Detail
-
create
@Deprecated public static SqlDialect create(java.sql.DatabaseMetaData databaseMetaData)
Deprecated.Replaced bySqlDialectFactory
Creates aSqlDialect
from a DatabaseMetaData.Does not maintain a reference to the DatabaseMetaData -- or, more importantly, to its
Connection
-- after this call has returned.- Parameters:
databaseMetaData
- used to determine which dialect of SQL to generate
-
emptyContext
protected static SqlDialect.Context emptyContext()
Creates an empty context. UseEMPTY_CONTEXT
if possible.
-
getProduct
@Deprecated public static SqlDialect.DatabaseProduct getProduct(java.lang.String productName, java.lang.String productVersion)
Deprecated.Converts a product name and version (per the JDBC driver) into a product enumeration.- Parameters:
productName
- Product nameproductVersion
- Product version- Returns:
- database product
-
getTypeSystem
public RelDataTypeSystem getTypeSystem()
Returns the type system implementation for this dialect.
-
quoteIdentifier
public java.lang.String quoteIdentifier(java.lang.String val)
Encloses an identifier in quotation marks appropriate for the current SQL dialect.For example,
quoteIdentifier("emp")
yields a string containing"emp"
in Oracle, and a string containing[emp]
in Access.- Parameters:
val
- Identifier to quote- Returns:
- Quoted identifier
-
quoteIdentifier
public java.lang.StringBuilder quoteIdentifier(java.lang.StringBuilder buf, java.lang.String val)
Encloses an identifier in quotation marks appropriate for the current SQL dialect, writing the result to aStringBuilder
.For example,
quoteIdentifier("emp")
yields a string containing"emp"
in Oracle, and a string containing[emp]
in Access.- Parameters:
buf
- Bufferval
- Identifier to quote- Returns:
- The buffer
-
quoteIdentifier
public java.lang.StringBuilder quoteIdentifier(java.lang.StringBuilder buf, java.util.List<java.lang.String> identifiers)
Quotes a multi-part identifier.- Parameters:
buf
- Bufferidentifiers
- List of parts of the identifier to quote- Returns:
- The buffer
-
identifierNeedsToBeQuoted
public boolean identifierNeedsToBeQuoted(java.lang.String val)
Returns whether a given identifier needs to be quoted.
-
quoteStringLiteral
public java.lang.String quoteStringLiteral(java.lang.String val)
Converts a string into a string literal. For example,can't run
becomes'can''t run'
.
-
unparseDateTimeLiteral
public void unparseDateTimeLiteral(SqlWriter writer, SqlAbstractDateTimeLiteral literal, int leftPrec, int rightPrec)
-
unparseSqlDatetimeArithmetic
public void unparseSqlDatetimeArithmetic(SqlWriter writer, SqlCall call, SqlKind sqlKind, int leftPrec, int rightPrec)
-
unparseSqlIntervalQualifier
public void unparseSqlIntervalQualifier(SqlWriter writer, SqlIntervalQualifier qualifier, RelDataTypeSystem typeSystem)
Converts an interval qualifier to a SQL string. The default implementation returns strings such asINTERVAL '1 2:3:4' DAY(4) TO SECOND(4)
.
-
unparseSqlIntervalLiteral
public void unparseSqlIntervalLiteral(SqlWriter writer, SqlIntervalLiteral literal, int leftPrec, int rightPrec)
Converts an interval literal to a SQL string. The default implementation returns strings such asINTERVAL '1 2:3:4' DAY(4) TO SECOND(4)
.
-
containsNonAscii
private static boolean containsNonAscii(java.lang.String s)
Returns whether the string contains any characters outside the comfortable 7-bit ASCII range (32 through 127).- Parameters:
s
- String- Returns:
- Whether string contains any non-7-bit-ASCII characters
-
quoteStringLiteralUnicode
public void quoteStringLiteralUnicode(java.lang.StringBuilder buf, java.lang.String val)
Converts a string into a unicode string literal. For example,can't{tab}run\
becomesu'can''t\0009run\\'
.
-
unquoteStringLiteral
public java.lang.String unquoteStringLiteral(java.lang.String val)
Converts a string literal back into a string. For example,'can''t run'
becomescan't run
.
-
allowsAs
protected boolean allowsAs()
-
requiresAliasForFromItems
protected boolean requiresAliasForFromItems()
-
hasImplicitTableAlias
public boolean hasImplicitTableAlias()
Returns whether a qualified table in the FROM clause has an implicit alias which consists of just the table name.For example, in
SqlDialect.DatabaseProduct.ORACLE
SELECT * FROM sales.emp
is equivalent to
SELECT * FROM sales.emp AS emp
and therefore
SELECT emp.empno FROM sales.emp
is valid. But
SqlDialect.DatabaseProduct.DB2
does not have an implicit alias, so the previous query it not valid; you need to writeSELECT sales.emp.empno FROM sales.emp
Returns true for all databases except DB2.
-
quoteTimestampLiteral
public java.lang.String quoteTimestampLiteral(java.sql.Timestamp timestamp)
Converts a timestamp to a SQL timestamp literal, e.g.TIMESTAMP '2009-12-17 12:34:56'
.Timestamp values do not have a time zone. We therefore interpret them as the number of milliseconds after the UTC epoch, and the formatted value is that time in UTC.
In particular,
quoteTimestampLiteral(new Timestamp(0));
returns
TIMESTAMP '1970-01-01 00:00:00'
, regardless of the JVM's time zone.- Parameters:
timestamp
- Timestamp- Returns:
- SQL timestamp literal
-
getDatabaseProduct
@Deprecated public SqlDialect.DatabaseProduct getDatabaseProduct()
Deprecated.To be removed without replacementReturns the database this dialect belongs to,SqlDialect.DatabaseProduct.UNKNOWN
if not known, never null.Please be judicious in how you use this method. If you wish to determine whether a dialect has a particular capability or behavior, it is usually better to add a method to SqlDialect and override that method in particular sub-classes of SqlDialect.
- Returns:
- Database product
-
supportsCharSet
public boolean supportsCharSet()
Returns whether the dialect supports character set names as part of a data type, for instanceVARCHAR(30) CHARACTER SET `ISO-8859-1`
.
-
supportsAggregateFunction
public boolean supportsAggregateFunction(SqlKind kind)
-
supportsWindowFunctions
public boolean supportsWindowFunctions()
Returns whether this dialect supports window functions (OVER clause).
-
supportsFunction
public boolean supportsFunction(SqlOperator operator, RelDataType type, java.util.List<RelDataType> paramTypes)
Returns whether this dialect supports a given function or operator. It only applies to built-in scalar functions and operators, since user-defined functions and procedures should be read by JdbcSchema.
-
getCalendarPolicy
public SqlDialect.CalendarPolicy getCalendarPolicy()
-
getCastSpec
public SqlNode getCastSpec(RelDataType type)
-
rewriteSingleValueExpr
public SqlNode rewriteSingleValueExpr(SqlNode aggCall)
Rewrite SINGLE_VALUE into expression based on database variants E.g. HSQLDB, MYSQL, ORACLE, etc
-
emulateNullDirection
public SqlNode emulateNullDirection(SqlNode node, boolean nullsFirst, boolean desc)
Returns the SqlNode for emulating the null direction for the given field ornull
if no emulation needs to be done.- Parameters:
node
- The SqlNode representing the expressionnullsFirst
- Whether nulls should come firstdesc
- Whether the sort direction isRelFieldCollation.Direction.DESCENDING
orRelFieldCollation.Direction.STRICTLY_DESCENDING
- Returns:
- A SqlNode for null direction emulation or
null
if not required
-
emulateNullDirectionWithIsNull
protected SqlNode emulateNullDirectionWithIsNull(SqlNode node, boolean nullsFirst, boolean desc)
-
supportsOffsetFetch
@Deprecated public boolean supportsOffsetFetch()
Deprecated.This method is no longer used. To change how the dialect unparses offset/fetch, override theunparseOffsetFetch(org.apache.calcite.sql.SqlWriter, org.apache.calcite.sql.SqlNode, org.apache.calcite.sql.SqlNode)
method.Returns whether the dialect supports OFFSET/FETCH clauses introduced by SQL:2008, for instanceOFFSET 10 ROWS FETCH NEXT 20 ROWS ONLY
. If false, we assume that the dialect supports the alternative syntaxLIMIT 20 OFFSET 10
.
-
unparseOffsetFetch
public void unparseOffsetFetch(SqlWriter writer, SqlNode offset, SqlNode fetch)
Converts an offset and fetch into SQL.At least one of
offset
andfetch
must be provided.Common options:
OFFSET offset ROWS FETCH NEXT fetch ROWS ONLY
(ANSI standard SQL, Oracle, PostgreSQL, and the default)LIMIT fetch OFFSET offset
(Apache Hive, MySQL, Redshift)
- Parameters:
writer
- Writeroffset
- Number of rows to skip before emitting, or nullfetch
- Number of rows to fetch, or null- See Also:
unparseFetchUsingAnsi(SqlWriter, SqlNode, SqlNode)
,unparseFetchUsingLimit(SqlWriter, SqlNode, SqlNode)
-
unparseFetchUsingAnsi
protected final void unparseFetchUsingAnsi(SqlWriter writer, SqlNode offset, SqlNode fetch)
Unparses offset/fetch using ANSI standard "OFFSET offset ROWS FETCH NEXT fetch ROWS ONLY" syntax.
-
unparseFetchUsingLimit
protected final void unparseFetchUsingLimit(SqlWriter writer, SqlNode offset, SqlNode fetch)
Unparses offset/fetch using "LIMIT fetch OFFSET offset" syntax.
-
supportsNestedAggregations
public boolean supportsNestedAggregations()
Returns whether the dialect supports nested aggregations, for instanceSELECT SUM(SUM(1))
.
-
getNullCollation
public NullCollation getNullCollation()
Returns how NULL values are sorted if an ORDER BY item does not contain NULLS ASCENDING or NULLS DESCENDING.
-
defaultNullDirection
public RelFieldCollation.NullDirection defaultNullDirection(RelFieldCollation.Direction direction)
Returns whether NULL values are sorted first or last, in this dialect, in an ORDER BY item of a given direction.
-
supportsAliasedValues
public boolean supportsAliasedValues()
Returns whether the dialect supports VALUES in a sub-query with and an "AS t(column, ...)" values to define column names.Currently, only Oracle does not. For this, we generate "SELECT v0 AS c0, v1 AS c1 ... UNION ALL ...". We may need to refactor this method when we support VALUES for other dialects.
-
-