Skip to content

Commit

Permalink
Fix handling of MySQL databases with underscores
Browse files Browse the repository at this point in the history
In MySqlClient#getTables() schemaName is passed as catalog parameter
of DatabaseMetaData#getTables() becasue MySQL does not have schemas.
DatabaseMetaData#getTables() requires the catalog parameter to match
the catalog name. There should be no wildcard escaping there, as it
does not accept a pattern as it does for tableName or schemaName.
  • Loading branch information
kokosing authored and electrum committed Nov 30, 2015
1 parent fa763cf commit dbd9288
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
5 changes: 5 additions & 0 deletions presto-docs/src/main/sphinx/release/release-0.128.rst
Expand Up @@ -14,3 +14,8 @@ General Changes
align with the grouping columns used by a preceding operation such as
``GROUP BY``, ``DISTINCT``, etc. When this triggers, the join may fail to
produce some of the output rows.

MySQL Changes
-------------

* Fix handling of MySQL database names with underscores.
Expand Up @@ -91,7 +91,7 @@ protected ResultSet getTables(Connection connection, String schemaName, String t
DatabaseMetaData metadata = connection.getMetaData();
String escape = metadata.getSearchStringEscape();
return metadata.getTables(
escapeNamePattern(schemaName, escape),
schemaName,
null,
escapeNamePattern(tableName, escape),
new String[] {"TABLE"});
Expand Down
Expand Up @@ -13,14 +13,18 @@
*/
package com.facebook.presto.plugin.mysql;

import com.facebook.presto.Session;
import com.facebook.presto.tests.AbstractTestIntegrationSmokeTest;
import io.airlift.testing.mysql.TestingMySqlServer;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

import static com.facebook.presto.plugin.mysql.MySqlQueryRunner.createMySqlQueryRunner;
import static com.facebook.presto.testing.TestingSession.testSessionBuilder;
import static io.airlift.testing.Closeables.closeAllRuntimeException;
import static io.airlift.tpch.TpchTable.ORDERS;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

@Test
public class TestMySqlIntegrationSmokeTest
Expand All @@ -31,7 +35,7 @@ public class TestMySqlIntegrationSmokeTest
public TestMySqlIntegrationSmokeTest()
throws Exception
{
this(new TestingMySqlServer("testuser", "testpass", "tpch"));
this(new TestingMySqlServer("testuser", "testpass", "tpch", "test_database"));
}

public TestMySqlIntegrationSmokeTest(TestingMySqlServer mysqlServer)
Expand All @@ -52,4 +56,24 @@ public final void destroy()
{
closeAllRuntimeException(mysqlServer);
}

@Test
public void testNameEscaping()
throws Exception
{
Session session = testSessionBuilder()
.setCatalog("mysql")
.setSchema("test_database")
.build();

assertFalse(queryRunner.tableExists(session, "test_table"));

assertQuery(session, "CREATE TABLE test_table AS SELECT 123 x", "SELECT 1");
assertTrue(queryRunner.tableExists(session, "test_table"));

assertQuery(session, "SELECT * FROM test_table", "SELECT 123");

assertQueryTrue(session, "DROP TABLE test_table");
assertFalse(queryRunner.tableExists(session, "test_table"));
}
}
Expand Up @@ -129,7 +129,13 @@ protected void assertQueryOrdered(Session session, @Language("SQL") String actua
protected void assertQueryTrue(@Language("SQL") String sql)
throws Exception
{
assertQuery(sql, "SELECT true");
assertQueryTrue(getSession(), sql);
}

protected void assertQueryTrue(Session session, @Language("SQL") String sql)
throws Exception
{
assertQuery(session, sql, "SELECT true");
}

public void assertApproximateQuery(Session session, @Language("SQL") String actual, @Language("SQL") String expected)
Expand Down

0 comments on commit dbd9288

Please sign in to comment.