NishthaShah commented on code in PR #20441: URL: https://github.com/apache/druid/pull/20441#discussion_r4206323146
########## processing/src/main/java/org/apache/druid/query/planning/RequireTimeConditionAnalyzer.java: ########## @@ -0,0 +1,131 @@ +/* + * 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.druid.query.planning; + +import org.apache.druid.java.util.common.Intervals; +import org.apache.druid.query.BaseQuery; +import org.apache.druid.query.DataSource; +import org.apache.druid.query.JoinDataSource; +import org.apache.druid.query.Query; +import org.apache.druid.query.QueryDataSource; +import org.apache.druid.query.spec.QuerySegmentSpec; + +/** + * Walks a query tree to decide whether every physical leaf datasource has an + * ancestor query with a bounded {@link QuerySegmentSpec}, for the purpose of + * enforcing {@code druid.sql.planner.requireTimeCondition}. + * + * <p>Unlike {@link ExecutionVertex#getEffectiveQuerySegmentSpec()}, which is + * pruned at the execution-vertex boundary (see + * {@link Query#mayCollapseQueryDataSource()}), this walker descends into every + * {@link QueryDataSource} and every left input of a {@link JoinDataSource}. + * {@code requireTimeCondition} asks a validation question ("is there a + * {@code __time} filter anywhere that bounds the base scan?") that is not + * bounded by the execution vertex, and {@code ExecutionVertex}'s segment spec + * is load-bearing for segment selection (see {@code CachingClusteredClient}, + * {@code BaseQuery#getQuerySegmentWalker}, and {@code BrokerQueryResource}) — + * so this validation is resolved outside {@code ExecutionVertex}. + * + * <p>Right-hand inputs of joins are intentionally not descended: a + * {@code __time} filter there does not bound the base table (see + * {@code CalciteQueryTest#testRequireTimeConditionSemiJoinNegative}). + */ +public final class RequireTimeConditionAnalyzer +{ + private RequireTimeConditionAnalyzer() + { + } + + /** + * @return true if every physical leaf datasource in {@code query} is + * descended from an ancestor query with a bounded interval. Global + * datasources (lookups, inline) always satisfy the check. + */ + public static boolean hasTimeFilterOnAllLegs(Query<?> query) + { + TimeFilterExplorer explorer = new TimeFilterExplorer(query); + return explorer.allLegsBounded; + } + + private static class TimeFilterExplorer extends ExecutionVertexShuttle + { + boolean allLegsBounded = true; + + TimeFilterExplorer(Query<?> query) + { + traverse(query); + } + + @Override + protected boolean mayTraverseQuery(Query<?> query) + { + return true; + } + + @Override + protected boolean mayTraverseDataSource(EVNode node) + { + return true; + } + + @Override + protected Query<?> visitQuery(Query<?> query) + { + return query; + } + + @Override + protected DataSource visit(DataSource dataSource, boolean leaf) + { + if (!isPhysicalLeaf(dataSource) || dataSource.isGlobal()) { + return dataSource; + } + boolean crossedJoinLegBoundary = false; + for (int i = parents.size() - 1; i >= 0; i--) { + EVNode ancestor = parents.get(i); + if (!ancestor.isQuery() && ancestor.index != null && ancestor.index != 0 && i > 0) { + EVNode parentOfAncestor = parents.get(i - 1); + if (!parentOfAncestor.isQuery() && parentOfAncestor.dataSource instanceof JoinDataSource) { + crossedJoinLegBoundary = true; + } + } + if (ancestor.isQuery() && !crossedJoinLegBoundary && isBounded(ancestor.getQuery())) { Review Comment: Thanks. Pushed a regression commit that adds two tests in RequireTimeConditionAnalyzerTest to cover the "bounded outer over unbounded inner" shape being called out: Verified the fix end-to-end for the specific SQL query for (outer interval mask an unbounded inner scan) `SELECT COUNT(*) FROM wikipedia foo JOIN (SELECT page, __time FROM wikipedia LIMIT 10) bar ON foo.page = bar.page WHERE foo.__time >= TIMESTAMP '2015-01-01';` Native plan is `Timeseries([2015-01-01/∞)) over Join(TableDataSource(wikipedia), QueryDataSource(ScanQuery(wikipedia, ETERNITY, LIMIT 10)))` — the exact P1 shape. - Pre-fix (`ExecutionVertex.getEffectiveQuerySegmentSpec only`): wrongly succeeds, returns 17. The outer [2015-01-01/∞) masks the unbounded inner scan. - Post-fix (walker descends every join leg): correctly rejects with `CannotBuildQueryException("requireTimeCondition is enabled, all queries must include a filter condition on the __time column")`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
