Skip to content
Snippets Groups Projects
Unverified Commit b35b0dbb authored by Sean Owen's avatar Sean Owen
Browse files

[SPARK-17614][SQL] sparkSession.read() .jdbc(***) use the sql syntax "where...

[SPARK-17614][SQL] sparkSession.read() .jdbc(***) use the sql syntax "where 1=0" that Cassandra does not support

## What changes were proposed in this pull request?

Use dialect's table-exists query rather than hard-coded WHERE 1=0 query

## How was this patch tested?

Existing tests.

Author: Sean Owen <sowen@cloudera.com>

Closes #15196 from srowen/SPARK-17614.
parent f7082ac1
No related branches found
No related tags found
No related merge requests found
......@@ -58,11 +58,11 @@ object JDBCRDD extends Logging {
val dialect = JdbcDialects.get(url)
val conn: Connection = JdbcUtils.createConnectionFactory(url, properties)()
try {
val statement = conn.prepareStatement(s"SELECT * FROM $table WHERE 1=0")
val statement = conn.prepareStatement(dialect.getSchemaQuery(table))
try {
val rs = statement.executeQuery()
try {
return JdbcUtils.getSchema(rs, dialect)
JdbcUtils.getSchema(rs, dialect)
} finally {
rs.close()
}
......@@ -72,8 +72,6 @@ object JDBCRDD extends Logging {
} finally {
conn.close()
}
throw new RuntimeException("This line is unreachable.")
}
/**
......
......@@ -19,7 +19,7 @@ package org.apache.spark.sql.jdbc
import java.sql.Connection
import org.apache.spark.annotation.DeveloperApi
import org.apache.spark.annotation.{DeveloperApi, Since}
import org.apache.spark.sql.types._
/**
......@@ -99,6 +99,19 @@ abstract class JdbcDialect extends Serializable {
s"SELECT * FROM $table WHERE 1=0"
}
/**
* The 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.
* @param table The name of the table.
* @return The SQL query to use for discovering the schema.
*/
@Since("2.1.0")
def getSchemaQuery(table: String): String = {
s"SELECT * FROM $table WHERE 1=0"
}
/**
* 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.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment