forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-45139][SQL] Add DatabricksDialect to handle SQL type conversion
### What changes were proposed in this pull request? This PR adds `DatabricksDialect` to Spark to allow users to query Databricks clusters and Databricks SQL warehouses with more precise SQL type conversion and quote identifiers instead of doing it manually in the code. ### Why are the changes needed? The PR fixes type conversion and makes it easier to query Databricks clusters. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? I added unit tests in JDBCSuite to check conversion. ### Was this patch authored or co-authored using generative AI tooling? No. Closes apache#42896 from sadikovi/add_databricks_dialect. Authored-by: Ivan Sadikov <ivan.sadikov@databricks.com> Signed-off-by: Dongjoon Hyun <dhyun@apple.com>
- Loading branch information
1 parent
4f74fc5
commit 2710dbe
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
sql/core/src/main/scala/org/apache/spark/sql/jdbc/DatabricksDialect.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql.jdbc | ||
|
||
import java.sql.Connection | ||
|
||
import scala.collection.mutable.ArrayBuilder | ||
|
||
import org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions | ||
import org.apache.spark.sql.execution.datasources.v2.TableSampleInfo | ||
import org.apache.spark.sql.types._ | ||
|
||
private case object DatabricksDialect extends JdbcDialect { | ||
|
||
override def canHandle(url: String): Boolean = { | ||
url.startsWith("jdbc:databricks") | ||
} | ||
|
||
override def getCatalystType( | ||
sqlType: Int, | ||
typeName: String, | ||
size: Int, | ||
md: MetadataBuilder): Option[DataType] = { | ||
sqlType match { | ||
case java.sql.Types.TINYINT => Some(ByteType) | ||
case java.sql.Types.SMALLINT => Some(ShortType) | ||
case java.sql.Types.REAL => Some(FloatType) | ||
case _ => None | ||
} | ||
} | ||
|
||
override def getJDBCType(dt: DataType): Option[JdbcType] = dt match { | ||
case BooleanType => Some(JdbcType("BOOLEAN", java.sql.Types.BOOLEAN)) | ||
case DoubleType => Some(JdbcType("DOUBLE", java.sql.Types.DOUBLE)) | ||
case StringType => Some(JdbcType("STRING", java.sql.Types.VARCHAR)) | ||
case BinaryType => Some(JdbcType("BINARY", java.sql.Types.BINARY)) | ||
case _ => None | ||
} | ||
|
||
override def quoteIdentifier(colName: String): String = { | ||
s"`$colName`" | ||
} | ||
|
||
override def supportsLimit: Boolean = true | ||
|
||
override def supportsOffset: Boolean = true | ||
|
||
override def supportsTableSample: Boolean = true | ||
|
||
override def getTableSample(sample: TableSampleInfo): String = { | ||
s"TABLESAMPLE (${(sample.upperBound - sample.lowerBound) * 100}) REPEATABLE (${sample.seed})" | ||
} | ||
|
||
// Override listSchemas to run "show schemas" as a PreparedStatement instead of | ||
// invoking getMetaData.getSchemas as it may not work correctly in older versions of the driver. | ||
override def schemasExists(conn: Connection, options: JDBCOptions, schema: String): Boolean = { | ||
val stmt = conn.prepareStatement("SHOW SCHEMAS") | ||
val rs = stmt.executeQuery() | ||
while (rs.next()) { | ||
if (rs.getString(1) == schema) { | ||
return true | ||
} | ||
} | ||
false | ||
} | ||
|
||
// Override listSchemas to run "show schemas" as a PreparedStatement instead of | ||
// invoking getMetaData.getSchemas as it may not work correctly in older versions of the driver. | ||
override def listSchemas(conn: Connection, options: JDBCOptions): Array[Array[String]] = { | ||
val schemaBuilder = ArrayBuilder.make[Array[String]] | ||
val stmt = conn.prepareStatement("SHOW SCHEMAS") | ||
val rs = stmt.executeQuery() | ||
while (rs.next()) { | ||
schemaBuilder += Array(rs.getString(1)) | ||
} | ||
schemaBuilder.result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters