-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-22880][SQL] Add cascadeTruncate option to JDBC datasource #20057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5416452
c262993
7e0ff07
bc75051
a365f79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ import java.sql.{Connection, Date, Timestamp} | |
| import org.apache.commons.lang3.StringUtils | ||
|
|
||
| import org.apache.spark.annotation.{DeveloperApi, InterfaceStability, Since} | ||
| import org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| /** | ||
|
|
@@ -120,12 +121,27 @@ abstract class JdbcDialect extends Serializable { | |
| * The 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. | ||
| * @param table The name of the table. | ||
| * @param table The table to truncate | ||
| * @return The SQL query to use for truncating a table | ||
| */ | ||
| @Since("2.3.0") | ||
| def getTruncateQuery(table: String): String = { | ||
| s"TRUNCATE TABLE $table" | ||
| getTruncateQuery(table, isCascadingTruncateTable) | ||
| } | ||
|
|
||
| /** | ||
| * The 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. | ||
| * @param table The table to truncate | ||
| * @param cascade Whether or not to cascade the truncation | ||
| * @return The SQL query to use for truncating a table | ||
| */ | ||
| @Since("2.4.0") | ||
| def getTruncateQuery( | ||
| table: String, | ||
| cascade: Option[Boolean] = isCascadingTruncateTable): String = { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: indent. |
||
| s"TRUNCATE TABLE $table" | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,4 +95,20 @@ private case object OracleDialect extends JdbcDialect { | |
| } | ||
|
|
||
| override def isCascadingTruncateTable(): Option[Boolean] = Some(false) | ||
|
|
||
| /** | ||
| * The SQL query used to truncate a table. | ||
| * @param table The table to truncate | ||
| * @param cascade Whether or not to cascade the truncation. Default value is the | ||
| * value of isCascadingTruncateTable() | ||
| * @return The SQL query to use for truncating a table | ||
| */ | ||
| override def getTruncateQuery( | ||
|
||
| table: String, | ||
| cascade: Option[Boolean] = isCascadingTruncateTable): String = { | ||
| cascade match { | ||
| case Some(true) => s"TRUNCATE TABLE $table CASCADE" | ||
| case _ => s"TRUNCATE TABLE $table" | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,15 +85,27 @@ private object PostgresDialect extends JdbcDialect { | |
| s"SELECT 1 FROM $table LIMIT 1" | ||
| } | ||
|
|
||
| override def isCascadingTruncateTable(): Option[Boolean] = Some(false) | ||
|
|
||
| /** | ||
| * The SQL query used to truncate a table. For Postgres, the default behaviour is to | ||
| * also truncate any descendant tables. As this is a (possibly unwanted) side-effect, | ||
| * the Postgres dialect adds 'ONLY' to truncate only the table in question | ||
| * @param table The name of the table. | ||
| * @return The SQL query to use for truncating a table | ||
| */ | ||
| override def getTruncateQuery(table: String): String = { | ||
| s"TRUNCATE TABLE ONLY $table" | ||
| * The SQL query used to truncate a table. For Postgres, the default behaviour is to | ||
| * also truncate any descendant tables. As this is a (possibly unwanted) side-effect, | ||
| * the Postgres dialect adds 'ONLY' to truncate only the table in question | ||
| * @param table The table to truncate | ||
| * @param cascade Whether or not to cascade the truncation. Default value is the value of | ||
| * isCascadingTruncateTable(). Cascading a truncation will truncate tables | ||
| * with a foreign key relationship to the target table. However, it will not | ||
| * truncate tables with an inheritance relationship to the target table, as | ||
| * the truncate query always includes "ONLY" to prevent this behaviour. | ||
| * @return The SQL query to use for truncating a table | ||
| */ | ||
| override def getTruncateQuery( | ||
| table: String, | ||
| cascade: Option[Boolean] = isCascadingTruncateTable): String = { | ||
| cascade match { | ||
| case Some(true) => s"TRUNCATE TABLE ONLY $table CASCADE" | ||
|
||
| case _ => s"TRUNCATE TABLE ONLY $table" | ||
| } | ||
| } | ||
|
|
||
| override def beforeFetch(connection: Connection, properties: Map[String, String]): Unit = { | ||
|
|
@@ -110,5 +122,4 @@ private object PostgresDialect extends JdbcDialect { | |
| } | ||
| } | ||
|
|
||
| override def isCascadingTruncateTable(): Option[Boolean] = Some(false) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry. I'm not sure that I lost the previous context, but what about
getOrElselikeval isTruncateon line 121?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I didn't do that is because of the existence of the
isCascadingTruncateTablefunction for each dialect. According to the docs, that indicates whether or not aTRUNCATE TABLEcommand results in cascading behaviour by default for a given dialect. I thought it would be nice to then use that value as the default value forisCascadeTruncate. In that way, if there ever is a dialect that cascades truncations by default, we don't 'hardcode' a default value.