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 68d7a78
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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 +46,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 @@ -71,8 +72,7 @@ private CachingCalciteSchema(@Nullable CalciteSchema parent, Schema schema,
@Nullable List<? extends List<String>> path,
LookupDecorator lookupDecorator) {
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 +107,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
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 @@ -30,6 +29,7 @@
import org.apache.calcite.schema.Wrapper;
import org.apache.calcite.schema.impl.MaterializedViewTable;
import org.apache.calcite.schema.impl.StarTable;
import org.apache.calcite.schema.lookup.LazyReference;
import org.apache.calcite.schema.lookup.LikePattern;
import org.apache.calcite.schema.lookup.Lookup;
import org.apache.calcite.schema.lookup.Named;
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
@@ -0,0 +1,43 @@
/*
* 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.schema.lookup;

import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;

/**
* This class can be used to lazily initialize an object.
*
* @param <T> Element Type
*/
public class LazyReference<T> {

private final AtomicReference<T> value = new AtomicReference<>();

public T getOrCompute(Supplier<T> supplier) {
while (true) {
T result = value.get();
if (result != null) {
return result;
}
T computed = supplier.get();
if (value.compareAndSet(null, computed)) {
return computed;
}
}
}
}

0 comments on commit 68d7a78

Please sign in to comment.