zoudan commented on code in PR #3200:
URL: https://github.com/apache/calcite/pull/3200#discussion_r1193710573


##########
core/src/main/java/org/apache/calcite/jdbc/CalciteMetaImpl.java:
##########
@@ -307,13 +307,13 @@ private static ImmutableMap.Builder<DatabaseProperty, 
Object> addProperty(
       Pat schemaPattern,
       Pat tableNamePattern,
       Pat columnNamePattern) {
-    final Predicate1<String> tableNameMatcher = matcher(tableNamePattern);
+//    final Predicate1<String> tableNameMatcher = matcher(tableNamePattern);

Review Comment:
   remove this line



##########
example/csv/src/main/java/org/apache/calcite/adapter/csv/CsvLazySchema.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * 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.calcite.adapter.csv;
+
+import org.apache.calcite.adapter.file.JsonScannableTable;
+import org.apache.calcite.avatica.Meta;
+import org.apache.calcite.jdbc.CalciteMetaImpl;
+import org.apache.calcite.linq4j.function.Predicate1;
+import org.apache.calcite.schema.Table;
+import org.apache.calcite.schema.impl.AbstractSchema;
+import org.apache.calcite.util.Source;
+import org.apache.calcite.util.Sources;
+
+import com.google.common.collect.ImmutableMap;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.io.File;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Schema mapped onto a directory of CSV files. Each table in the schema
+ * is a CSV file in that directory.
+ */
+public class CsvLazySchema extends AbstractSchema {
+  private final File directoryFile;
+  private final CsvTable.Flavor flavor;
+  private Map<String, Table> tableMap;
+
+  /**
+   * Creates a CSV schema.
+   *
+   * @param directoryFile Directory that holds {@code .csv} files
+   * @param flavor     Whether to instantiate flavor tables that undergo
+   *                   query optimization
+   */
+  public CsvLazySchema(File directoryFile, CsvTable.Flavor flavor) {
+    super();
+    this.directoryFile = directoryFile;
+    this.flavor = flavor;
+    this.tableMap = createTableMap();
+  }
+
+  /** Looks for a suffix on a string and returns
+   * either the string with the suffix removed
+   * or the original string. */
+  private static String trim(String s, String suffix) {
+    String trimmed = trimOrNull(s, suffix);
+    return trimmed != null ? trimmed : s;
+  }
+
+  /** Looks for a suffix on a string and returns
+   * either the string with the suffix removed
+   * or null. */
+  private static String trimOrNull(String s, String suffix) {
+    return s.endsWith(suffix)
+        ? s.substring(0, s.length() - suffix.length())
+        : null;
+  }
+
+
+  private Map<String, Table> createTableMap() {
+    // Look for files in the directory ending in ".csv", ".csv.gz", ".json",
+    // ".json.gz".
+    final Source baseSource = Sources.of(directoryFile);
+    File[] files = directoryFile.listFiles((dir, name) -> {
+      final String nameSansGz = trim(name, ".gz");
+      return nameSansGz.endsWith(".csv")
+          || nameSansGz.endsWith(".json");
+    });
+    if (files == null) {
+      System.out.println("directory " + directoryFile + " not found");

Review Comment:
   use log instead of stdout



##########
example/csv/src/main/java/org/apache/calcite/adapter/csv/CsvLazySchema.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * 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.calcite.adapter.csv;
+
+import org.apache.calcite.adapter.file.JsonScannableTable;
+import org.apache.calcite.avatica.Meta;
+import org.apache.calcite.jdbc.CalciteMetaImpl;
+import org.apache.calcite.linq4j.function.Predicate1;
+import org.apache.calcite.schema.Table;
+import org.apache.calcite.schema.impl.AbstractSchema;
+import org.apache.calcite.util.Source;
+import org.apache.calcite.util.Sources;
+
+import com.google.common.collect.ImmutableMap;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.io.File;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Schema mapped onto a directory of CSV files. Each table in the schema
+ * is a CSV file in that directory.
+ */
+public class CsvLazySchema extends AbstractSchema {
+  private final File directoryFile;
+  private final CsvTable.Flavor flavor;
+  private Map<String, Table> tableMap;
+
+  /**
+   * Creates a CSV schema.
+   *
+   * @param directoryFile Directory that holds {@code .csv} files
+   * @param flavor     Whether to instantiate flavor tables that undergo
+   *                   query optimization
+   */
+  public CsvLazySchema(File directoryFile, CsvTable.Flavor flavor) {
+    super();
+    this.directoryFile = directoryFile;
+    this.flavor = flavor;
+    this.tableMap = createTableMap();
+  }
+
+  /** Looks for a suffix on a string and returns
+   * either the string with the suffix removed
+   * or the original string. */
+  private static String trim(String s, String suffix) {
+    String trimmed = trimOrNull(s, suffix);
+    return trimmed != null ? trimmed : s;
+  }
+
+  /** Looks for a suffix on a string and returns
+   * either the string with the suffix removed
+   * or null. */
+  private static String trimOrNull(String s, String suffix) {
+    return s.endsWith(suffix)
+        ? s.substring(0, s.length() - suffix.length())
+        : null;
+  }
+
+
+  private Map<String, Table> createTableMap() {
+    // Look for files in the directory ending in ".csv", ".csv.gz", ".json",
+    // ".json.gz".
+    final Source baseSource = Sources.of(directoryFile);
+    File[] files = directoryFile.listFiles((dir, name) -> {
+      final String nameSansGz = trim(name, ".gz");
+      return nameSansGz.endsWith(".csv")
+          || nameSansGz.endsWith(".json");
+    });
+    if (files == null) {
+      System.out.println("directory " + directoryFile + " not found");
+      files = new File[0];
+    }
+    // Build a map from table name to table; each file becomes a table.
+    final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder();
+    for (File file : files) {
+      Source source = Sources.of(file);
+      Source sourceSansGz = source.trim(".gz");
+      final Source sourceSansJson = sourceSansGz.trimOrNull(".json");
+      if (sourceSansJson != null) {
+        final Table table = new JsonScannableTable(source);
+        builder.put(sourceSansJson.relative(baseSource).path(), table);
+      }
+      final Source sourceSansCsv = sourceSansGz.trimOrNull(".csv");
+      if (sourceSansCsv != null) {
+        final Table table = createTable(source);
+        builder.put(sourceSansCsv.relative(baseSource).path(), table);
+      }
+    }
+    return builder.build();
+  }
+
+  @Override public final @Nullable Table getTable(String name, boolean 
caseSensitive) {
+    Table result = null;
+    if (caseSensitive) {
+      result = tableMap.get(name);
+    } else {
+      Optional<Map.Entry<String, Table>> first = tableMap.entrySet()
+          .stream()
+          .filter(entry -> name.equalsIgnoreCase(entry.getKey()))
+          .findFirst();
+
+      if (first.isPresent()) {
+        result = first.get().getValue();
+      }
+    }
+    return result;
+  }
+

Review Comment:
   remove empty line



##########
core/src/main/java/org/apache/calcite/schema/impl/DelegatingSchema.java:
##########
@@ -62,9 +62,11 @@ public DelegatingSchema(Schema schema) {
   }
 
   @Override public @Nullable Table getTable(String name) {
+    return getTable(name, true);
+  }
+  @Override public @Nullable Table getTable(String name, boolean 
caseSensitive) {
     return schema.getTable(name);
   }
-

Review Comment:
   keep the empty line between methods



##########
core/src/main/java/org/apache/calcite/schema/Schema.java:
##########
@@ -71,6 +75,8 @@ public interface Schema {
    */
   Set<String> getTableNames();
 
+  @Nullable Table getTable(String name, boolean caseSensitive);

Review Comment:
   add annotations



##########
example/csv/src/main/java/org/apache/calcite/adapter/csv/CsvLazySchema.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * 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.calcite.adapter.csv;
+
+import org.apache.calcite.adapter.file.JsonScannableTable;
+import org.apache.calcite.avatica.Meta;
+import org.apache.calcite.jdbc.CalciteMetaImpl;
+import org.apache.calcite.linq4j.function.Predicate1;
+import org.apache.calcite.schema.Table;
+import org.apache.calcite.schema.impl.AbstractSchema;
+import org.apache.calcite.util.Source;
+import org.apache.calcite.util.Sources;
+
+import com.google.common.collect.ImmutableMap;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.io.File;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Schema mapped onto a directory of CSV files. Each table in the schema
+ * is a CSV file in that directory.
+ */
+public class CsvLazySchema extends AbstractSchema {
+  private final File directoryFile;
+  private final CsvTable.Flavor flavor;
+  private Map<String, Table> tableMap;
+
+  /**
+   * Creates a CSV schema.
+   *
+   * @param directoryFile Directory that holds {@code .csv} files
+   * @param flavor     Whether to instantiate flavor tables that undergo
+   *                   query optimization
+   */
+  public CsvLazySchema(File directoryFile, CsvTable.Flavor flavor) {
+    super();
+    this.directoryFile = directoryFile;
+    this.flavor = flavor;
+    this.tableMap = createTableMap();
+  }
+
+  /** Looks for a suffix on a string and returns
+   * either the string with the suffix removed
+   * or the original string. */
+  private static String trim(String s, String suffix) {
+    String trimmed = trimOrNull(s, suffix);
+    return trimmed != null ? trimmed : s;
+  }
+
+  /** Looks for a suffix on a string and returns
+   * either the string with the suffix removed
+   * or null. */
+  private static String trimOrNull(String s, String suffix) {
+    return s.endsWith(suffix)
+        ? s.substring(0, s.length() - suffix.length())
+        : null;
+  }
+

Review Comment:
   remove one empty line



##########
example/csv/src/test/java/org/apache/calcite/test/CsvTest.java:
##########
@@ -1025,6 +1033,72 @@ private String range(int first, int count) {
     }
   }
 
+
+  @Test void testSelectByLazyScheme() throws SQLException {
+    sql("model-lazy-scheme", "select * from EMPS").ok();
+  }
+
+  @Test void testSelectByLazySchemeCaseSensitive() throws SQLException {
+    Properties properties = new Properties();
+    properties.put("caseSensitive", "false");
+    sql("model-lazy-scheme", "select * from DEPTS1", properties).ok();
+  }
+

Review Comment:
   remove empty line



##########
core/src/main/java/org/apache/calcite/schema/impl/DelegatingSchema.java:
##########
@@ -62,9 +62,11 @@ public DelegatingSchema(Schema schema) {
   }
 
   @Override public @Nullable Table getTable(String name) {
+    return getTable(name, true);
+  }

Review Comment:
   add empty line



##########
example/csv/src/test/java/org/apache/calcite/test/CsvTest.java:
##########
@@ -1025,6 +1033,72 @@ private String range(int first, int count) {
     }
   }
 
+
+  @Test void testSelectByLazyScheme() throws SQLException {
+    sql("model-lazy-scheme", "select * from EMPS").ok();
+  }
+
+  @Test void testSelectByLazySchemeCaseSensitive() throws SQLException {
+    Properties properties = new Properties();
+    properties.put("caseSensitive", "false");
+    sql("model-lazy-scheme", "select * from DEPTS1", properties).ok();
+  }
+
+
+  @Test void testDateTypeLazySchemeCopy() throws SQLException {
+    Properties info = new Properties();
+    info.put("model", jsonPath("bug-lazy-scheme"));
+
+    try (Connection connection =
+        DriverManager.getConnection("jdbc:calcite:", info)) {
+      ResultSet res =
+          connection.getMetaData().getColumns(null, null,
+              "DATE", "JOINEDAT");
+      res.next();
+      assertEquals(res.getInt("DATA_TYPE"), java.sql.Types.DATE);
+
+      res =
+          connection.getMetaData().getColumns(null, null,
+              "DATE", "JOINTIME");
+      res.next();
+      assertEquals(res.getInt("DATA_TYPE"), java.sql.Types.TIME);
+
+      res =
+          connection.getMetaData().getColumns(null, null,
+              "DATE", "JOINTIMES");
+      res.next();
+      assertEquals(res.getInt("DATA_TYPE"), java.sql.Types.TIMESTAMP);
+
+      Statement statement = connection.createStatement();
+      final String sql = "select \"JOINEDAT\", \"JOINTIME\", \"JOINTIMES\" "
+          + "from \"DATE\" where EMPNO = 100";
+      ResultSet resultSet = statement.executeQuery(sql);
+      resultSet.next();
+
+      // date
+      assertEquals(java.sql.Date.class, resultSet.getDate(1).getClass());
+      assertEquals(java.sql.Date.valueOf("1996-08-03"),
+          resultSet.getDate(1));
+
+      // time
+      assertEquals(java.sql.Time.class, resultSet.getTime(2).getClass());
+      assertEquals(java.sql.Time.valueOf("00:01:02"),
+          resultSet.getTime(2));
+
+      // timestamp
+      assertEquals(java.sql.Timestamp.class,
+          resultSet.getTimestamp(3).getClass());
+      assertEquals(java.sql.Timestamp.valueOf("1996-08-03 00:01:02"),
+          resultSet.getTimestamp(3));
+
+    }
+  }
+//  @Test void testShowTablesByLazySchemeShowTables() throws SQLException {

Review Comment:
   remove these commented out code



##########
core/src/main/java/org/apache/calcite/schema/Schema.java:
##########
@@ -71,6 +75,8 @@ public interface Schema {
    */
   Set<String> getTableNames();
 
+  @Nullable Table getTable(String name, boolean caseSensitive);

Review Comment:
   Why we add `caseSensitive` here? I do not get what is the relationship 
between it and `lazy`



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to