kramerul commented on code in PR #4100:
URL: https://github.com/apache/calcite/pull/4100#discussion_r1950698749


##########
core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcSchema.java:
##########
@@ -237,80 +252,86 @@ public DataSource getDataSource() {
     return Schemas.subSchemaExpression(parentSchema, name, JdbcSchema.class);
   }
 
-  protected Multimap<String, Function> getFunctions() {
-    // TODO: populate map from JDBC metadata
-    return ImmutableMultimap.of();
+  private Stream<MetaImpl.MetaTable> getMetaTableStream(String 
tableNamePattern) {
+    final Pair<@Nullable String, @Nullable String> catalogSchema = 
getCatalogSchema();
+    final Stream<MetaImpl.MetaTable> tableDefs;
+    Connection connection = null;
+    ResultSet resultSet = null;
+    try {
+      connection = dataSource.getConnection();
+      final DatabaseMetaData metaData = connection.getMetaData();
+      resultSet =
+          metaData.getTables(catalogSchema.left, catalogSchema.right, 
tableNamePattern, null);
+      tableDefs = asStream(connection, resultSet)
+          .map(JdbcSchema::metaDataMapper);
+    } catch (SQLException e) {
+      close(connection, null, resultSet);
+      throw new RuntimeException(
+          "Exception while reading tables", e);
+    }
+    return tableDefs;
   }
 
-  @Override public final Collection<Function> getFunctions(String name) {
-    return getFunctions().get(name); // never null
+  private static Stream<ResultSet> asStream(Connection connection, ResultSet 
resultSet) {
+    return StreamSupport.stream(
+        new Spliterators.AbstractSpliterator<ResultSet>(
+            Long.MAX_VALUE, Spliterator.ORDERED) {
+          @Override public boolean tryAdvance(Consumer<? super ResultSet> 
action) {
+            try {
+              if (!resultSet.next()) {
+                return false;
+              }
+              action.accept(resultSet);
+              return true;
+            } catch (SQLException ex) {
+              throw new RuntimeException(ex);
+            }
+          }
+        }, false).onClose(() -> close(connection, null, resultSet));
   }
 
-  @Override public final Set<String> getFunctionNames() {
-    return getFunctions().keySet();
+  private JdbcTable jdbcTableMapper(MetaImpl.MetaTable tableDef) {
+    return new JdbcTable(this, tableDef.tableCat, tableDef.tableSchem, 
tableDef.tableName,
+        getTableType(tableDef.tableType));
   }
 
-  private ImmutableMap<String, JdbcTable> computeTables() {
-    Connection connection = null;
-    ResultSet resultSet = null;
+  private static MetaImpl.MetaTable metaDataMapper(ResultSet resultSet) {
     try {
-      connection = dataSource.getConnection();
-      final Pair<@Nullable String, @Nullable String> catalogSchema = 
getCatalogSchema(connection);
-      final String catalog = catalogSchema.left;
-      final String schema = catalogSchema.right;
-      final Iterable<MetaImpl.MetaTable> tableDefs;
-      Foo threadMetadata = THREAD_METADATA.get();
-      if (threadMetadata != null) {
-        tableDefs = threadMetadata.apply(catalog, schema);
-      } else {
-        final List<MetaImpl.MetaTable> tableDefList = new ArrayList<>();
-        final DatabaseMetaData metaData = connection.getMetaData();
-        resultSet = metaData.getTables(catalog, schema, null, null);
-        while (resultSet.next()) {
-          final String catalogName = resultSet.getString(1);
-          final String schemaName = resultSet.getString(2);
-          final String tableName = resultSet.getString(3);
-          final String tableTypeName = resultSet.getString(4);
-          tableDefList.add(
-              new MetaImpl.MetaTable(catalogName, schemaName, tableName,
-                  tableTypeName));
-        }
-        tableDefs = tableDefList;
-      }
+      return new MetaImpl.MetaTable(intern(resultSet.getString(1)), 
intern(resultSet.getString(2)),
+          intern(resultSet.getString(3)),
+          intern(resultSet.getString(4)));
+    } catch (SQLException e) {
+      throw new RuntimeException(e);
+    }
+  }
 
-      final ImmutableMap.Builder<String, JdbcTable> builder =
-          ImmutableMap.builder();
-      for (MetaImpl.MetaTable tableDef : tableDefs) {
-        // Clean up table type. In particular, this ensures that 'SYSTEM 
TABLE',
-        // returned by Phoenix among others, maps to TableType.SYSTEM_TABLE.
-        // We know enum constants are upper-case without spaces, so we can't
-        // make things worse.
-        //
-        // PostgreSQL returns tableTypeName==null for pg_toast* tables
-        // This can happen if you start JdbcSchema off a "public" PG schema
-        // The tables are not designed to be queried by users, however we do
-        // not filter them as we keep all the other table types.
-        final String tableTypeName2 =
-            tableDef.tableType == null
+  private static @Nullable String intern(@Nullable String string) {
+    if (string == null) {
+      return null;
+    }
+    return string.intern();

Review Comment:
   I removed the `intern` method. This is no longer required. We needed this to 
ensure that we require as less memory as possible. With the new lazy loading 
approach, this is nor longer 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.

To unsubscribe, e-mail: [email protected]

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

Reply via email to