Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ dependencies {
testImplementation("net.hydromatic:foodmart-queries")
testImplementation("net.hydromatic:quidem")
testImplementation("org.apache.calcite.avatica:avatica-server")
testImplementation("io.github.classgraph:classgraph:4.8.173")
testImplementation("org.apache.commons:commons-pool2")
testImplementation("org.hsqldb:hsqldb::jdk8")
testImplementation("sqlline:sqlline")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.apache.calcite.test;

import static org.junit.jupiter.api.Assertions.assertTrue;

import io.github.classgraph.ClassGraph;
import io.github.classgraph.ClassInfo;
import io.github.classgraph.ScanResult;

import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.RelShuttle;

import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
* Discovers every concrete {@link RelNode} implementation on the classpath and
* verifies {@link RelShuttle} declares a compatible {@code visit} method.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

verifies whether

*/
class RelNodeVisitCoverageTest {

@Test void relShuttleCoversAllRelNodes() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so what's the result of the test?
The JavaDoc of the base shuttle should probably list the rules for a class to be visited; otherwise we will be arguing forever.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a work in progress. The current logic can obtain the set of all classes that inherit from RelNode, as well as the set of all classes visited in RelShuttle. The current test only compares whether these two sets are equal. We might also need a criterion to determine which classes should be included in the visit and which classes should be excluded.

Set<Class<? extends RelNode>> relNodeClasses = findRelNodeClasses();
Set<Class<?>> visitParameters = visitParameterTypes();

Set<Class<? extends RelNode>> missing = relNodeClasses.stream()
.filter(relClass -> visitParameters.stream()
.noneMatch(relClass::isAssignableFrom))
.collect(Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing(Class::getName))));

assertTrue(missing.isEmpty(),
() -> "RelShuttle.visit(...) is missing for: " + missing);
}

private Set<Class<?>> visitParameterTypes() {
Set<Class<?>> visitParameters = new HashSet<>();
for (Method method : RelShuttle.class.getMethods()) {
if ("visit".equals(method.getName()) && method.getParameterCount() == 1) {
visitParameters.add(method.getParameterTypes()[0]);
}
}
return visitParameters;
}

private Set<Class<? extends RelNode>> findRelNodeClasses() {
Set<Class<? extends RelNode>> result =
new TreeSet<>(Comparator.comparing(Class::getName));
try (ScanResult scan = new ClassGraph()
.enableClassInfo()
.ignoreClassVisibility()
.acceptPackages(RelNode.class.getPackageName())
.scan()) {
for (ClassInfo classInfo : scan.getClassesImplementing(RelNode.class.getName())) {
Class<? extends RelNode> clazz = classInfo.loadClass(RelNode.class);
if (isConcreteRelNode(clazz)) {
result.add(clazz);
}
}
}
return result;
}

private boolean isConcreteRelNode(Class<?> clazz) {
return RelNode.class.isAssignableFrom(clazz)
&& !clazz.isInterface()
&& !Modifier.isAbstract(clazz.getModifiers());
}
}
Loading