-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-12437][SQL] [WIP]Encapsulate the table and column name in quotes #10403
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
6328632
0d44970
4da9469
0bbdcd7
598e445
d6e7a00
b73f131
bc4dd2c
494b8fa
5b76128
fe27b7b
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 |
|---|---|---|
|
|
@@ -249,13 +249,21 @@ object JdbcUtils extends Logging { | |
| val sb = new StringBuilder() | ||
| val dialect = JdbcDialects.get(url) | ||
| df.schema.fields foreach { field => { | ||
| val name = field.name | ||
| val name = dialect.quoteIdentifier(field.name) | ||
| val typ: String = getJdbcType(field.dataType, dialect).databaseTypeDefinition | ||
| val nullable = if (field.nullable) "" else "NOT NULL" | ||
| sb.append(s", $name $typ $nullable") | ||
| }} | ||
| if (sb.length < 2) "" else sb.substring(2) | ||
| } | ||
|
|
||
| /** | ||
| * Parse the table name string for this RDD. | ||
|
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. This is confusing, since the variable Can you call it
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. Adjusted as suggested |
||
| */ | ||
| def schemaQualifiedTableName(table: String, url: String): String = { | ||
| val dialect = JdbcDialects.get(url) | ||
| dialect.schemaQualifiedTableName(table) | ||
| } | ||
|
|
||
| /** | ||
| * Saves the RDD to the database in a single transaction. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,34 @@ private case object MySQLDialect extends JdbcDialect { | |
| override def quoteIdentifier(colName: String): String = { | ||
| s"`$colName`" | ||
| } | ||
|
|
||
| /** | ||
| * Process table name in case of containing special characters like dot seperating database name | ||
| * followed by table name (eg "some database"."some-table-name") or | ||
| * in case it contains characters that require quotes (e.g. space). | ||
| */ | ||
| override def schemaQualifiedTableName(tableName: String): String = { | ||
| //Removing quotes so that we can add them correctly. | ||
| val tableNameWithoutQuotes = tableName.replace("\"", "").replace("\'", "") | ||
|
|
||
| //If block for addressing the case of . (eg "some database"."some-table-name") | ||
| if (tableNameWithoutQuotes.contains(".")) { | ||
| val tableNameList = tableNameWithoutQuotes.split('.') | ||
| tableNameList.foldLeft("") { (leftStr, rightStr) => | ||
|
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. Woah, this is kinda dense and confusing. Can you please rewrite this in a simpler fashion and add comments? This is just my drive-by first impression.
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. Also, can you add unit tests for this function? Not end-to-end tests, but a test which exercises this method in isolation, similar to the ones that I have in
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. I'm still not a fan of this
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. Ping. Any updates here?
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. Will address it these week sometime |
||
| if (!"".equals(rightStr.trim())) { | ||
| if ("".equals(leftStr.trim())) { | ||
| leftStr + s"`$rightStr`" | ||
| } else { | ||
| leftStr + "." + s"`$rightStr`" | ||
| } | ||
| } else { | ||
| leftStr | ||
| } | ||
| } | ||
| } else { | ||
| s"`$tableNameWithoutQuotes`" | ||
| } | ||
| } | ||
|
|
||
| override def getTableExistsQuery(table: String): String = { | ||
| s"SELECT 1 FROM $table LIMIT 1" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * 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.Types | ||
| import org.scalatest.BeforeAndAfter | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.test.SharedSQLContext | ||
|
|
||
|
|
||
| class MySQLDialectSuite extends SparkFunSuite with BeforeAndAfter with SharedSQLContext { | ||
|
|
||
| val testMySQLDialect = JdbcDialects.get("jdbc:mysql://localhost:3306/mysql?user=root&password=rootpass"); | ||
|
|
||
| test("testing schemaQualifiedTableName") { | ||
| assert( testMySQLDialect.schemaQualifiedTableName("some shcema.some table").equals("`some shcema`.`some table`")) | ||
| assert( testMySQLDialect.schemaQualifiedTableName("some shcema.some table space.some table").equals("`some shcema`.`some table space`.`some table`")) | ||
| assert( testMySQLDialect.schemaQualifiedTableName("some table").equals("`some shcema`")) | ||
| } | ||
| } |
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.
Would it be possible to add a table or tables to the dataPreparation function that have column names that are key words? For example:
create table
escaped names(keyBIGINT,long descriptionTEXT);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.
@pjfanning Added test as suggested
For example:
create table
escaped names(keyBIGINT,long descriptionTEXT);