Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
323 changes: 213 additions & 110 deletions cpp/src/reader/expression.cc

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions cpp/src/reader/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,21 @@ class QueryExpression {
void destory();

private:
Expression* combine_two_global_time_filter(Expression* left,
Expression* right,
Expression* combine_two_global_time_filter(Expression* parent,
ExpressionType type);
Expression* handle_one_global_time_filter(
Expression* left, Expression* expression,
Expression* parent, Expression* global, Expression* rest,
std::vector<Path>& selected_series, ExpressionType type);
void add_time_filter_to_query_filter(Filter* time_filter,
bool add_time_filter_to_query_filter(Filter* time_filter,
Expression* expression);
void add_time_filter_to_series_filter(Filter* time_filter,
bool add_time_filter_to_series_filter(Filter* time_filter,
Expression* single_series_expr);
Expression* push_global_time_filter_to_all_series(
Expression* time_filter, std::vector<Path>& selected_series);
Filter* time_filter, std::vector<Path>& selected_series);
Expression* merge_second_tree_to_first_tree(Expression* left_expression,
Expression* right_expression);
bool update_filter_with_or(Expression* expression, Filter* filter,
Path& path);
const Path& path);

public:
bool has_filter_;
Expand Down
19 changes: 19 additions & 0 deletions cpp/src/reader/filter/and_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,27 @@ class AndFilter : public BinaryFilter {
}
}
}
for (TimeRange* range : *left_time_ranges) {
delete range;
}
for (TimeRange* range : *right_time_ranges) {
delete range;
}
delete left_time_ranges;
delete right_time_ranges;
return result;
}

Filter* clone() const override {
Filter* left = left_->clone();
Filter* right = right_->clone();
if (left == nullptr || right == nullptr) {
delete left;
delete right;
return nullptr;
}
return new AndFilter(left, right);
}
};

} // namespace storage
Expand Down
7 changes: 6 additions & 1 deletion cpp/src/reader/filter/binary_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ class BinaryFilter : public Filter {
BinaryFilter() : Filter(), left_(nullptr), right_(nullptr) {}
BinaryFilter(Filter* left, Filter* right)
: Filter(), left_(left), right_(right) {}
virtual ~BinaryFilter() {}
// A binary filter owns its operands: callers hand over freshly created
// filters (or filters detached from the expression tree they wrapped).
virtual ~BinaryFilter() {
delete left_;
delete right_;
}

void set_left(Filter* left) { left_ = left; }
void set_right(Filter* right) { right_ = right; }
Expand Down
6 changes: 6 additions & 0 deletions cpp/src/reader/filter/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ class Filter {
return nullptr;
}

// Returns an independent copy owned by the caller, or nullptr when the
// filter cannot be cloned. Callers that must attach the same predicate to
// several owners (e.g. pushing a global time filter into every series
// leaf) treat nullptr as a failure rather than sharing ownership.
virtual Filter* clone() const { return nullptr; }

// True when the predicate accepts every timestamp (e.g. TimeBetween over
// the full int64 range). Callers may then skip per-row evaluation.
virtual bool is_unbounded_time_filter() const { return false; }
Expand Down
24 changes: 24 additions & 0 deletions cpp/src/reader/filter/or_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class OrFilter : public BinaryFilter {
int left_index = 0, right_index = 0;
int left_size = left_time_ranges->size();
int right_size = right_time_ranges->size();
if (left_size == 0 && right_size == 0) {
delete left_time_ranges;
delete right_time_ranges;
return result;
}
TimeRange* range = choose_next_range(
left_time_ranges, right_time_ranges, left_index, right_index);
while (left_index < left_size || right_index < right_size) {
Expand All @@ -94,9 +99,28 @@ class OrFilter : public BinaryFilter {
}
}
result->push_back(new TimeRange(range->start_time_, range->end_time_));
for (TimeRange* child_range : *left_time_ranges) {
delete child_range;
}
for (TimeRange* child_range : *right_time_ranges) {
delete child_range;
}
delete left_time_ranges;
delete right_time_ranges;
return result;
}

Filter* clone() const override {
Filter* left = left_->clone();
Filter* right = right_->clone();
if (left == nullptr || right == nullptr) {
delete left;
delete right;
return nullptr;
}
return new OrFilter(left, right);
}

private:
TimeRange* choose_next_range(std::vector<TimeRange*>* left_time_ranges,
std::vector<TimeRange*>* right_time_ranges,
Expand Down
18 changes: 18 additions & 0 deletions cpp/src/reader/filter/time_operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class TimeBetween : public Filter {

bool is_contiguous_time_filter() const override { return !not_; }

Filter* clone() const override {
return new TimeBetween(value1_, value2_, not_);
}

private:
int64_t value1_;
int64_t value2_;
Expand Down Expand Up @@ -86,6 +90,8 @@ class TimeIn : public Filter {

FilterType get_filter_type() { return type_; }

Filter* clone() const override { return new TimeIn(values_, not_); }

private:
std::vector<int64_t> values_;
FilterType type_;
Expand Down Expand Up @@ -113,6 +119,8 @@ class TimeEq : public Filter {

FilterType get_filter_type() { return type_; }

Filter* clone() const override { return new TimeEq(value_); }

private:
int64_t value_;
FilterType type_;
Expand All @@ -139,6 +147,8 @@ class TimeNotEq : public Filter {

FilterType get_filter_type() { return type_; }

Filter* clone() const override { return new TimeNotEq(value_); }

private:
int64_t value_;
FilterType type_;
Expand All @@ -165,6 +175,8 @@ class TimeGt : public Filter {

FilterType get_filter_type() { return type_; }

Filter* clone() const override { return new TimeGt(value_); }

private:
int64_t value_;
FilterType type_;
Expand Down Expand Up @@ -192,6 +204,8 @@ class TimeGtEq : public Filter {
void reset_value(int64_t val) { value_ = val; }
FilterType get_filter_type() { return type_; }

Filter* clone() const override { return new TimeGtEq(value_); }

private:
int64_t value_;
FilterType type_;
Expand All @@ -218,6 +232,8 @@ class TimeLt : public Filter {

FilterType get_filter_type() { return type_; }

Filter* clone() const override { return new TimeLt(value_); }

private:
int64_t value_;
FilterType type_;
Expand All @@ -244,6 +260,8 @@ class TimeLtEq : public Filter {

FilterType get_filter_type() { return type_; }

Filter* clone() const override { return new TimeLtEq(value_); }

private:
int64_t value_;
FilterType type_;
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/reader/tsfile_executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ int TsFileExecutor::execute(QueryExpression* query_expr, ResultSet*& ret_qds) {
if (query_exprs_->has_filter_) {
regular_expr = query_exprs_->optimize(origin_expr, paths);
if (regular_expr == nullptr) {
// optimize() consumes the input tree, including on failure.
query_exprs_->expression_ = nullptr;
return E_INVALID_ARG;
}
query_exprs_->set_expression(regular_expr);
Expand Down
38 changes: 36 additions & 2 deletions cpp/test/reader/expression_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,52 @@

#include <vector>

#include "reader/filter/time_operator.h"

namespace storage {

namespace {

// A filter without clone(): pushing it onto several owners must fail instead
// of sharing a single instance (and double-freeing it later).
class StatelessFilter : public Filter {
public:
bool satisfy(Statistic*) override { return true; }
bool satisfy(int64_t, int64_t) override { return true; }
};

} // namespace

TEST(QueryExpressionTest, OptimizeFailsWhenAndCannotCloneTimeFilter) {
QueryExpression query_expression;
std::vector<Path> paths{Path("root.sg.d1.s1")};
Expression* expression = new Expression(
AND_EXPR, new Expression(GLOBALTIME_EXPR, new StatelessFilter()),
new Expression(SERIES_EXPR, paths.at(0), new TimeGtEq(0)));

EXPECT_EQ(query_expression.optimize(expression, paths), nullptr);
}

TEST(QueryExpressionTest, OptimizeFailsWhenOrCannotCloneTimeFilter) {
QueryExpression query_expression;
std::vector<Path> paths{Path("root.sg.d1.s1")};
Expression* expression = new Expression(
OR_EXPR, new Expression(GLOBALTIME_EXPR, new StatelessFilter()),
new Expression(SERIES_EXPR, paths.at(0), new TimeGtEq(0)));

EXPECT_EQ(query_expression.optimize(expression, paths), nullptr);
}

TEST(QueryExpressionTest, EmptySelectedSeriesReturnsNull) {
QueryExpression query_expression;
std::vector<Path> selected_series;
// optimize() consumes the input tree, including on failure: the empty
// selected-series list makes the global time filter impossible to push.
Expression* expression = new Expression(
OR_EXPR, new Expression(GLOBALTIME_EXPR, static_cast<Filter*>(nullptr)),
new Expression(SERIES_EXPR, Path(), static_cast<Filter*>(nullptr)));

EXPECT_EQ(query_expression.optimize(expression, selected_series), nullptr);

delete expression;
}

} // namespace storage
Loading
Loading