-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-22729][SQL] Add getTruncateQuery to JdbcDialect #19911
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 1 commit
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 |
|---|---|---|
|
|
@@ -48,5 +48,7 @@ private object DB2Dialect extends JdbcDialect { | |
| case _ => None | ||
| } | ||
|
|
||
| override def isCascadingTruncateTable(): Option[Boolean] = Some(false) | ||
| override def getTruncateQuery(table: String): String = { | ||
| s"TRUNCATE $table" | ||
|
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. Why override the default here? 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. (And all others that are not Postgres?)
Contributor
Author
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. The One thing I don't like about this solution is the code duplication across dialects. But it does prevent dialects from inheriting default behaviour and (accidentally) truncating with side effects. 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. Gotcha. It is also backward incompatible. Ie if someone implemented his/her own dialect one needs to update it (but removing the isCasc...) does this already so I assume that is ok |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,6 +116,16 @@ abstract class JdbcDialect extends Serializable { | |
| s"SELECT * FROM $table WHERE 1=0" | ||
| } | ||
|
|
||
| /** | ||
| * 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. | ||
| * @return The SQL query to use for truncating a table | ||
| */ | ||
| @Since("2.3.0") | ||
|
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. @danielvdende .
Contributor
Author
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. So create a new "improvement" JIRA and change the ticket number in this PR? (just checking I understand you correctly)
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. Yep. And, we need to fix the following in the PR description, too.
Contributor
Author
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. Done |
||
| def getTruncateQuery(table: String): String | ||
|
|
||
| /** | ||
| * 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. | ||
|
|
@@ -147,14 +157,6 @@ abstract class JdbcDialect extends Serializable { | |
| case arrayValue: Array[Any] => arrayValue.map(compileValue).mkString(", ") | ||
| case _ => value | ||
| } | ||
|
|
||
| /** | ||
| * Return Some[true] iff `TRUNCATE TABLE` causes 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). | ||
| */ | ||
| def isCascadingTruncateTable(): Option[Boolean] = None | ||
|
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. We can't drop this, because this is a public API.
Contributor
Author
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. As a middle ground, could it be marked deprecated? Seeing as we don't really need it anymore...
Contributor
Author
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. Although, custom third party dialects may want it. |
||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,17 @@ private object PostgresDialect extends JdbcDialect { | |
| s"SELECT 1 FROM $table LIMIT 1" | ||
| } | ||
|
|
||
| /** | ||
| * 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 ONLY $table" | ||
|
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. Could you add a function comment about why we use
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. Also, please add the similar explanation to
Contributor
Author
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. yes will do
Contributor
Author
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. Done |
||
| } | ||
|
|
||
| override def beforeFetch(connection: Connection, properties: Map[String, String]): Unit = { | ||
| super.beforeFetch(connection, properties) | ||
|
|
||
|
|
@@ -97,8 +108,5 @@ private object PostgresDialect extends JdbcDialect { | |
| if (properties.getOrElse(JDBCOptions.JDBC_BATCH_FETCH_SIZE, "0").toInt > 0) { | ||
| connection.setAutoCommit(false) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| override def isCascadingTruncateTable(): Option[Boolean] = Some(true) | ||
| } | ||
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.
We still need to respect it, but we can enhance PostgresDialect to support the JDBC option
truncate