zanmato1984 commented on code in PR #42106:
URL: https://github.com/apache/arrow/pull/42106#discussion_r1635358856


##########
cpp/src/arrow/compute/special_forms/if_else_special_form.cc:
##########
@@ -0,0 +1,62 @@
+// 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.
+
+// NOTE: API is EXPERIMENTAL and will change without going through a
+// deprecation cycle.
+
+#include "arrow/compute/special_forms/if_else_special_form.h"
+#include "arrow/compute/exec.h"
+#include "arrow/compute/expression.h"
+#include "arrow/compute/expression_internal.h"
+
+namespace arrow {
+namespace compute {
+
+Result<Datum> IfElseSpecialForm::Execute(const Expression::Call& call,
+                                         const ExecBatch& input,
+                                         ExecContext* exec_context) {
+  // The kernel (if_else) is not selection-vector-aware, so the input should 
not have a
+  // selection vector.
+  DCHECK(!call.kernel->selection_vector_aware && !input.selection_vector);
+
+  std::vector<Datum> arguments(call.arguments.size());
+  ARROW_ASSIGN_OR_RAISE(arguments[0],
+                        ExecuteScalarExpression(call.arguments[0], input, 
exec_context));
+  // Use cond as selection vector for IF branch.
+  // TODO: Consider chunked array for arguments[0].
+  auto if_sel = std::make_shared<SelectionVector>(arguments[0].array());
+  // Duplicate and invert cond as selection vector for ELSE branch.
+  ARROW_ASSIGN_OR_RAISE(
+      auto else_sel,
+      if_sel->Copy(CPUDevice::memory_manager(exec_context->memory_pool())));
+  RETURN_NOT_OK(else_sel->Invert());
+
+  ExecBatch if_input = input;
+  if_input.selection_vector = std::move(if_sel);
+  ARROW_ASSIGN_OR_RAISE(
+      arguments[1], ExecuteScalarExpression(call.arguments[1], if_input, 
exec_context));
+  ExecBatch else_input = input;
+  else_input.selection_vector = std::move(else_sel);
+  ARROW_ASSIGN_OR_RAISE(
+      arguments[2], ExecuteScalarExpression(call.arguments[2], else_input, 
exec_context));
+
+  // Leveraging if_else kernel with all arguments evaluated.
+  return ExecuteCallNonRecursive(call, input, arguments, exec_context);

Review Comment:
   @felipecrv This part may look quite different than you originally 
imagined/proposed. For example, you might wonder why no "scatter" at all? 
That's because scatter is handled more generically in 
`ExecuteScalarExpression`. And even why is that? First it takes less code. And 
more importantly, I would imagine not only special forms being the producers of 
selection vector, but also acero nodes like `Filter`: a `Filter` node produces 
a selection vector, i.e., late materializing the filtered rows, and all the 
subsequent nodes could keep working on this selection vector without even 
knowing its existence.
   
   On the other hand, the current way loses the opportunity of optimization for 
out-of-order scatter all branches at once. That's true. But we can always 
improve it - all the information needed (kernel, arguments, etc.) is already 
there - it just takes more code.



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

Reply via email to