Class JdbcDialect
- All Implemented Interfaces:
- Serializable,- org.apache.spark.internal.Logging
- Direct Known Subclasses:
- AggregatedDialect,- DatabricksDialect,- DB2Dialect,- DerbyDialect,- MsSqlServerDialect,- MySQLDialect,- OracleDialect,- PostgresDialect,- SnowflakeDialect,- TeradataDialect
 Currently, the only thing done by the dialect is type mapping.
 getCatalystType is used when reading from a JDBC table and getJDBCType
 is used when writing to a JDBC table.  If getCatalystType returns null,
 the default type handling is used for the given JDBC type.  Similarly,
 if getJDBCType returns (null, None), the default type handling is used
 for the given Catalyst type.
- See Also:
- 
Nested Class SummaryNested classes/interfaces inherited from interface org.apache.spark.internal.Loggingorg.apache.spark.internal.Logging.LogStringContext, org.apache.spark.internal.Logging.SparkShellLoggingFilter
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionString[]alterTable(String tableName, scala.collection.immutable.Seq<TableChange> changes, int dbMajorVersion) Alter an existing table.voidbeforeFetch(Connection connection, scala.collection.immutable.Map<String, String> properties) Override connection specific properties to run before a select is made.abstract booleanCheck if this dialect instance can handle a certain jdbc url.classifyException(String message, Throwable e) Deprecated.Please override the classifyException method with an error condition.classifyException(Throwable e, String condition, scala.collection.immutable.Map<String, String> messageParameters, String description, boolean isRuntime) Gets a dialect exception, classifies it and wraps it byAnalysisException.scala.Option<String>compileAggregate(AggregateFunc aggFunction) Deprecated.use org.apache.spark.sql.jdbc.JdbcDialect.compileExpression instead.scala.Option<String>compileExpression(Expression expr) Converts V2 expression to String representing a SQL expression.compileValue(Object value) Converts value to SQL expression.Converts an instance ofjava.sql.Dateto a customjava.sql.Datevalue.Converts an instance ofjava.sql.Timestampto a customjava.sql.Timestampvalue.Convert java.sql.Timestamp to a LocalDateTime representing the same wall-clock time as the value stored in a remote database.Converts a LocalDateTime representing a TimestampNTZ type to an instance ofjava.sql.Timestamp.scala.Function1<Object,Connection> createConnectionFactory(org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions options) Returns a factory for creating connections to the given JDBC URL.createIndex(String indexName, Identifier tableIdent, NamedReference[] columns, Map<NamedReference, Map<String, String>> columnsProperties, Map<String, String> properties) Build a create index SQL statement.voidcreateSchema(Statement statement, String schema, String comment) Create schema with an optional comment.voidcreateTable(Statement statement, String tableName, String strSchema, org.apache.spark.sql.execution.datasources.jdbc.JdbcOptionsInWrite options) Create the table if the table does not exist.dropIndex(String indexName, Identifier tableIdent) Build a drop index SQL statement.dropSchema(String schema, boolean cascade) Build a SQL statement to drop the given table.scala.collection.immutable.Seq<scala.Tuple2<String,UnboundFunction>> List the user-defined functions in jdbc dialect.getAddColumnQuery(String tableName, String columnName, String dataType) scala.Option<DataType>getCatalystType(int sqlType, String typeName, int size, MetadataBuilder md) Get the custom datatype mapping for the given jdbc meta information.longgetDayTimeIntervalAsMicros(String daytimeStr) Converts a day-time interval string to a long valuemicros.getDeleteColumnQuery(String tableName, String columnName) Return the DB-specific quoted and fully qualified table namegetJdbcSQLQueryBuilder(org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions options) Returns the SQL builder for the SELECT statement.scala.Option<JdbcType>getJDBCType(DataType dt) Retrieve the jdbc / sql type for a given datatype.getLimitClause(Integer limit) Returns the LIMIT clause for the SELECT statementgetOffsetClause(Integer offset) Returns the OFFSET clause for the SELECT statementgetRenameColumnQuery(String tableName, String columnName, String newName, int dbMajorVersion) getSchemaCommentQuery(String schema, String comment) getSchemaQuery(String table) The SQL query that should be used to discover the schema of a table.getTableCommentQuery(String table, String comment) getTableExistsQuery(String table) Get the SQL query that should be used to find if the given table exists.getTableSample(org.apache.spark.sql.execution.datasources.v2.TableSampleInfo sample) getTruncateQuery(String table) The SQL query that should be used to truncate a table.getTruncateQuery(String table, scala.Option<Object> cascade) The SQL query that should be used to truncate a table.getUpdateColumnNullabilityQuery(String tableName, String columnName, boolean isNullable) getUpdateColumnTypeQuery(String tableName, String columnName, String newDataType) intgetYearMonthIntervalAsMonths(String yearmonthStr) Converts an year-month interval string to an int valuemonths.booleanindexExists(Connection conn, String indexName, Identifier tableIdent, org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions options) Checks whether an index existsinsertIntoTable(String table, StructField[] fields) Returns an Insert SQL statement template for inserting a row into the target table via JDBC conn.scala.Option<Object>Return Some[true] iffTRUNCATE TABLEcauses cascading default.booleanbooleanisSupportedFunction(String funcName) Returns whether the database supports function.booleanisSyntaxErrorBestEffort(SQLException exception) Attempts to determine if the given SQLException is a SQL syntax error.listIndexes(Connection conn, Identifier tableIdent, org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions options) Lists all the indexes in this table.String[][]listSchemas(Connection conn, org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions options) Lists all the schemas in this table.quoteIdentifier(String colName) Quotes the identifier.removeSchemaCommentQuery(String schema) renameTable(String oldTable, String newTable) Deprecated.Please override renameTable method with identifiers.renameTable(Identifier oldTable, Identifier newTable) Rename an existing table.booleanschemasExists(Connection conn, org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions options, String schema) Check schema exists or not.booleanbooleanReturns true if dialect supports JOIN operator.booleanReturns ture if dialect supports LIMIT clause.booleanReturns ture if dialect supports OFFSET clause.booleanvoidupdateExtraColumnMeta(Connection conn, ResultSetMetaData rsmd, int columnIdx, MetadataBuilder metadata) Get extra column metadata for the given column.Methods inherited from class java.lang.Objectequals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.spark.internal.LogginginitializeForcefully, initializeLogIfNecessary, initializeLogIfNecessary, initializeLogIfNecessary$default$2, isTraceEnabled, log, logBasedOnLevel, logDebug, logDebug, logDebug, logDebug, logError, logError, logError, logError, logInfo, logInfo, logInfo, logInfo, logName, LogStringContext, logTrace, logTrace, logTrace, logTrace, logWarning, logWarning, logWarning, logWarning, MDC, org$apache$spark$internal$Logging$$log_, org$apache$spark$internal$Logging$$log__$eq, withLogContext
- 
Constructor Details- 
JdbcDialectpublic JdbcDialect()
 
- 
- 
Method Details- 
alterTablepublic String[] alterTable(String tableName, scala.collection.immutable.Seq<TableChange> changes, int dbMajorVersion) Alter an existing table.- Parameters:
- tableName- The name of the table to be altered.
- changes- Changes to apply to the table.
- dbMajorVersion- (undocumented)
- Returns:
- The SQL statements to use for altering the table.
 
- 
beforeFetchpublic void beforeFetch(Connection connection, scala.collection.immutable.Map<String, String> properties) Override connection specific properties to run before a select is made. This is in place to allow dialects that need special treatment to optimize behavior.- Parameters:
- connection- The connection object
- properties- The connection properties. This is passed through from the relation.
 
- 
canHandleCheck if this dialect instance can handle a certain jdbc url.- Parameters:
- url- the jdbc url.
- Returns:
- True if the dialect can be applied on the given jdbc url.
- Throws:
- NullPointerException- if the url is null.
 
- 
classifyExceptionpublic Throwable classifyException(Throwable e, String condition, scala.collection.immutable.Map<String, String> messageParameters, String description, boolean isRuntime) Gets a dialect exception, classifies it and wraps it byAnalysisException.- Parameters:
- e- The dialect specific exception.
- condition- The error condition assigned in the case of an unclassified- e
- messageParameters- The message parameters of- errorClass
- description- The error description
- isRuntime- Whether the exception is a runtime exception or not.
- Returns:
- SparkThrowable + Throwableor its sub-class.
 
- 
classifyExceptionDeprecated.Please override the classifyException method with an error condition. Since 4.0.0.Gets a dialect exception, classifies it and wraps it byAnalysisException.- Parameters:
- message- The error message to be placed to the returned exception.
- e- The dialect specific exception.
- Returns:
- AnalysisExceptionor its sub-class.
 
- 
compileAggregateDeprecated.use org.apache.spark.sql.jdbc.JdbcDialect.compileExpression instead. Since 3.4.0.Converts aggregate function to String representing a SQL expression.- Parameters:
- aggFunction- The aggregate function to be converted.
- Returns:
- Converted value.
 
- 
compileExpressionConverts V2 expression to String representing a SQL expression.- Parameters:
- expr- The V2 expression to be converted.
- Returns:
- Converted value.
 
- 
compileValueConverts value to SQL expression.- Parameters:
- value- The value to be converted.
- Returns:
- Converted value.
 
- 
convertJavaDateToDateConverts an instance ofjava.sql.Dateto a customjava.sql.Datevalue.- Parameters:
- d- the date value returned from JDBC ResultSet getDate method.
- Returns:
- the date value after conversion
 
- 
convertJavaTimestampToTimestampConverts an instance ofjava.sql.Timestampto a customjava.sql.Timestampvalue.- Parameters:
- t- represents a specific instant in time based on the hybrid calendar which combines Julian and Gregorian calendars.
- Returns:
- the timestamp value to convert to
- Throws:
- IllegalArgumentException- if t is null
 
- 
convertJavaTimestampToTimestampNTZConvert java.sql.Timestamp to a LocalDateTime representing the same wall-clock time as the value stored in a remote database. JDBC dialects should override this function to provide implementations that suit their JDBC drivers.- Parameters:
- t- Timestamp returned from JDBC driver getTimestamp method.
- Returns:
- A LocalDateTime representing the same wall clock time as the timestamp in database.
 
- 
convertTimestampNTZToJavaTimestampConverts a LocalDateTime representing a TimestampNTZ type to an instance ofjava.sql.Timestamp.- Parameters:
- ldt- representing a TimestampNTZType.
- Returns:
- A Java Timestamp representing this LocalDateTime.
 
- 
createConnectionFactorypublic scala.Function1<Object,Connection> createConnectionFactory(org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions options) Returns a factory for creating connections to the given JDBC URL. In general, creating a connection has nothing to do with JDBC partition id. But sometimes it is needed, such as a database with multiple shard nodes.- Parameters:
- options- - JDBC options that contains url, table and other information.
- Returns:
- The factory method for creating JDBC connections with the RDD partition ID. -1 means the connection is being created at the driver side.
- Throws:
- IllegalArgumentException- if the driver could not open a JDBC connection.
 
- 
createIndexpublic String createIndex(String indexName, Identifier tableIdent, NamedReference[] columns, Map<NamedReference, Map<String, String>> columnsProperties, Map<String, String> properties) Build a create index SQL statement.- Parameters:
- indexName- the name of the index to be created
- tableIdent- the table on which index to be created
- columns- the columns on which index to be created
- columnsProperties- the properties of the columns on which index to be created
- properties- the properties of the index to be created
- Returns:
- the SQL statement to use for creating the index.
 
- 
createSchemaCreate schema with an optional comment. Empty string means no comment.- Parameters:
- statement- (undocumented)
- schema- (undocumented)
- comment- (undocumented)
 
- 
createTablepublic void createTable(Statement statement, String tableName, String strSchema, org.apache.spark.sql.execution.datasources.jdbc.JdbcOptionsInWrite options) Create the table if the table does not exist. To allow certain options to append when create a new table, which can be table_options or partition_options. E.g., "CREATE TABLE t (name string) ENGINE=InnoDB DEFAULT CHARSET=utf8"- Parameters:
- statement- The Statement object used to execute SQL statements.
- tableName- The name of the table to be created.
- strSchema- The schema of the table to be created.
- options- The JDBC options. It contains the create table option, which can be table_options or partition_options.
 
- 
dropIndexBuild a drop index SQL statement.- Parameters:
- indexName- the name of the index to be dropped.
- tableIdent- the table on which index to be dropped.
- Returns:
- the SQL statement to use for dropping the index.
 
- 
dropSchema
- 
dropTableBuild a SQL statement to drop the given table.- Parameters:
- table- the table name
- Returns:
- The SQL statement to use for drop the table.
 
- 
functionsList the user-defined functions in jdbc dialect.- Returns:
- a sequence of tuple from function name to user-defined function.
 
- 
getAddColumnQuery
- 
getCatalystTypepublic scala.Option<DataType> getCatalystType(int sqlType, String typeName, int size, MetadataBuilder md) Get the custom datatype mapping for the given jdbc meta information.Guidelines for mapping database defined timestamps to Spark SQL timestamps: - 
     TIMESTAMP WITHOUT TIME ZONE if preferTimestampNTZ ->
     TimestampNTZType
- 
     TIMESTAMP WITHOUT TIME ZONE if !preferTimestampNTZ ->
     TimestampType(LTZ)
- TIMESTAMP WITH TIME ZONE -> TimestampType(LTZ)
- TIMESTAMP WITH LOCAL TIME ZONE -> TimestampType(LTZ)
- 
     If the TIMESTAMP cannot be distinguished by sqlTypeandtypeName, preferTimestampNTZ is respected for now, but we may need to add another option in the future if necessary.
 - Parameters:
- sqlType- Refers to- Typesconstants, or other constants defined by the target database, e.g.- -101is Oracle's TIMESTAMP WITH TIME ZONE type. This value is returned by- ResultSetMetaData.getColumnType(int).
- typeName- The column type name used by the database (e.g. "BIGINT UNSIGNED"). This is sometimes used to determine the target data type when- sqlTypeis not sufficient if multiple database types are conflated into a single id. This value is returned by- ResultSetMetaData.getColumnTypeName(int).
- size- The size of the type, e.g. the maximum precision for numeric types, length for character string, etc. This value is returned by- ResultSetMetaData.getPrecision(int).
- md- Result metadata associated with this type. This contains additional information from- ResultSetMetaDataor user specified options.- 
               isTimestampNTZ: Whether read a TIMESTAMP WITHOUT TIME ZONE value asTimestampNTZTypeor not. This is configured byJDBCOptions.preferTimestampNTZ.
- 
               scale: The length of fractional partResultSetMetaData.getScale(int)
 
- 
               
- Returns:
- An option the actual DataType (subclasses of DataType) or None if the default type mapping should be used.
 
- 
     TIMESTAMP WITHOUT TIME ZONE if preferTimestampNTZ ->
     
- 
getDayTimeIntervalAsMicrosConverts a day-time interval string to a long valuemicros.- Parameters:
- daytimeStr- the day-time interval string
- Returns:
- the number of total microseconds in the interval
- Throws:
- IllegalArgumentException- if the input string is invalid
 
- 
getDeleteColumnQuery
- 
getFullyQualifiedQuotedTableNameReturn the DB-specific quoted and fully qualified table name- Parameters:
- ident- (undocumented)
- Returns:
- (undocumented)
 
- 
getJDBCTypeRetrieve the jdbc / sql type for a given datatype.- Parameters:
- dt- The datatype (e.g.- StringType)
- Returns:
- The new JdbcType if there is an override for this DataType
 
- 
getJdbcSQLQueryBuilderpublic JdbcSQLQueryBuilder getJdbcSQLQueryBuilder(org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions options) Returns the SQL builder for the SELECT statement.- Parameters:
- options- (undocumented)
- Returns:
- (undocumented)
 
- 
getLimitClauseReturns the LIMIT clause for the SELECT statement- Parameters:
- limit- (undocumented)
- Returns:
- (undocumented)
 
- 
getOffsetClauseReturns the OFFSET clause for the SELECT statement- Parameters:
- offset- (undocumented)
- Returns:
- (undocumented)
 
- 
getRenameColumnQuery
- 
getSchemaCommentQuery
- 
getSchemaQueryThe SQL query that should be used to discover the schema of a table. It only needs to ensure that the result set has the same schema as the table, such as by calling "SELECT * ...". Dialects can override this method to return a query that works best in a particular database.- Parameters:
- table- The name of the table.
- Returns:
- The SQL query to use for discovering the schema.
 
- 
getTableCommentQuery
- 
getTableExistsQueryGet the SQL query that should be used to find if the given table exists. Dialects can override this method to return a query that works best in a particular database.- Parameters:
- table- The name of the table.
- Returns:
- The SQL query to use for checking the table.
 
- 
getTableSample
- 
getTruncateQueryThe SQL query that should be used to truncate a table. Dialects can override this method to return a query that is suitable for a particular database. For PostgreSQL, for instance, a different query is used to prevent "TRUNCATE" affecting other tables.- Parameters:
- table- The table to truncate
- Returns:
- The SQL query to use for truncating a table
 
- 
getTruncateQueryThe SQL query that should be used to truncate a table. Dialects can override this method to return a query that is suitable for a particular database. For PostgreSQL, for instance, a different query is used to prevent "TRUNCATE" affecting other tables.- Parameters:
- table- The table to truncate
- cascade- Whether or not to cascade the truncation
- Returns:
- The SQL query to use for truncating a table
 
- 
getUpdateColumnNullabilityQuery
- 
getUpdateColumnTypeQuery
- 
getYearMonthIntervalAsMonthsConverts an year-month interval string to an int valuemonths.- Parameters:
- yearmonthStr- the year-month interval string
- Returns:
- the number of total months in the interval
- Throws:
- IllegalArgumentException- if the input string is invalid
 
- 
indexExistspublic boolean indexExists(Connection conn, String indexName, Identifier tableIdent, org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions options) Checks whether an index exists- Parameters:
- indexName- the name of the index
- tableIdent- the table on which index to be checked
- options- JDBCOptions of the table
- conn- (undocumented)
- Returns:
- true if the index with indexNameexists in the table withtableName, false otherwise
 
- 
insertIntoTableReturns an Insert SQL statement template for inserting a row into the target table via JDBC conn. Use "?" as placeholder for each value to be inserted. E.g.INSERT INTO t ("name", "age", "gender") VALUES (?, ?, ?)- Parameters:
- table- The name of the table.
- fields- The fields of the row that will be inserted.
- Returns:
- The SQL query to use for insert data into table.
 
- 
isCascadingTruncateTableReturn Some[true] iffTRUNCATE TABLEcauses cascading default. Some[true] : TRUNCATE TABLE causes cascading. Some[false] : TRUNCATE TABLE does not cause cascading. None: The behavior of TRUNCATE TABLE is unknown (default).- Returns:
- (undocumented)
 
- 
isObjectNotFoundException
- 
isSupportedFunctionReturns whether the database supports function.- Parameters:
- funcName- Upper-cased function name
- Returns:
- True if the database supports function.
 
- 
isSyntaxErrorBestEffortAttempts to determine if the given SQLException is a SQL syntax error.This check is best-effort: it may not detect all syntax errors across all JDBC dialects. However, if this method returns true, the exception is guaranteed to be a syntax error. This is used to decide whether to wrap the exception in a more appropriate Spark exception. - Parameters:
- exception- (undocumented)
- Returns:
- true if the exception is confidently identified as a syntax error; false otherwise.
 
- 
listIndexespublic TableIndex[] listIndexes(Connection conn, Identifier tableIdent, org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions options) Lists all the indexes in this table.- Parameters:
- conn- (undocumented)
- tableIdent- (undocumented)
- options- (undocumented)
- Returns:
- (undocumented)
 
- 
listSchemaspublic String[][] listSchemas(Connection conn, org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions options) Lists all the schemas in this table.- Parameters:
- conn- (undocumented)
- options- (undocumented)
- Returns:
- (undocumented)
 
- 
quoteIdentifierQuotes the identifier. This is used to put quotes around the identifier in case the column name is a reserved keyword, or in case it contains characters that require quotes (e.g. space).- Parameters:
- colName- (undocumented)
- Returns:
- (undocumented)
 
- 
removeSchemaCommentQuery
- 
renameTableDeprecated.Please override renameTable method with identifiers. Since 3.5.0.Rename an existing table.- Parameters:
- oldTable- The existing table.
- newTable- New name of the table.
- Returns:
- The SQL statement to use for renaming the table.
 
- 
renameTableRename an existing table.- Parameters:
- oldTable- The existing table.
- newTable- New name of the table.
- Returns:
- The SQL statement to use for renaming the table.
 
- 
schemasExistspublic boolean schemasExists(Connection conn, org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions options, String schema) Check schema exists or not.- Parameters:
- conn- (undocumented)
- options- (undocumented)
- schema- (undocumented)
- Returns:
- (undocumented)
 
- 
supportsHintpublic boolean supportsHint()
- 
supportsJoinpublic boolean supportsJoin()Returns true if dialect supports JOIN operator.- Returns:
- (undocumented)
 
- 
supportsLimitpublic boolean supportsLimit()Returns ture if dialect supports LIMIT clause.Note: Some build-in dialect supports LIMIT clause with some trick, please see: OracleDialect.OracleSQLQueryBuilderandMsSqlServerDialect.MsSqlServerSQLQueryBuilder.- Returns:
- (undocumented)
 
- 
supportsOffsetpublic boolean supportsOffset()Returns ture if dialect supports OFFSET clause.Note: Some build-in dialect supports OFFSET clause with some trick, please see: OracleDialect.OracleSQLQueryBuilderandMySQLDialect.MySQLSQLQueryBuilder.- Returns:
- (undocumented)
 
- 
supportsTableSamplepublic boolean supportsTableSample()
- 
updateExtraColumnMetapublic void updateExtraColumnMeta(Connection conn, ResultSetMetaData rsmd, int columnIdx, MetadataBuilder metadata) Get extra column metadata for the given column.- Parameters:
- conn- The connection currently connection being used.
- rsmd- The metadata of the current result set.
- columnIdx- The index of the column.
- metadata- The metadata builder to store the extra column information.
 
 
-