From 1f04cb7dd8cd45d9b4c11ac375711b20730197c4 Mon Sep 17 00:00:00 2001 From: cdmikechen Date: Sun, 25 Aug 2024 17:13:37 +0800 Subject: [PATCH 1/2] 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 12ef87c7..9e787fff 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,6 +882,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 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]"); } } } From 3b241e64a2c5bbbbfc790dcf6c14c486a78fb98b Mon Sep 17 00:00:00 2001 From: cdmikechen Date: Tue, 29 Oct 2024 13:17:32 +0800 Subject: [PATCH 2/2] add version check --- .../jdbc/DatabendDatabaseMetaData.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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 9e787fff..822d5ec1 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDatabaseMetaData.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendDatabaseMetaData.java @@ -882,7 +882,7 @@ 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"))) { + 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, "); @@ -899,6 +899,24 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam 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 {