You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@drill.apache.org by GitBox <gi...@apache.org> on 2019/12/26 15:05:38 UTC

[GitHub] [drill] ihuzenko commented on a change in pull request #1940: DRILL-7406: Update Calcite to 1.21.0

ihuzenko commented on a change in pull request #1940: DRILL-7406: Update Calcite to 1.21.0
URL: https://github.com/apache/drill/pull/1940#discussion_r361473590
 
 

 ##########
 File path: contrib/storage-jdbc/src/main/java/org/apache/drill/exec/store/jdbc/JdbcCatalogSchema.java
 ##########
 @@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.drill.exec.store.jdbc;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.calcite.schema.Schema;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.schema.Table;
+import org.apache.calcite.sql.SqlDialect;
+import org.apache.drill.exec.store.AbstractSchema;
+import org.apache.drill.exec.store.SchemaFactory;
+
+class JdbcCatalogSchema extends AbstractSchema {
+
+  private final Map<String, CapitalizingJdbcSchema> schemaMap;
+  private final CapitalizingJdbcSchema defaultSchema;
+
+  JdbcCatalogSchema(String name, DataSource source, SqlDialect dialect, DrillJdbcConvention convention, boolean caseSensitive) {
+    super(Collections.emptyList(), name);
+    this.schemaMap = new HashMap<>();
+    String connectionSchemaName = null;
+    try (Connection con = source.getConnection();
+         ResultSet set = con.getMetaData().getCatalogs()) {
+      connectionSchemaName = con.getSchema();
+      while (set.next()) {
+        final String catalogName = set.getString(1);
+        CapitalizingJdbcSchema schema = new CapitalizingJdbcSchema(
+            getSchemaPath(), catalogName, source, dialect, convention, catalogName, null, caseSensitive);
+        schemaMap.put(schema.getName(), schema);
+      }
+    } catch (SQLException e) {
+      JdbcStoragePlugin.logger.warn("Failure while attempting to load JDBC schema.", e);
+    }
+
+    // unable to read catalog list.
+    if (schemaMap.isEmpty()) {
+
+      // try to add a list of schemas to the schema map.
+      boolean schemasAdded = addSchemas(source, dialect, convention, caseSensitive);
+
+      if (!schemasAdded) {
+        // there were no schemas, just create a default one (the jdbc system doesn't support catalogs/schemas).
+        schemaMap.put(SchemaFactory.DEFAULT_WS_NAME, new CapitalizingJdbcSchema(Collections.emptyList(), name, source, dialect, convention, null, null, caseSensitive));
+      }
+    } else {
+      // We already have catalogs. Add schemas in this context of their catalogs.
+      addSchemas(source, dialect, convention, caseSensitive);
+    }
+
+    defaultSchema = determineDefaultSchema(connectionSchemaName);
+  }
+
+  private CapitalizingJdbcSchema determineDefaultSchema(String connectionSchemaName) {
+    CapitalizingJdbcSchema connSchema;
+    if (connectionSchemaName == null ||
+        (connSchema = schemaMap.get(connectionSchemaName.toLowerCase())) == null) {
+      // todo: this selection of default schema isn't correct, users are getting implicit random schema as default
+      connSchema = schemaMap.values().iterator().next();
+    }
+    return (CapitalizingJdbcSchema) connSchema.getDefaultSchema();
+  }
+
+  void setHolder(SchemaPlus plusOfThis) {
+    for (String s : getSubSchemaNames()) {
+      CapitalizingJdbcSchema inner = getSubSchema(s);
+      SchemaPlus holder = plusOfThis.add(s, inner);
+      inner.setHolder(holder);
+    }
+  }
+
+  private boolean addSchemas(DataSource source, SqlDialect dialect, DrillJdbcConvention convention, boolean caseSensitive) {
+    boolean added = false;
+    try (Connection con = source.getConnection();
+         ResultSet set = con.getMetaData().getSchemas()) {
+      while (set.next()) {
+        final String schemaName = set.getString(1);
+        final String catalogName = set.getString(2);
+
+        String parentKey = catalogName == null ? null : catalogName.toLowerCase();
+        CapitalizingJdbcSchema parentSchema = schemaMap.get(parentKey);
+        if (parentSchema == null) {
+          CapitalizingJdbcSchema schema = new CapitalizingJdbcSchema(getSchemaPath(), schemaName, source, dialect,
+              convention, catalogName, schemaName, caseSensitive);
+
+          // if a catalog schema doesn't exist, we'll add this at the top level.
+          schemaMap.put(schema.getName(), schema);
+        } else {
+          CapitalizingJdbcSchema schema = new CapitalizingJdbcSchema(parentSchema.getSchemaPath(), schemaName,
+              source, dialect,
+              convention, catalogName, schemaName, caseSensitive);
+          parentSchema.schemaMap.put(schema.getName(), schema);
+        }
+        added = true;
+      }
+    } catch (SQLException e) {
+      JdbcStoragePlugin.logger.warn("Failure while attempting to load JDBC schema.", e);
+    }
+
+    return added;
+  }
+
+
+  @Override
+  public String getTypeName() {
+    return JdbcStorageConfig.NAME;
+  }
+
+  @Override
+  public Schema getDefaultSchema() {
+    return defaultSchema;
+  }
+
+  @Override
+  public CapitalizingJdbcSchema getSubSchema(String name) {
+    return schemaMap.get(name);
+  }
+
+  @Override
+  public Set<String> getSubSchemaNames() {
+    return schemaMap.keySet();
+  }
+
+  @Override
+  public Table getTable(String name) {
+    if (defaultSchema != null) {
+      try {
+        return defaultSchema.getTable(name);
+      } catch (RuntimeException e) {
 
 Review comment:
   Migrated code from ```JdbcStoragePlugin```. Since the method from ```try``` doesn't declare checked exceptions the ```catch``` is just enough.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services