-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-12010][SQL] Spark JDBC requires support for column-name-free INSERT syntax #10380
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
3211963
0772db3
a59c1aa
c130e31
79d3e0d
cae0f58
5a7f262
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,15 +62,12 @@ object JdbcUtils extends Logging { | |
| /** | ||
| * Returns a PreparedStatement that inserts a row into table via conn. | ||
| */ | ||
| def insertStatement(conn: Connection, table: String, rddSchema: StructType): PreparedStatement = { | ||
| val sql = new StringBuilder(s"INSERT INTO $table VALUES (") | ||
| var fieldsLeft = rddSchema.fields.length | ||
| while (fieldsLeft > 0) { | ||
| sql.append("?") | ||
| if (fieldsLeft > 1) sql.append(", ") else sql.append(")") | ||
| fieldsLeft = fieldsLeft - 1 | ||
| } | ||
| conn.prepareStatement(sql.toString()) | ||
| def insertStatement(conn: Connection, | ||
|
Contributor
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: This method is only used by the
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. @hvanhovell
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. @hvanhovell
Contributor
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. @CK50 I don't have a strong opinion on this. I'd personally remove it, that's all. |
||
| dialect: JdbcDialect, | ||
| table: String, | ||
| rddSchema: StructType): PreparedStatement = { | ||
| val sql = dialect.getInsertStatement(table, rddSchema) | ||
| conn.prepareStatement(sql) | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -126,6 +123,7 @@ object JdbcUtils extends Logging { | |
| dialect: JdbcDialect): Iterator[Byte] = { | ||
| val conn = getConnection() | ||
| var committed = false | ||
| // Temp work-around. Not to be commited!!!! | ||
| val supportsTransactions = try { | ||
| conn.getMetaData().supportsDataManipulationTransactionsOnly() || | ||
| conn.getMetaData().supportsDataDefinitionAndDataManipulationTransactions() | ||
|
|
@@ -139,7 +137,7 @@ object JdbcUtils extends Logging { | |
| if (supportsTransactions) { | ||
| conn.setAutoCommit(false) // Everything in the same db transaction. | ||
| } | ||
| val stmt = insertStatement(conn, table, rddSchema) | ||
| val stmt = insertStatement(conn, dialect, table, rddSchema) | ||
| try { | ||
| var rowCount = 0 | ||
| while (iterator.hasNext) { | ||
|
|
||
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.
Hm, the only problem here is that this is a public method, and while it feels like it was intended to be a Spark-only utility method, I'm not sure it's marked as such.
It's not a big deal to retain it and implement in terms of the new method. However it's now a function of a dialect, which is not an argument here. I suppose any dialect will do since they all behave the same now. This method could then be deprecated.
However: yeah, the behavior is actually the same for all dialects now. Really, this has come full circle and can just be a modification to this method, which was already the same for all dialects. Is there reason to believe the insert statement might vary later? Then I could see keeping the current structure here and just deprecating this method.
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.
Yeah, you are right about that. We'll break a public API. Is that a problem, since this is probably going into 2.0?
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.
That's a fair point, though I think the intent was to back-port this to 1.6.x at least, as it's a moderately important fix. Conceptually, I don't think the API has changed; the insert statement is still not dialect-specific. Hence it seems like the current API is even desirable to maintain for now.
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.
Ok, we have keep the method if we are back-porting. I have yet to encouter a database that doesn't support this insert syntax (did a bit more googling); so it seems safe to put in the generic method.