forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscalar_macro_function.cpp
More file actions
52 lines (42 loc) · 1.75 KB
/
scalar_macro_function.cpp
File metadata and controls
52 lines (42 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//===----------------------------------------------------------------------===//
// DuckDB
//
// duckdb/function/scalar_macro_function.hpp
//
//
//===----------------------------------------------------------------------===//
#include "duckdb/function/scalar_macro_function.hpp"
#include "duckdb/function/macro_function.hpp"
#include "duckdb/parser/expression/constant_expression.hpp"
#include "duckdb/parser/parsed_expression_iterator.hpp"
namespace duckdb {
ScalarMacroFunction::ScalarMacroFunction(unique_ptr<ParsedExpression> expression)
: MacroFunction(MacroType::SCALAR_MACRO), expression(std::move(expression)) {
}
ScalarMacroFunction::ScalarMacroFunction(void) : MacroFunction(MacroType::SCALAR_MACRO) {
}
unique_ptr<MacroFunction> ScalarMacroFunction::Copy() const {
auto result = make_uniq<ScalarMacroFunction>();
result->expression = expression->Copy();
CopyProperties(*result);
return std::move(result);
}
void RemoveQualificationRecursive(unique_ptr<ParsedExpression> &expr) {
if (expr->GetExpressionType() == ExpressionType::COLUMN_REF) {
auto &col_ref = expr->Cast<ColumnRefExpression>();
auto &col_names = col_ref.column_names;
if (col_names.size() == 2 && col_names[0].find(DummyBinding::DUMMY_NAME) != string::npos) {
col_names.erase(col_names.begin());
}
} else {
ParsedExpressionIterator::EnumerateChildren(
*expr, [](unique_ptr<ParsedExpression> &child) { RemoveQualificationRecursive(child); });
}
}
string ScalarMacroFunction::ToSQL() const {
// In case of nested macro's we need to fix it a bit
auto expression_copy = expression->Copy();
RemoveQualificationRecursive(expression_copy);
return MacroFunction::ToSQL() + StringUtil::Format("(%s)", expression_copy->ToString());
}
} // namespace duckdb