Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move common code related to expressions to ExpressionUtil
- Loading branch information
Showing
6 changed files
with
115 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//===----------------------------------------------------------------------===// | ||
// DuckDB | ||
// | ||
// duckdb/parser/expression_util.hpp | ||
// | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#pragma once | ||
|
||
#include "duckdb/parser/base_expression.hpp" | ||
|
||
namespace duckdb { | ||
class ParsedExpression; | ||
class Expression; | ||
|
||
class ExpressionUtil { | ||
public: | ||
//! ListEquals: check if a list of two expressions is equal (order is important) | ||
static bool ListEquals(const vector<unique_ptr<ParsedExpression>> &a, const vector<unique_ptr<ParsedExpression>> &b); | ||
static bool ListEquals(const vector<unique_ptr<Expression>> &a, const vector<unique_ptr<Expression>> &b); | ||
//! SetEquals: check if two sets of expressions are equal (order is not important) | ||
static bool SetEquals(const vector<unique_ptr<ParsedExpression>> &a, const vector<unique_ptr<ParsedExpression>> &b); | ||
static bool SetEquals(const vector<unique_ptr<Expression>> &a, const vector<unique_ptr<Expression>> &b); | ||
private: | ||
template<class T> | ||
static bool ExpressionListEquals(const vector<unique_ptr<T>> &a, const vector<unique_ptr<T>> &b); | ||
template<class T> | ||
static bool ExpressionSetEquals(const vector<unique_ptr<T>> &a, const vector<unique_ptr<T>> &b); | ||
|
||
}; | ||
|
||
} // namespace duckdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include "duckdb/parser/expression_util.hpp" | ||
#include "duckdb/planner/expression.hpp" | ||
#include "duckdb/parser/parsed_expression.hpp" | ||
#include "duckdb/parser/expression_map.hpp" | ||
|
||
using namespace duckdb; | ||
using namespace std; | ||
|
||
template<class T> | ||
bool ExpressionUtil::ExpressionListEquals(const vector<unique_ptr<T>> &a, const vector<unique_ptr<T>> &b) { | ||
if (a.size() != b.size()) { | ||
return false; | ||
} | ||
for(index_t i = 0; i < a.size(); i++) { | ||
if (!(*a[i] == *b[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
template<class T> | ||
bool ExpressionUtil::ExpressionSetEquals(const vector<unique_ptr<T>> &a, const vector<unique_ptr<T>> &b) { | ||
if (a.size() != b.size()) { | ||
return false; | ||
} | ||
// we create a map of expression -> count for the left side | ||
// we keep the count because the same expression can occur multiple times (e.g. "1 AND 1" is legal) | ||
// in this case we track the following value: map["Constant(1)"] = 2 | ||
expression_map_t<index_t> map; | ||
for(index_t i = 0; i < a.size(); i++) { | ||
map[a[i].get()]++; | ||
} | ||
// now on the right side we reduce the counts again | ||
// if the conjunctions are identical, all the counts will be 0 after the | ||
for(auto &expr : b) { | ||
auto entry = map.find(expr.get()); | ||
// first we check if we can find the expression in the map at all | ||
if (entry == map.end()) { | ||
return false; | ||
} | ||
// if we found it we check the count; if the count is already 0 we return false | ||
// this happens if e.g. the left side contains "1 AND X", and the right side contains "1 AND 1" | ||
// "1" is contained in the map, however, the right side contains the expression twice | ||
// hence we know the children are not identical in this case because the LHS and RHS have a different count for the Constant(1) expression | ||
if (entry->second == 0) { | ||
return false; | ||
} | ||
entry->second--; | ||
} | ||
return true; | ||
} | ||
|
||
bool ExpressionUtil::ListEquals(const vector<unique_ptr<ParsedExpression>> &a, const vector<unique_ptr<ParsedExpression>> &b) { | ||
return ExpressionListEquals<ParsedExpression>(a, b); | ||
} | ||
|
||
bool ExpressionUtil::ListEquals(const vector<unique_ptr<Expression>> &a, const vector<unique_ptr<Expression>> &b) { | ||
return ExpressionListEquals<Expression>(a, b); | ||
} | ||
|
||
bool ExpressionUtil::SetEquals(const vector<unique_ptr<ParsedExpression>> &a, const vector<unique_ptr<ParsedExpression>> &b) { | ||
return ExpressionSetEquals<ParsedExpression>(a, b); | ||
} | ||
|
||
bool ExpressionUtil::SetEquals(const vector<unique_ptr<Expression>> &a, const vector<unique_ptr<Expression>> &b) { | ||
return ExpressionSetEquals<Expression>(a, b); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters