felipepessoto commented on code in PR #13658: URL: https://github.com/apache/gluten/pull/13658#discussion_r4637280907
########## cpp/velox/utils/DynamicFilterUtils.cc: ########## @@ -0,0 +1,98 @@ +/* + * 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. + */ + +#include "utils/DynamicFilterUtils.h" + +#include "velox/connectors/common/FileTableHandle.h" +#include "velox/functions/FunctionRegistry.h" + +using namespace facebook::velox; + +namespace gluten { + +namespace { + +bool callsNonDeterministicFunction(const core::ITypedExpr& expr) { + if (const auto* call = dynamic_cast<const core::CallTypedExpr*>(&expr)) { + // Special forms such as if, switch, and, or and cast are not registered functions; they are + // deterministic when their inputs are. + if (isDeterministic(call->name()) == false) { + return true; + } + } + if (const auto* lambda = dynamic_cast<const core::LambdaTypedExpr*>(&expr)) { + if (lambda->body() != nullptr && callsNonDeterministicFunction(*lambda->body())) { + return true; + } + } + for (const auto& input : expr.inputs()) { + if (input != nullptr && callsNonDeterministicFunction(*input)) { + return true; + } + } + return false; +} + +bool evaluatesNonDeterministicExpr(const core::PlanNode& node) { + if (const auto* project = dynamic_cast<const core::AbstractProjectNode*>(&node)) { + for (const auto& projection : project->projections()) { + if (projection != nullptr && callsNonDeterministicFunction(*projection)) { + return true; + } + } + } else if (const auto* filter = dynamic_cast<const core::FilterNode*>(&node)) { + return filter->filter() != nullptr && callsNonDeterministicFunction(*filter->filter()); + } else if (const auto* scan = dynamic_cast<const core::TableScanNode*>(&node)) { + // A filter pushed into the scan, such as `rand() < 0.5`, runs after the scan applies the + // dynamic filters it accepted. + const auto* fileTable = dynamic_cast<const connector::FileTableHandle*>(scan->tableHandle().get()); + return fileTable != nullptr && fileTable->remainingFilter() != nullptr && + callsNonDeterministicFunction(*fileTable->remainingFilter()); + } + return false; +} + +// The nodes a dynamic filter from the probe can reach: the probe input and, below it, the first +// input of each node down to the leaf. Other inputs, such as a build side, run in other pipelines. +bool hasNonDeterministicExprOnProbePath(const core::PlanNode& probeInput) { + for (const core::PlanNode* node = &probeInput; node != nullptr;) { + if (evaluatesNonDeterministicExpr(*node)) { + return true; + } + node = node->sources().empty() ? nullptr : node->sources()[0].get(); + } + return false; +} + +} // namespace + +bool hasNonDeterministicExprBelowHashProbe(const core::PlanNode& plan) { + if (const auto* join = dynamic_cast<const core::HashJoinNode*>(&plan)) { + if (!join->sources().empty() && join->sources()[0] != nullptr && + hasNonDeterministicExprOnProbePath(*join->sources()[0])) { + return true; + } + } + for (const auto& source : plan.sources()) { + if (source != nullptr && hasNonDeterministicExprBelowHashProbe(*source)) { Review Comment: On a left-deep chain of deterministic joins, the probe-path walk above goes all the way to the leaf for each join. This recursion then visits the next join and starts the same walk again. That makes the check quadratic in join depth and repeats function-registry lookups during setup of every task. Could we compute both results in one bottom-up pass: whether the first-input path contains a non-deterministic expression, and whether the subtree contains an affected join? ########## cpp/velox/utils/DynamicFilterUtils.cc: ########## @@ -0,0 +1,98 @@ +/* + * 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. + */ + +#include "utils/DynamicFilterUtils.h" + +#include "velox/connectors/common/FileTableHandle.h" +#include "velox/functions/FunctionRegistry.h" + +using namespace facebook::velox; + +namespace gluten { + +namespace { + +bool callsNonDeterministicFunction(const core::ITypedExpr& expr) { + if (const auto* call = dynamic_cast<const core::CallTypedExpr*>(&expr)) { + // Special forms such as if, switch, and, or and cast are not registered functions; they are + // deterministic when their inputs are. + if (isDeterministic(call->name()) == false) { + return true; + } + } + if (const auto* lambda = dynamic_cast<const core::LambdaTypedExpr*>(&expr)) { + if (lambda->body() != nullptr && callsNonDeterministicFunction(*lambda->body())) { + return true; + } + } + for (const auto& input : expr.inputs()) { + if (input != nullptr && callsNonDeterministicFunction(*input)) { + return true; + } + } + return false; +} + +bool evaluatesNonDeterministicExpr(const core::PlanNode& node) { + if (const auto* project = dynamic_cast<const core::AbstractProjectNode*>(&node)) { + for (const auto& projection : project->projections()) { + if (projection != nullptr && callsNonDeterministicFunction(*projection)) { + return true; + } + } + } else if (const auto* filter = dynamic_cast<const core::FilterNode*>(&node)) { + return filter->filter() != nullptr && callsNonDeterministicFunction(*filter->filter()); + } else if (const auto* scan = dynamic_cast<const core::TableScanNode*>(&node)) { + // A filter pushed into the scan, such as `rand() < 0.5`, runs after the scan applies the + // dynamic filters it accepted. + const auto* fileTable = dynamic_cast<const connector::FileTableHandle*>(scan->tableHandle().get()); + return fileTable != nullptr && fileTable->remainingFilter() != nullptr && + callsNonDeterministicFunction(*fileTable->remainingFilter()); + } + return false; +} + +// The nodes a dynamic filter from the probe can reach: the probe input and, below it, the first +// input of each node down to the leaf. Other inputs, such as a build side, run in other pipelines. +bool hasNonDeterministicExprOnProbePath(const core::PlanNode& probeInput) { + for (const core::PlanNode* node = &probeInput; node != nullptr;) { + if (evaluatesNonDeterministicExpr(*node)) { + return true; + } + node = node->sources().empty() ? nullptr : node->sources()[0].get(); + } + return false; +} + +} // namespace + +bool hasNonDeterministicExprBelowHashProbe(const core::PlanNode& plan) { + if (const auto* join = dynamic_cast<const core::HashJoinNode*>(&plan)) { Review Comment: Can we limit this check to join types that can produce a dynamic filter? [Velox does not push one for a left outer join](https://github.com/IBM/velox/blob/67fa1bbee62e9f8752b3c34cc92f179022cd2ac6/velox/exec/HashProbe.cpp#L569-L588). For example, take a selective inner join, project `rand(42)` on its output, then feed that into a left outer join in the same task. The inner join can safely prune its scan because `rand` runs after it. As written, the outer join trips this guard and disables that pruning too. This would make a useful regression test. -- 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]
