Skip to content

Commit

Permalink
Reduce required memory for JdbcTable
Browse files Browse the repository at this point in the history
  • Loading branch information
kramerul committed Oct 20, 2023
1 parent c77efe8 commit 95be0f4
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,10 @@ private ImmutableMap<String, JdbcTable> computeTables() {
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);
final String catalogName = intern(resultSet.getString(1));
final String schemaName = intern(resultSet.getString(2));
final String tableName = intern(resultSet.getString(3));
final String tableTypeName = intern(resultSet.getString(4));
tableDefList.add(
new MetaImpl.MetaTable(catalogName, schemaName, tableName,
tableTypeName));
Expand All @@ -279,27 +279,9 @@ private ImmutableMap<String, JdbcTable> computeTables() {
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
? null
: tableDef.tableType.toUpperCase(Locale.ROOT).replace(' ', '_');
final TableType tableType =
Util.enumVal(TableType.OTHER, tableTypeName2);
if (tableType == TableType.OTHER && tableTypeName2 != null) {
LOGGER.info("Unknown table type: {}", tableTypeName2);
}
final JdbcTable table =
new JdbcTable(this, tableDef.tableCat, tableDef.tableSchem,
tableDef.tableName, tableType);
tableDef.tableName, getTableType(tableDef.tableType));
builder.put(tableDef.tableName, table);
}
return builder.build();
Expand All @@ -311,6 +293,32 @@ private ImmutableMap<String, JdbcTable> computeTables() {
}
}

private static String intern(@Nullable String string) {
if ( string == null) return null;

Check failure on line 297 in core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcSchema.java

View workflow job for this annotation

GitHub Actions / CheckerFramework (JDK 11)

[Task :core:compileJava] [return.type.incompatible] incompatible types in return. if ( string == null) return null; ^ type of expression: null (NullType)

Check failure on line 297 in core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcSchema.java

View workflow job for this annotation

GitHub Actions / CheckerFramework (JDK 11), Guava 29

[Task :core:compileJava] [return.type.incompatible] incompatible types in return. if ( string == null) return null; ^ type of expression: null (NullType)
return string.intern();
}
private static TableType getTableType(String tableTypeName) {
// 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 =
tableTypeName == null
? null
: tableTypeName.toUpperCase(Locale.ROOT).replace(' ', '_');
final TableType tableType =
Util.enumVal(TableType.OTHER, tableTypeName2);
if (tableType == TableType.OTHER && tableTypeName2 != null) {
LOGGER.info("Unknown table type: {}", tableTypeName2);
}
return tableType;
}

/** Returns [major, minor] version from a database metadata. */
private static List<Integer> version(DatabaseMetaData metaData) throws SQLException {
return ImmutableList.of(metaData.getJDBCMajorVersion(),
Expand Down

0 comments on commit 95be0f4

Please sign in to comment.