Skip to content

Commit

Permalink
reference set instead of unordered set
Browse files Browse the repository at this point in the history
  • Loading branch information
lnkuiper committed Jul 14, 2023
1 parent 64b5fc7 commit 73fc69c
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/planner/binder/expression/bind_macro_expression.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "duckdb/catalog/catalog_entry/scalar_macro_catalog_entry.hpp"
#include "duckdb/common/reference_map.hpp"
#include "duckdb/common/string_util.hpp"
#include "duckdb/function/scalar_macro_function.hpp"
#include "duckdb/parser/expression/function_expression.hpp"
Expand Down Expand Up @@ -44,15 +45,15 @@ void ExpressionBinder::ReplaceMacroParametersRecursive(unique_ptr<ParsedExpressi
}

static void DetectInfiniteMacroRecursion(ClientContext &context, unique_ptr<ParsedExpression> &expr,
unordered_set<CatalogEntry *> &expanded_macros) {
reference_set_t<CatalogEntry> &expanded_macros) {
optional_ptr<ScalarMacroCatalogEntry> recursive_macro;
switch (expr->GetExpressionClass()) {
case ExpressionClass::FUNCTION: {
auto &func = expr->Cast<FunctionExpression>();
auto function = Catalog::GetEntry(context, CatalogType::SCALAR_FUNCTION_ENTRY, func.catalog, func.schema,
func.function_name, OnEntryNotFound::RETURN_NULL);
if (function && function->type == CatalogType::MACRO_ENTRY) {
if (expanded_macros.find(function.get()) != expanded_macros.end()) {
if (expanded_macros.find(*function) != expanded_macros.end()) {
throw BinderException("Infinite recursion detected in macro \"%s\"", func.function_name);
} else {
recursive_macro = &function->Cast<ScalarMacroCatalogEntry>();
Expand All @@ -75,11 +76,11 @@ static void DetectInfiniteMacroRecursion(ClientContext &context, unique_ptr<Pars
if (recursive_macro) {
auto &macro_def = recursive_macro->function->Cast<ScalarMacroFunction>();
auto rec_expr = macro_def.expression->Copy();
expanded_macros.insert(recursive_macro.get());
expanded_macros.insert(*recursive_macro);
ParsedExpressionIterator::EnumerateChildren(*rec_expr, [&](unique_ptr<ParsedExpression> &child) {
DetectInfiniteMacroRecursion(context, child, expanded_macros);
});
expanded_macros.erase(recursive_macro.get());
expanded_macros.erase(*recursive_macro);
} else {
ParsedExpressionIterator::EnumerateChildren(*expr, [&](unique_ptr<ParsedExpression> &child) {
DetectInfiniteMacroRecursion(context, child, expanded_macros);
Expand Down Expand Up @@ -126,8 +127,8 @@ BindResult ExpressionBinder::BindMacro(FunctionExpression &function, ScalarMacro
expr = macro_def.expression->Copy();

// detect infinite recursion
unordered_set<CatalogEntry *> expanded_macros;
expanded_macros.insert(&macro_func);
reference_set_t<CatalogEntry> expanded_macros;
expanded_macros.insert(macro_func);
DetectInfiniteMacroRecursion(context, expr, expanded_macros);

// now replace the parameters
Expand Down

0 comments on commit 73fc69c

Please sign in to comment.