From 3b9bee1a44bbe835cc4961bb0c1ef8f52587c841 Mon Sep 17 00:00:00 2001 From: cdmikechen Date: Sun, 25 Aug 2024 17:13:37 +0800 Subject: [PATCH] support view for table list --- .../jdbc/DatabendDatabaseMetaData.java | 19 +++++++++++++++---- .../com/databend/jdbc/TestBasicDriver.java | 6 +++--- .../jdbc/TestDatabendDatabaseMetaData.java | 11 +++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDatabaseMetaData.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDatabaseMetaData.java index 06618951..8b58a9b5 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDatabaseMetaData.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDatabaseMetaData.java @@ -13,10 +13,7 @@ import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import java.sql.Statement; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -889,6 +886,20 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam buildFilters(sql, filters); sql.append("\nORDER BY table_type, table_catalog, table_schema, table_name"); + if (types == null || types.length == 0 || Arrays.stream(types).allMatch(t -> t.equalsIgnoreCase("VIEW"))) { + // add view + sql.append("\n union all "); + sql.append("\nselect database TABLE_CAT, database TABLE_SCHEM, name TABLE_NAME, 'VIEW' TABLE_TYPE, null REMARKS, "); + sql.append("'' as TYPE_CAT, engine as TYPE_SCHEM, engine as TYPE_NAME, '' as SELF_REFERENCING_COL_NAME, '' as REF_GENERATION "); + sql.append("from system.views "); + filters = new ArrayList<>(); + emptyStringEqualsFilter(filters, "database", catalog); + emptyStringLikeFilter(filters, "database", schemaPattern); + optionalStringLikeFilter(filters, "name", tableNamePattern); + buildFilters(sql, filters); + sql.append("\nORDER BY TABLE_CAT, TABLE_NAME, TABLE_TYPE"); + } + return select(sql.toString()); } diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestBasicDriver.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestBasicDriver.java index c8758e6d..e779bdcd 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestBasicDriver.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestBasicDriver.java @@ -334,7 +334,7 @@ public void testSelectWithPreparement() } } - @Test(groups = {"IT", "FLAKY"}) + @Test(groups = {"IT"}) public void testSelectGeometry() throws SQLException, ParseException { // skip due to failed cluster tests @@ -344,7 +344,7 @@ public void testSelectGeometry() throws SQLException, ParseException { connection.createStatement().execute("INSERT INTO cities (id, name, location) VALUES (1, 'New York', 'POINT (-73.935242 40.73061))');"); connection.createStatement().execute("INSERT INTO cities (id, name, location) VALUES (2, 'Null', null);"); Statement statement = connection.createStatement(); - try (ResultSet r = statement.executeQuery("select location from cities")) { + try (ResultSet r = statement.executeQuery("select location from cities order by id")) { r.next(); Assert.assertEquals("{\"type\": \"Point\", \"coordinates\": [-73.935242,40.73061]}", r.getObject(1)); r.next(); @@ -353,7 +353,7 @@ public void testSelectGeometry() throws SQLException, ParseException { // set geometry_output_format to wkb connection.createStatement().execute("set geometry_output_format='WKB'"); - try (ResultSet r = statement.executeQuery("select location from cities")) { + try (ResultSet r = statement.executeQuery("select location from cities order by id")) { r.next(); byte[] wkb = r.getBytes(1); WKBReader wkbReader = new WKBReader(); diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDatabaseMetaData.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDatabaseMetaData.java index 53ac3cc9..a95b5684 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDatabaseMetaData.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDatabaseMetaData.java @@ -64,9 +64,11 @@ public void setUp() c.createStatement().execute("drop table if exists test_column_meta"); c.createStatement().execute("drop table if exists decimal_test"); c.createStatement().execute("drop table if exists test_comment"); + c.createStatement().execute("drop view if exists v_test_comment"); c.createStatement().execute("create table test_column_meta (nu1 uint8 null, u1 uint8, u2 uint16, u3 uint32, u4 uint64, i1 int8, i2 int16, i3 int32, i4 int64, f1 float32, f2 float64, s1 string,d1 date, d2 datetime, v1 variant, a1 array(int64), t1 Tuple(x Int64, y Int64 NULL)) engine = fuse"); c.createStatement().execute("create table decimal_test (a decimal(4,2))"); c.createStatement().execute("create table test_comment (a int comment 'test comment')"); + c.createStatement().execute("create view v_test_comment as select * from test_comment"); // json data } @@ -121,6 +123,15 @@ public void testGetTables() throws Exception { DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet rs = connection.getMetaData().getTables(null, null, null, null)) { assertTableMetadata(rs); + // test for view + boolean foundView = false; + while (rs.next()) { + if ("v_test_comment".equals(rs.getString(3))) { + foundView = true; + break; + } + } + Assert.assertTrue(foundView, "can not found view [v_test_comment]"); } } }