Skip to content

Commit

Permalink
[CALCITE-6728] Introduce new methods to lookup tables and schemas ins…
Browse files Browse the repository at this point in the history
…ide schemas
  • Loading branch information
kramerul committed Dec 19, 2024
1 parent 0fdbe02 commit 587728b
Show file tree
Hide file tree
Showing 18 changed files with 451 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import javax.sql.DataSource;
Expand Down Expand Up @@ -68,8 +69,8 @@ public class JdbcCatalogSchema extends JdbcBaseSchema implements Wrapper {

/** default schema name, lazily initialized. */
@SuppressWarnings({"method.invocation.invalid", "Convert2MethodRef"})
private final Supplier<String> defaultSchemaName =
Suppliers.memoize(() -> computeDefaultSchemaName());
private final Supplier<Optional<String>> defaultSchemaName =
Suppliers.memoize(() -> Optional.ofNullable(computeDefaultSchemaName()));

/** Creates a JdbcCatalogSchema. */
public JdbcCatalogSchema(DataSource dataSource, SqlDialect dialect,
Expand Down Expand Up @@ -150,7 +151,7 @@ public static JdbcCatalogSchema create(
return subSchemas;
}

private String computeDefaultSchemaName() {
private @Nullable String computeDefaultSchemaName() {
try (Connection connection = dataSource.getConnection()) {
return connection.getSchema();
} catch (SQLException e) {
Expand All @@ -160,7 +161,7 @@ private String computeDefaultSchemaName() {

/** Returns the name of the default sub-schema. */
public @Nullable String getDefaultSubSchemaName() {
return defaultSchemaName.get();
return defaultSchemaName.get().orElse(null);
}

/** Returns the data source. */
Expand Down
33 changes: 15 additions & 18 deletions core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@
import org.apache.calcite.sql.SqlDialectFactoryImpl;
import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.util.LazyReference;
import org.apache.calcite.util.Pair;
import org.apache.calcite.util.Util;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Ordering;

import org.checkerframework.checker.initialization.qual.UnknownInitialization;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -88,7 +88,7 @@ public class JdbcSchema extends JdbcBaseSchema implements Schema, Wrapper {
final @Nullable String schema;
public final SqlDialect dialect;
final JdbcConvention convention;
private final Lookup<Table> tables;
private final LazyReference<Lookup<Table>> tables = new LazyReference<>();
private final Lookup<JdbcSchema> subSchemas = Lookup.empty();

@Experimental
Expand All @@ -113,21 +113,6 @@ public JdbcSchema(DataSource dataSource, SqlDialect dialect,
this.convention = convention;
this.catalog = catalog;
this.schema = schema;
@UnknownInitialization
JdbcSchema self = this;
this.tables = new IgnoreCaseLookup<Table>() {
@Override public @Nullable Table get(String name) {
try (Stream<MetaImpl.MetaTable> s = self.getMetaTableStream(name)) {
return s.findFirst().map(it -> jdbcTableMapper(it)).orElse(null);
}
}

@Override public Set<String> getNames(LikePattern pattern) {
try (Stream<MetaImpl.MetaTable> s = self.getMetaTableStream(pattern.pattern)) {
return s.map(it -> it.tableName).collect(Collectors.toSet());
}
}
};
}

public static JdbcSchema create(
Expand Down Expand Up @@ -229,7 +214,19 @@ public static DataSource dataSource(String url, @Nullable String driverClassName
}

@Override public Lookup<Table> tables() {
return tables;
return tables.getOrCompute(() -> new IgnoreCaseLookup<Table>() {
@Override public @Nullable Table get(String name) {
try (Stream<MetaImpl.MetaTable> s = getMetaTableStream(name)) {
return s.findFirst().map(it -> jdbcTableMapper(it)).orElse(null);
}
}

@Override public Set<String> getNames(LikePattern pattern) {
try (Stream<MetaImpl.MetaTable> s = getMetaTableStream(pattern.pattern)) {
return s.map(it -> it.tableName).collect(Collectors.toSet());
}
}
});
}

@Override public Lookup<? extends Schema> subSchemas() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.calcite.jdbc;

import org.apache.calcite.linq4j.function.Function1;
import org.apache.calcite.rel.type.RelProtoDataType;
import org.apache.calcite.schema.Function;
import org.apache.calcite.schema.Schema;
Expand All @@ -37,6 +36,7 @@

import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;

import static org.apache.calcite.linq4j.Nullness.castNonNull;

Expand All @@ -45,7 +45,7 @@
* functions and sub-schemas.
*/
class CachingCalciteSchema extends CalciteSchema {
private final ImmutableList<CachedLookup<?>> caches;
private final ConcurrentLinkedDeque<CachedLookup<?>> caches = new ConcurrentLinkedDeque<>();
private final Cached<NameSet> implicitFunctionCache;
private final Cached<NameSet> implicitTypeCache;

Expand All @@ -54,8 +54,7 @@ class CachingCalciteSchema extends CalciteSchema {
/** Creates a CachingCalciteSchema. */
CachingCalciteSchema(@Nullable CalciteSchema parent, Schema schema,
String name) {
this(parent, schema, name, null, null, null, null, null, null, null, null,
new LookupDecorator());
this(parent, schema, name, null, null, null, null, null, null, null, null);
}

@SuppressWarnings({"argument.type.incompatible", "return.type.incompatible"})
Expand All @@ -68,11 +67,9 @@ private CachingCalciteSchema(@Nullable CalciteSchema parent, Schema schema,
@Nullable NameMultimap<FunctionEntry> functionMap,
@Nullable NameSet functionNames,
@Nullable NameMap<FunctionEntry> nullaryFunctionMap,
@Nullable List<? extends List<String>> path,
LookupDecorator lookupDecorator) {
@Nullable List<? extends List<String>> path) {
super(parent, schema, name, subSchemaMap, tableMap, latticeMap, typeMap,
functionMap, functionNames, nullaryFunctionMap, path, lookupDecorator);
this.caches = lookupDecorator.cacheBuilder.build();
functionMap, functionNames, nullaryFunctionMap, path);
this.implicitFunctionCache =
new AbstractCached<NameSet>() {
@Override public NameSet build() {
Expand Down Expand Up @@ -107,6 +104,12 @@ private CachingCalciteSchema(@Nullable CalciteSchema parent, Schema schema,
return new CachingCalciteSchema(this, schema, name);
}

@Override protected <S> Lookup<S> decorateLookup(Lookup<S> lookup) {
CachedLookup<S> cachedLookup = new CachedLookup<>(lookup);
caches.add(cachedLookup);
return cachedLookup;
}

/** Adds a child schema of this schema. */
@Override public CalciteSchema add(String name, Schema schema) {
final CalciteSchema calciteSchema =
Expand Down Expand Up @@ -202,7 +205,7 @@ private CachingCalciteSchema(@Nullable CalciteSchema parent, Schema schema,
CalciteSchema snapshot =
new CachingCalciteSchema(parent, schema.snapshot(version), name, null,
tableMap, latticeMap, typeMap,
functionMap, functionNames, nullaryFunctionMap, getPath(), new LookupDecorator());
functionMap, functionNames, nullaryFunctionMap, getPath());
for (CalciteSchema subSchema : subSchemaMap.map().values()) {
CalciteSchema subSchemaSnapshot = subSchema.snapshot(snapshot, version);
snapshot.subSchemaMap.put(subSchema.name, subSchemaSnapshot);
Expand Down Expand Up @@ -275,18 +278,4 @@ private abstract class AbstractCached<T> implements Cached<T> {
built = false;
}
}

/**
* This class is used to decorate lookups with a cache.
*/
private static class LookupDecorator implements Function1<Lookup<?>, Lookup<?>> {

final ImmutableList.Builder<CachedLookup<?>> cacheBuilder = ImmutableList.builder();

@Override public Lookup apply(final Lookup lookup) {
CachedLookup cachedLookup = new CachedLookup(lookup);
cacheBuilder.add(cachedLookup);
return cachedLookup;
}
}
}
62 changes: 29 additions & 33 deletions core/src/main/java/org/apache/calcite/jdbc/CalciteSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.calcite.jdbc;

import org.apache.calcite.linq4j.function.Experimental;
import org.apache.calcite.linq4j.function.Function1;
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.materialize.Lattice;
import org.apache.calcite.rel.type.RelProtoDataType;
Expand All @@ -33,6 +32,7 @@
import org.apache.calcite.schema.lookup.LikePattern;
import org.apache.calcite.schema.lookup.Lookup;
import org.apache.calcite.schema.lookup.Named;
import org.apache.calcite.util.LazyReference;
import org.apache.calcite.util.NameMap;
import org.apache.calcite.util.NameMultimap;
import org.apache.calcite.util.NameSet;
Expand Down Expand Up @@ -69,17 +69,16 @@ public abstract class CalciteSchema {
/** Tables explicitly defined in this schema. Does not include tables in
* {@link #schema}. */
protected final NameMap<TableEntry> tableMap;
private final Lookup<TableEntry> tables;
private final LazyReference<Lookup<TableEntry>> tables = new LazyReference<>();
protected final NameMultimap<FunctionEntry> functionMap;
protected final NameMap<TypeEntry> typeMap;
protected final NameMap<LatticeEntry> latticeMap;
protected final NameSet functionNames;
protected final NameMap<FunctionEntry> nullaryFunctionMap;
protected final NameMap<CalciteSchema> subSchemaMap;
private final Lookup<CalciteSchema> subSchemas;
private final LazyReference<Lookup<CalciteSchema>> subSchemas = new LazyReference<>();
private @Nullable List<? extends List<String>> path;


protected CalciteSchema(@Nullable CalciteSchema parent, Schema schema,
String name,
@Nullable NameMap<CalciteSchema> subSchemaMap,
Expand All @@ -90,21 +89,6 @@ protected CalciteSchema(@Nullable CalciteSchema parent, Schema schema,
@Nullable NameSet functionNames,
@Nullable NameMap<FunctionEntry> nullaryFunctionMap,
@Nullable List<? extends List<String>> path) {
this(parent, schema, name, subSchemaMap, tableMap, latticeMap, typeMap, functionMap,
functionNames, nullaryFunctionMap, path, l -> l);
}

protected CalciteSchema(@Nullable CalciteSchema parent, Schema schema,
String name,
@Nullable NameMap<CalciteSchema> subSchemaMap,
@Nullable NameMap<TableEntry> tableMap,
@Nullable NameMap<LatticeEntry> latticeMap,
@Nullable NameMap<TypeEntry> typeMap,
@Nullable NameMultimap<FunctionEntry> functionMap,
@Nullable NameSet functionNames,
@Nullable NameMap<FunctionEntry> nullaryFunctionMap,
@Nullable List<? extends List<String>> path,
Function1<Lookup<?>, Lookup<?>> lookupDecorator) {
this.parent = parent;
this.schema = schema;
this.name = name;
Expand All @@ -129,14 +113,26 @@ protected CalciteSchema(@Nullable CalciteSchema parent, Schema schema,
this.typeMap = typeMap;
}
this.path = path;
this.tables =
Lookup.concat(Lookup.of(this.tableMap),
(Lookup<TableEntry>) lookupDecorator.apply(
schema.tables().map((s, n) -> tableEntry(n, s))));
this.subSchemas =
Lookup.concat(Lookup.of(this.subSchemaMap),
(Lookup<CalciteSchema>) lookupDecorator.apply(
schema.subSchemas().map((s, n) -> createSubSchema(s, n))));
}

public Lookup<TableEntry> tables() {
return this.tables.getOrCompute(() ->
Lookup.concat(
Lookup.of(this.tableMap),
decorateLookup(schema.tables().map((s, n) -> tableEntry(n, s)))));

}

public Lookup<CalciteSchema> subSchemas() {
return subSchemas.getOrCompute(() ->
Lookup.concat(
Lookup.of(this.subSchemaMap),
decorateLookup(schema.subSchemas().map((s, n) -> createSubSchema(s, n)))));
}

/** The derived class is able to decorate the lookup. */
protected <S> Lookup<S> decorateLookup(Lookup<S> lookup) {
return lookup;
}

/** Creates a sub-schema with a given name that is defined implicitly. */
Expand Down Expand Up @@ -265,8 +261,8 @@ public List<String> path(@Nullable String name) {
public final @Nullable CalciteSchema getSubSchema(String schemaName,
boolean caseSensitive) {
return caseSensitive
? subSchemas.get(schemaName)
: Named.entityOrNull(subSchemas.getIgnoreCase(schemaName));
? subSchemas().get(schemaName)
: Named.entityOrNull(subSchemas().getIgnoreCase(schemaName));
}

/** Adds a child schema of this schema. */
Expand All @@ -284,7 +280,7 @@ public List<String> path(@Nullable String name) {

/** Returns a table with the given name. Does not look for views. */
public final @Nullable TableEntry getTable(String tableName, boolean caseSensitive) {
return Lookup.get(tables, tableName, caseSensitive);
return Lookup.get(tables(), tableName, caseSensitive);
}

public String getName() {
Expand Down Expand Up @@ -345,7 +341,7 @@ public final Set<String> getTableNames() {
/** Returns the set of filtered table names. Includes implicit and explicit tables
* and functions with zero parameters. */
public final Set<String> getTableNames(LikePattern pattern) {
return tables.getNames(pattern);
return tables().getNames(pattern);
}

/** Returns the set of all types names. */
Expand Down Expand Up @@ -653,11 +649,11 @@ CalciteSchema calciteSchema() {
}

@Override public Lookup<Table> tables() {
return CalciteSchema.this.tables.map((table, name) -> table.getTable());
return CalciteSchema.this.tables().map((table, name) -> table.getTable());
}

@Override public Lookup<? extends SchemaPlus> subSchemas() {
return CalciteSchema.this.subSchemas.map((schema, name) -> schema.plus());
return CalciteSchema.this.subSchemas().map((schema, name) -> schema.plus());
}

@Override public @Nullable Table getTable(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.ImmutableSortedSet;

import org.checkerframework.checker.initialization.qual.UnderInitialization;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.Collection;
Expand Down Expand Up @@ -101,8 +100,7 @@ private SimpleCalciteSchema(@Nullable CalciteSchema parent,
return null;
}

@Override protected CalciteSchema createSubSchema(@UnderInitialization SimpleCalciteSchema this,
Schema schema, String name) {
@Override protected CalciteSchema createSubSchema(Schema schema, String name) {
return new SimpleCalciteSchema(this, schema, name);
}

Expand Down
Loading

0 comments on commit 587728b

Please sign in to comment.