Copilot commented on code in PR #13357:
URL: https://github.com/apache/gluten/pull/13357#discussion_r4483787514


##########
cpp/velox/compute/WholeStageResultIterator.cc:
##########
@@ -521,7 +522,19 @@ void WholeStageResultIterator::collectMetrics() {
           "numMemoryAllocations", 
opStats->numMemoryAllocations)("spilledInputBytes", opStats->spilledInputBytes)(
           "spilledBytes", opStats->spilledBytes)("spilledRows", 
opStats->spilledRows)(
           "spilledPartitions", opStats->spilledPartitions)("spilledFiles", 
opStats->spilledFiles)(
-          "physicalWrittenBytes", 
opStats->physicalWrittenBytes)("customStats", customStats));
+          "physicalWrittenBytes", 
opStats->physicalWrittenBytes)("customStats", customStats);
+      // Rows processed per Delta counter function, keyed by its slot name. 
Only FilterProject
+      // operators export these, and only when the plan calls a counter, so 
other operators get no key.
+      folly::dynamic expressionStats = folly::dynamic::object();
+      for (const auto& exprStats : opStats->expressionStats) {
+        if (isIncrementMetricFunction(exprStats.first)) {
+          expressionStats[exprStats.first] = exprStats.second.numProcessedRows;
+        }
+      }
+      if (!expressionStats.empty()) {
+        operatorJson["expressionStats"] = std::move(expressionStats);
+      }

Review Comment:
   This walks `opStats->expressionStats` and builds an object for every 
operator unconditionally. Since this PR already computes 
`usesIncrementMetricFunctions_`, you can skip this entire block when 
`usesIncrementMetricFunctions_` is false to avoid extra iteration/allocation on 
queries that don’t use counters (and keep the exported metrics shape maximally 
stable).



##########
cpp/velox/operators/functions/IncrementMetricFunction.cc:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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 "operators/functions/IncrementMetricFunction.h"
+
+#include <charconv>
+#include <memory>
+#include <optional>
+#include <string_view>
+#include <system_error>
+#include <unordered_set>
+#include <vector>
+
+#include "velox/core/Expressions.h"
+#include "velox/core/QueryConfig.h"
+#include "velox/expression/EvalCtx.h"
+#include "velox/expression/FunctionSignature.h"
+#include "velox/expression/VectorFunction.h"
+
+namespace gluten {
+namespace {
+
+using namespace facebook::velox;
+
+/// Returns its only argument. See the header for why the name, not the body, 
carries the meaning.
+class IncrementMetricFunction : public exec::VectorFunction {
+ public:
+  void apply(
+      const SelectivityVector& rows,
+      std::vector<VectorPtr>& args,
+      const TypePtr& /*outputType*/,
+      exec::EvalCtx& context,
+      VectorPtr& result) const override {
+    VELOX_CHECK_EQ(args.size(), 1);
+    context.moveOrCopyResult(args[0], rows, result);
+  }
+};
+
+void collectIncrementMetricCalls(const core::ITypedExpr& expr, 
std::unordered_set<std::string>& names) {
+  if (const auto* call = dynamic_cast<const core::CallTypedExpr*>(&expr)) {
+    if (isIncrementMetricFunction(call->name())) {
+      names.insert(call->name());
+    }
+  }
+  if (const auto* lambda = dynamic_cast<const core::LambdaTypedExpr*>(&expr)) {
+    if (lambda->body() != nullptr) {
+      collectIncrementMetricCalls(*lambda->body(), names);
+    }
+    return;
+  }
+  for (const auto& input : expr.inputs()) {
+    if (input != nullptr) {
+      collectIncrementMetricCalls(*input, names);
+    }
+  }
+}
+
+void collectIncrementMetricCallsInProjects(const core::PlanNode& plan, 
std::unordered_set<std::string>& names) {
+  if (const auto* project = dynamic_cast<const core::ProjectNode*>(&plan)) {
+    for (const auto& projection : project->projections()) {
+      if (projection != nullptr) {
+        collectIncrementMetricCalls(*projection, names);
+      }
+    }
+  }
+  for (const auto& source : plan.sources()) {
+    if (source != nullptr) {
+      collectIncrementMetricCallsInProjects(*source, names);
+    }
+  }
+}
+
+} // namespace
+
+std::string incrementMetricFunctionName(int32_t slot) {
+  return std::string(kIncrementMetricFunctionPrefix) + std::to_string(slot);
+}
+
+bool isIncrementMetricFunction(const std::string& functionName) {
+  const std::string_view prefix{kIncrementMetricFunctionPrefix};
+  if (functionName.size() <= prefix.size() || functionName.compare(0, 
prefix.size(), prefix) != 0) {
+    return false;
+  }
+  int32_t slot = -1;
+  const char* const end = functionName.data() + functionName.size();
+  const auto [parsedEnd, error] = std::from_chars(functionName.data() + 
prefix.size(), end, slot);
+  // The last check rejects spellings that parse to a slot but are not its 
name, such as a leading
+  // zero.
+  return error == std::errc() && parsedEnd == end && slot >= 0 && slot < 
kIncrementMetricFunctionSlots &&
+      functionName == incrementMetricFunctionName(slot);
+}
+
+void registerIncrementMetricFunctions() {
+  // Not deterministic: keeps Velox from constant-folding or 
dictionary-peeling the call, either of
+  // which would change how many rows it is evaluated on. No default null 
behavior: a null input
+  // still counts, as it does on Spark.
+  const auto metadata = 
exec::VectorFunctionMetadataBuilder().deterministic(false).defaultNullBehavior(false).build();
+  for (int32_t slot = 0; slot < kIncrementMetricFunctionSlots; ++slot) {
+    exec::registerVectorFunction(
+        incrementMetricFunctionName(slot),
+        
{exec::FunctionSignatureBuilder().typeVariable("T").returnType("T").argumentType("T").build()},
+        std::make_unique<IncrementMetricFunction>(),
+        metadata);
+  }
+}
+
+bool usesIncrementMetricFunctions(const core::PlanNode& plan) {
+  std::unordered_set<std::string> names;
+  collectIncrementMetricCallsInProjects(plan, names);
+  return !names.empty();
+}
+
+void checkIncrementMetricFunctionsInProjects(
+    const std::unordered_map<uint64_t, std::string>& functionMap,
+    const core::PlanNode& plan) {
+  std::optional<std::unordered_set<std::string>> calledInProjects;
+  for (const auto& entry : functionMap) {
+    // Declarations are spelled `<name>:<argument types>`.
+    const auto name = entry.second.substr(0, entry.second.find(':'));
+    if (!isIncrementMetricFunction(name)) {
+      continue;
+    }
+    if (!calledInProjects.has_value()) {
+      calledInProjects.emplace();
+      collectIncrementMetricCallsInProjects(plan, *calledInProjects);
+    }
+    VELOX_CHECK(
+        calledInProjects->count(name) > 0,
+        "Delta counter {} is called outside a projection, where the JVM does 
not credit it",

Review Comment:
   This error message is misleading for at least one concrete failure mode: 
when a counter is declared in `functionMap` but never called anywhere in the 
plan, the check fails but the message still says it was called outside a 
projection. Consider rewording to reflect the actual condition being validated 
(e.g., not found in any projection expressions), and optionally mention that it 
may be unused or used outside a projection.



##########
cpp/velox/operators/functions/IncrementMetricFunction.cc:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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 "operators/functions/IncrementMetricFunction.h"
+
+#include <charconv>
+#include <memory>
+#include <optional>
+#include <string_view>
+#include <system_error>
+#include <unordered_set>
+#include <vector>
+
+#include "velox/core/Expressions.h"
+#include "velox/core/QueryConfig.h"
+#include "velox/expression/EvalCtx.h"
+#include "velox/expression/FunctionSignature.h"
+#include "velox/expression/VectorFunction.h"
+
+namespace gluten {
+namespace {
+
+using namespace facebook::velox;
+
+/// Returns its only argument. See the header for why the name, not the body, 
carries the meaning.
+class IncrementMetricFunction : public exec::VectorFunction {
+ public:
+  void apply(
+      const SelectivityVector& rows,
+      std::vector<VectorPtr>& args,
+      const TypePtr& /*outputType*/,
+      exec::EvalCtx& context,
+      VectorPtr& result) const override {
+    VELOX_CHECK_EQ(args.size(), 1);
+    context.moveOrCopyResult(args[0], rows, result);
+  }
+};
+
+void collectIncrementMetricCalls(const core::ITypedExpr& expr, 
std::unordered_set<std::string>& names) {
+  if (const auto* call = dynamic_cast<const core::CallTypedExpr*>(&expr)) {
+    if (isIncrementMetricFunction(call->name())) {
+      names.insert(call->name());
+    }
+  }
+  if (const auto* lambda = dynamic_cast<const core::LambdaTypedExpr*>(&expr)) {
+    if (lambda->body() != nullptr) {
+      collectIncrementMetricCalls(*lambda->body(), names);
+    }
+    return;
+  }
+  for (const auto& input : expr.inputs()) {
+    if (input != nullptr) {
+      collectIncrementMetricCalls(*input, names);
+    }
+  }
+}
+
+void collectIncrementMetricCallsInProjects(const core::PlanNode& plan, 
std::unordered_set<std::string>& names) {
+  if (const auto* project = dynamic_cast<const core::ProjectNode*>(&plan)) {
+    for (const auto& projection : project->projections()) {
+      if (projection != nullptr) {
+        collectIncrementMetricCalls(*projection, names);
+      }
+    }
+  }
+  for (const auto& source : plan.sources()) {
+    if (source != nullptr) {
+      collectIncrementMetricCallsInProjects(*source, names);
+    }
+  }
+}
+
+} // namespace
+
+std::string incrementMetricFunctionName(int32_t slot) {
+  return std::string(kIncrementMetricFunctionPrefix) + std::to_string(slot);
+}
+
+bool isIncrementMetricFunction(const std::string& functionName) {
+  const std::string_view prefix{kIncrementMetricFunctionPrefix};
+  if (functionName.size() <= prefix.size() || functionName.compare(0, 
prefix.size(), prefix) != 0) {
+    return false;
+  }
+  int32_t slot = -1;
+  const char* const end = functionName.data() + functionName.size();
+  const auto [parsedEnd, error] = std::from_chars(functionName.data() + 
prefix.size(), end, slot);
+  // The last check rejects spellings that parse to a slot but are not its 
name, such as a leading
+  // zero.
+  return error == std::errc() && parsedEnd == end && slot >= 0 && slot < 
kIncrementMetricFunctionSlots &&
+      functionName == incrementMetricFunctionName(slot);
+}
+
+void registerIncrementMetricFunctions() {
+  // Not deterministic: keeps Velox from constant-folding or 
dictionary-peeling the call, either of
+  // which would change how many rows it is evaluated on. No default null 
behavior: a null input
+  // still counts, as it does on Spark.
+  const auto metadata = 
exec::VectorFunctionMetadataBuilder().deterministic(false).defaultNullBehavior(false).build();
+  for (int32_t slot = 0; slot < kIncrementMetricFunctionSlots; ++slot) {
+    exec::registerVectorFunction(
+        incrementMetricFunctionName(slot),
+        
{exec::FunctionSignatureBuilder().typeVariable("T").returnType("T").argumentType("T").build()},
+        std::make_unique<IncrementMetricFunction>(),
+        metadata);
+  }
+}
+
+bool usesIncrementMetricFunctions(const core::PlanNode& plan) {
+  std::unordered_set<std::string> names;
+  collectIncrementMetricCallsInProjects(plan, names);
+  return !names.empty();

Review Comment:
   `usesIncrementMetricFunctions` only needs a boolean, but it currently 
allocates and populates an `unordered_set` and traverses the full plan tree 
even after the first match. Consider adding a short-circuiting traversal 
variant (e.g., return early once an IncrementMetric call is found) to reduce 
overhead on large plans.



-- 
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]

Reply via email to