Awesome06 commented on code in PR #55425:
URL: https://github.com/apache/arrow/pull/55425#discussion_r4560134545


##########
cpp/src/arrow/compute/kernels/scalar_compare.cc:
##########
@@ -380,6 +381,225 @@ struct VarArgsCompareFunction : ScalarFunction {
   }
 };
 
+// Shared equality comparison between a BinaryView array and any 
BaseBinaryScalar.
+// Pins scalar length and 4-byte prefix in registers for early rejection before
+// dereferencing view variadic buffers.
+template <typename Op>
+Status ViewArrayCompareBinaryScalarEqual(KernelContext* ctx, const ArraySpan& 
arg0,
+                                         const Scalar& arg1, ExecResult* out) {
+  constexpr bool kIsEqual = std::is_same_v<Op, Equal>;
+  if (!arg1.is_valid) {
+    return applicator::OutputAdapter<BooleanType>::Write(ctx, 
out->array_span_mutable(),
+                                                         []() -> bool { return 
false; });
+  }
+  const auto* views0 = arg0.GetValues<BinaryViewType::c_type>(1);
+  const auto* buffers0 = arg0.GetVariadicBuffers().data();
+  const auto& scalar = checked_cast<const BaseBinaryScalar&>(arg1);
+  std::string_view s = scalar.view();
+  BinaryViewType::c_type s_val = util::ToBinaryView(s, 0, 0);

Review Comment:
   Great catch. If a `LargeBinary` scalar has `s.size() > INT32_MAX`, passing 
it to `util::ToBinaryView` truncates the 64-bit length to 32-bit, which could 
falsely match a view.
   
   Since a `BinaryView` element can never exceed `INT32_MAX`, any scalar > 2 
GiB can never match any valid row. I've added an early guard at the top of 
`ViewArrayCompareBinaryScalarEqual`:
   
   ```cpp
   if (ARROW_PREDICT_FALSE(s.size() > 
static_cast<size_t>(std::numeric_limits<int32_t>::max()))) {
     return applicator::OutputAdapter<BooleanType>::Write(
         ctx, out->array_span_mutable(), [&]() -> bool { return !kIsEqual; });
   }
   ```
   
   This ensures that for scalars > 2 GiB, `equal` evaluates to `false` and 
`not_equal` evaluates to `true`, preserving exact parity with the previous 
implementation.



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