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 12ef87c7..822d5ec1 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDatabaseMetaData.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDatabaseMetaData.java @@ -12,10 +12,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; @@ -885,9 +882,41 @@ 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 (checkVersionAddView() && types != null && 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()); } + // This handles bug that existed a while, views were not included in information_schema.tables + // https://github.com/datafuselabs/databend/issues/16039 + private boolean checkVersionAddView() throws SQLException { + // the same fix for python-sdk + // https://github.com/databendlabs/databend-sqlalchemy/blob/3226f10e0f8b6aa85185208583977037b33ec99f/databend_sqlalchemy/databend_dialect.py#L819 + String version = getDatabaseProductVersion(); + Pattern pattern = Pattern.compile("v(\\d+)\\.(\\d+)\\.(\\d+)"); + Matcher matcher = pattern.matcher(version); + if (matcher.find()) { + // > 1.2.410 and <= 1.2.566 + if (Integer.parseInt(matcher.group(1)) != 1) return false; + if (Integer.parseInt(matcher.group(2)) != 2) return false; + int minorVersion = Integer.parseInt(matcher.group(3)); + return minorVersion > 410 && minorVersion <= 566; + } + return false; + } + @Override public ResultSet getSchemas() throws SQLException { 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 dbda69a5..d3bae702 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestBasicDriver.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestBasicDriver.java @@ -325,7 +325,7 @@ public void testSelectWithPreparement() } } - @Test(groups = {"IT", "FLAKY"}) + @Test(groups = {"IT"}) public void testSelectGeometry() throws SQLException, ParseException { // skip due to failed cluster tests @@ -335,7 +335,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(); @@ -344,7 +344,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 0977a901..45a61d11 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDatabaseMetaData.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDatabaseMetaData.java @@ -59,9 +59,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 } @@ -110,6 +112,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]"); } } }