Skip to content

Commit

Permalink
[CALCITE-4882] Introduce new Lambda-based Metadata framework
Browse files Browse the repository at this point in the history
- Introduce a Lambda-based RelMetadataQuery creator for AOT environments
- Add new microbenchmark for metadata retrieval on a five-way-join
- Fix incorrect results for disabled test JdbcTest.testJoinFiveWay()
- Update jmh-gradle-plugin version.
  • Loading branch information
jacques-n committed Nov 10, 2021
1 parent bebe473 commit 043f24d
Show file tree
Hide file tree
Showing 16 changed files with 2,096 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
import org.apache.calcite.util.ControlFlowException;
import org.apache.calcite.util.Util;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;
import com.google.common.util.concurrent.UncheckedExecutionException;

import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.codehaus.commons.compiler.CompileException;
import org.codehaus.commons.compiler.CompilerFactoryFactory;
Expand Down Expand Up @@ -425,4 +427,10 @@ private Key(Class<? extends MetadataHandler<?>> handlerClass,
&& ((Key) obj).provider.equals(provider);
}
}

@API(status = API.Status.INTERNAL)
@VisibleForTesting
public static void clearStaticCache() {
HANDLERS.invalidateAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.rel.metadata.lambda;

import org.apache.calcite.rel.RelNode;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.annotation.concurrent.ThreadSafe;

/**
* A LambdaProvider that maintains a cache for each RelNode class/Lambda class combination,
* delegating to an underlying LambdaProvider to do actual work.
*/
@ThreadSafe
class CachingLambdaProvider implements LambdaProvider {

private final LambdaProvider provider;
private final LoadingCache<Class<? extends RelNode>, LoadingCache<Class, Object>> cache;

CachingLambdaProvider(final LambdaProvider provider) {
this.provider = provider;
this.cache = CacheBuilder.newBuilder()
.build(new CacheLoader<Class, LoadingCache<Class, Object>>() {
@Override public LoadingCache<Class, Object> load(final Class relNodeClazz)
throws Exception {
return CacheBuilder.<Class, Object>newBuilder().build(
new CacheLoader<Class, Object>() {
@Override public Object load(final Class lambdaClazz) throws Exception {
return provider.get((Class<? extends RelNode>) relNodeClazz, lambdaClazz);
}
});
}
});
}

public <R extends RelNode, T extends MetadataLambda> List<T> get(
Class<R> relNodeClazz,
Class<T> lambdaClazz) throws ExecutionException {
return (List<T>) cache.get(relNodeClazz).get(lambdaClazz);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.rel.metadata.lambda;

import org.apache.calcite.plan.RelOptPredicateList;
import org.apache.calcite.rel.RelDistribution;
import org.apache.calcite.rel.RelDistributions;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.metadata.RelMdUtil;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.sql.SqlExplainLevel;
import org.apache.calcite.util.ImmutableBitSet;

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

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

/**
* Canonicalizes methods from null to other standard outputs.
*/
class CanonicalizingIRMQ extends DelegatingIRMQ {
CanonicalizingIRMQ(IRelMetadataQuery delegate) {
super(delegate);
}

@Override public @Nullable RelDistribution distribution(final RelNode rel) {
RelDistribution distribution = super.distribution(rel);
if (distribution == null) {
return RelDistributions.ANY;
}
return distribution;
}

public Double getRowCount(RelNode rel) {
return RelMdUtil.validateResult(castNonNull(super.getRowCount(rel)));
}

public @Nullable Double getPopulationSize(RelNode rel, ImmutableBitSet groupKey) {
return RelMdUtil.validateResult(super.getPopulationSize(rel, groupKey));
}

public @Nullable Double getPercentageOriginalRows(RelNode rel) {
Double result = super.getPercentageOriginalRows(rel);
return RelMdUtil.validatePercentage(result);
}

public @Nullable Double getSelectivity(RelNode rel, @Nullable RexNode predicate) {
return RelMdUtil.validatePercentage(super.getSelectivity(rel, predicate));
}

public @Nullable Double getDistinctRowCount(
RelNode rel,
ImmutableBitSet groupKey,
@Nullable RexNode predicate) {
return RelMdUtil.validateResult(super.getDistinctRowCount(rel, groupKey, predicate));
}

public RelOptPredicateList getPulledUpPredicates(RelNode rel) {
RelOptPredicateList result = super.getPulledUpPredicates(rel);
return result != null ? result : RelOptPredicateList.EMPTY;
}

public Boolean isVisibleInExplain(RelNode rel,
SqlExplainLevel explainLevel) {
Boolean b = super.isVisibleInExplain(rel, explainLevel);
return b == null || b;
}
}
Loading

0 comments on commit 043f24d

Please sign in to comment.