This is an automated email from the ASF dual-hosted git repository.
dongjoon pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/orc.git
The following commit(s) were added to refs/heads/main by this push:
new e2476d1ef ORC-1880: [C++] Add invalid argument check for NOT Operator
in ExpressionTree
e2476d1ef is described below
commit e2476d1efa7f2877cd4527e686e101e3860088b6
Author: luffy-zh <[email protected]>
AuthorDate: Tue Apr 22 18:35:11 2025 +0900
ORC-1880: [C++] Add invalid argument check for NOT Operator in
ExpressionTree
### What changes were proposed in this pull request?
Add invalid argument check for NOT Operator in ExpressionTree.cc
### Why are the changes needed?
Operator::NOT expects to have exactly one child, but there is currently no
validation to enforce this requirement. If no child is provided, it may result
in a crash during the construction of the SearchArgument.
### How was this patch tested?
The unit tests in TestSearchArgument.cc can effectively cover this patch.
### Was this patch authored or co-authored using generative AI tooling?
NO
Closes #2196 from luffy-zh/ORC-1880.
Authored-by: luffy-zh <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
---
c++/src/sargs/ExpressionTree.cc | 6 ++++++
c++/test/TestSearchArgument.cc | 9 +++++++++
2 files changed, 15 insertions(+)
diff --git a/c++/src/sargs/ExpressionTree.cc b/c++/src/sargs/ExpressionTree.cc
index e49bca4b7..58dd13817 100644
--- a/c++/src/sargs/ExpressionTree.cc
+++ b/c++/src/sargs/ExpressionTree.cc
@@ -110,6 +110,9 @@ namespace orc {
return result;
}
case Operator::NOT:
+ if (children_.size() != 1) {
+ throw std::invalid_argument("NOT operator must have exactly one
child");
+ }
return !children_.at(0)->evaluate(leaves);
case Operator::LEAF:
return leaves[leaf_];
@@ -159,6 +162,9 @@ namespace orc {
sstream << ')';
break;
case Operator::NOT:
+ if (children_.size() != 1) {
+ throw std::invalid_argument("NOT operator must have exactly one
child");
+ }
sstream << "(not " << children_.at(0)->toString() << ')';
break;
case Operator::LEAF:
diff --git a/c++/test/TestSearchArgument.cc b/c++/test/TestSearchArgument.cc
index bf9b82ea5..e51ee1e8b 100644
--- a/c++/test/TestSearchArgument.cc
+++ b/c++/test/TestSearchArgument.cc
@@ -481,4 +481,13 @@ namespace orc {
std::invalid_argument);
}
+ TEST(TestSearchArgument, testBadTreeNode) {
+ auto invalidNode =
std::make_shared<ExpressionTree>(ExpressionTree::Operator::NOT, NodeList{});
+ EXPECT_THROW(invalidNode->toString(), std::invalid_argument);
+
+ std::vector<TruthValue> leaves;
+ leaves.push_back(TruthValue::YES);
+ EXPECT_THROW(invalidNode->evaluate(leaves), std::invalid_argument);
+ }
+
} // namespace orc