bkietz commented on a change in pull request #7026:
URL: https://github.com/apache/arrow/pull/7026#discussion_r417342047



##########
File path: cpp/src/arrow/dataset/filter.cc
##########
@@ -1261,7 +1264,212 @@ Result<std::shared_ptr<RecordBatch>> 
TreeEvaluator::Filter(
   return batch->Slice(0, 0);
 }
 
-std::shared_ptr<ScalarExpression> scalar(bool value) { return 
scalar(MakeScalar(value)); }
+std::shared_ptr<Expression> scalar(bool value) { return 
scalar(MakeScalar(value)); }
+
+struct SerializeImpl {
+  Result<std::shared_ptr<StructArray>> ToArray(const Expression& expr) const {
+    return VisitExpression(expr, *this);
+  }
+
+  Result<std::shared_ptr<StructArray>> TaggedWithChildren(const Expression& 
expr,
+                                                          ArrayVector 
children) const {
+    children.emplace_back();
+    ARROW_ASSIGN_OR_RAISE(children.back(),
+                          MakeArrayFromScalar(Int32Scalar(expr.type()), 1));
+
+    return StructArray::Make(children, 
std::vector<std::string>(children.size(), ""));
+  }
+
+  Result<std::shared_ptr<StructArray>> operator()(const FieldExpression& expr) 
const {
+    ARROW_ASSIGN_OR_RAISE(auto name, 
MakeArrayFromScalar(StringScalar(expr.name()), 1));
+    return TaggedWithChildren(expr, {name});
+  }
+
+  Result<std::shared_ptr<StructArray>> operator()(const ScalarExpression& 
expr) const {
+    ARROW_ASSIGN_OR_RAISE(auto value, MakeArrayFromScalar(*expr.value(), 1));
+    return TaggedWithChildren(expr, {value});
+  }
+
+  Result<std::shared_ptr<StructArray>> operator()(const UnaryExpression& expr) 
const {
+    ARROW_ASSIGN_OR_RAISE(auto operand, ToArray(*expr.operand()));
+    return TaggedWithChildren(expr, {operand});
+  }
+
+  Result<std::shared_ptr<StructArray>> operator()(const CastExpression& expr) 
const {
+    ARROW_ASSIGN_OR_RAISE(auto operand, ToArray(*expr.operand()));
+
+    std::shared_ptr<Array> is_like_expr, to;
+    if (const auto& to_type = expr.to_type()) {
+      ARROW_ASSIGN_OR_RAISE(is_like_expr, 
MakeArrayFromScalar(BooleanScalar(false), 1));
+      ARROW_ASSIGN_OR_RAISE(to, MakeArrayOfNull(to_type, 1));
+    }
+    if (const auto& like_expr = expr.like_expr()) {
+      ARROW_ASSIGN_OR_RAISE(is_like_expr, 
MakeArrayFromScalar(BooleanScalar(true), 1));
+      ARROW_ASSIGN_OR_RAISE(to, ToArray(*like_expr));
+    }
+
+    return TaggedWithChildren(expr, {operand, is_like_expr, to});
+  }
+
+  Result<std::shared_ptr<StructArray>> operator()(const BinaryExpression& 
expr) const {
+    ARROW_ASSIGN_OR_RAISE(auto left_operand, ToArray(*expr.left_operand()));
+    ARROW_ASSIGN_OR_RAISE(auto right_operand, ToArray(*expr.right_operand()));
+    return TaggedWithChildren(expr, {left_operand, right_operand});
+  }
+
+  Result<std::shared_ptr<StructArray>> operator()(
+      const ComparisonExpression& expr) const {
+    ARROW_ASSIGN_OR_RAISE(auto left_operand, ToArray(*expr.left_operand()));
+    ARROW_ASSIGN_OR_RAISE(auto right_operand, ToArray(*expr.right_operand()));
+    ARROW_ASSIGN_OR_RAISE(auto op, MakeArrayFromScalar(Int32Scalar(expr.op()), 
1));
+    return TaggedWithChildren(expr, {left_operand, right_operand, op});
+  }
+
+  Result<std::shared_ptr<StructArray>> operator()(const InExpression& expr) 
const {
+    ARROW_ASSIGN_OR_RAISE(auto operand, ToArray(*expr.operand()));
+
+    auto set_type = list(expr.set()->type());
+
+    ARROW_ASSIGN_OR_RAISE(auto set_offsets, AllocateBuffer(sizeof(int32_t) * 
2));
+    reinterpret_cast<int32_t*>(set_offsets->mutable_data())[0] = 0;
+    reinterpret_cast<int32_t*>(set_offsets->mutable_data())[1] =
+        static_cast<int32_t>(expr.set()->length());
+
+    auto set_values = expr.set();
+
+    auto set = std::make_shared<ListArray>(std::move(set_type), 1, 
std::move(set_offsets),
+                                           std::move(set_values));
+    return TaggedWithChildren(expr, {operand, set});
+  }
+
+  Result<std::shared_ptr<StructArray>> operator()(const Expression& expr) 
const {
+    return Status::NotImplemented("serialization of ", expr.ToString());
+  }
+};
+
+Result<std::shared_ptr<Buffer>> Expression::Serialize() const {
+  ARROW_ASSIGN_OR_RAISE(auto array, SerializeImpl{}.ToArray(*this));

Review comment:
       will do

##########
File path: cpp/src/arrow/dataset/filter.cc
##########
@@ -1261,7 +1264,212 @@ Result<std::shared_ptr<RecordBatch>> 
TreeEvaluator::Filter(
   return batch->Slice(0, 0);
 }
 
-std::shared_ptr<ScalarExpression> scalar(bool value) { return 
scalar(MakeScalar(value)); }
+std::shared_ptr<Expression> scalar(bool value) { return 
scalar(MakeScalar(value)); }
+
+struct SerializeImpl {

Review comment:
       will do




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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to