This is an automated email from the ASF dual-hosted git repository.

jcamacho pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git

commit 3071a157bd3c2b2c94d0abc7534d6285aa5a3268
Author: Jesus Camacho Rodriguez <[email protected]>
AuthorDate: Fri Dec 7 15:42:13 2018 -0800

    [CALCITE-2733] Use catalog and schema from JDBC connection string to 
retrieve tables if specified
    
    Close apache/calcite#962
---
 .../apache/calcite/adapter/jdbc/JdbcSchema.java    | 12 ++++++++++
 .../org/apache/calcite/test/JdbcAdapterTest.java   | 28 ++++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcSchema.java 
b/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcSchema.java
index 37893d7..0142adc 100644
--- a/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcSchema.java
+++ b/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcSchema.java
@@ -232,6 +232,18 @@ public class JdbcSchema implements Schema {
     try {
       connection = dataSource.getConnection();
       DatabaseMetaData metaData = connection.getMetaData();
+      String catalog;
+      String schema;
+      if (metaData.getJDBCMajorVersion() > 4
+          || (metaData.getJDBCMajorVersion() == 4 && 
metaData.getJDBCMinorVersion() >= 1)) {
+        // From JDBC 4.1, catalog and schema can be retrieved from the 
connection object,
+        // hence try to get it from there if it was not specified by user
+        catalog = Util.first(this.catalog, connection.getCatalog());
+        schema = Util.first(this.schema, connection.getSchema());
+      } else {
+        catalog = this.catalog;
+        schema = this.schema;
+      }
       resultSet = metaData.getTables(
           catalog,
           schema,
diff --git a/core/src/test/java/org/apache/calcite/test/JdbcAdapterTest.java 
b/core/src/test/java/org/apache/calcite/test/JdbcAdapterTest.java
index eae066b..6f18029 100644
--- a/core/src/test/java/org/apache/calcite/test/JdbcAdapterTest.java
+++ b/core/src/test/java/org/apache/calcite/test/JdbcAdapterTest.java
@@ -438,6 +438,34 @@ public class JdbcAdapterTest {
             + "FROM \"foodmart\".\"expense_fact\"");
   }
 
+  @Test public void testTablesNoCatalogSchema() {
+    final String model =
+        JdbcTest.FOODMART_MODEL
+            .replace("jdbcCatalog: 'foodmart'", "jdbcCatalog: null")
+            .replace("jdbcSchema: 'foodmart'", "jdbcSchema: null");
+    // Since Calcite uses PostgreSQL JDBC driver version >= 4.1,
+    // catalog/schema can be retrieved from JDBC connection and
+    // this test succeeds
+    CalciteAssert.model(model)
+        // Calcite uses PostgreSQL JDBC driver version >= 4.1
+        .enable(CalciteAssert.DB == DatabaseInstance.POSTGRESQL)
+        .query("select \"store_id\", \"account_id\", \"exp_date\","
+            + " \"time_id\", \"category_id\", \"currency_id\", \"amount\","
+            + " last_value(\"time_id\") over ()"
+            + " as \"last_version\" from \"expense_fact\"")
+        .runs();
+    // Since Calcite uses HSQLDB JDBC driver version < 4.1,
+    // catalog/schema cannot be retrieved from JDBC connection and
+    // this test fails
+    CalciteAssert.model(model)
+        .enable(CalciteAssert.DB == DatabaseInstance.HSQLDB)
+        .query("select \"store_id\", \"account_id\", \"exp_date\","
+            + " \"time_id\", \"category_id\", \"currency_id\", \"amount\","
+            + " last_value(\"time_id\") over ()"
+            + " as \"last_version\" from \"expense_fact\"")
+        .throws_("'expense_fact' not found");
+  }
+
   /** Test case for
    * <a 
href="https://issues.apache.org/jira/browse/CALCITE-1506";>[CALCITE-1506]
    * Push OVER Clause to underlying SQL via JDBC adapter</a>.

Reply via email to