Repository: duckdb/duckdb (language: python)

Issue:
Scoping problem with function argument
### What happens?

Somehow, `z` seems to disappear:
```sql
create or replace function demo(n, z) as table (
  select list_transform(range(0,n), x -> z) as row
);  

from demo(3,0);
```
```
Binder Error: Referenced column "z" not found in FROM clause!
```



### To Reproduce

```sql
create or replace function demo(n, z) as table (
  select list_transform(range(0,n), x -> z) as row
);  

from demo(3,0);
```

### OS:

macos

### DuckDB Version:

v1.0, v1.1.3-dev38 │

### DuckDB Client:

CLI

### Hardware:

_No response_

### Full Name:

Peter Koppstein

### Affiliation:

Princeton University

### What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.

I have tested with a source build

### Did you include all relevant data sets for reproducing the issue?

Not applicable - the reproduction does not require a data set

### Did you include all code required to reproduce the issue?

- [X] Yes, I have

### Did you include all relevant configuration (e.g., CPU architecture, Python version, Linux distribution) to reproduce the issue?

- [X] Yes, I have


Discussion / hints:
If it helps, this seems to work when adding the var within an expression (0+z, x-z, etc)
```sql
create or replace function demo(n, z) as table (
    select list_transform(range(0,n), x -> 0+z) as row
  );
from demo(3,1);
```
```text
┌───────────┐
│    row    │
│  int32[]  │
├───────────┤
│ [1, 1, 1] │
└───────────┘
@lcostantino - Good to know, thanks. I was able to find another workaround too (adding a FROM clause).  Hopefully the problem will be easy for the gurus to fix. 

Fix patch:
diff --git a/src/planner/binder/expression/bind_macro_expression.cpp b/src/planner/binder/expression/bind_macro_expression.cpp
index caa6ba777da4..358dd5db03fc 100644
--- a/src/planner/binder/expression/bind_macro_expression.cpp
+++ b/src/planner/binder/expression/bind_macro_expression.cpp
@@ -1,4 +1,5 @@
 #include "duckdb/catalog/catalog_entry/scalar_macro_catalog_entry.hpp"
+#include "duckdb/common/enums/expression_type.hpp"
 #include "duckdb/common/reference_map.hpp"
 #include "duckdb/common/string_util.hpp"
 #include "duckdb/function/scalar_macro_function.hpp"
@@ -26,12 +27,8 @@ void ExpressionBinder::ReplaceMacroParametersInLambda(FunctionExpression &functi
 
 		if (!error_message.empty()) {
 			// Possibly a JSON function, replace both LHS and RHS.
-			ParsedExpressionIterator::EnumerateChildren(*lambda_expr.lhs, [&](unique_ptr<ParsedExpression> &child) {
-				ReplaceMacroParameters(child, lambda_params);
-			});
-			ParsedExpressionIterator::EnumerateChildren(*lambda_expr.expr, [&](unique_ptr<ParsedExpression> &child) {
-				ReplaceMacroParameters(child, lambda_params);
-			});
+			ReplaceMacroParameters(lambda_expr.lhs, lambda_params);
+			ReplaceMacroParameters(lambda_expr.expr, lambda_params);
 			continue;
 		}
 
@@ -43,9 +40,7 @@ void ExpressionBinder::ReplaceMacroParametersInLambda(FunctionExpression &functi
 		}
 
 		// Only replace in the RHS of the expression.
-		ParsedExpressionIterator::EnumerateChildren(*lambda_expr.expr, [&](unique_ptr<ParsedExpression> &child) {
-			ReplaceMacroParameters(child, lambda_params);
-		});
+		ReplaceMacroParameters(lambda_expr.expr, lambda_params);
 
 		lambda_params.pop_back();
 	}


Test patch:
diff --git a/test/sql/function/list/lambdas/lambdas_and_functions.test b/test/sql/function/list/lambdas/lambdas_and_functions.test
new file mode 100644
index 000000000000..99515e95f426
--- /dev/null
+++ b/test/sql/function/list/lambdas/lambdas_and_functions.test
@@ -0,0 +1,35 @@
+# name: test/sql/function/list/lambdas/lambdas_and_functions.test
+# description: Test mixing lambdas and functions
+# group: [lambdas]
+
+require json
+
+statement ok
+create or replace function demo(n, z) as table (
+  select list_transform(range(0,n), x -> z) as row
+);
+
+query I
+from demo(3,0);
+----
+[0, 0, 0]
+
+statement ok
+create or replace function demo(n, z) as table (
+  select list_transform(range(0,n), x -> 0+z) as row
+);
+
+query I
+from demo(3,0);
+----
+[0, 0, 0]
+
+statement ok
+create or replace function demo(n, z) as table (
+  select list_transform(range(0,n), x -> (z->'a')) as row
+);
+
+query I
+from demo(3, {'a': 2});
+----
+[2, 2, 2]
\ No newline at end of file