You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@spark.apache.org by "Nicolas Lalevée (JIRA)" <ji...@apache.org> on 2017/12/09 17:24:00 UTC

[jira] [Comment Edited] (SPARK-22740) [SQL][JDBC] Reserved SQL words are not escaped for table names

    [ https://issues.apache.org/jira/browse/SPARK-22740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16284845#comment-16284845 ] 

Nicolas Lalevée edited comment on SPARK-22740 at 12/9/17 5:23 PM:
------------------------------------------------------------------

A db with different reserved words: that the niceness of encoding the table name, it doesn't care which word is reserved or not, enclosing with back quote defines that the string is not a word but definitively the name of a column, a database or a table.

About the SHOW TABLES exemple: I wanted to show a kind of query which, if generated automatically with the table name would fail to work properly. In JdbcDialect#getTableExistsQuery, a possible implementation could be to return {{s"SHOW TABLES LIKE '$table'"}} rather than {{s"SELECT * FROM $table WHERE 1=0"}}. If the table name is not actually just a table name, but the encoded name, the first query wouldn't work. This is why the semantic of the parameter 'table' is important.

By the way, after having another look at the code in spark, spark is already doing it for column names, with JdbcDialect#quoteIdentifier. And it is implemented with back quote in the MySQLDialect.


was (Author: hibou):
A db with different reserved words: that the niceness of encoding the table name, it doesn't care which word is reserved or not, enclosing with back quote defines that the string is not a word but definitively the name of a column, a database or a table.

About the SHOW TABLES exemple: I wanted to show a kind of query which, if generated automatically with the table name would fail to work properly. In JdbcDialect#getTableExistsQuery, a possible implementation could be to return {{s"SHOW TABLES LIKE '$table'"}} rather than {{s"SELECT * FROM $table WHERE 1=0"}}. If the table name is not actually just a table name, but the encoded name, the first query wouldn't work. This is why the semantic of the parameter 'table' is important.

By the way, after having another look at the code in spark, spark is already doing it for column names, with JdbcType#quoteIdentifier. And it is implemented with back quote in the MySQLDialect.

> [SQL][JDBC] Reserved SQL words are not escaped for table names
> --------------------------------------------------------------
>
>                 Key: SPARK-22740
>                 URL: https://issues.apache.org/jira/browse/SPARK-22740
>             Project: Spark
>          Issue Type: Bug
>          Components: SQL
>    Affects Versions: 2.2.0
>            Reporter: Nicolas Lalevée
>
> I have a mysql database where there is a table named 'group'.
> Then in Java this line fails:
> {code:java}
> sparkSession.read().jdbc("jdbc:mysql://localhost/scoop", "group", new Properties()).createTempView("mysql_group");
> {code}
> with this exception:
> {noformat}
> com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group WHERE 1=0' at line 1
> 	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
> 	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
> 	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
> 	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
> 	at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
> 	at com.mysql.jdbc.Util.getInstance(Util.java:386)
> 	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
> 	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
> 	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
> 	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468)
> 	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)
> 	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2719)
> 	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
> 	at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2318)
> 	at org.apache.spark.sql.execution.datasources.jdbc.JDBCRDD$.resolveTable(JDBCRDD.scala:62)
> 	at org.apache.spark.sql.execution.datasources.jdbc.JDBCRelation.<init>(JDBCRelation.scala:113)
> 	at org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider.createRelation(JdbcRelationProvider.scala:47)
> 	at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:306)
> 	at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:178)
> 	at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:146)
> 	at org.apache.spark.sql.DataFrameReader.jdbc(DataFrameReader.scala:193)
> {noformat}
> In mysql, the table name should escaped with back ticks: {{SELECT * FROM `group` WHERE 1=0}}
> I tried to work on a fix, but the "table name" in the code of spark may be the qualified name of the table, with the name of the database, so this is not trivial.
> Instead of generating: {{SELECT * FROM `mydb.group` WHERE 1=0}}
> This should be generated: {{SELECT * FROM `mydb`.`group` WHERE 1=0}}
> Here are the functions that I have found that might be affected by reserved word as a table name:
> - JdbcDialects#getTableExistsQuery
> - JdbcDialects#getSchemaQuery
> - MySQLDialect#getTableExistsQuery
> - PostgresDialects#getTableExistsQuery
> - JdbcUtils#dropTable
> - JdbcUtils#truncateTable
> - JdbcUtils#getInsertStatement
> - JdbcUtils#createTable



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@spark.apache.org
For additional commands, e-mail: issues-help@spark.apache.org