Skip to content

Commit

Permalink
address feedback comments
Browse files Browse the repository at this point in the history
  • Loading branch information
aocsa committed Nov 21, 2023
1 parent 9d77d88 commit dcea667
Showing 1 changed file with 68 additions and 56 deletions.
124 changes: 68 additions & 56 deletions cpp/tests/ast/transform_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

#include <cstdint>
#include <cudf/ast/expressions.hpp>
#include <cudf/column/column.hpp>
#include <cudf/column/column_view.hpp>
Expand Down Expand Up @@ -344,6 +343,74 @@ TEST_F(TransformTest, ImbalancedTreeArithmeticDeep)
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view(), verbosity);
}

TEST_F(TransformTest, DeeplyNestedArithmeticLogicalExpression)
{
// Test logic for deeply nested arithmetic and logical expressions.
constexpr int64_t kLeftDepthLevel = 100;
constexpr int64_t kRightDepthLevel = 75;

auto generate_ast_expr = [](int64_t depth_level,
cudf::ast::column_reference col_ref,
cudf::ast::ast_operator root_operator,
bool nested_left_tree) {
auto expressions = std::list<cudf::ast::operation>();
std::vector<cudf::ast::ast_operator> arithmetic_operators = {cudf::ast::ast_operator::ADD,
cudf::ast::ast_operator::SUB};

auto op = arithmetic_operators[rand() % arithmetic_operators.size()];
expressions.push_back(cudf::ast::operation(op, col_ref, col_ref));

for (int64_t i = 0; i < depth_level - 1; i++) {
if (i == depth_level - 2) {
op = root_operator;
} else {
op = arithmetic_operators[rand() % arithmetic_operators.size()];
}
if (nested_left_tree) {
expressions.push_back(cudf::ast::operation(op, expressions.back(), col_ref));
} else {
expressions.push_back(cudf::ast::operation(op, col_ref, expressions.back()));
}
}
return expressions;
};

auto c_0 = column_wrapper<int64_t>{0, 0, 0};
auto c_1 = column_wrapper<int32_t>{0, 0, 0};
auto table = cudf::table_view{{c_0, c_1}};

auto expressions = std::list<cudf::ast::operation>();

auto col_ref_0 = cudf::ast::column_reference(0);
auto col_ref_1 = cudf::ast::column_reference(1);

auto left_expression =
generate_ast_expr(kLeftDepthLevel, col_ref_0, cudf::ast::ast_operator::LESS, false);
auto right_expression =
generate_ast_expr(kRightDepthLevel, col_ref_1, cudf::ast::ast_operator::EQUAL, true);

auto expression_tree = cudf::ast::operation(
cudf::ast::ast_operator::LOGICAL_OR, left_expression.back(), right_expression.back());

// Expression:
// OR(<(-(-(+(-($0, $0), $0), $0), $0), $0), ==($1, -($1, +($1, +($1, -($1, $1))))))
// ...
// OR(<($L, $0), ==($1, $R))
// true
//
// Breakdown:
// - Left Operand ($L): (-(-(+(-($0, $0), $0), $0), $0), $0)
// - Right Operand ($R): -($1, +($1, +($1, -($1, $1))))
// Explanation:
// If all $1 values and $R values are zeros, then the result is true because of the equality
// check, combined with the OR operator at OR(<($L, $0), ==($1, $R))

auto result = cudf::compute_column(table, expression_tree);
auto expected = column_wrapper<bool>{true, true, true};

CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view(), verbosity);
}

TEST_F(TransformTest, MultiLevelTreeComparator)
{
auto c_0 = column_wrapper<int32_t>{3, 20, 1, 50};
Expand Down Expand Up @@ -753,59 +820,4 @@ TEST_F(TransformTest, NullLogicalOr)
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view(), verbosity);
}

TEST_F(TransformTest, DeeplyNestedArithmeticLogicalExpression)
{
// Test logic for deeply nested arithmetic and logical expressions.
constexpr int64_t kLeftDepthLevel = 100;
constexpr int64_t kRightDepthLevel = 75;

auto generate_ast_expr = [](int64_t depth_level, cudf::ast::column_reference col_ref, cudf::ast::ast_operator root_operator, bool nested_left_tree){
auto expressions = std::list<cudf::ast::operation>();
std::vector<cudf::ast::ast_operator> arithmetic_operators = {cudf::ast::ast_operator::ADD, cudf::ast::ast_operator::SUB};

auto op = arithmetic_operators[rand() % arithmetic_operators.size()];
expressions.push_back(cudf::ast::operation(op, col_ref, col_ref));

for (int64_t i = 0; i < depth_level - 1; i++) {
if (i == depth_level - 2) {
op = root_operator;
} else {
op = arithmetic_operators[rand() % arithmetic_operators.size()];
}
if (nested_left_tree) {
expressions.push_back(cudf::ast::operation(op, expressions.back(), col_ref));
} else {
expressions.push_back(cudf::ast::operation(op, col_ref, expressions.back()));
}
}
return expressions;
};

auto c_0 = column_wrapper<int64_t>{0, 0, 0};
auto c_1 = column_wrapper<int32_t>{0, 0, 0};
auto table = cudf::table_view{{c_0, c_1}};

auto expressions = std::list<cudf::ast::operation>();

auto col_ref_0 = cudf::ast::column_reference(0);
auto col_ref_1 = cudf::ast::column_reference(1);


auto left_expression = generate_ast_expr(kLeftDepthLevel, col_ref_0, cudf::ast::ast_operator::LESS, false);
auto right_expression = generate_ast_expr(kRightDepthLevel, col_ref_1, cudf::ast::ast_operator::EQUAL, true);

auto expression_tree = cudf::ast::operation(
cudf::ast::ast_operator::LOGICAL_OR, left_expression.back(), right_expression.back());
// expression: OR(<=(-(-(+(-($0, $0), $0), $0), $0), $0), ==($0, -($0, +($0, +($0, -($0, $0))))))
// ...
// OR(<=(0, 0), ==(0, 0))
// true

auto result = cudf::compute_column(table, expression_tree);
auto expected = column_wrapper<bool>{true, true, true};

CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view(), verbosity);
}


CUDF_TEST_PROGRAM_MAIN()

0 comments on commit dcea667

Please sign in to comment.