-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[CALCITE-7511] Route RelShuttle dispatch through type-specific visit overloads #4928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
ff4a2ec
154455d
5d947f9
114f878
e2ed0c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /* | ||
| * 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.test; | ||
|
|
||
| import org.apache.calcite.rel.AbstractRelNode; | ||
| import org.apache.calcite.rel.RelNode; | ||
| import org.apache.calcite.rel.RelShuttle; | ||
|
|
||
| import com.google.common.reflect.ClassPath; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.IOException; | ||
| 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; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** Guards against introducing new dispatch gaps in {@link RelShuttle}. */ | ||
| class RelShuttleCoverageTest { | ||
|
|
||
| private static final Set<String> SCANNED_PACKAGES = | ||
| Set.of("org.apache.calcite.rel.core", | ||
| "org.apache.calcite.rel.logical"); | ||
|
|
||
| /** Pre-existing gaps that predate this safety net. Each should be addressed in its own follow-up | ||
| * JIRA before being removed from this list. */ | ||
| private static final Set<String> KNOWN_UNCOVERED_RELS = | ||
| Set.of(// core | ||
| "org.apache.calcite.rel.core.Collect", | ||
| "org.apache.calcite.rel.core.Sample", | ||
| "org.apache.calcite.rel.core.Uncollect", | ||
| "org.apache.calcite.rel.core.Combine", | ||
| // logical | ||
| "org.apache.calcite.rel.logical.LogicalConditionalCorrelate", | ||
| "org.apache.calcite.rel.logical.LogicalSortExchange", | ||
| "org.apache.calcite.rel.logical.LogicalTableSpool"); | ||
|
|
||
| /** Every concrete RelNode in the scanned packages must be covered by a non-{@code visit(RelNode)} | ||
| * overload on {@link RelShuttle}. Without this, a {@code RelShuttleImpl} subclass that customizes | ||
| * the type-specific visitor would silently not be called for the rel. */ | ||
| @Test void everyRelNodeHasMatchingVisitOverload() throws IOException { | ||
| final Set<Class<?>> visitParameters = collectVisitParameterTypes(); | ||
| final Set<Class<? extends RelNode>> relClasses = findConcreteRelNodesInPackages(); | ||
|
|
||
| final Set<Class<? extends RelNode>> uncovered = relClasses.stream() | ||
| .filter(c -> visitParameters.stream().noneMatch(vp -> vp.isAssignableFrom(c))) | ||
| .filter(c -> !KNOWN_UNCOVERED_RELS.contains(c.getName())) | ||
| .collect( | ||
| Collectors.toCollection(() -> | ||
| new TreeSet<>(Comparator.comparing(Class::getName)))); | ||
|
|
||
| assertTrue(uncovered.isEmpty(), | ||
| () -> "RelNodes with no RelShuttle.visit(...) overload covering them " | ||
| + "(only the generic visit(RelNode) catch-all matches): " + uncovered); | ||
|
|
||
| // Fail-closed: flag any KNOWN_UNCOVERED_RELS entry that is no longer a gap so it gets removed. | ||
| final Set<String> obsoleteSkips = relClasses.stream() | ||
| .filter(c -> KNOWN_UNCOVERED_RELS.contains(c.getName())) | ||
| .filter(c -> visitParameters.stream().anyMatch(vp -> vp.isAssignableFrom(c))) | ||
| .map(Class::getName) | ||
| .collect(Collectors.toCollection(TreeSet::new)); | ||
| assertTrue(obsoleteSkips.isEmpty(), | ||
| () -> "KNOWN_UNCOVERED_RELS entries that are now covered (remove them): " + obsoleteSkips); | ||
| } | ||
|
|
||
| /** Every {@link RelShuttle#visit(...)} parameter type (other than {@code RelNode}) must declare | ||
| * its own {@code accept(RelShuttle)} so dispatch routes through the type-specific overload. */ | ||
| @Test void everyVisitParameterTypeDeclaresAccept() { | ||
| final Set<Class<?>> missingAccept = collectVisitParameterTypes().stream() | ||
| .filter(c -> !declaresAcceptRelShuttle(c)) | ||
| .collect( | ||
| Collectors.toCollection(() -> | ||
| new TreeSet<>(Comparator.comparing(Class::getName)))); | ||
|
|
||
| assertTrue(missingAccept.isEmpty(), | ||
| () -> "RelShuttle.visit(X) parameter types whose X does not declare accept(RelShuttle): " | ||
| + missingAccept); | ||
| } | ||
|
|
||
| /** Visit parameter types other than {@link RelNode} (the catch-all fallback). */ | ||
| private static Set<Class<?>> collectVisitParameterTypes() { | ||
| final Set<Class<?>> params = new HashSet<>(); | ||
| for (Method method : RelShuttle.class.getMethods()) { | ||
| if ("visit".equals(method.getName()) && method.getParameterCount() == 1) { | ||
| Class<?> paramType = method.getParameterTypes()[0]; | ||
| if (paramType != RelNode.class) { | ||
| params.add(paramType); | ||
| } | ||
| } | ||
| } | ||
| return params; | ||
| } | ||
|
|
||
| private static boolean declaresAcceptRelShuttle(Class<?> clazz) { | ||
| try { | ||
| clazz.getDeclaredMethod("accept", RelShuttle.class); | ||
| return true; | ||
| } catch (NoSuchMethodException e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private static Set<Class<? extends RelNode>> findConcreteRelNodesInPackages() throws IOException { | ||
| final Set<Class<? extends RelNode>> classes = new HashSet<>(); | ||
| final ClassPath classPath = ClassPath.from(RelShuttleCoverageTest.class.getClassLoader()); | ||
| for (String packageName : SCANNED_PACKAGES) { | ||
| for (ClassPath.ClassInfo info : classPath.getTopLevelClasses(packageName)) { | ||
| final Class<?> clazz = info.load(); | ||
| if (RelNode.class.isAssignableFrom(clazz) | ||
| && !Modifier.isAbstract(clazz.getModifiers()) | ||
| && clazz != AbstractRelNode.class) { | ||
| @SuppressWarnings("unchecked") | ||
| Class<? extends RelNode> relClass = (Class<? extends RelNode>) clazz; | ||
| classes.add(relClass); | ||
| } | ||
| } | ||
| } | ||
| return classes; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,26 @@ The same applies to `SqlBabelCreateTable` and `SqlUnpivot`. | |
| * [<a href="https://issues.apache.org/jira/browse/CALCITE-6942">CALCITE-6942</a>] | ||
| Rename the method `decorrelateFetchOneSort` to `decorrelateSortWithRowNumber`. | ||
|
|
||
| * [<a href="https://issues.apache.org/jira/browse/CALCITE-7511">CALCITE-7511</a>] | ||
| The abstract `TableFunctionScan`, `Window`, and `Snapshot` rel classes now override | ||
| `accept(RelShuttle)`, and `RelShuttle` declares new `visit(Window)` and `visit(Snapshot)` | ||
| overloads (with default implementations in `RelShuttleImpl` / `RelHomogeneousShuttle`). | ||
| Two consumer-facing changes follow: | ||
| (1) Callers that implement `RelShuttle` directly must implement the two new methods. | ||
| (2) Callers that subclassed `RelShuttleImpl` and routed `Window` / `Snapshot` / | ||
| `TableFunctionScan` through `visit(RelNode other)` with `instanceof` checks should | ||
| migrate to the type-specific `visit(Window)` / `visit(Snapshot)` / | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this include a pointer to the code that is migrated in this PR?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes that will be better. |
||
| `visit(TableFunctionScan)` overrides; those `instanceof` branches in `visit(RelNode)` | ||
| will silently stop being called for these types. Note that because the override is on | ||
| the abstract parents, **all** subclasses dispatch through the type-specific overload — | ||
| not just the `Logical*` variant. For example, `EnumerableTableFunctionScan` now also routes through | ||
| `visit(TableFunctionScan)`; the override added earlier in 1.42.0 originally lived on | ||
| `LogicalTableFunctionScan` and has been moved up. | ||
| Also adds the previously-missing `RelHomogeneousShuttle.visit(LogicalAsofJoin)` forwarding override. | ||
| Subclasses of `RelHomogeneousShuttle` that relied on `LogicalAsofJoin` not being routed through their | ||
| `visit(RelNode)` override will now see it routed there; this matches the behavior of every other rel | ||
| type in the homogeneous shuttle. | ||
|
|
||
| #### New features | ||
| {: #new-features-1-42-0} | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why aren't we fixing these too? Is it too disruptive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I felt it was expanding the scope substantially. On a second thought, I decided to bite the bullet by fixing it for those other
UNCOVERED_RELSas well. I will shortly update the PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated it for the other
UNCOVERED_RELSas well. Please review.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before merging this let's send a message to the dev list warning people about it, and giving them a chance to comment. Do you want to send it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I can send about this breaking change in dev list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Started a discussion in dev mailing list.