diff --git a/plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestPostgreSqlConnectorTest.java b/plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestPostgreSqlConnectorTest.java index 6e04bf34cdc3..04e2aadd8b28 100644 --- a/plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestPostgreSqlConnectorTest.java +++ b/plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestPostgreSqlConnectorTest.java @@ -175,7 +175,6 @@ public void testViews() @Test public void testPostgreSqlMaterializedView() - throws Exception { onRemoteDatabase().execute("CREATE MATERIALIZED VIEW test_mv as SELECT * FROM orders"); assertTrue(getQueryRunner().tableExists(getSession(), "test_mv")); @@ -243,39 +242,38 @@ public void testPartitionedTables() @Test public void testTableWithNoSupportedColumns() - throws Exception { String unsupportedDataType = "interval"; String supportedDataType = "varchar(5)"; - try (AutoCloseable ignore1 = withTable("no_supported_columns", format("(c %s)", unsupportedDataType)); - AutoCloseable ignore2 = withTable("supported_columns", format("(good %s)", supportedDataType)); - AutoCloseable ignore3 = withTable("no_columns", "()")) { - assertThat(computeActual("SHOW TABLES").getOnlyColumnAsSet()).contains("orders", "no_supported_columns", "supported_columns", "no_columns"); + try (TestTable noSupportedColumns = new TestTable(onRemoteDatabase(), "no_supported_columns", format("(c %s)", unsupportedDataType)); + TestTable supportedColumns = new TestTable(onRemoteDatabase(), "supported_columns", format("(good %s)", supportedDataType)); + TestTable noColumns = new TestTable(onRemoteDatabase(), "no_columns", "()")) { + assertThat(computeActual("SHOW TABLES").getOnlyColumnAsSet()).contains("orders", noSupportedColumns.getName(), supportedColumns.getName(), noColumns.getName()); - assertQueryFails("SELECT c FROM no_supported_columns", "\\QTable 'tpch.no_supported_columns' has no supported columns (all 1 columns are not supported)"); - assertQueryFails("SELECT * FROM no_supported_columns", "\\QTable 'tpch.no_supported_columns' has no supported columns (all 1 columns are not supported)"); - assertQueryFails("SELECT 'a' FROM no_supported_columns", "\\QTable 'tpch.no_supported_columns' has no supported columns (all 1 columns are not supported)"); + assertQueryFails("SELECT c FROM " + noSupportedColumns.getName(), "\\QTable 'tpch." + noSupportedColumns.getName() + "' has no supported columns (all 1 columns are not supported)"); + assertQueryFails("SELECT * FROM " + noSupportedColumns.getName(), "\\QTable 'tpch." + noSupportedColumns.getName() + "' has no supported columns (all 1 columns are not supported)"); + assertQueryFails("SELECT 'a' FROM " + noSupportedColumns.getName(), "\\QTable 'tpch." + noSupportedColumns.getName() + "' has no supported columns (all 1 columns are not supported)"); - assertQueryFails("SELECT c FROM no_columns", "\\QTable 'tpch.no_columns' has no supported columns (all 0 columns are not supported)"); - assertQueryFails("SELECT * FROM no_columns", "\\QTable 'tpch.no_columns' has no supported columns (all 0 columns are not supported)"); - assertQueryFails("SELECT 'a' FROM no_columns", "\\QTable 'tpch.no_columns' has no supported columns (all 0 columns are not supported)"); + assertQueryFails("SELECT c FROM " + noColumns.getName(), "\\QTable 'tpch." + noColumns.getName() + "' has no supported columns (all 0 columns are not supported)"); + assertQueryFails("SELECT * FROM " + noColumns.getName(), "\\QTable 'tpch." + noColumns.getName() + "' has no supported columns (all 0 columns are not supported)"); + assertQueryFails("SELECT 'a' FROM " + noColumns.getName(), "\\QTable 'tpch." + noColumns.getName() + "' has no supported columns (all 0 columns are not supported)"); assertQueryFails("SELECT c FROM non_existent", ".* Table .*tpch.non_existent.* does not exist"); assertQueryFails("SELECT * FROM non_existent", ".* Table .*tpch.non_existent.* does not exist"); assertQueryFails("SELECT 'a' FROM non_existent", ".* Table .*tpch.non_existent.* does not exist"); - assertQueryFails("SHOW COLUMNS FROM no_supported_columns", "\\QTable 'tpch.no_supported_columns' has no supported columns (all 1 columns are not supported)"); - assertQueryFails("SHOW COLUMNS FROM no_columns", "\\QTable 'tpch.no_columns' has no supported columns (all 0 columns are not supported)"); + assertQueryFails("SHOW COLUMNS FROM " + noSupportedColumns.getName(), "\\QTable 'tpch." + noSupportedColumns.getName() + "' has no supported columns (all 1 columns are not supported)"); + assertQueryFails("SHOW COLUMNS FROM " + noColumns.getName(), "\\QTable 'tpch." + noColumns.getName() + "' has no supported columns (all 0 columns are not supported)"); // Other tables should be visible in SHOW TABLES (the no_supported_columns might be included or might be not) and information_schema.tables assertThat(computeActual("SHOW TABLES").getOnlyColumn()) - .contains("orders", "no_supported_columns", "supported_columns", "no_columns"); + .contains("orders", noSupportedColumns.getName(), supportedColumns.getName(), noColumns.getName()); assertThat(computeActual("SELECT table_name FROM information_schema.tables WHERE table_schema = 'tpch'").getOnlyColumn()) - .contains("orders", "no_supported_columns", "supported_columns", "no_columns"); + .contains("orders", noSupportedColumns.getName(), supportedColumns.getName(), noColumns.getName()); // Other tables should be introspectable with SHOW COLUMNS and information_schema.columns - assertQuery("SHOW COLUMNS FROM supported_columns", "VALUES ('good', 'varchar(5)', '', '')"); + assertQuery("SHOW COLUMNS FROM " + supportedColumns.getName(), "VALUES ('good', 'varchar(5)', '', '')"); // Listing columns in all tables should not fail due to tables with no columns computeActual("SELECT column_name FROM information_schema.columns WHERE table_schema = 'tpch'"); @@ -288,13 +286,13 @@ public void testInsertWithFailureDoesNotLeaveBehindOrphanedTable() { String schemaName = format("tmp_schema_%s", UUID.randomUUID().toString().replaceAll("-", "")); try (AutoCloseable schema = withSchema(schemaName); - AutoCloseable table = withTable(format("%s.test_cleanup", schemaName), "(x INTEGER)")) { - assertQuery(format("SELECT table_name FROM information_schema.tables WHERE table_schema = '%s'", schemaName), "VALUES 'test_cleanup'"); + TestTable table = new TestTable(onRemoteDatabase(), format("%s.test_cleanup", schemaName), "(x INTEGER)")) { + assertQuery(format("SELECT table_name FROM information_schema.tables WHERE table_schema = '%s'", schemaName), "VALUES '" + table.getName().replace(schemaName + ".", "") + "'"); - onRemoteDatabase().execute(format("ALTER TABLE %s.test_cleanup ADD CHECK (x > 0)", schemaName)); + onRemoteDatabase().execute("ALTER TABLE " + table.getName() + " ADD CHECK (x > 0)"); - assertQueryFails(format("INSERT INTO %s.test_cleanup (x) VALUES (0)", schemaName), "ERROR: new row .* violates check constraint [\\s\\S]*"); - assertQuery(format("SELECT table_name FROM information_schema.tables WHERE table_schema = '%s'", schemaName), "VALUES 'test_cleanup'"); + assertQueryFails("INSERT INTO " + table.getName() + " (x) VALUES (0)", "ERROR: new row .* violates check constraint [\\s\\S]*"); + assertQuery(format("SELECT table_name FROM information_schema.tables WHERE table_schema = '%s'", schemaName), "VALUES '" + table.getName().replace(schemaName + ".", "") + "'"); } } @@ -623,31 +621,30 @@ public void testStringJoinPushdownWithCollate() @Test public void testDecimalPredicatePushdown() - throws Exception { - try (AutoCloseable ignore = withTable("test_decimal_pushdown", + try (TestTable table = new TestTable(onRemoteDatabase(), "test_decimal_pushdown", "(short_decimal decimal(9, 3), long_decimal decimal(30, 10))")) { - onRemoteDatabase().execute("INSERT INTO test_decimal_pushdown VALUES (123.321, 123456789.987654321)"); + onRemoteDatabase().execute("INSERT INTO " + table.getName() + " VALUES (123.321, 123456789.987654321)"); - assertThat(query("SELECT * FROM test_decimal_pushdown WHERE short_decimal <= 124")) + assertThat(query("SELECT * FROM " + table.getName() + " WHERE short_decimal <= 124")) .matches("VALUES (CAST(123.321 AS decimal(9,3)), CAST(123456789.987654321 AS decimal(30, 10)))") .isFullyPushedDown(); - assertThat(query("SELECT * FROM test_decimal_pushdown WHERE short_decimal <= 124")) + assertThat(query("SELECT * FROM " + table.getName() + " WHERE short_decimal <= 124")) .matches("VALUES (CAST(123.321 AS decimal(9,3)), CAST(123456789.987654321 AS decimal(30, 10)))") .isFullyPushedDown(); - assertThat(query("SELECT * FROM test_decimal_pushdown WHERE long_decimal <= 123456790")) + assertThat(query("SELECT * FROM " + table.getName() + " WHERE long_decimal <= 123456790")) .matches("VALUES (CAST(123.321 AS decimal(9,3)), CAST(123456789.987654321 AS decimal(30, 10)))") .isFullyPushedDown(); - assertThat(query("SELECT * FROM test_decimal_pushdown WHERE short_decimal <= 123.321")) + assertThat(query("SELECT * FROM " + table.getName() + " WHERE short_decimal <= 123.321")) .matches("VALUES (CAST(123.321 AS decimal(9,3)), CAST(123456789.987654321 AS decimal(30, 10)))") .isFullyPushedDown(); - assertThat(query("SELECT * FROM test_decimal_pushdown WHERE long_decimal <= 123456789.987654321")) + assertThat(query("SELECT * FROM " + table.getName() + " WHERE long_decimal <= 123456789.987654321")) .matches("VALUES (CAST(123.321 AS decimal(9,3)), CAST(123456789.987654321 AS decimal(30, 10)))") .isFullyPushedDown(); - assertThat(query("SELECT * FROM test_decimal_pushdown WHERE short_decimal = 123.321")) + assertThat(query("SELECT * FROM " + table.getName() + " WHERE short_decimal = 123.321")) .matches("VALUES (CAST(123.321 AS decimal(9,3)), CAST(123456789.987654321 AS decimal(30, 10)))") .isFullyPushedDown(); - assertThat(query("SELECT * FROM test_decimal_pushdown WHERE long_decimal = 123456789.987654321")) + assertThat(query("SELECT * FROM " + table.getName() + " WHERE long_decimal = 123456789.987654321")) .matches("VALUES (CAST(123.321 AS decimal(9,3)), CAST(123456789.987654321 AS decimal(30, 10)))") .isFullyPushedDown(); } @@ -655,21 +652,20 @@ public void testDecimalPredicatePushdown() @Test public void testCharPredicatePushdown() - throws Exception { - try (AutoCloseable ignore = withTable("test_char_pushdown", + try (TestTable table = new TestTable(onRemoteDatabase(), "test_char_pushdown", "(char_1 char(1), char_5 char(5), char_10 char(10))")) { - onRemoteDatabase().execute("INSERT INTO test_char_pushdown VALUES" + + onRemoteDatabase().execute("INSERT INTO " + table.getName() + " VALUES" + "('0', '0' , '0' )," + "('1', '12345', '1234567890')"); - assertThat(query("SELECT * FROM test_char_pushdown WHERE char_1 = '0' AND char_5 = '0'")) + assertThat(query("SELECT * FROM " + table.getName() + " WHERE char_1 = '0' AND char_5 = '0'")) .matches("VALUES (CHAR'0', CHAR'0 ', CHAR'0 ')") .isFullyPushedDown(); - assertThat(query("SELECT * FROM test_char_pushdown WHERE char_5 = CHAR'12345' AND char_10 = '1234567890'")) + assertThat(query("SELECT * FROM " + table.getName() + " WHERE char_5 = CHAR'12345' AND char_10 = '1234567890'")) .matches("VALUES (CHAR'1', CHAR'12345', CHAR'1234567890')") .isFullyPushedDown(); - assertThat(query("SELECT * FROM test_char_pushdown WHERE char_10 = CHAR'0'")) + assertThat(query("SELECT * FROM " + table.getName() + " WHERE char_10 = CHAR'0'")) .matches("VALUES (CHAR'0', CHAR'0 ', CHAR'0 ')") .isFullyPushedDown(); } @@ -752,19 +748,17 @@ public void testNativeMultipleInClauses() */ @Test public void testTimestampColumnAndTimestampWithTimeZoneConstant() - throws Exception { - String tableName = "test_timestamptz_unwrap_cast" + randomTableSuffix(); - try (AutoCloseable ignored = withTable(tableName, "(id integer, ts_col timestamp(6))")) { - onRemoteDatabase().execute("INSERT INTO " + tableName + " (id, ts_col) VALUES " + + try (TestTable table = new TestTable(onRemoteDatabase(), "test_timestamptz_unwrap_cast", "(id integer, ts_col timestamp(6))")) { + onRemoteDatabase().execute("INSERT INTO " + table.getName() + " (id, ts_col) VALUES " + "(1, timestamp '2020-01-01 01:01:01.000')," + "(2, timestamp '2019-01-01 01:01:01.000')"); - assertThat(query(format("SELECT id FROM %s WHERE ts_col >= TIMESTAMP '2019-01-01 00:00:00 %s'", tableName, getSession().getTimeZoneKey().getId()))) + assertThat(query(format("SELECT id FROM %s WHERE ts_col >= TIMESTAMP '2019-01-01 00:00:00 %s'", table.getName(), getSession().getTimeZoneKey().getId()))) .matches("VALUES 1, 2") .isFullyPushedDown(); - assertThat(query(format("SELECT id FROM %s WHERE ts_col >= TIMESTAMP '2019-01-01 00:00:00 %s'", tableName, "UTC"))) + assertThat(query(format("SELECT id FROM %s WHERE ts_col >= TIMESTAMP '2019-01-01 00:00:00 %s'", table.getName(), "UTC"))) .matches("VALUES 1") .isFullyPushedDown(); } @@ -784,16 +778,6 @@ private AutoCloseable withSchema(String schema) return () -> onRemoteDatabase().execute(format("DROP SCHEMA %s", schema)); } - /** - * @deprecated Use {@link TestTable} instead. - */ - @Deprecated - private AutoCloseable withTable(String tableName, String tableDefinition) - { - onRemoteDatabase().execute(format("CREATE TABLE %s%s", tableName, tableDefinition)); - return () -> onRemoteDatabase().execute(format("DROP TABLE %s", tableName)); - } - @Override protected SqlExecutor onRemoteDatabase() {