Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cpp/src/arrow/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ if(ARROW_COMPUTE)
compute/exec/order_by_impl.cc
compute/exec/partition_util.cc
compute/exec/options.cc
compute/exec/parsing.cc
compute/exec/project_node.cc
compute/exec/query_context.cc
compute/exec/sink_node.cc
Expand Down
17 changes: 17 additions & 0 deletions cpp/src/arrow/compute/exec/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <variant>
#include <vector>
Expand Down Expand Up @@ -128,6 +129,22 @@ class ARROW_EXPORT Expression {
explicit Expression(Datum literal);
explicit Expression(Parameter parameter);

/*
Grammar:
Expr -> FieldRef | Literal | Call

FieldRef -> Field | Field FieldRef
Field -> . Name | [ Number ]

Literal -> $ TypeName : Value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing rules for escaping right? I think those need to be part of the grammar.


Call -> Name ( ExprList )
ExprList -> Expr | Expr , ExprList

Name, TypeName, Value, and Number take the obvious values
*/
static Result<Expression> FromString(std::string_view expr);

private:
using Impl = std::variant<Datum, Parameter, Call>;
std::shared_ptr<Impl> impl_;
Expand Down
60 changes: 60 additions & 0 deletions cpp/src/arrow/compute/exec/expression_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <gtest/gtest.h>

#include "arrow/compute/exec/expression_internal.h"
#include "arrow/compute/exec/test_util.h"
#include "arrow/compute/function_internal.h"
#include "arrow/compute/registry.h"
#include "arrow/testing/gtest_util.h"
Expand Down Expand Up @@ -1562,6 +1563,65 @@ TEST(Expression, SerializationRoundTrips) {
equal(field_ref("beta"), literal(3.25f))}));
}

TEST(Expression, ParseBasic) {
const char* expr_str = "add($int32:1, .i32_0)";
ASSERT_OK_AND_ASSIGN(Expression expr, Expression::FromString(expr_str));
ExecBatch batch = ExecBatchFromJSON({int32(), int32()}, "[[1, 2], [1, 2]]");
std::shared_ptr<Schema> sch =
schema({field("i32_0", int32()), field("i32_1", int32())});
ASSERT_OK_AND_ASSIGN(expr, expr.Bind(*sch.get()));
ASSERT_OK_AND_ASSIGN(Datum result, ExecuteScalarExpression(expr, batch));
const int32_t* vals =
reinterpret_cast<const int32_t*>(result.array()->buffers[1]->data());
ASSERT_EQ(result.array()->length, 2);
ASSERT_EQ(vals[0], 2);
ASSERT_EQ(vals[1], 2);
}

TEST(Expression, ParseComplexExpr) {
const char* expr_str = "add(multiply(.m, .x), .b)";
ASSERT_OK_AND_ASSIGN(Expression expr, Expression::FromString(expr_str));
ExecBatch batch =
ExecBatchFromJSON({int32(), int32(), int32()}, "[[3, 1, 1], [1, 1, 0], [3, 3, 1]]");
std::shared_ptr<Schema> sch =
schema({field("m", int32()), field("x", int32()), field("b", int32())});
ASSERT_OK_AND_ASSIGN(expr, expr.Bind(*sch.get()));
ASSERT_OK_AND_ASSIGN(Datum result, ExecuteScalarExpression(expr, batch));
const int32_t* vals =
reinterpret_cast<const int32_t*>(result.array()->buffers[1]->data());
ASSERT_EQ(result.array()->length, 3);
ASSERT_EQ(vals[0], 4);
ASSERT_EQ(vals[1], 1);
ASSERT_EQ(vals[2], 10);
}

TEST(Expression, ParseComplexScalar) {
const char* expr_str = "add($duration(MILLI):10, $duration(MILLI):20)";
ASSERT_OK_AND_ASSIGN(Expression expr, Expression::FromString(expr_str));
std::shared_ptr<Schema> empty_schema = schema({});
ASSERT_OK_AND_ASSIGN(expr, expr.Bind(*empty_schema.get()));
ASSERT_OK_AND_ASSIGN(Datum result, ExecuteScalarExpression(expr, {}));
DurationScalar expected(30, TimeUnit::MILLI);
ASSERT_TRUE(result.scalar()->Equals(expected));
}

TEST(Expression, ParseEscaped) {
const char* expr_str = "$utf8:hello\\, \\(world\\)";
ASSERT_OK_AND_ASSIGN(Expression expr, Expression::FromString(expr_str));
std::shared_ptr<Schema> empty_schema = schema({});
ASSERT_OK_AND_ASSIGN(expr, expr.Bind(*empty_schema.get()));
ASSERT_OK_AND_ASSIGN(Datum result, ExecuteScalarExpression(expr, {}));
StringScalar expected("hello, (world)");
ASSERT_TRUE(result.scalar()->Equals(expected));
}

TEST(Expression, ParseErrorMessage) {
const char* expr_str = "$asdfasdf:horoshaya_kartoshka";
EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid,
testing::HasSubstr("...asdf:horoshaya_karto..."),
Expression::FromString(expr_str));
}

TEST(Projection, AugmentWithNull) {
// NB: input contains *no columns* except i32
auto input = ArrayFromJSON(struct_({kBoringSchema->GetFieldByName("i32")}),
Expand Down
Loading