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

2019-12-30 Thread GitBox
arina-ielchiieva 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_r362072204
 
 

 ##
 File path: 
exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ShowTablesHandler.java
 ##
 @@ -37,17 +32,19 @@
 import org.apache.calcite.tools.RelConversionException;
 import org.apache.calcite.tools.ValidationException;
 import org.apache.calcite.util.NlsString;
-import org.apache.calcite.util.Pair;
 import org.apache.calcite.util.Util;
 import org.apache.drill.common.exceptions.UserException;
 import org.apache.drill.exec.planner.sql.SchemaUtilites;
-import org.apache.drill.exec.planner.sql.SqlConverter;
 import org.apache.drill.exec.planner.sql.parser.DrillParserUtil;
 import org.apache.drill.exec.planner.sql.parser.SqlShowTables;
 import org.apache.drill.exec.store.AbstractSchema;
 import org.apache.drill.exec.store.ischema.InfoSchemaTableType;
 import org.apache.drill.exec.work.foreman.ForemanSetupException;
 
+import static 
org.apache.drill.exec.store.ischema.InfoSchemaConstants.IS_SCHEMA_NAME;
+import static 
org.apache.drill.exec.store.ischema.InfoSchemaConstants.SHRD_COL_TABLE_NAME;
+import static 
org.apache.drill.exec.store.ischema.InfoSchemaConstants.SHRD_COL_TABLE_SCHEMA;
 
 Review comment:
   I guess who initially added such constants named them this way, it means 
shared, referring to the column names which are the same for all information 
schema tables. You can rename all constants if you think this is needed.


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:
us...@infra.apache.org


With regards,
Apache Git Services


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

2019-12-25 Thread GitBox
arina-ielchiieva 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_r361323751
 
 

 ##
 File path: 
contrib/storage-jdbc/src/main/java/org/apache/drill/exec/store/jdbc/JdbcStoragePlugin.java
 ##
 @@ -465,8 +93,7 @@ public SqlDialect getDialect() {
   }
 
   @Override
-  public AbstractGroupScan getPhysicalScan(String userName, JSONOptions 
selection, List columns)
-  throws IOException {
+  public AbstractGroupScan getPhysicalScan(String userName, JSONOptions 
selection, List columns) {
 throw new UnsupportedOperationException();
 
 Review comment:
   Please add message to the exception.


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:
us...@infra.apache.org


With regards,
Apache Git Services


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

2019-12-25 Thread GitBox
arina-ielchiieva 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_r361323795
 
 

 ##
 File path: 
contrib/storage-jdbc/src/test/java/org/apache/drill/exec/store/jdbc/TestJdbcWithMySQLandH2PluginsIT.java
 ##
 @@ -0,0 +1,638 @@
+/*
+ * 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 com.wix.mysql.EmbeddedMysql;
+import com.wix.mysql.ScriptResolver;
+import com.wix.mysql.config.MysqldConfig;
+import com.wix.mysql.config.SchemaConfig;
+import com.wix.mysql.distribution.Version;
+import org.apache.drill.categories.JdbcStorageTest;
+import org.apache.drill.common.config.DrillProperties;
+import org.apache.drill.exec.ExecConstants;
+import org.apache.drill.exec.expr.fn.impl.DateUtility;
+
+import org.apache.drill.exec.server.DrillbitContext;
+import org.apache.drill.exec.store.StoragePluginRegistryImpl;
+import org.apache.drill.exec.util.StoragePluginTestUtils;
+import org.apache.drill.test.ClusterFixture;
+import org.apache.drill.test.ClusterTest;
+import org.apache.drill.test.QueryTestUtil;
+import org.h2.tools.RunScript;
+import org.joda.time.DateTimeZone;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Assume;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.io.FileReader;
+import java.math.BigDecimal;
+import java.net.URL;
+import java.nio.file.Paths;
+import java.sql.Connection;
+import java.sql.DriverManager;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+/**
+ * JDBC storage plugin tests against H2 and MySQL data sources.
+ */
+@Category(JdbcStorageTest.class)
+public class TestJdbcWithMySQLandH2PluginsIT extends ClusterTest {
+
+  private static final String TABLE_PATH = "jdbcmulti/";
+  private static final String TABLE_NAME = String.format("%s.`%s`", 
StoragePluginTestUtils.DFS_PLUGIN_NAME, TABLE_PATH);
+
+  private static EmbeddedMysql mysqld;
+
+  @BeforeClass
+  public static void init() throws Exception {
+startCluster(ClusterFixture.builder(dirTestWatcher));
+dirTestWatcher.copyResourceToRoot(Paths.get(TABLE_PATH));
+initH2();
+initMySQL();
+  }
+
+  @AfterClass
+  public static void stopMysql() {
+if (mysqld != null) {
+  mysqld.stop();
+}
+  }
+
+  @After
+  public void resetSchema() {
+client.resetSession(DrillProperties.SCHEMA);
+  }
+
+  private static void initH2() throws Exception {
+Class.forName("org.h2.Driver");
+String connString = String.format(
+"jdbc:h2:%s", dirTestWatcher.getTmpDir().getCanonicalPath());
+
+try (Connection connection = DriverManager.getConnection(connString, 
"root", "root")) {
+  URL scriptFile = 
TestJdbcWithMySQLandH2PluginsIT.class.getClassLoader().getResource("h2-test-data.sql");
+  Assert.assertNotNull("Script for test tables generation 
'h2-test-data.sql' " +
 
 Review comment:
   Mostly in Drill static import is used in tests.


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:
us...@infra.apache.org


With regards,
Apache Git Services


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

2019-12-25 Thread GitBox
arina-ielchiieva 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_r361323267
 
 

 ##
 File path: 
contrib/storage-jdbc/src/main/java/org/apache/drill/exec/store/jdbc/CapitalizingJdbcSchema.java
 ##
 @@ -0,0 +1,122 @@
+/*
+ * 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.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.calcite.adapter.jdbc.JdbcConvention;
+import org.apache.calcite.adapter.jdbc.JdbcSchema;
+import org.apache.calcite.schema.Function;
+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;
+
+class CapitalizingJdbcSchema extends AbstractSchema {
+
+  final Map schemaMap;
 
 Review comment:
   private?


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:
us...@infra.apache.org


With regards,
Apache Git Services


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

2019-12-25 Thread GitBox
arina-ielchiieva 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_r361323595
 
 

 ##
 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 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, cas

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

2019-12-25 Thread GitBox
arina-ielchiieva 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_r361323447
 
 

 ##
 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 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);
 
 Review comment:
   Why are using logger from different class?


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:
us...@infra.apache.org


With regards,
Apache Git Services


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

2019-12-25 Thread GitBox
arina-ielchiieva 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_r361323519
 
 

 ##
 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 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
 
 Review comment:
   Please do not add todo-s in production code unless you have created Jira 
with the description of the needed fix in order to add Jira number here.


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:
us...@infra.apache.org


With regards,
Apache Git Services


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

2019-12-25 Thread GitBox
arina-ielchiieva 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_r361324076
 
 

 ##
 File path: 
contrib/storage-jdbc/src/test/java/org/apache/drill/exec/store/jdbc/TestJdbcWithMySQLandH2PluginsIT.java
 ##
 @@ -0,0 +1,638 @@
+/*
+ * 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 com.wix.mysql.EmbeddedMysql;
+import com.wix.mysql.ScriptResolver;
+import com.wix.mysql.config.MysqldConfig;
+import com.wix.mysql.config.SchemaConfig;
+import com.wix.mysql.distribution.Version;
+import org.apache.drill.categories.JdbcStorageTest;
+import org.apache.drill.common.config.DrillProperties;
+import org.apache.drill.exec.ExecConstants;
+import org.apache.drill.exec.expr.fn.impl.DateUtility;
+
+import org.apache.drill.exec.server.DrillbitContext;
+import org.apache.drill.exec.store.StoragePluginRegistryImpl;
+import org.apache.drill.exec.util.StoragePluginTestUtils;
+import org.apache.drill.test.ClusterFixture;
+import org.apache.drill.test.ClusterTest;
+import org.apache.drill.test.QueryTestUtil;
+import org.h2.tools.RunScript;
+import org.joda.time.DateTimeZone;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Assume;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.io.FileReader;
+import java.math.BigDecimal;
+import java.net.URL;
+import java.nio.file.Paths;
+import java.sql.Connection;
+import java.sql.DriverManager;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+/**
+ * JDBC storage plugin tests against H2 and MySQL data sources.
+ */
+@Category(JdbcStorageTest.class)
+public class TestJdbcWithMySQLandH2PluginsIT extends ClusterTest {
+
+  private static final String TABLE_PATH = "jdbcmulti/";
+  private static final String TABLE_NAME = String.format("%s.`%s`", 
StoragePluginTestUtils.DFS_PLUGIN_NAME, TABLE_PATH);
+
+  private static EmbeddedMysql mysqld;
+
+  @BeforeClass
+  public static void init() throws Exception {
+startCluster(ClusterFixture.builder(dirTestWatcher));
+dirTestWatcher.copyResourceToRoot(Paths.get(TABLE_PATH));
+initH2();
+initMySQL();
+  }
+
+  @AfterClass
+  public static void stopMysql() {
+if (mysqld != null) {
+  mysqld.stop();
+}
+  }
+
+  @After
+  public void resetSchema() {
+client.resetSession(DrillProperties.SCHEMA);
+  }
+
+  private static void initH2() throws Exception {
+Class.forName("org.h2.Driver");
+String connString = String.format(
+"jdbc:h2:%s", dirTestWatcher.getTmpDir().getCanonicalPath());
+
+try (Connection connection = DriverManager.getConnection(connString, 
"root", "root")) {
+  URL scriptFile = 
TestJdbcWithMySQLandH2PluginsIT.class.getClassLoader().getResource("h2-test-data.sql");
+  Assert.assertNotNull("Script for test tables generation 
'h2-test-data.sql' " +
+  "cannot be found in test resources", scriptFile);
+  try (FileReader fileReader = new FileReader(scriptFile.getFile())) {
+RunScript.execute(connection, fileReader);
+  }
+}
+
+JdbcStorageConfig jdbcStorageConfig = new JdbcStorageConfig(
+"org.h2.Driver",
+connString,
+"root",
+"root",
+true);
+jdbcStorageConfig.setEnabled(true);
+
+String pluginName = "h2";
+DrillbitContext context = cluster.drillbit().getContext();
+JdbcStoragePlugin jdbcStoragePlugin = new 
JdbcStoragePlugin(jdbcStorageConfig,
+context, pluginName);
+StoragePluginRegistryImpl pluginRegistry = (StoragePluginRegistryImpl) 
context.getStorage();
+pluginRegistry.addPluginToPersistentStoreIfAbsent(pluginName, 
jdbcStorageConfig, jdbcStoragePlugin);
+  }
+
+  private static void initMySQL() throws Exception {
+String mysqlPluginName = "mysql";
+String mysqlDBName = "drill_mysql_test";
+int mysqlPort = QueryTestUtil.getFreePortNumber(2215, 300);
+
+MysqldConfig config = MysqldConfig.aMys

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

2019-12-25 Thread GitBox
arina-ielchiieva 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_r361323645
 
 

 ##
 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 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, cas

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

2019-12-25 Thread GitBox
arina-ielchiieva 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_r361324240
 
 

 ##
 File path: 
exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/conversion/DrillTableKey.java
 ##
 @@ -0,0 +1,54 @@
+/*
+ * 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.planner.sql.conversion;
+
+import java.util.List;
+import java.util.Objects;
+
+import org.apache.drill.common.expression.SchemaPath;
+import org.apache.drill.exec.planner.logical.DrillTable;
+import org.apache.drill.metastore.metadata.TableMetadataProvider;
+import org.apache.drill.shaded.guava.com.google.common.cache.LoadingCache;
+
+/**
+ * Key for storing / obtaining {@link TableMetadataProvider} instance from 
{@link LoadingCache}.
+ */
+final class DrillTableKey {
+  private final SchemaPath key;
+  final DrillTable drillTable;
 
 Review comment:
   private?


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:
us...@infra.apache.org


With regards,
Apache Git Services


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

2019-12-25 Thread GitBox
arina-ielchiieva 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_r361323585
 
 

 ##
 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 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, cas

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

2019-12-25 Thread GitBox
arina-ielchiieva 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_r361323411
 
 

 ##
 File path: 
contrib/storage-jdbc/src/main/java/org/apache/drill/exec/store/jdbc/DrillJdbcConvention.java
 ##
 @@ -0,0 +1,84 @@
+/*
+ * 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 java.util.List;
+import java.util.Set;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+import org.apache.calcite.adapter.jdbc.JdbcConvention;
+import org.apache.calcite.adapter.jdbc.JdbcRules;
+import org.apache.calcite.adapter.jdbc.JdbcToEnumerableConverterRule;
+import org.apache.calcite.linq4j.tree.ConstantUntypedNull;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.sql.SqlDialect;
+import org.apache.drill.exec.planner.RuleInstance;
+import org.apache.drill.exec.planner.logical.DrillRelFactories;
+import org.apache.drill.shaded.guava.com.google.common.collect.ImmutableSet;
+
+/**
+ * Convention with set of rules to register for jdbc plugin
+ */
+class DrillJdbcConvention extends JdbcConvention {
+
+  /**
+   * Unwanted Calcite's JdbcRules are filtered out by the predicate
+   */
+  private static final Predicate CALCITE_RULES_TO_INCLUDE = rule 
-> {
+final Class rc = rule.getClass();
 
 Review comment:
   I tend not to add final keyword relying on the effective final notion but 
it's up to you.


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:
us...@infra.apache.org


With regards,
Apache Git Services


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

2019-12-25 Thread GitBox
arina-ielchiieva 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_r361323935
 
 

 ##
 File path: 
contrib/storage-jdbc/src/test/java/org/apache/drill/exec/store/jdbc/TestJdbcWithMySQLandH2PluginsIT.java
 ##
 @@ -0,0 +1,638 @@
+/*
+ * 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 com.wix.mysql.EmbeddedMysql;
+import com.wix.mysql.ScriptResolver;
+import com.wix.mysql.config.MysqldConfig;
+import com.wix.mysql.config.SchemaConfig;
+import com.wix.mysql.distribution.Version;
+import org.apache.drill.categories.JdbcStorageTest;
+import org.apache.drill.common.config.DrillProperties;
+import org.apache.drill.exec.ExecConstants;
+import org.apache.drill.exec.expr.fn.impl.DateUtility;
+
+import org.apache.drill.exec.server.DrillbitContext;
+import org.apache.drill.exec.store.StoragePluginRegistryImpl;
+import org.apache.drill.exec.util.StoragePluginTestUtils;
+import org.apache.drill.test.ClusterFixture;
+import org.apache.drill.test.ClusterTest;
+import org.apache.drill.test.QueryTestUtil;
+import org.h2.tools.RunScript;
+import org.joda.time.DateTimeZone;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Assume;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.io.FileReader;
+import java.math.BigDecimal;
+import java.net.URL;
+import java.nio.file.Paths;
+import java.sql.Connection;
+import java.sql.DriverManager;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+/**
+ * JDBC storage plugin tests against H2 and MySQL data sources.
+ */
+@Category(JdbcStorageTest.class)
+public class TestJdbcWithMySQLandH2PluginsIT extends ClusterTest {
+
+  private static final String TABLE_PATH = "jdbcmulti/";
+  private static final String TABLE_NAME = String.format("%s.`%s`", 
StoragePluginTestUtils.DFS_PLUGIN_NAME, TABLE_PATH);
+
+  private static EmbeddedMysql mysqld;
+
+  @BeforeClass
+  public static void init() throws Exception {
+startCluster(ClusterFixture.builder(dirTestWatcher));
+dirTestWatcher.copyResourceToRoot(Paths.get(TABLE_PATH));
+initH2();
+initMySQL();
+  }
+
+  @AfterClass
+  public static void stopMysql() {
+if (mysqld != null) {
+  mysqld.stop();
+}
+  }
+
+  @After
+  public void resetSchema() {
+client.resetSession(DrillProperties.SCHEMA);
+  }
+
+  private static void initH2() throws Exception {
+Class.forName("org.h2.Driver");
+String connString = String.format(
+"jdbc:h2:%s", dirTestWatcher.getTmpDir().getCanonicalPath());
+
+try (Connection connection = DriverManager.getConnection(connString, 
"root", "root")) {
+  URL scriptFile = 
TestJdbcWithMySQLandH2PluginsIT.class.getClassLoader().getResource("h2-test-data.sql");
+  Assert.assertNotNull("Script for test tables generation 
'h2-test-data.sql' " +
+  "cannot be found in test resources", scriptFile);
+  try (FileReader fileReader = new FileReader(scriptFile.getFile())) {
+RunScript.execute(connection, fileReader);
+  }
+}
+
+JdbcStorageConfig jdbcStorageConfig = new JdbcStorageConfig(
+"org.h2.Driver",
+connString,
+"root",
+"root",
+true);
+jdbcStorageConfig.setEnabled(true);
+
+String pluginName = "h2";
+DrillbitContext context = cluster.drillbit().getContext();
 
 Review comment:
   Please create method in `ClusterFixture`, for example `definePlugin`, 
similar to `defineWorkspace` instead. 


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:
us...@infra.apache.org


With regards,
Apache Git Services


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

2019-12-25 Thread GitBox
arina-ielchiieva 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_r361323570
 
 

 ##
 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 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();
 
 Review comment:
   Is casting without instance of check safe here?


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:
us...@infra.apache.org


With regards,
Apache Git Services