-
Notifications
You must be signed in to change notification settings - Fork 66
feat: add expression visitors #311
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
Changes from all commits
a743525
c8a9a7f
39c4f9c
095d428
b615c68
79efeee
f0bff76
0d26f4a
18fcee6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,95 @@ | ||||||
| /* | ||||||
| * Licensed to the Apache Software Foundation (ASF) under one | ||||||
| * or more contributor license agreements. See the NOTICE file | ||||||
| * distributed with this work for additional information | ||||||
| * regarding copyright ownership. The ASF licenses this file | ||||||
| * to you under the Apache License, Version 2.0 (the | ||||||
| * "License"); you may not use this file except in compliance | ||||||
| * with the License. You may obtain a copy of the License at | ||||||
| * | ||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| * | ||||||
| * Unless required by applicable law or agreed to in writing, | ||||||
| * software distributed under the License is distributed on an | ||||||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||
| * KIND, either express or implied. See the License for the | ||||||
| * specific language governing permissions and limitations | ||||||
| * under the License. | ||||||
| */ | ||||||
|
|
||||||
| #include "iceberg/expression/binder.h" | ||||||
|
|
||||||
| namespace iceberg { | ||||||
|
|
||||||
| Binder::Binder(const Schema& schema, bool case_sensitive) | ||||||
| : schema_(schema), case_sensitive_(case_sensitive) {} | ||||||
|
|
||||||
| Result<std::shared_ptr<Expression>> Binder::Bind(const Schema& schema, | ||||||
| const std::shared_ptr<Expression>& expr, | ||||||
| bool case_sensitive) { | ||||||
| Binder binder(schema, case_sensitive); | ||||||
| return Visit<std::shared_ptr<Expression>, Binder>(expr, binder); | ||||||
| } | ||||||
|
|
||||||
| Result<std::shared_ptr<Expression>> Binder::AlwaysTrue() { return True::Instance(); } | ||||||
|
|
||||||
| Result<std::shared_ptr<Expression>> Binder::AlwaysFalse() { return False::Instance(); } | ||||||
|
|
||||||
| Result<std::shared_ptr<Expression>> Binder::Not( | ||||||
| const std::shared_ptr<Expression>& child_result) { | ||||||
| return iceberg::Not::MakeFolded(child_result); | ||||||
| } | ||||||
|
|
||||||
| Result<std::shared_ptr<Expression>> Binder::And( | ||||||
| const std::shared_ptr<Expression>& left_result, | ||||||
| const std::shared_ptr<Expression>& right_result) { | ||||||
| return iceberg::And::MakeFolded(left_result, right_result); | ||||||
| } | ||||||
|
|
||||||
| Result<std::shared_ptr<Expression>> Binder::Or( | ||||||
| const std::shared_ptr<Expression>& left_result, | ||||||
| const std::shared_ptr<Expression>& right_result) { | ||||||
| return iceberg::Or::MakeFolded(left_result, right_result); | ||||||
| } | ||||||
|
|
||||||
| Result<std::shared_ptr<Expression>> Binder::Predicate( | ||||||
| const std::shared_ptr<UnboundPredicate>& pred) { | ||||||
| ICEBERG_DCHECK(pred != nullptr, "Predicate cannot be null"); | ||||||
| return pred->Bind(schema_, case_sensitive_); | ||||||
| } | ||||||
|
|
||||||
| Result<std::shared_ptr<Expression>> Binder::Predicate( | ||||||
| const std::shared_ptr<BoundPredicate>& pred) { | ||||||
| ICEBERG_DCHECK(pred != nullptr, "Predicate cannot be null"); | ||||||
| return InvalidExpression("Found already bound predicate: {}", pred->ToString()); | ||||||
| } | ||||||
|
|
||||||
| Result<bool> IsBoundVisitor::IsBound(const std::shared_ptr<Expression>& expr) { | ||||||
| ICEBERG_DCHECK(expr != nullptr, "Expression cannot be null"); | ||||||
| IsBoundVisitor visitor; | ||||||
| return Visit<bool, IsBoundVisitor>(expr, visitor); | ||||||
| } | ||||||
|
|
||||||
| Result<bool> IsBoundVisitor::AlwaysTrue() { return true; } | ||||||
|
|
||||||
| Result<bool> IsBoundVisitor::AlwaysFalse() { return true; } | ||||||
|
|
||||||
| Result<bool> IsBoundVisitor::Not(bool child_result) { return child_result; } | ||||||
|
|
||||||
| Result<bool> IsBoundVisitor::And(bool left_result, bool right_result) { | ||||||
| return left_result && right_result; | ||||||
| } | ||||||
|
|
||||||
| Result<bool> IsBoundVisitor::Or(bool left_result, bool right_result) { | ||||||
| return left_result && right_result; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is correct because this is not a logical operation. We can only return true if both sides are bound expressions. See java impl for reference: https://github.com/apache/iceberg/blob/main/api/src/main/java/org/apache/iceberg/expressions/Binder.java#L208-L245 |
||||||
| } | ||||||
|
|
||||||
| Result<bool> IsBoundVisitor::Predicate(const std::shared_ptr<BoundPredicate>& pred) { | ||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| Result<bool> IsBoundVisitor::Predicate(const std::shared_ptr<UnboundPredicate>& pred) { | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| } // namespace iceberg | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| /// \file iceberg/expression/binder.h | ||
| /// Bind an expression to a schema. | ||
|
|
||
| #include "iceberg/expression/expression_visitor.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| class ICEBERG_EXPORT Binder : public ExpressionVisitor<std::shared_ptr<Expression>> { | ||
| public: | ||
| Binder(const Schema& schema, bool case_sensitive); | ||
|
|
||
| static Result<std::shared_ptr<Expression>> Bind(const Schema& schema, | ||
| const std::shared_ptr<Expression>& expr, | ||
| bool case_sensitive); | ||
|
|
||
| Result<std::shared_ptr<Expression>> AlwaysTrue() override; | ||
| Result<std::shared_ptr<Expression>> AlwaysFalse() override; | ||
| Result<std::shared_ptr<Expression>> Not( | ||
| const std::shared_ptr<Expression>& child_result) override; | ||
| Result<std::shared_ptr<Expression>> And( | ||
| const std::shared_ptr<Expression>& left_result, | ||
| const std::shared_ptr<Expression>& right_result) override; | ||
| Result<std::shared_ptr<Expression>> Or( | ||
| const std::shared_ptr<Expression>& left_result, | ||
| const std::shared_ptr<Expression>& right_result) override; | ||
| Result<std::shared_ptr<Expression>> Predicate( | ||
| const std::shared_ptr<BoundPredicate>& pred) override; | ||
| Result<std::shared_ptr<Expression>> Predicate( | ||
| const std::shared_ptr<UnboundPredicate>& pred) override; | ||
|
|
||
| private: | ||
| const Schema& schema_; | ||
| const bool case_sensitive_; | ||
| }; | ||
|
|
||
| class ICEBERG_EXPORT IsBoundVisitor : public ExpressionVisitor<bool> { | ||
| public: | ||
| static Result<bool> IsBound(const std::shared_ptr<Expression>& expr); | ||
|
|
||
| Result<bool> AlwaysTrue() override; | ||
| Result<bool> AlwaysFalse() override; | ||
| Result<bool> Not(bool child_result) override; | ||
| Result<bool> And(bool left_result, bool right_result) override; | ||
| Result<bool> Or(bool left_result, bool right_result) override; | ||
| Result<bool> Predicate(const std::shared_ptr<BoundPredicate>& pred) override; | ||
| Result<bool> Predicate(const std::shared_ptr<UnboundPredicate>& pred) override; | ||
| }; | ||
|
|
||
| // TODO(gangwu): add the Java parity `ReferenceVisitor` | ||
|
|
||
| } // namespace iceberg |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does additional commentary need to be added here? If an already bound expression is encountered, choose to report an error rather than directly using it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean that we need to add a comment on why an error is returned here?