Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QST] Recusively Generating AST Expressions (C++ libcudf) #15653

Open
shriramch opened this issue May 4, 2024 · 8 comments
Open

[QST] Recusively Generating AST Expressions (C++ libcudf) #15653

shriramch opened this issue May 4, 2024 · 8 comments
Labels
question Further information is requested

Comments

@shriramch
Copy link

I would like to implement a function that can generate AST expressions, for example, by recursively converting a different expression class, such as an Apache Arrow arrow::compute::expression object, to a cudf::ast::expression object.

Is this even possible? libcudf's AST expression classes only accept references that are owned by the caller function; this is a problem since we can not recurse anymore. Is there any way to do this, to implement functions that can generate AST expressions instead of hardcoding expressions in a caller?

@shriramch shriramch added the question Further information is requested label May 4, 2024
@shriramch shriramch changed the title [QST] Recusively Generating AST Expressions (C++) [QST] Recusively Generating AST Expressions (C++ libcudf) May 4, 2024
@bdice
Copy link
Contributor

bdice commented May 4, 2024

@shriramch You can use an owning object like a std::deque<expression> or std::list<expression> where references are stable (std::vector can reallocate and invalidate references). The lifetime of the AST expressions would then be tied to the lifetime of that container, and you can return it or pass it around.

@bdice
Copy link
Contributor

bdice commented May 4, 2024

For an example of constructing an expression tree, see this benchmark:

// Note that a std::list is required here because of its guarantees against reference invalidation
// when items are added or removed. References to items in a std::vector are not safe if the
// vector must re-allocate.
auto expressions = std::list<cudf::ast::operation>();
// Construct tree that chains additions like (((a + b) + c) + d)
auto const op = cudf::ast::ast_operator::ADD;
if (reuse_columns) {
expressions.push_back(cudf::ast::operation(op, column_refs.at(0), column_refs.at(0)));
for (cudf::size_type i = 0; i < tree_levels - 1; i++) {
expressions.push_back(cudf::ast::operation(op, expressions.back(), column_refs.at(0)));
}
} else {
expressions.push_back(cudf::ast::operation(op, column_refs.at(0), column_refs.at(1)));
std::transform(std::next(column_refs.cbegin(), 2),
column_refs.cend(),
std::back_inserter(expressions),
[&](auto const& column_ref) {
return cudf::ast::operation(op, expressions.back(), column_ref);
});
}
auto const& expression_tree_root = expressions.back();

@vyasr
Copy link
Contributor

vyasr commented May 7, 2024

FWIW we'd also be OK with changing the current model, see #10744. If you would like to make that change, a PR is welcome!

@shriramch
Copy link
Author

@bdice Thank you, this approach worked.

@shriramch
Copy link
Author

@vyasr Sure, I'd like to try this.

@vyasr
Copy link
Contributor

vyasr commented May 15, 2024

@shriramch OK great! Feel free to post here if you need help in getting a development environment set up for cudf!

@shriramch
Copy link
Author

@vyasr Is it possible to get a remote development environment? Unfortunately I can't do this on my current PC.

@vyasr
Copy link
Contributor

vyasr commented May 17, 2024

Would you be able to spin up an EC2 or GCP instance for this? I think getting access on one of the cloud services would be the easiest. Paperspace is another good option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
Status: In Progress
Development

No branches or pull requests

4 participants
@vyasr @bdice @shriramch and others