Skip to content

Commit daa6bba

Browse files
committed
Refactor BaseJdbcConnectorTest native-query tests
Make testNativeQuery* conditional based on whether connector supports native-query passthrough table functions. This removes a lot of duplication and also makes it easier to verify correct behaviour without overriding tests.
1 parent 2449f9d commit daa6bba

File tree

5 files changed

+35
-318
lines changed

5 files changed

+35
-318
lines changed

plugin/trino-base-jdbc/src/test/java/io/trino/plugin/jdbc/BaseJdbcConnectorTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_JOIN_PUSHDOWN_WITH_VARCHAR_INEQUALITY;
9595
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_LIMIT_PUSHDOWN;
9696
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_MERGE;
97+
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_NATIVE_QUERY;
9798
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_PREDICATE_ARITHMETIC_EXPRESSION_PUSHDOWN;
9899
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_PREDICATE_EXPRESSION_PUSHDOWN_WITH_LIKE;
99100
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_PREDICATE_PUSHDOWN;
@@ -1699,15 +1700,29 @@ public static Object[][] batchSizeAndTotalNumberOfRowsToInsertDataProvider()
16991700
};
17001701
}
17011702

1703+
@Test
1704+
public void verifySupportsNativeQueryDeclaration()
1705+
{
1706+
if (hasBehavior(SUPPORTS_NATIVE_QUERY)) {
1707+
// Covered by testNativeQuerySelectFromNation
1708+
return;
1709+
}
1710+
assertQueryFails(
1711+
format("SELECT * FROM TABLE(system.query(query => 'SELECT name FROM %s.nation WHERE nationkey = 0'))", getSession().getSchema().orElseThrow()),
1712+
"line 1:21: Table function system.query not registered");
1713+
}
1714+
17021715
@Test
17031716
public void testNativeQuerySimple()
17041717
{
1718+
skipTestUnless(hasBehavior(SUPPORTS_NATIVE_QUERY));
17051719
assertQuery("SELECT * FROM TABLE(system.query(query => 'SELECT 1'))", "VALUES 1");
17061720
}
17071721

17081722
@Test
17091723
public void testNativeQueryParameters()
17101724
{
1725+
skipTestUnless(hasBehavior(SUPPORTS_NATIVE_QUERY));
17111726
Session session = Session.builder(getSession())
17121727
.addPreparedStatement("my_query_simple", "SELECT * FROM TABLE(system.query(query => ?))")
17131728
.addPreparedStatement("my_query", "SELECT * FROM TABLE(system.query(query => format('SELECT %s FROM %s', ?, ?)))")
@@ -1719,6 +1734,7 @@ public void testNativeQueryParameters()
17191734
@Test
17201735
public void testNativeQuerySelectFromNation()
17211736
{
1737+
skipTestUnless(hasBehavior(SUPPORTS_NATIVE_QUERY));
17221738
assertQuery(
17231739
format("SELECT * FROM TABLE(system.query(query => 'SELECT name FROM %s.nation WHERE nationkey = 0'))", getSession().getSchema().orElseThrow()),
17241740
"VALUES 'ALGERIA'");
@@ -1727,6 +1743,7 @@ public void testNativeQuerySelectFromNation()
17271743
@Test
17281744
public void testNativeQuerySelectFromTestTable()
17291745
{
1746+
skipTestUnless(hasBehavior(SUPPORTS_NATIVE_QUERY));
17301747
try (TestTable testTable = simpleTable()) {
17311748
assertQuery(
17321749
format("SELECT * FROM TABLE(system.query(query => 'SELECT * FROM %s'))", testTable.getName()),
@@ -1737,6 +1754,7 @@ public void testNativeQuerySelectFromTestTable()
17371754
@Test
17381755
public void testNativeQueryColumnAlias()
17391756
{
1757+
skipTestUnless(hasBehavior(SUPPORTS_NATIVE_QUERY));
17401758
// The output column type may differ per connector. Skipping the check because it's unrelated to the test purpose.
17411759
assertThat(query(format("SELECT region_name FROM TABLE(system.query(query => 'SELECT name AS region_name FROM %s.region WHERE regionkey = 0'))", getSession().getSchema().orElseThrow())))
17421760
.skippingTypesCheck()
@@ -1746,6 +1764,7 @@ public void testNativeQueryColumnAlias()
17461764
@Test
17471765
public void testNativeQueryColumnAliasNotFound()
17481766
{
1767+
skipTestUnless(hasBehavior(SUPPORTS_NATIVE_QUERY));
17491768
assertQueryFails(
17501769
format("SELECT name FROM TABLE(system.query(query => 'SELECT name AS region_name FROM %s.region'))", getSession().getSchema().orElseThrow()),
17511770
".* Column 'name' cannot be resolved");
@@ -1757,6 +1776,7 @@ public void testNativeQueryColumnAliasNotFound()
17571776
@Test
17581777
public void testNativeQuerySelectUnsupportedType()
17591778
{
1779+
skipTestUnless(hasBehavior(SUPPORTS_NATIVE_QUERY));
17601780
try (TestTable testTable = createTableWithUnsupportedColumn()) {
17611781
String unqualifiedTableName = testTable.getName().replaceAll("^\\w+\\.", "");
17621782
// Check that column 'two' is not supported.
@@ -1770,6 +1790,7 @@ public void testNativeQuerySelectUnsupportedType()
17701790
@Test
17711791
public void testNativeQueryCreateStatement()
17721792
{
1793+
skipTestUnless(hasBehavior(SUPPORTS_NATIVE_QUERY));
17731794
assertFalse(getQueryRunner().tableExists(getSession(), "numbers"));
17741795
assertThatThrownBy(() -> query("SELECT * FROM TABLE(system.query(query => 'CREATE TABLE numbers(n INTEGER)'))"))
17751796
.hasMessageContaining("Query not supported: ResultSetMetaData not available for query: CREATE TABLE numbers(n INTEGER)");
@@ -1779,6 +1800,7 @@ public void testNativeQueryCreateStatement()
17791800
@Test
17801801
public void testNativeQueryInsertStatementTableDoesNotExist()
17811802
{
1803+
skipTestUnless(hasBehavior(SUPPORTS_NATIVE_QUERY));
17821804
assertFalse(getQueryRunner().tableExists(getSession(), "non_existent_table"));
17831805
assertThatThrownBy(() -> query("SELECT * FROM TABLE(system.query(query => 'INSERT INTO non_existent_table VALUES (1)'))"))
17841806
.hasMessageContaining("Failed to get table handle for prepared query");
@@ -1787,6 +1809,7 @@ public void testNativeQueryInsertStatementTableDoesNotExist()
17871809
@Test
17881810
public void testNativeQueryInsertStatementTableExists()
17891811
{
1812+
skipTestUnless(hasBehavior(SUPPORTS_NATIVE_QUERY));
17901813
try (TestTable testTable = simpleTable()) {
17911814
assertThatThrownBy(() -> query(format("SELECT * FROM TABLE(system.query(query => 'INSERT INTO %s VALUES (3)'))", testTable.getName())))
17921815
.hasMessageContaining(format("Query not supported: ResultSetMetaData not available for query: INSERT INTO %s VALUES (3)", testTable.getName()));
@@ -1797,6 +1820,7 @@ public void testNativeQueryInsertStatementTableExists()
17971820
@Test
17981821
public void testNativeQueryIncorrectSyntax()
17991822
{
1823+
skipTestUnless(hasBehavior(SUPPORTS_NATIVE_QUERY));
18001824
assertThatThrownBy(() -> query("SELECT * FROM TABLE(system.query(query => 'some wrong syntax'))"))
18011825
.hasMessageContaining("Failed to get table handle for prepared query");
18021826
}

plugin/trino-clickhouse/src/test/java/io/trino/plugin/clickhouse/TestClickHouseConnectorTest.java

Lines changed: 3 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import com.google.common.collect.ImmutableList;
1717
import com.google.common.collect.ImmutableMap;
18-
import io.trino.Session;
1918
import io.trino.plugin.jdbc.BaseJdbcConnectorTest;
2019
import io.trino.sql.planner.plan.AggregationNode;
2120
import io.trino.testing.MaterializedResult;
@@ -81,6 +80,9 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
8180
case SUPPORTS_NEGATIVE_DATE:
8281
return false;
8382

83+
case SUPPORTS_NATIVE_QUERY:
84+
return false;
85+
8486
default:
8587
return super.hasBehavior(connectorBehavior);
8688
}
@@ -660,115 +662,6 @@ public void testCharTrailingSpace()
660662
throw new SkipException("Implement test for ClickHouse");
661663
}
662664

663-
@Override
664-
public void testNativeQuerySimple()
665-
{
666-
// table function disabled for ClickHouse, because it doesn't provide ResultSetMetaData, so the result relation type cannot be determined
667-
assertQueryFails("SELECT * FROM TABLE(system.query(query => 'SELECT 1'))", "line 1:21: Table function system.query not registered");
668-
}
669-
670-
@Override
671-
public void testNativeQueryParameters()
672-
{
673-
// table function disabled for ClickHouse, because it doesn't provide ResultSetMetaData, so the result relation type cannot be determined
674-
Session session = Session.builder(getSession())
675-
.addPreparedStatement("my_query_simple", "SELECT * FROM TABLE(system.query(query => ?))")
676-
.addPreparedStatement("my_query", "SELECT * FROM TABLE(system.query(query => format('SELECT %s FROM %s', ?, ?)))")
677-
.build();
678-
assertQueryFails(session, "EXECUTE my_query_simple USING 'SELECT 1 a'", "line 1:21: Table function system.query not registered");
679-
assertQueryFails(session, "EXECUTE my_query USING 'a', '(SELECT 2 a) t'", "line 1:21: Table function system.query not registered");
680-
}
681-
682-
@Override
683-
public void testNativeQuerySelectFromNation()
684-
{
685-
// table function disabled for ClickHouse, because it doesn't provide ResultSetMetaData, so the result relation type cannot be determined
686-
assertQueryFails(
687-
format("SELECT * FROM TABLE(system.query(query => 'SELECT name FROM %s.nation WHERE nationkey = 0'))", getSession().getSchema().orElseThrow()),
688-
"line 1:21: Table function system.query not registered");
689-
}
690-
691-
@Override
692-
public void testNativeQuerySelectFromTestTable()
693-
{
694-
// table function disabled for ClickHouse, because it doesn't provide ResultSetMetaData, so the result relation type cannot be determined
695-
try (TestTable testTable = simpleTable()) {
696-
assertQueryFails(
697-
format("SELECT * FROM TABLE(system.query(query => 'SELECT * FROM %s'))", testTable.getName()),
698-
"line 1:21: Table function system.query not registered");
699-
}
700-
}
701-
702-
@Override
703-
public void testNativeQueryColumnAlias()
704-
{
705-
// table function disabled for ClickHouse, because it doesn't provide ResultSetMetaData, so the result relation type cannot be determined
706-
assertQueryFails(
707-
"SELECT * FROM TABLE(system.query(query => 'SELECT name AS region_name FROM tpch.region WHERE regionkey = 0'))",
708-
".* Table function system.query not registered");
709-
}
710-
711-
@Override
712-
public void testNativeQueryColumnAliasNotFound()
713-
{
714-
// table function disabled for ClickHouse, because it doesn't provide ResultSetMetaData, so the result relation type cannot be determined
715-
assertQueryFails(
716-
"SELECT name FROM TABLE(system.query(query => 'SELECT name AS region_name FROM tpch.region'))",
717-
".* Table function system.query not registered");
718-
}
719-
720-
@Override
721-
public void testNativeQuerySelectUnsupportedType()
722-
{
723-
// table function disabled for ClickHouse, because it doesn't provide ResultSetMetaData, so the result relation type cannot be determined
724-
try (TestTable testTable = createTableWithUnsupportedColumn()) {
725-
String unqualifiedTableName = testTable.getName().replaceAll("^\\w+\\.", "");
726-
// Check that column 'two' is not supported.
727-
assertQuery("SELECT column_name FROM information_schema.columns WHERE table_name = '" + unqualifiedTableName + "'", "VALUES 'one', 'three'");
728-
assertUpdate("INSERT INTO " + testTable.getName() + " (one, three) VALUES (123, 'test')", 1);
729-
assertThatThrownBy(() -> query(format("SELECT * FROM TABLE(system.query(query => 'SELECT * FROM %s'))", testTable.getName())))
730-
.hasMessage("line 1:21: Table function system.query not registered");
731-
}
732-
}
733-
734-
@Override
735-
public void testNativeQueryCreateStatement()
736-
{
737-
// table function disabled for ClickHouse, because it doesn't provide ResultSetMetaData, so the result relation type cannot be determined
738-
assertFalse(getQueryRunner().tableExists(getSession(), "numbers"));
739-
assertThatThrownBy(() -> query("SELECT * FROM TABLE(system.query(query => 'CREATE TABLE numbers(n INTEGER)'))"))
740-
.hasMessage("line 1:21: Table function system.query not registered");
741-
assertFalse(getQueryRunner().tableExists(getSession(), "numbers"));
742-
}
743-
744-
@Override
745-
public void testNativeQueryInsertStatementTableDoesNotExist()
746-
{
747-
// table function disabled for ClickHouse, because it doesn't provide ResultSetMetaData, so the result relation type cannot be determined
748-
assertFalse(getQueryRunner().tableExists(getSession(), "non_existent_table"));
749-
assertThatThrownBy(() -> query("SELECT * FROM TABLE(system.query(query => 'INSERT INTO non_existent_table VALUES (1)'))"))
750-
.hasMessage("line 1:21: Table function system.query not registered");
751-
}
752-
753-
@Override
754-
public void testNativeQueryInsertStatementTableExists()
755-
{
756-
// table function disabled for ClickHouse, because it doesn't provide ResultSetMetaData, so the result relation type cannot be determined
757-
try (TestTable testTable = simpleTable()) {
758-
assertThatThrownBy(() -> query(format("SELECT * FROM TABLE(system.query(query => 'INSERT INTO %s VALUES (3)'))", testTable.getName())))
759-
.hasMessage("line 1:21: Table function system.query not registered");
760-
assertQuery("SELECT * FROM " + testTable.getName(), "VALUES 1, 2");
761-
}
762-
}
763-
764-
@Override
765-
public void testNativeQueryIncorrectSyntax()
766-
{
767-
// table function disabled for ClickHouse, because it doesn't provide ResultSetMetaData, so the result relation type cannot be determined
768-
assertThatThrownBy(() -> query("SELECT * FROM TABLE(system.query(query => 'some wrong syntax'))"))
769-
.hasMessage("line 1:21: Table function system.query not registered");
770-
}
771-
772665
@Override
773666
protected TestTable simpleTable()
774667
{

0 commit comments

Comments
 (0)