diff --git a/cpp/tests/ast/transform_tests.cpp b/cpp/tests/ast/transform_tests.cpp index d79590039d0..418c89a7e73 100644 --- a/cpp/tests/ast/transform_tests.cpp +++ b/cpp/tests/ast/transform_tests.cpp @@ -14,7 +14,6 @@ * limitations under the License. */ -#include #include #include #include @@ -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(); + std::vector 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{0, 0, 0}; + auto c_1 = column_wrapper{0, 0, 0}; + auto table = cudf::table_view{{c_0, c_1}}; + + auto expressions = std::list(); + + 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{true, true, true}; + + CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view(), verbosity); +} + TEST_F(TransformTest, MultiLevelTreeComparator) { auto c_0 = column_wrapper{3, 20, 1, 50}; @@ -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(); - std::vector 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{0, 0, 0}; - auto c_1 = column_wrapper{0, 0, 0}; - auto table = cudf::table_view{{c_0, c_1}}; - - auto expressions = std::list(); - - 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{true, true, true}; - - CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view(), verbosity); -} - - CUDF_TEST_PROGRAM_MAIN()