Skip to content

Commit 7efab4f

Browse files
authored
feat: add predicate leaf functions (#33)
* feat: add predicate leaf functions * fix review for like * fix clang tidy
1 parent 036dd3a commit 7efab4f

32 files changed

Lines changed: 1713 additions & 0 deletions
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "paimon/common/predicate/and.h"
20+
21+
#include "paimon/common/predicate/or.h"
22+
23+
namespace paimon {
24+
25+
const CompoundFunction& And::Negate() const {
26+
return Or::Instance();
27+
}
28+
29+
} // namespace paimon

src/paimon/common/predicate/and.h

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
21+
#include <cstddef>
22+
#include <cstdint>
23+
#include <memory>
24+
#include <string>
25+
#include <utility>
26+
#include <vector>
27+
28+
#include "arrow/array/array_base.h"
29+
#include "fmt/format.h"
30+
#include "paimon/common/predicate/compound_function.h"
31+
#include "paimon/common/predicate/predicate_filter.h"
32+
#include "paimon/predicate/predicate.h"
33+
#include "paimon/result.h"
34+
#include "paimon/status.h"
35+
36+
namespace arrow {
37+
class Schema;
38+
} // namespace arrow
39+
40+
namespace paimon {
41+
class InternalArray;
42+
class InternalRow;
43+
44+
/// A `CompoundFunction` to eval and.
45+
class And : public CompoundFunction {
46+
public:
47+
static const And& Instance() {
48+
static const And instance = And();
49+
return instance;
50+
}
51+
52+
Result<std::vector<char>> Test(
53+
const arrow::Array& array,
54+
const std::vector<std::shared_ptr<Predicate>>& children) const override {
55+
std::vector<char> is_valid(array.length(), true);
56+
for (const auto& child : children) {
57+
auto child_filter = std::dynamic_pointer_cast<PredicateFilter>(child);
58+
if (!child_filter) {
59+
return Status::Invalid(
60+
fmt::format("child filter {} does not support Test", child->ToString()));
61+
}
62+
PAIMON_ASSIGN_OR_RAISE(std::vector<char> child_valid, child_filter->Test(array));
63+
for (size_t i = 0; i < is_valid.size(); i++) {
64+
is_valid[i] = (is_valid[i] & child_valid[i]);
65+
}
66+
}
67+
return is_valid;
68+
}
69+
70+
Result<bool> Test(const std::shared_ptr<arrow::Schema>& schema, const InternalRow& row,
71+
const std::vector<std::shared_ptr<Predicate>>& children) const override {
72+
for (const auto& child : children) {
73+
auto child_filter = std::dynamic_pointer_cast<PredicateFilter>(child);
74+
if (!child_filter) {
75+
return Status::Invalid(
76+
fmt::format("child filter {} does not support Test", child->ToString()));
77+
}
78+
PAIMON_ASSIGN_OR_RAISE(bool is_valid, child_filter->Test(schema, row));
79+
if (!is_valid) {
80+
return false;
81+
}
82+
}
83+
return true;
84+
}
85+
86+
Result<bool> Test(const std::shared_ptr<arrow::Schema>& schema, int64_t row_count,
87+
const InternalRow& min_values, const InternalRow& max_values,
88+
const InternalArray& null_counts,
89+
const std::vector<std::shared_ptr<Predicate>>& children) const override {
90+
for (const auto& child : children) {
91+
auto child_filter = std::dynamic_pointer_cast<PredicateFilter>(child);
92+
if (!child_filter) {
93+
return Status::Invalid(
94+
fmt::format("child filter {} does not support Test", child->ToString()));
95+
}
96+
PAIMON_ASSIGN_OR_RAISE(bool is_valid, child_filter->Test(schema, row_count, min_values,
97+
max_values, null_counts));
98+
if (!is_valid) {
99+
return false;
100+
}
101+
}
102+
return true;
103+
}
104+
105+
Type GetType() const override {
106+
return Type::AND;
107+
}
108+
const CompoundFunction& Negate() const override;
109+
std::string ToString() const override {
110+
return "And";
111+
}
112+
113+
private:
114+
And() = default;
115+
};
116+
} // namespace paimon
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "paimon/common/predicate/contains.h"
20+
21+
namespace paimon {
22+
23+
Result<bool> Contains::TestString(const std::string& field, const std::string& pattern) const {
24+
return field.find(pattern) != std::string::npos;
25+
}
26+
} // namespace paimon
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
21+
#include <string>
22+
23+
#include "paimon/common/predicate/string_leaf_binary_function.h"
24+
#include "paimon/result.h"
25+
26+
namespace paimon {
27+
/// A `StringLeafBinaryFunction` to eval filter like '%abc%'.
28+
class Contains : public StringLeafBinaryFunction {
29+
public:
30+
static const Contains& Instance() {
31+
static const Contains instance = Contains();
32+
return instance;
33+
}
34+
35+
Type GetType() const override {
36+
return Type::CONTAINS;
37+
}
38+
39+
std::string ToString() const override {
40+
return "Contains";
41+
}
42+
43+
Result<bool> TestString(const std::string& field, const std::string& pattern) const override;
44+
45+
private:
46+
Contains() = default;
47+
};
48+
} // namespace paimon
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "paimon/common/predicate/ends_with.h"
20+
21+
#include "paimon/common/utils/string_utils.h"
22+
23+
namespace paimon {
24+
25+
Result<bool> EndsWith::TestString(const std::string& field, const std::string& pattern) const {
26+
return StringUtils::EndsWith(field, pattern);
27+
}
28+
} // namespace paimon
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
21+
#include <string>
22+
23+
#include "paimon/common/predicate/string_leaf_binary_function.h"
24+
#include "paimon/result.h"
25+
26+
namespace paimon {
27+
/// A `StringLeafBinaryFunction` to eval filter like '%abc' or filter like '_abc'.
28+
class EndsWith : public StringLeafBinaryFunction {
29+
public:
30+
static const EndsWith& Instance() {
31+
static const EndsWith instance = EndsWith();
32+
return instance;
33+
}
34+
35+
Type GetType() const override {
36+
return Type::ENDS_WITH;
37+
}
38+
39+
std::string ToString() const override {
40+
return "EndsWith";
41+
}
42+
43+
Result<bool> TestString(const std::string& field, const std::string& pattern) const override;
44+
45+
private:
46+
EndsWith() = default;
47+
};
48+
} // namespace paimon
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "paimon/common/predicate/equal.h"
20+
21+
#include "paimon/common/predicate/not_equal.h"
22+
23+
namespace paimon {
24+
class LeafFunction;
25+
26+
const LeafFunction* Equal::Negate() const {
27+
return &NotEqual::Instance();
28+
}
29+
30+
} // namespace paimon
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
21+
#include <cstdint>
22+
#include <optional>
23+
#include <string>
24+
#include <utility>
25+
26+
#include "paimon/common/predicate/null_false_leaf_binary_function.h"
27+
#include "paimon/predicate/literal.h"
28+
#include "paimon/result.h"
29+
30+
namespace paimon {
31+
class LeafFunction;
32+
33+
/// A `NullFalseLeafBinaryFunction` to eval equal.
34+
class Equal : public NullFalseLeafBinaryFunction {
35+
public:
36+
static const Equal& Instance() {
37+
static const Equal instance = Equal();
38+
return instance;
39+
}
40+
41+
Result<bool> Test(const Literal& field, const Literal& literal) const override {
42+
PAIMON_ASSIGN_OR_RAISE(int32_t compare_res, field.CompareTo(literal));
43+
return compare_res == 0;
44+
}
45+
46+
Result<bool> Test(int64_t row_count, const Literal& min_value, const Literal& max_value,
47+
const std::optional<int64_t>& null_count,
48+
const Literal& literal) const override {
49+
PAIMON_ASSIGN_OR_RAISE(int32_t min_res, literal.CompareTo(min_value));
50+
PAIMON_ASSIGN_OR_RAISE(int32_t max_res, literal.CompareTo(max_value));
51+
return min_res >= 0 && max_res <= 0;
52+
}
53+
54+
Type GetType() const override {
55+
return Type::EQUAL;
56+
}
57+
const LeafFunction* Negate() const override;
58+
std::string ToString() const override {
59+
return "Equal";
60+
}
61+
62+
private:
63+
Equal() = default;
64+
};
65+
66+
} // namespace paimon

0 commit comments

Comments
 (0)