duckdb 0.3.1-dev306.0 → 0.3.1-dev332.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/duckdb.cpp CHANGED
@@ -1028,10 +1028,12 @@ public:
1028
1028
  static void ResolveParameterType(LogicalType &type);
1029
1029
  static void ResolveParameterType(unique_ptr<Expression> &expr);
1030
1030
 
1031
- protected:
1031
+ //! Bind the given expresion. Unlike Bind(), this does *not* mute the given ParsedExpression.
1032
+ //! Exposed to be used from sub-binders that aren't subclasses of ExpressionBinder.
1032
1033
  virtual BindResult BindExpression(unique_ptr<ParsedExpression> *expr_ptr, idx_t depth,
1033
1034
  bool root_expression = false);
1034
1035
 
1036
+ protected:
1035
1037
  BindResult BindExpression(BetweenExpression &expr, idx_t depth);
1036
1038
  BindResult BindExpression(CaseExpression &expr, idx_t depth);
1037
1039
  BindResult BindExpression(CollateExpression &expr, idx_t depth);
@@ -1331,6 +1333,7 @@ public:
1331
1333
 
1332
1334
  namespace duckdb {
1333
1335
  class BoundResultModifier;
1336
+ class BoundSelectNode;
1334
1337
  class ClientContext;
1335
1338
  class ExpressionBinder;
1336
1339
  class LimitModifier;
@@ -1544,7 +1547,6 @@ private:
1544
1547
 
1545
1548
  BoundStatement BindSummarize(ShowStatement &stmt);
1546
1549
  unique_ptr<BoundResultModifier> BindLimit(LimitModifier &limit_mod);
1547
- unique_ptr<Expression> BindFilter(unique_ptr<ParsedExpression> condition);
1548
1550
  unique_ptr<Expression> BindOrderExpression(OrderBinder &order_binder, unique_ptr<ParsedExpression> expr);
1549
1551
 
1550
1552
  unique_ptr<LogicalOperator> PlanFilter(unique_ptr<Expression> condition, unique_ptr<LogicalOperator> root);
@@ -24400,6 +24402,9 @@ static bool IntegerCastLoop(const char *buf, idx_t len, T &result, bool strict)
24400
24402
  return false;
24401
24403
  }
24402
24404
  pos++;
24405
+ if (pos >= len) {
24406
+ return false;
24407
+ }
24403
24408
  int64_t exponent = 0;
24404
24409
  int negative = buf[pos] == '-';
24405
24410
  if (negative) {
@@ -58187,10 +58192,8 @@ SinkFinalizeType PhysicalHashJoin::Finalize(Pipeline &pipeline, Event &event, Cl
58187
58192
  D_ASSERT(sink.hash_table->equality_types.size() == 1);
58188
58193
  auto key_type = sink.hash_table->equality_types[0];
58189
58194
  use_perfect_hash = sink.perfect_join_executor->BuildPerfectHashTable(key_type);
58190
- } else {
58191
- sink.perfect_join_executor.reset();
58192
58195
  }
58193
- // In case large build side or duplicates, use regular hash join
58196
+ // In case of a large build side or duplicates, use regular hash join
58194
58197
  if (!use_perfect_hash) {
58195
58198
  sink.perfect_join_executor.reset();
58196
58199
  sink.hash_table->Finalize();
@@ -66462,7 +66465,7 @@ void CheckForPerfectJoinOpt(LogicalComparisonJoin &op, PerfectHashJoinStats &joi
66462
66465
  return;
66463
66466
  }
66464
66467
  }
66465
- // with integral types
66468
+ // with integral internal types
66466
66469
  for (auto &&join_stat : op.join_stats) {
66467
66470
  if (!TypeIsInteger(join_stat->type.InternalType()) || join_stat->type.InternalType() == PhysicalType::INT128) {
66468
66471
  // perfect join not possible for non-integral types or hugeint
@@ -66487,7 +66490,7 @@ void CheckForPerfectJoinOpt(LogicalComparisonJoin &op, PerfectHashJoinStats &joi
66487
66490
  join_state.build_min = stats_build->min;
66488
66491
  join_state.build_max = stats_build->max;
66489
66492
  join_state.estimated_cardinality = op.estimated_cardinality;
66490
- if (build_range.type().id() == LogicalTypeId::DECIMAL) {
66493
+ if (!build_range.type().IsIntegral()) {
66491
66494
  switch (build_range.type().InternalType()) {
66492
66495
  case PhysicalType::INT16:
66493
66496
  join_state.build_range = build_range.value_.smallint;
@@ -66499,19 +66502,15 @@ void CheckForPerfectJoinOpt(LogicalComparisonJoin &op, PerfectHashJoinStats &joi
66499
66502
  join_state.build_range = build_range.value_.bigint;
66500
66503
  break;
66501
66504
  case PhysicalType::INT128:
66502
- throw InternalException("PhysicalType::INT128 not yet implemented for Perfect HJ");
66503
- // join_state.build_range = build_range.GetValue<idx_t>();
66504
- // break;
66505
+ // we do not support hugeint for this optimization
66506
+ return;
66505
66507
  default:
66506
66508
  throw InternalException("Invalid Physical Type for Decimals");
66507
66509
  }
66508
66510
  } else {
66509
66511
  join_state.build_range = build_range.GetValue<idx_t>(); // cast integer types into idx_t
66510
66512
  }
66511
- if (join_state.build_range > MAX_BUILD_SIZE) {
66512
- return;
66513
- }
66514
- if (stats_probe->max.is_null || stats_probe->min.is_null) {
66513
+ if (join_state.build_range > MAX_BUILD_SIZE || stats_probe->max.is_null || stats_probe->min.is_null) {
66515
66514
  return;
66516
66515
  }
66517
66516
  if (stats_build->min <= stats_probe->min && stats_probe->max <= stats_build->max) {
@@ -127199,45 +127198,44 @@ private:
127199
127198
  // Expression Transform
127200
127199
  //===--------------------------------------------------------------------===//
127201
127200
  //! Transform a Postgres boolean expression into an Expression
127202
- unique_ptr<ParsedExpression> TransformBoolExpr(duckdb_libpgquery::PGBoolExpr *root, idx_t depth);
127201
+ unique_ptr<ParsedExpression> TransformBoolExpr(duckdb_libpgquery::PGBoolExpr *root);
127203
127202
  //! Transform a Postgres case expression into an Expression
127204
- unique_ptr<ParsedExpression> TransformCase(duckdb_libpgquery::PGCaseExpr *root, idx_t depth);
127203
+ unique_ptr<ParsedExpression> TransformCase(duckdb_libpgquery::PGCaseExpr *root);
127205
127204
  //! Transform a Postgres type cast into an Expression
127206
- unique_ptr<ParsedExpression> TransformTypeCast(duckdb_libpgquery::PGTypeCast *root, idx_t depth);
127205
+ unique_ptr<ParsedExpression> TransformTypeCast(duckdb_libpgquery::PGTypeCast *root);
127207
127206
  //! Transform a Postgres coalesce into an Expression
127208
- unique_ptr<ParsedExpression> TransformCoalesce(duckdb_libpgquery::PGAExpr *root, idx_t depth);
127207
+ unique_ptr<ParsedExpression> TransformCoalesce(duckdb_libpgquery::PGAExpr *root);
127209
127208
  //! Transform a Postgres column reference into an Expression
127210
- unique_ptr<ParsedExpression> TransformColumnRef(duckdb_libpgquery::PGColumnRef *root, idx_t depth);
127209
+ unique_ptr<ParsedExpression> TransformColumnRef(duckdb_libpgquery::PGColumnRef *root);
127211
127210
  //! Transform a Postgres constant value into an Expression
127212
- unique_ptr<ConstantExpression> TransformValue(duckdb_libpgquery::PGValue val, idx_t depth);
127211
+ unique_ptr<ConstantExpression> TransformValue(duckdb_libpgquery::PGValue val);
127213
127212
  //! Transform a Postgres operator into an Expression
127214
- unique_ptr<ParsedExpression> TransformAExpr(duckdb_libpgquery::PGAExpr *root, idx_t depth);
127213
+ unique_ptr<ParsedExpression> TransformAExpr(duckdb_libpgquery::PGAExpr *root);
127215
127214
  //! Transform a Postgres abstract expression into an Expression
127216
- unique_ptr<ParsedExpression> TransformExpression(duckdb_libpgquery::PGNode *node, idx_t depth);
127215
+ unique_ptr<ParsedExpression> TransformExpression(duckdb_libpgquery::PGNode *node);
127217
127216
  //! Transform a Postgres function call into an Expression
127218
- unique_ptr<ParsedExpression> TransformFuncCall(duckdb_libpgquery::PGFuncCall *root, idx_t depth);
127217
+ unique_ptr<ParsedExpression> TransformFuncCall(duckdb_libpgquery::PGFuncCall *root);
127219
127218
  //! Transform a Postgres boolean expression into an Expression
127220
- unique_ptr<ParsedExpression> TransformInterval(duckdb_libpgquery::PGIntervalConstant *root, idx_t depth);
127219
+ unique_ptr<ParsedExpression> TransformInterval(duckdb_libpgquery::PGIntervalConstant *root);
127221
127220
  //! Transform a Postgres lambda node [e.g. (x, y) -> x + y] into a lambda expression
127222
- unique_ptr<ParsedExpression> TransformLambda(duckdb_libpgquery::PGLambdaFunction *node, idx_t depth);
127221
+ unique_ptr<ParsedExpression> TransformLambda(duckdb_libpgquery::PGLambdaFunction *node);
127223
127222
  //! Transform a Postgres array access node (e.g. x[1] or x[1:3])
127224
- unique_ptr<ParsedExpression> TransformArrayAccess(duckdb_libpgquery::PGAIndirection *node, idx_t depth);
127223
+ unique_ptr<ParsedExpression> TransformArrayAccess(duckdb_libpgquery::PGAIndirection *node);
127225
127224
  //! Transform a positional reference (e.g. #1)
127226
- unique_ptr<ParsedExpression> TransformPositionalReference(duckdb_libpgquery::PGPositionalReference *node,
127227
- idx_t depth);
127228
- unique_ptr<ParsedExpression> TransformStarExpression(duckdb_libpgquery::PGNode *node, idx_t depth);
127225
+ unique_ptr<ParsedExpression> TransformPositionalReference(duckdb_libpgquery::PGPositionalReference *node);
127226
+ unique_ptr<ParsedExpression> TransformStarExpression(duckdb_libpgquery::PGNode *node);
127229
127227
 
127230
127228
  //! Transform a Postgres constant value into an Expression
127231
- unique_ptr<ParsedExpression> TransformConstant(duckdb_libpgquery::PGAConst *c, idx_t depth);
127232
- unique_ptr<ParsedExpression> TransformGroupingFunction(duckdb_libpgquery::PGGroupingFunc *n, idx_t depth);
127233
- unique_ptr<ParsedExpression> TransformResTarget(duckdb_libpgquery::PGResTarget *root, idx_t depth);
127234
- unique_ptr<ParsedExpression> TransformNullTest(duckdb_libpgquery::PGNullTest *root, idx_t depth);
127235
- unique_ptr<ParsedExpression> TransformParamRef(duckdb_libpgquery::PGParamRef *node, idx_t depth);
127236
- unique_ptr<ParsedExpression> TransformNamedArg(duckdb_libpgquery::PGNamedArgExpr *root, idx_t depth);
127229
+ unique_ptr<ParsedExpression> TransformConstant(duckdb_libpgquery::PGAConst *c);
127230
+ unique_ptr<ParsedExpression> TransformGroupingFunction(duckdb_libpgquery::PGGroupingFunc *n);
127231
+ unique_ptr<ParsedExpression> TransformResTarget(duckdb_libpgquery::PGResTarget *root);
127232
+ unique_ptr<ParsedExpression> TransformNullTest(duckdb_libpgquery::PGNullTest *root);
127233
+ unique_ptr<ParsedExpression> TransformParamRef(duckdb_libpgquery::PGParamRef *node);
127234
+ unique_ptr<ParsedExpression> TransformNamedArg(duckdb_libpgquery::PGNamedArgExpr *root);
127237
127235
 
127238
- unique_ptr<ParsedExpression> TransformSQLValueFunction(duckdb_libpgquery::PGSQLValueFunction *node, idx_t depth);
127236
+ unique_ptr<ParsedExpression> TransformSQLValueFunction(duckdb_libpgquery::PGSQLValueFunction *node);
127239
127237
 
127240
- unique_ptr<ParsedExpression> TransformSubquery(duckdb_libpgquery::PGSubLink *root, idx_t depth);
127238
+ unique_ptr<ParsedExpression> TransformSubquery(duckdb_libpgquery::PGSubLink *root);
127241
127239
  //===--------------------------------------------------------------------===//
127242
127240
  // Constraints transform
127243
127241
  //===--------------------------------------------------------------------===//
@@ -127249,7 +127247,7 @@ private:
127249
127247
  //===--------------------------------------------------------------------===//
127250
127248
  // Collation transform
127251
127249
  //===--------------------------------------------------------------------===//
127252
- unique_ptr<ParsedExpression> TransformCollateExpr(duckdb_libpgquery::PGCollateClause *collate, idx_t depth);
127250
+ unique_ptr<ParsedExpression> TransformCollateExpr(duckdb_libpgquery::PGCollateClause *collate);
127253
127251
 
127254
127252
  string TransformCollation(duckdb_libpgquery::PGCollateClause *collate);
127255
127253
 
@@ -127304,15 +127302,24 @@ private:
127304
127302
  bool TransformOrderBy(duckdb_libpgquery::PGList *order, vector<OrderByNode> &result);
127305
127303
 
127306
127304
  //! Transform a Postgres SELECT clause into a list of Expressions
127307
- void TransformExpressionList(duckdb_libpgquery::PGList &list, vector<unique_ptr<ParsedExpression>> &result,
127308
- idx_t depth);
127305
+ void TransformExpressionList(duckdb_libpgquery::PGList &list, vector<unique_ptr<ParsedExpression>> &result);
127309
127306
 
127310
127307
  //! Transform a Postgres PARTITION BY/ORDER BY specification into lists of expressions
127311
- void TransformWindowDef(duckdb_libpgquery::PGWindowDef *window_spec, WindowExpression *expr, idx_t depth);
127308
+ void TransformWindowDef(duckdb_libpgquery::PGWindowDef *window_spec, WindowExpression *expr);
127312
127309
  //! Transform a Postgres window frame specification into frame expressions
127313
- void TransformWindowFrame(duckdb_libpgquery::PGWindowDef *window_spec, WindowExpression *expr, idx_t depth);
127310
+ void TransformWindowFrame(duckdb_libpgquery::PGWindowDef *window_spec, WindowExpression *expr);
127314
127311
 
127315
127312
  unique_ptr<SampleOptions> TransformSampleOptions(duckdb_libpgquery::PGNode *options);
127313
+
127314
+ private:
127315
+ //! Default stack limit of the transformer
127316
+ constexpr static idx_t MAX_STACK_SIZE = 1024 * 100;
127317
+ //! For iterative functions that create recursive objects, this is our default stack size per element
127318
+ constexpr static idx_t DEFAULT_ENTRY_STACK_SIZE = 100;
127319
+ int *root = nullptr;
127320
+
127321
+ void InitializeStackCheck(int *stack_check_var);
127322
+ void StackCheck(idx_t extra_stack = 0);
127316
127323
  };
127317
127324
 
127318
127325
  } // namespace duckdb
@@ -129340,11 +129347,11 @@ unique_ptr<Constraint> Transformer::TransformConstraint(duckdb_libpgquery::PGLis
129340
129347
  return make_unique<UniqueConstraint>(columns, is_primary_key);
129341
129348
  }
129342
129349
  case duckdb_libpgquery::PG_CONSTR_CHECK: {
129343
- auto expression = TransformExpression(constraint->raw_expr, 0);
129350
+ auto expression = TransformExpression(constraint->raw_expr);
129344
129351
  if (expression->HasSubquery()) {
129345
129352
  throw ParserException("subqueries prohibited in CHECK constraints");
129346
129353
  }
129347
- return make_unique<CheckConstraint>(TransformExpression(constraint->raw_expr, 0));
129354
+ return make_unique<CheckConstraint>(TransformExpression(constraint->raw_expr));
129348
129355
  }
129349
129356
  default:
129350
129357
  throw NotImplementedException("Constraint type not handled yet!");
@@ -129367,7 +129374,7 @@ unique_ptr<Constraint> Transformer::TransformConstraint(duckdb_libpgquery::PGLis
129367
129374
  case duckdb_libpgquery::PG_CONSTR_NULL:
129368
129375
  return nullptr;
129369
129376
  case duckdb_libpgquery::PG_CONSTR_DEFAULT:
129370
- column.default_value = TransformExpression(constraint->raw_expr, 0);
129377
+ column.default_value = TransformExpression(constraint->raw_expr);
129371
129378
  return nullptr;
129372
129379
  case duckdb_libpgquery::PG_CONSTR_FOREIGN:
129373
129380
  default:
@@ -129384,15 +129391,15 @@ unique_ptr<Constraint> Transformer::TransformConstraint(duckdb_libpgquery::PGLis
129384
129391
 
129385
129392
  namespace duckdb {
129386
129393
 
129387
- unique_ptr<ParsedExpression> Transformer::TransformArrayAccess(duckdb_libpgquery::PGAIndirection *indirection_node,
129388
- idx_t depth) {
129394
+ unique_ptr<ParsedExpression> Transformer::TransformArrayAccess(duckdb_libpgquery::PGAIndirection *indirection_node) {
129389
129395
  // transform the source expression
129390
129396
  unique_ptr<ParsedExpression> result;
129391
- result = TransformExpression(indirection_node->arg, depth + 1);
129397
+ result = TransformExpression(indirection_node->arg);
129392
129398
 
129393
129399
  // now go over the indices
129394
129400
  // note that a single indirection node can contain multiple indices
129395
129401
  // this happens for e.g. more complex accesses (e.g. (foo).field1[42])
129402
+ idx_t list_size = 0;
129396
129403
  for (auto node = indirection_node->indirection->head; node != nullptr; node = node->next) {
129397
129404
  auto target = reinterpret_cast<duckdb_libpgquery::PGNode *>(node->data.ptr_value);
129398
129405
  D_ASSERT(target);
@@ -129406,15 +129413,15 @@ unique_ptr<ParsedExpression> Transformer::TransformArrayAccess(duckdb_libpgquery
129406
129413
  if (index->is_slice) {
129407
129414
  // slice
129408
129415
  children.push_back(!index->lidx ? make_unique<ConstantExpression>(Value())
129409
- : TransformExpression(index->lidx, depth + 1));
129416
+ : TransformExpression(index->lidx));
129410
129417
  children.push_back(!index->uidx ? make_unique<ConstantExpression>(Value())
129411
- : TransformExpression(index->uidx, depth + 1));
129418
+ : TransformExpression(index->uidx));
129412
129419
  result = make_unique<OperatorExpression>(ExpressionType::ARRAY_SLICE, move(children));
129413
129420
  } else {
129414
129421
  // array access
129415
129422
  D_ASSERT(!index->lidx);
129416
129423
  D_ASSERT(index->uidx);
129417
- children.push_back(TransformExpression(index->uidx, depth + 1));
129424
+ children.push_back(TransformExpression(index->uidx));
129418
129425
  result = make_unique<OperatorExpression>(ExpressionType::ARRAY_EXTRACT, move(children));
129419
129426
  }
129420
129427
  break;
@@ -129423,13 +129430,15 @@ unique_ptr<ParsedExpression> Transformer::TransformArrayAccess(duckdb_libpgquery
129423
129430
  auto val = (duckdb_libpgquery::PGValue *)target;
129424
129431
  vector<unique_ptr<ParsedExpression>> children;
129425
129432
  children.push_back(move(result));
129426
- children.push_back(TransformValue(*val, depth + 1));
129433
+ children.push_back(TransformValue(*val));
129427
129434
  result = make_unique<OperatorExpression>(ExpressionType::STRUCT_EXTRACT, move(children));
129428
129435
  break;
129429
129436
  }
129430
129437
  default:
129431
129438
  throw NotImplementedException("Unimplemented subscript type");
129432
129439
  }
129440
+ list_size++;
129441
+ StackCheck(list_size * DEFAULT_ENTRY_STACK_SIZE);
129433
129442
  }
129434
129443
  return result;
129435
129444
  }
@@ -129441,10 +129450,10 @@ unique_ptr<ParsedExpression> Transformer::TransformArrayAccess(duckdb_libpgquery
129441
129450
 
129442
129451
  namespace duckdb {
129443
129452
 
129444
- unique_ptr<ParsedExpression> Transformer::TransformBoolExpr(duckdb_libpgquery::PGBoolExpr *root, idx_t depth) {
129453
+ unique_ptr<ParsedExpression> Transformer::TransformBoolExpr(duckdb_libpgquery::PGBoolExpr *root) {
129445
129454
  unique_ptr<ParsedExpression> result;
129446
129455
  for (auto node = root->args->head; node != nullptr; node = node->next) {
129447
- auto next = TransformExpression(reinterpret_cast<duckdb_libpgquery::PGNode *>(node->data.ptr_value), depth + 1);
129456
+ auto next = TransformExpression(reinterpret_cast<duckdb_libpgquery::PGNode *>(node->data.ptr_value));
129448
129457
 
129449
129458
  switch (root->boolop) {
129450
129459
  case duckdb_libpgquery::PG_AND_EXPR: {
@@ -129492,7 +129501,7 @@ unique_ptr<ParsedExpression> Transformer::TransformBoolExpr(duckdb_libpgquery::P
129492
129501
 
129493
129502
  namespace duckdb {
129494
129503
 
129495
- unique_ptr<ParsedExpression> Transformer::TransformCase(duckdb_libpgquery::PGCaseExpr *root, idx_t depth) {
129504
+ unique_ptr<ParsedExpression> Transformer::TransformCase(duckdb_libpgquery::PGCaseExpr *root) {
129496
129505
  D_ASSERT(root);
129497
129506
 
129498
129507
  auto case_node = make_unique<CaseExpression>();
@@ -129500,22 +129509,21 @@ unique_ptr<ParsedExpression> Transformer::TransformCase(duckdb_libpgquery::PGCas
129500
129509
  CaseCheck case_check;
129501
129510
 
129502
129511
  auto w = reinterpret_cast<duckdb_libpgquery::PGCaseWhen *>(cell->data.ptr_value);
129503
- auto test_raw = TransformExpression(reinterpret_cast<duckdb_libpgquery::PGNode *>(w->expr), depth + 1);
129512
+ auto test_raw = TransformExpression(reinterpret_cast<duckdb_libpgquery::PGNode *>(w->expr));
129504
129513
  unique_ptr<ParsedExpression> test;
129505
- auto arg = TransformExpression(reinterpret_cast<duckdb_libpgquery::PGNode *>(root->arg), depth + 1);
129514
+ auto arg = TransformExpression(reinterpret_cast<duckdb_libpgquery::PGNode *>(root->arg));
129506
129515
  if (arg) {
129507
129516
  case_check.when_expr =
129508
129517
  make_unique<ComparisonExpression>(ExpressionType::COMPARE_EQUAL, move(arg), move(test_raw));
129509
129518
  } else {
129510
129519
  case_check.when_expr = move(test_raw);
129511
129520
  }
129512
- case_check.then_expr = TransformExpression(reinterpret_cast<duckdb_libpgquery::PGNode *>(w->result), depth + 1);
129521
+ case_check.then_expr = TransformExpression(reinterpret_cast<duckdb_libpgquery::PGNode *>(w->result));
129513
129522
  case_node->case_checks.push_back(move(case_check));
129514
129523
  }
129515
129524
 
129516
129525
  if (root->defresult) {
129517
- case_node->else_expr =
129518
- TransformExpression(reinterpret_cast<duckdb_libpgquery::PGNode *>(root->defresult), depth + 1);
129526
+ case_node->else_expr = TransformExpression(reinterpret_cast<duckdb_libpgquery::PGNode *>(root->defresult));
129519
129527
  } else {
129520
129528
  case_node->else_expr = make_unique<ConstantExpression>(Value(LogicalType::SQLNULL));
129521
129529
  }
@@ -129531,7 +129539,7 @@ unique_ptr<ParsedExpression> Transformer::TransformCase(duckdb_libpgquery::PGCas
129531
129539
 
129532
129540
  namespace duckdb {
129533
129541
 
129534
- unique_ptr<ParsedExpression> Transformer::TransformTypeCast(duckdb_libpgquery::PGTypeCast *root, idx_t depth) {
129542
+ unique_ptr<ParsedExpression> Transformer::TransformTypeCast(duckdb_libpgquery::PGTypeCast *root) {
129535
129543
  D_ASSERT(root);
129536
129544
 
129537
129545
  // get the type to cast to
@@ -129546,7 +129554,7 @@ unique_ptr<ParsedExpression> Transformer::TransformTypeCast(duckdb_libpgquery::P
129546
129554
  }
129547
129555
  }
129548
129556
  // transform the expression node
129549
- auto expression = TransformExpression(root->arg, depth + 1);
129557
+ auto expression = TransformExpression(root->arg);
129550
129558
  bool try_cast = root->tryCast;
129551
129559
 
129552
129560
  // now create a cast operation
@@ -129561,7 +129569,7 @@ namespace duckdb {
129561
129569
 
129562
129570
  // COALESCE(a,b,c) returns the first argument that is NOT NULL, so
129563
129571
  // rewrite into CASE(a IS NOT NULL, a, CASE(b IS NOT NULL, b, c))
129564
- unique_ptr<ParsedExpression> Transformer::TransformCoalesce(duckdb_libpgquery::PGAExpr *root, idx_t depth) {
129572
+ unique_ptr<ParsedExpression> Transformer::TransformCoalesce(duckdb_libpgquery::PGAExpr *root) {
129565
129573
  D_ASSERT(root);
129566
129574
 
129567
129575
  auto coalesce_args = reinterpret_cast<duckdb_libpgquery::PGList *>(root->lexpr);
@@ -129570,8 +129578,7 @@ unique_ptr<ParsedExpression> Transformer::TransformCoalesce(duckdb_libpgquery::P
129570
129578
  auto coalesce_op = make_unique<OperatorExpression>(ExpressionType::OPERATOR_COALESCE);
129571
129579
  for (auto cell = coalesce_args->head; cell; cell = cell->next) {
129572
129580
  // get the value of the COALESCE
129573
- auto value_expr =
129574
- TransformExpression(reinterpret_cast<duckdb_libpgquery::PGNode *>(cell->data.ptr_value), depth + 1);
129581
+ auto value_expr = TransformExpression(reinterpret_cast<duckdb_libpgquery::PGNode *>(cell->data.ptr_value));
129575
129582
  coalesce_op->children.push_back(move(value_expr));
129576
129583
  }
129577
129584
  return move(coalesce_op);
@@ -129585,7 +129592,7 @@ unique_ptr<ParsedExpression> Transformer::TransformCoalesce(duckdb_libpgquery::P
129585
129592
 
129586
129593
  namespace duckdb {
129587
129594
 
129588
- unique_ptr<ParsedExpression> Transformer::TransformStarExpression(duckdb_libpgquery::PGNode *node, idx_t depth) {
129595
+ unique_ptr<ParsedExpression> Transformer::TransformStarExpression(duckdb_libpgquery::PGNode *node) {
129589
129596
  auto star = (duckdb_libpgquery::PGAStar *)node;
129590
129597
  auto result = make_unique<StarExpression>(star->relation ? star->relation : string());
129591
129598
  if (star->except_list) {
@@ -129603,8 +129610,7 @@ unique_ptr<ParsedExpression> Transformer::TransformStarExpression(duckdb_libpgqu
129603
129610
  for (auto head = star->replace_list->head; head; head = head->next) {
129604
129611
  auto list = (duckdb_libpgquery::PGList *)head->data.ptr_value;
129605
129612
  D_ASSERT(list->length == 2);
129606
- auto replace_expression =
129607
- TransformExpression((duckdb_libpgquery::PGNode *)list->head->data.ptr_value, depth + 1);
129613
+ auto replace_expression = TransformExpression((duckdb_libpgquery::PGNode *)list->head->data.ptr_value);
129608
129614
  auto value = (duckdb_libpgquery::PGValue *)list->tail->data.ptr_value;
129609
129615
  D_ASSERT(value->type == duckdb_libpgquery::T_PGString);
129610
129616
  string exclude_entry = value->val.str;
@@ -129620,7 +129626,7 @@ unique_ptr<ParsedExpression> Transformer::TransformStarExpression(duckdb_libpgqu
129620
129626
  return move(result);
129621
129627
  }
129622
129628
 
129623
- unique_ptr<ParsedExpression> Transformer::TransformColumnRef(duckdb_libpgquery::PGColumnRef *root, idx_t depth) {
129629
+ unique_ptr<ParsedExpression> Transformer::TransformColumnRef(duckdb_libpgquery::PGColumnRef *root) {
129624
129630
  auto fields = root->fields;
129625
129631
  auto head_node = (duckdb_libpgquery::PGNode *)fields->head->data.ptr_value;
129626
129632
  switch (head_node->type) {
@@ -129652,7 +129658,7 @@ unique_ptr<ParsedExpression> Transformer::TransformColumnRef(duckdb_libpgquery::
129652
129658
  }
129653
129659
  }
129654
129660
  case duckdb_libpgquery::T_PGAStar: {
129655
- return TransformStarExpression(head_node, depth);
129661
+ return TransformStarExpression(head_node);
129656
129662
  }
129657
129663
  default:
129658
129664
  throw NotImplementedException("ColumnRef not implemented!");
@@ -129668,7 +129674,7 @@ unique_ptr<ParsedExpression> Transformer::TransformColumnRef(duckdb_libpgquery::
129668
129674
 
129669
129675
  namespace duckdb {
129670
129676
 
129671
- unique_ptr<ConstantExpression> Transformer::TransformValue(duckdb_libpgquery::PGValue val, idx_t depth) {
129677
+ unique_ptr<ConstantExpression> Transformer::TransformValue(duckdb_libpgquery::PGValue val) {
129672
129678
  switch (val.type) {
129673
129679
  case duckdb_libpgquery::T_PGInteger:
129674
129680
  D_ASSERT(val.val.ival <= NumericLimits<int32_t>::Maximum());
@@ -129735,8 +129741,8 @@ unique_ptr<ConstantExpression> Transformer::TransformValue(duckdb_libpgquery::PG
129735
129741
  }
129736
129742
  }
129737
129743
 
129738
- unique_ptr<ParsedExpression> Transformer::TransformConstant(duckdb_libpgquery::PGAConst *c, idx_t depth) {
129739
- return TransformValue(c->val, depth + 1);
129744
+ unique_ptr<ParsedExpression> Transformer::TransformConstant(duckdb_libpgquery::PGAConst *c) {
129745
+ return TransformValue(c->val);
129740
129746
  }
129741
129747
 
129742
129748
  } // namespace duckdb
@@ -129746,10 +129752,10 @@ unique_ptr<ParsedExpression> Transformer::TransformConstant(duckdb_libpgquery::P
129746
129752
 
129747
129753
  namespace duckdb {
129748
129754
 
129749
- unique_ptr<ParsedExpression> Transformer::TransformResTarget(duckdb_libpgquery::PGResTarget *root, idx_t depth) {
129755
+ unique_ptr<ParsedExpression> Transformer::TransformResTarget(duckdb_libpgquery::PGResTarget *root) {
129750
129756
  D_ASSERT(root);
129751
129757
 
129752
- auto expr = TransformExpression(root->val, depth + 1);
129758
+ auto expr = TransformExpression(root->val);
129753
129759
  if (!expr) {
129754
129760
  return nullptr;
129755
129761
  }
@@ -129759,80 +129765,78 @@ unique_ptr<ParsedExpression> Transformer::TransformResTarget(duckdb_libpgquery::
129759
129765
  return expr;
129760
129766
  }
129761
129767
 
129762
- unique_ptr<ParsedExpression> Transformer::TransformNamedArg(duckdb_libpgquery::PGNamedArgExpr *root, idx_t depth) {
129768
+ unique_ptr<ParsedExpression> Transformer::TransformNamedArg(duckdb_libpgquery::PGNamedArgExpr *root) {
129763
129769
  D_ASSERT(root);
129764
129770
 
129765
- auto expr = TransformExpression((duckdb_libpgquery::PGNode *)root->arg, depth + 1);
129771
+ auto expr = TransformExpression((duckdb_libpgquery::PGNode *)root->arg);
129766
129772
  if (root->name) {
129767
129773
  expr->alias = string(root->name);
129768
129774
  }
129769
129775
  return expr;
129770
129776
  }
129771
129777
 
129772
- unique_ptr<ParsedExpression> Transformer::TransformExpression(duckdb_libpgquery::PGNode *node, idx_t depth) {
129778
+ unique_ptr<ParsedExpression> Transformer::TransformExpression(duckdb_libpgquery::PGNode *node) {
129773
129779
  if (!node) {
129774
129780
  return nullptr;
129775
129781
  }
129776
129782
 
129777
- if (depth > max_expression_depth) {
129778
- throw ParserException("Expression tree is too deep (maximum depth %d)", max_expression_depth);
129779
- }
129783
+ StackCheck();
129780
129784
 
129781
129785
  switch (node->type) {
129782
129786
  case duckdb_libpgquery::T_PGColumnRef:
129783
- return TransformColumnRef(reinterpret_cast<duckdb_libpgquery::PGColumnRef *>(node), depth);
129787
+ return TransformColumnRef(reinterpret_cast<duckdb_libpgquery::PGColumnRef *>(node));
129784
129788
  case duckdb_libpgquery::T_PGAConst:
129785
- return TransformConstant(reinterpret_cast<duckdb_libpgquery::PGAConst *>(node), depth);
129789
+ return TransformConstant(reinterpret_cast<duckdb_libpgquery::PGAConst *>(node));
129786
129790
  case duckdb_libpgquery::T_PGAExpr:
129787
- return TransformAExpr(reinterpret_cast<duckdb_libpgquery::PGAExpr *>(node), depth);
129791
+ return TransformAExpr(reinterpret_cast<duckdb_libpgquery::PGAExpr *>(node));
129788
129792
  case duckdb_libpgquery::T_PGFuncCall:
129789
- return TransformFuncCall(reinterpret_cast<duckdb_libpgquery::PGFuncCall *>(node), depth);
129793
+ return TransformFuncCall(reinterpret_cast<duckdb_libpgquery::PGFuncCall *>(node));
129790
129794
  case duckdb_libpgquery::T_PGBoolExpr:
129791
- return TransformBoolExpr(reinterpret_cast<duckdb_libpgquery::PGBoolExpr *>(node), depth);
129795
+ return TransformBoolExpr(reinterpret_cast<duckdb_libpgquery::PGBoolExpr *>(node));
129792
129796
  case duckdb_libpgquery::T_PGTypeCast:
129793
- return TransformTypeCast(reinterpret_cast<duckdb_libpgquery::PGTypeCast *>(node), depth);
129797
+ return TransformTypeCast(reinterpret_cast<duckdb_libpgquery::PGTypeCast *>(node));
129794
129798
  case duckdb_libpgquery::T_PGCaseExpr:
129795
- return TransformCase(reinterpret_cast<duckdb_libpgquery::PGCaseExpr *>(node), depth);
129799
+ return TransformCase(reinterpret_cast<duckdb_libpgquery::PGCaseExpr *>(node));
129796
129800
  case duckdb_libpgquery::T_PGSubLink:
129797
- return TransformSubquery(reinterpret_cast<duckdb_libpgquery::PGSubLink *>(node), depth);
129801
+ return TransformSubquery(reinterpret_cast<duckdb_libpgquery::PGSubLink *>(node));
129798
129802
  case duckdb_libpgquery::T_PGCoalesceExpr:
129799
- return TransformCoalesce(reinterpret_cast<duckdb_libpgquery::PGAExpr *>(node), depth);
129803
+ return TransformCoalesce(reinterpret_cast<duckdb_libpgquery::PGAExpr *>(node));
129800
129804
  case duckdb_libpgquery::T_PGNullTest:
129801
- return TransformNullTest(reinterpret_cast<duckdb_libpgquery::PGNullTest *>(node), depth);
129805
+ return TransformNullTest(reinterpret_cast<duckdb_libpgquery::PGNullTest *>(node));
129802
129806
  case duckdb_libpgquery::T_PGResTarget:
129803
- return TransformResTarget(reinterpret_cast<duckdb_libpgquery::PGResTarget *>(node), depth);
129807
+ return TransformResTarget(reinterpret_cast<duckdb_libpgquery::PGResTarget *>(node));
129804
129808
  case duckdb_libpgquery::T_PGParamRef:
129805
- return TransformParamRef(reinterpret_cast<duckdb_libpgquery::PGParamRef *>(node), depth);
129809
+ return TransformParamRef(reinterpret_cast<duckdb_libpgquery::PGParamRef *>(node));
129806
129810
  case duckdb_libpgquery::T_PGNamedArgExpr:
129807
- return TransformNamedArg(reinterpret_cast<duckdb_libpgquery::PGNamedArgExpr *>(node), depth);
129811
+ return TransformNamedArg(reinterpret_cast<duckdb_libpgquery::PGNamedArgExpr *>(node));
129808
129812
  case duckdb_libpgquery::T_PGSQLValueFunction:
129809
- return TransformSQLValueFunction(reinterpret_cast<duckdb_libpgquery::PGSQLValueFunction *>(node), depth);
129813
+ return TransformSQLValueFunction(reinterpret_cast<duckdb_libpgquery::PGSQLValueFunction *>(node));
129810
129814
  case duckdb_libpgquery::T_PGSetToDefault:
129811
129815
  return make_unique<DefaultExpression>();
129812
129816
  case duckdb_libpgquery::T_PGCollateClause:
129813
- return TransformCollateExpr(reinterpret_cast<duckdb_libpgquery::PGCollateClause *>(node), depth);
129817
+ return TransformCollateExpr(reinterpret_cast<duckdb_libpgquery::PGCollateClause *>(node));
129814
129818
  case duckdb_libpgquery::T_PGIntervalConstant:
129815
- return TransformInterval(reinterpret_cast<duckdb_libpgquery::PGIntervalConstant *>(node), depth);
129819
+ return TransformInterval(reinterpret_cast<duckdb_libpgquery::PGIntervalConstant *>(node));
129816
129820
  case duckdb_libpgquery::T_PGLambdaFunction:
129817
- return TransformLambda(reinterpret_cast<duckdb_libpgquery::PGLambdaFunction *>(node), depth);
129821
+ return TransformLambda(reinterpret_cast<duckdb_libpgquery::PGLambdaFunction *>(node));
129818
129822
  case duckdb_libpgquery::T_PGAIndirection:
129819
- return TransformArrayAccess(reinterpret_cast<duckdb_libpgquery::PGAIndirection *>(node), depth);
129823
+ return TransformArrayAccess(reinterpret_cast<duckdb_libpgquery::PGAIndirection *>(node));
129820
129824
  case duckdb_libpgquery::T_PGPositionalReference:
129821
- return TransformPositionalReference(reinterpret_cast<duckdb_libpgquery::PGPositionalReference *>(node), depth);
129825
+ return TransformPositionalReference(reinterpret_cast<duckdb_libpgquery::PGPositionalReference *>(node));
129822
129826
  case duckdb_libpgquery::T_PGGroupingFunc:
129823
- return TransformGroupingFunction(reinterpret_cast<duckdb_libpgquery::PGGroupingFunc *>(node), depth);
129827
+ return TransformGroupingFunction(reinterpret_cast<duckdb_libpgquery::PGGroupingFunc *>(node));
129824
129828
  default:
129825
129829
  throw NotImplementedException("Expr of type %d not implemented\n", (int)node->type);
129826
129830
  }
129827
129831
  }
129828
129832
 
129829
- void Transformer::TransformExpressionList(duckdb_libpgquery::PGList &list, vector<unique_ptr<ParsedExpression>> &result,
129830
- idx_t depth) {
129833
+ void Transformer::TransformExpressionList(duckdb_libpgquery::PGList &list,
129834
+ vector<unique_ptr<ParsedExpression>> &result) {
129831
129835
  for (auto node = list.head; node != nullptr; node = node->next) {
129832
129836
  auto target = reinterpret_cast<duckdb_libpgquery::PGNode *>(node->data.ptr_value);
129833
129837
  D_ASSERT(target);
129834
129838
 
129835
- auto expr = TransformExpression(target, depth + 1);
129839
+ auto expr = TransformExpression(target);
129836
129840
  D_ASSERT(expr);
129837
129841
 
129838
129842
  result.push_back(move(expr));
@@ -129881,25 +129885,24 @@ static ExpressionType WindowToExpressionType(string &fun_name) {
129881
129885
  return ExpressionType::WINDOW_AGGREGATE;
129882
129886
  }
129883
129887
 
129884
- void Transformer::TransformWindowDef(duckdb_libpgquery::PGWindowDef *window_spec, WindowExpression *expr, idx_t depth) {
129888
+ void Transformer::TransformWindowDef(duckdb_libpgquery::PGWindowDef *window_spec, WindowExpression *expr) {
129885
129889
  D_ASSERT(window_spec);
129886
129890
  D_ASSERT(expr);
129887
129891
 
129888
129892
  // next: partitioning/ordering expressions
129889
129893
  if (window_spec->partitionClause) {
129890
- TransformExpressionList(*window_spec->partitionClause, expr->partitions, depth);
129894
+ TransformExpressionList(*window_spec->partitionClause, expr->partitions);
129891
129895
  }
129892
129896
  TransformOrderBy(window_spec->orderClause, expr->orders);
129893
129897
  }
129894
129898
 
129895
- void Transformer::TransformWindowFrame(duckdb_libpgquery::PGWindowDef *window_spec, WindowExpression *expr,
129896
- idx_t depth) {
129899
+ void Transformer::TransformWindowFrame(duckdb_libpgquery::PGWindowDef *window_spec, WindowExpression *expr) {
129897
129900
  D_ASSERT(window_spec);
129898
129901
  D_ASSERT(expr);
129899
129902
 
129900
129903
  // finally: specifics of bounds
129901
- expr->start_expr = TransformExpression(window_spec->startOffset, depth + 1);
129902
- expr->end_expr = TransformExpression(window_spec->endOffset, depth + 1);
129904
+ expr->start_expr = TransformExpression(window_spec->startOffset);
129905
+ expr->end_expr = TransformExpression(window_spec->endOffset);
129903
129906
 
129904
129907
  if ((window_spec->frameOptions & FRAMEOPTION_END_UNBOUNDED_PRECEDING) ||
129905
129908
  (window_spec->frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)) {
@@ -129937,7 +129940,7 @@ void Transformer::TransformWindowFrame(duckdb_libpgquery::PGWindowDef *window_sp
129937
129940
  }
129938
129941
  }
129939
129942
 
129940
- unique_ptr<ParsedExpression> Transformer::TransformFuncCall(duckdb_libpgquery::PGFuncCall *root, idx_t depth) {
129943
+ unique_ptr<ParsedExpression> Transformer::TransformFuncCall(duckdb_libpgquery::PGFuncCall *root) {
129941
129944
  auto name = root->funcname;
129942
129945
  string schema, function_name;
129943
129946
  if (name->length == 2) {
@@ -129971,7 +129974,7 @@ unique_ptr<ParsedExpression> Transformer::TransformFuncCall(duckdb_libpgquery::P
129971
129974
 
129972
129975
  if (root->args) {
129973
129976
  vector<unique_ptr<ParsedExpression>> function_list;
129974
- TransformExpressionList(*root->args, function_list, depth);
129977
+ TransformExpressionList(*root->args, function_list);
129975
129978
 
129976
129979
  if (win_fun_type == ExpressionType::WINDOW_AGGREGATE) {
129977
129980
  for (auto &child : function_list) {
@@ -130023,8 +130026,8 @@ unique_ptr<ParsedExpression> Transformer::TransformFuncCall(duckdb_libpgquery::P
130023
130026
  window_ref = it->second;
130024
130027
  D_ASSERT(window_ref);
130025
130028
  }
130026
- TransformWindowDef(window_ref, expr.get(), depth);
130027
- TransformWindowFrame(window_spec, expr.get(), depth);
130029
+ TransformWindowDef(window_ref, expr.get());
130030
+ TransformWindowFrame(window_spec, expr.get());
130028
130031
  expr->query_location = root->location;
130029
130032
  return move(expr);
130030
130033
  }
@@ -130033,13 +130036,13 @@ unique_ptr<ParsedExpression> Transformer::TransformFuncCall(duckdb_libpgquery::P
130033
130036
  vector<unique_ptr<ParsedExpression>> children;
130034
130037
  if (root->args != nullptr) {
130035
130038
  for (auto node = root->args->head; node != nullptr; node = node->next) {
130036
- auto child_expr = TransformExpression((duckdb_libpgquery::PGNode *)node->data.ptr_value, depth + 1);
130039
+ auto child_expr = TransformExpression((duckdb_libpgquery::PGNode *)node->data.ptr_value);
130037
130040
  children.push_back(move(child_expr));
130038
130041
  }
130039
130042
  }
130040
130043
  unique_ptr<ParsedExpression> filter_expr;
130041
130044
  if (root->agg_filter) {
130042
- filter_expr = TransformExpression(root->agg_filter, depth + 1);
130045
+ filter_expr = TransformExpression(root->agg_filter);
130043
130046
  }
130044
130047
 
130045
130048
  auto order_bys = make_unique<OrderModifier>();
@@ -130148,8 +130151,7 @@ static string SQLValueOpToString(duckdb_libpgquery::PGSQLValueFunctionOp op) {
130148
130151
  }
130149
130152
  }
130150
130153
 
130151
- unique_ptr<ParsedExpression> Transformer::TransformSQLValueFunction(duckdb_libpgquery::PGSQLValueFunction *node,
130152
- idx_t depth) {
130154
+ unique_ptr<ParsedExpression> Transformer::TransformSQLValueFunction(duckdb_libpgquery::PGSQLValueFunction *node) {
130153
130155
  D_ASSERT(node);
130154
130156
  vector<unique_ptr<ParsedExpression>> children;
130155
130157
  auto fname = SQLValueOpToString(node->op);
@@ -130162,11 +130164,11 @@ unique_ptr<ParsedExpression> Transformer::TransformSQLValueFunction(duckdb_libpg
130162
130164
 
130163
130165
  namespace duckdb {
130164
130166
 
130165
- unique_ptr<ParsedExpression> Transformer::TransformGroupingFunction(duckdb_libpgquery::PGGroupingFunc *n, idx_t depth) {
130167
+ unique_ptr<ParsedExpression> Transformer::TransformGroupingFunction(duckdb_libpgquery::PGGroupingFunc *n) {
130166
130168
  auto op = make_unique<OperatorExpression>(ExpressionType::GROUPING_FUNCTION);
130167
130169
  for (auto node = n->args->head; node; node = node->next) {
130168
130170
  auto n = (duckdb_libpgquery::PGNode *)node->data.ptr_value;
130169
- op->children.push_back(TransformExpression(n, depth + 1));
130171
+ op->children.push_back(TransformExpression(n));
130170
130172
  }
130171
130173
  op->query_location = n->location;
130172
130174
  return move(op);
@@ -130181,7 +130183,7 @@ unique_ptr<ParsedExpression> Transformer::TransformGroupingFunction(duckdb_libpg
130181
130183
 
130182
130184
  namespace duckdb {
130183
130185
 
130184
- unique_ptr<ParsedExpression> Transformer::TransformInterval(duckdb_libpgquery::PGIntervalConstant *node, idx_t depth) {
130186
+ unique_ptr<ParsedExpression> Transformer::TransformInterval(duckdb_libpgquery::PGIntervalConstant *node) {
130185
130187
  // handle post-fix notation of INTERVAL
130186
130188
 
130187
130189
  // three scenarios
@@ -130191,7 +130193,7 @@ unique_ptr<ParsedExpression> Transformer::TransformInterval(duckdb_libpgquery::P
130191
130193
  unique_ptr<ParsedExpression> expr;
130192
130194
  switch (node->val_type) {
130193
130195
  case duckdb_libpgquery::T_PGAExpr:
130194
- expr = TransformExpression(node->eval, depth + 1);
130196
+ expr = TransformExpression(node->eval);
130195
130197
  break;
130196
130198
  case duckdb_libpgquery::T_PGString:
130197
130199
  expr = make_unique<ConstantExpression>(Value(node->sval));
@@ -130298,9 +130300,9 @@ unique_ptr<ParsedExpression> Transformer::TransformInterval(duckdb_libpgquery::P
130298
130300
 
130299
130301
  namespace duckdb {
130300
130302
 
130301
- unique_ptr<ParsedExpression> Transformer::TransformNullTest(duckdb_libpgquery::PGNullTest *root, idx_t depth) {
130303
+ unique_ptr<ParsedExpression> Transformer::TransformNullTest(duckdb_libpgquery::PGNullTest *root) {
130302
130304
  D_ASSERT(root);
130303
- auto arg = TransformExpression(reinterpret_cast<duckdb_libpgquery::PGNode *>(root->arg), depth + 1);
130305
+ auto arg = TransformExpression(reinterpret_cast<duckdb_libpgquery::PGNode *>(root->arg));
130304
130306
  if (root->argisrow) {
130305
130307
  throw NotImplementedException("IS NULL argisrow");
130306
130308
  }
@@ -130330,16 +130332,16 @@ static string ExtractColumnFromLambda(ParsedExpression &expr) {
130330
130332
  return colref.column_name;
130331
130333
  }
130332
130334
 
130333
- unique_ptr<ParsedExpression> Transformer::TransformLambda(duckdb_libpgquery::PGLambdaFunction *node, idx_t depth) {
130335
+ unique_ptr<ParsedExpression> Transformer::TransformLambda(duckdb_libpgquery::PGLambdaFunction *node) {
130334
130336
  vector<unique_ptr<ParsedExpression>> parameter_expressions;
130335
- TransformExpressionList(*node->parameters, parameter_expressions, depth + 1);
130337
+ TransformExpressionList(*node->parameters, parameter_expressions);
130336
130338
  vector<string> parameters;
130337
130339
  parameters.reserve(parameter_expressions.size());
130338
130340
  for (auto &expr : parameter_expressions) {
130339
130341
  parameters.push_back(ExtractColumnFromLambda(*expr));
130340
130342
  }
130341
130343
 
130342
- auto lambda_function = TransformExpression(node->function, depth + 1);
130344
+ auto lambda_function = TransformExpression(node->function);
130343
130345
  return make_unique<LambdaExpression>(move(parameters), move(lambda_function));
130344
130346
  }
130345
130347
 
@@ -130420,7 +130422,7 @@ unique_ptr<ParsedExpression> Transformer::TransformBinaryOperator(const string &
130420
130422
  }
130421
130423
  }
130422
130424
 
130423
- unique_ptr<ParsedExpression> Transformer::TransformAExpr(duckdb_libpgquery::PGAExpr *root, idx_t depth) {
130425
+ unique_ptr<ParsedExpression> Transformer::TransformAExpr(duckdb_libpgquery::PGAExpr *root) {
130424
130426
  D_ASSERT(root);
130425
130427
  auto name = string((reinterpret_cast<duckdb_libpgquery::PGValue *>(root->name->head->data.ptr_value))->val.str);
130426
130428
 
@@ -130429,8 +130431,8 @@ unique_ptr<ParsedExpression> Transformer::TransformAExpr(duckdb_libpgquery::PGAE
130429
130431
  case duckdb_libpgquery::PG_AEXPR_OP_ANY: {
130430
130432
  // left=ANY(right)
130431
130433
  // we turn this into left=ANY((SELECT UNNEST(right)))
130432
- auto left_expr = TransformExpression(root->lexpr, depth + 1);
130433
- auto right_expr = TransformExpression(root->rexpr, depth + 1);
130434
+ auto left_expr = TransformExpression(root->lexpr);
130435
+ auto right_expr = TransformExpression(root->rexpr);
130434
130436
 
130435
130437
  auto subquery_expr = make_unique<SubqueryExpression>();
130436
130438
  auto select_statement = make_unique<SelectStatement>();
@@ -130457,7 +130459,7 @@ unique_ptr<ParsedExpression> Transformer::TransformAExpr(duckdb_libpgquery::PGAE
130457
130459
  return move(subquery_expr);
130458
130460
  }
130459
130461
  case duckdb_libpgquery::PG_AEXPR_IN: {
130460
- auto left_expr = TransformExpression(root->lexpr, depth + 1);
130462
+ auto left_expr = TransformExpression(root->lexpr);
130461
130463
  ExpressionType operator_type;
130462
130464
  // this looks very odd, but seems to be the way to find out its NOT IN
130463
130465
  if (name == "<>") {
@@ -130469,14 +130471,14 @@ unique_ptr<ParsedExpression> Transformer::TransformAExpr(duckdb_libpgquery::PGAE
130469
130471
  }
130470
130472
  auto result = make_unique<OperatorExpression>(operator_type, move(left_expr));
130471
130473
  result->query_location = root->location;
130472
- TransformExpressionList(*((duckdb_libpgquery::PGList *)root->rexpr), result->children, depth);
130474
+ TransformExpressionList(*((duckdb_libpgquery::PGList *)root->rexpr), result->children);
130473
130475
  return move(result);
130474
130476
  }
130475
130477
  // rewrite NULLIF(a, b) into CASE WHEN a=b THEN NULL ELSE a END
130476
130478
  case duckdb_libpgquery::PG_AEXPR_NULLIF: {
130477
130479
  vector<unique_ptr<ParsedExpression>> children;
130478
- children.push_back(TransformExpression(root->lexpr, depth + 1));
130479
- children.push_back(TransformExpression(root->rexpr, depth + 1));
130480
+ children.push_back(TransformExpression(root->lexpr));
130481
+ children.push_back(TransformExpression(root->rexpr));
130480
130482
  return make_unique<FunctionExpression>("nullif", move(children));
130481
130483
  }
130482
130484
  // rewrite (NOT) X BETWEEN A AND B into (NOT) AND(GREATERTHANOREQUALTO(X,
@@ -130488,11 +130490,11 @@ unique_ptr<ParsedExpression> Transformer::TransformAExpr(duckdb_libpgquery::PGAE
130488
130490
  throw InternalException("(NOT) BETWEEN needs two args");
130489
130491
  }
130490
130492
 
130491
- auto input = TransformExpression(root->lexpr, depth + 1);
130492
- auto between_left = TransformExpression(
130493
- reinterpret_cast<duckdb_libpgquery::PGNode *>(between_args->head->data.ptr_value), depth + 1);
130494
- auto between_right = TransformExpression(
130495
- reinterpret_cast<duckdb_libpgquery::PGNode *>(between_args->tail->data.ptr_value), depth + 1);
130493
+ auto input = TransformExpression(root->lexpr);
130494
+ auto between_left =
130495
+ TransformExpression(reinterpret_cast<duckdb_libpgquery::PGNode *>(between_args->head->data.ptr_value));
130496
+ auto between_right =
130497
+ TransformExpression(reinterpret_cast<duckdb_libpgquery::PGNode *>(between_args->tail->data.ptr_value));
130496
130498
 
130497
130499
  auto compare_between = make_unique<BetweenExpression>(move(input), move(between_left), move(between_right));
130498
130500
  if (root->kind == duckdb_libpgquery::PG_AEXPR_BETWEEN) {
@@ -130503,8 +130505,8 @@ unique_ptr<ParsedExpression> Transformer::TransformAExpr(duckdb_libpgquery::PGAE
130503
130505
  }
130504
130506
  // rewrite SIMILAR TO into regexp_full_match('asdf', '.*sd.*')
130505
130507
  case duckdb_libpgquery::PG_AEXPR_SIMILAR: {
130506
- auto left_expr = TransformExpression(root->lexpr, depth + 1);
130507
- auto right_expr = TransformExpression(root->rexpr, depth + 1);
130508
+ auto left_expr = TransformExpression(root->lexpr);
130509
+ auto right_expr = TransformExpression(root->rexpr);
130508
130510
 
130509
130511
  vector<unique_ptr<ParsedExpression>> children;
130510
130512
  children.push_back(move(left_expr));
@@ -130539,14 +130541,14 @@ unique_ptr<ParsedExpression> Transformer::TransformAExpr(duckdb_libpgquery::PGAE
130539
130541
  }
130540
130542
  }
130541
130543
  case duckdb_libpgquery::PG_AEXPR_NOT_DISTINCT: {
130542
- auto left_expr = TransformExpression(root->lexpr, depth + 1);
130543
- auto right_expr = TransformExpression(root->rexpr, depth + 1);
130544
+ auto left_expr = TransformExpression(root->lexpr);
130545
+ auto right_expr = TransformExpression(root->rexpr);
130544
130546
  return make_unique<ComparisonExpression>(ExpressionType::COMPARE_NOT_DISTINCT_FROM, move(left_expr),
130545
130547
  move(right_expr));
130546
130548
  }
130547
130549
  case duckdb_libpgquery::PG_AEXPR_DISTINCT: {
130548
- auto left_expr = TransformExpression(root->lexpr, depth + 1);
130549
- auto right_expr = TransformExpression(root->rexpr, depth + 1);
130550
+ auto left_expr = TransformExpression(root->lexpr);
130551
+ auto right_expr = TransformExpression(root->rexpr);
130550
130552
  return make_unique<ComparisonExpression>(ExpressionType::COMPARE_DISTINCT_FROM, move(left_expr),
130551
130553
  move(right_expr));
130552
130554
  }
@@ -130554,8 +130556,8 @@ unique_ptr<ParsedExpression> Transformer::TransformAExpr(duckdb_libpgquery::PGAE
130554
130556
  default:
130555
130557
  break;
130556
130558
  }
130557
- auto left_expr = TransformExpression(root->lexpr, depth + 1);
130558
- auto right_expr = TransformExpression(root->rexpr, depth + 1);
130559
+ auto left_expr = TransformExpression(root->lexpr);
130560
+ auto right_expr = TransformExpression(root->rexpr);
130559
130561
 
130560
130562
  if (!left_expr) {
130561
130563
  // prefix operator
@@ -130575,7 +130577,7 @@ unique_ptr<ParsedExpression> Transformer::TransformAExpr(duckdb_libpgquery::PGAE
130575
130577
 
130576
130578
  namespace duckdb {
130577
130579
 
130578
- unique_ptr<ParsedExpression> Transformer::TransformParamRef(duckdb_libpgquery::PGParamRef *node, idx_t depth) {
130580
+ unique_ptr<ParsedExpression> Transformer::TransformParamRef(duckdb_libpgquery::PGParamRef *node) {
130579
130581
  D_ASSERT(node);
130580
130582
  auto expr = make_unique<ParameterExpression>();
130581
130583
  if (node->number == 0) {
@@ -130594,8 +130596,7 @@ unique_ptr<ParsedExpression> Transformer::TransformParamRef(duckdb_libpgquery::P
130594
130596
 
130595
130597
  namespace duckdb {
130596
130598
 
130597
- unique_ptr<ParsedExpression> Transformer::TransformPositionalReference(duckdb_libpgquery::PGPositionalReference *node,
130598
- idx_t depth) {
130599
+ unique_ptr<ParsedExpression> Transformer::TransformPositionalReference(duckdb_libpgquery::PGPositionalReference *node) {
130599
130600
  if (node->position <= 0) {
130600
130601
  throw ParserException("Positional reference node needs to be >= 1");
130601
130602
  }
@@ -130611,7 +130612,7 @@ unique_ptr<ParsedExpression> Transformer::TransformPositionalReference(duckdb_li
130611
130612
 
130612
130613
  namespace duckdb {
130613
130614
 
130614
- unique_ptr<ParsedExpression> Transformer::TransformSubquery(duckdb_libpgquery::PGSubLink *root, idx_t depth) {
130615
+ unique_ptr<ParsedExpression> Transformer::TransformSubquery(duckdb_libpgquery::PGSubLink *root) {
130615
130616
  D_ASSERT(root);
130616
130617
  auto subquery_expr = make_unique<SubqueryExpression>();
130617
130618
 
@@ -130628,7 +130629,7 @@ unique_ptr<ParsedExpression> Transformer::TransformSubquery(duckdb_libpgquery::P
130628
130629
  case duckdb_libpgquery::PG_ALL_SUBLINK: {
130629
130630
  // comparison with ANY() or ALL()
130630
130631
  subquery_expr->subquery_type = SubqueryType::ANY;
130631
- subquery_expr->child = TransformExpression(root->testexpr, depth + 1);
130632
+ subquery_expr->child = TransformExpression(root->testexpr);
130632
130633
  // get the operator name
130633
130634
  if (!root->operName) {
130634
130635
  // simple IN
@@ -131665,7 +131666,7 @@ static void AddCubeSets(const GroupingSet &current_set, vector<GroupingSet> &res
131665
131666
 
131666
131667
  void Transformer::TransformGroupByExpression(duckdb_libpgquery::PGNode *n, GroupingExpressionMap &map,
131667
131668
  GroupByNode &result, vector<idx_t> &indexes) {
131668
- auto expression = TransformExpression(n, 0);
131669
+ auto expression = TransformExpression(n);
131669
131670
  AddGroupByExpression(move(expression), map, result, indexes);
131670
131671
  }
131671
131672
 
@@ -131797,7 +131798,7 @@ bool Transformer::TransformOrderBy(duckdb_libpgquery::PGList *order, vector<Orde
131797
131798
  } else {
131798
131799
  throw NotImplementedException("Unimplemented order by type");
131799
131800
  }
131800
- auto order_expression = TransformExpression(target, 0);
131801
+ auto order_expression = TransformExpression(target);
131801
131802
  result.emplace_back(type, null_order, move(order_expression));
131802
131803
  } else {
131803
131804
  throw NotImplementedException("ORDER BY list member type %d\n", temp->type);
@@ -131834,7 +131835,7 @@ unique_ptr<SampleOptions> Transformer::TransformSampleOptions(duckdb_libpgquery:
131834
131835
  auto result = make_unique<SampleOptions>();
131835
131836
  auto &sample_options = (duckdb_libpgquery::PGSampleOptions &)*options;
131836
131837
  auto &sample_size = (duckdb_libpgquery::PGSampleSize &)*sample_options.sample_size;
131837
- auto sample_value = TransformValue(sample_size.sample_size, 0)->value;
131838
+ auto sample_value = TransformValue(sample_size.sample_size)->value;
131838
131839
  result->is_percentage = sample_size.is_percentage;
131839
131840
  if (sample_size.is_percentage) {
131840
131841
  // sample size is given in sample_size: use system sampling
@@ -131871,6 +131872,9 @@ unique_ptr<SampleOptions> Transformer::TransformSampleOptions(duckdb_libpgquery:
131871
131872
  namespace duckdb {
131872
131873
 
131873
131874
  LogicalType Transformer::TransformTypeName(duckdb_libpgquery::PGTypeName *type_name) {
131875
+ if (type_name->type != duckdb_libpgquery::T_PGTypeName) {
131876
+ throw ParserException("Expected a type");
131877
+ }
131874
131878
  auto name = (reinterpret_cast<duckdb_libpgquery::PGValue *>(type_name->names->tail->data.ptr_value)->val.str);
131875
131879
  // transform it to the SQL type
131876
131880
  LogicalTypeId base_type = TransformStringToLogicalType(name);
@@ -131885,7 +131889,9 @@ LogicalType Transformer::TransformTypeName(duckdb_libpgquery::PGTypeName *type_n
131885
131889
 
131886
131890
  for (auto node = type_name->typmods->head; node; node = node->next) {
131887
131891
  auto &type_val = *((duckdb_libpgquery::PGList *)node->data.ptr_value);
131888
- D_ASSERT(type_val.length == 2);
131892
+ if (type_val.length != 2) {
131893
+ throw ParserException("Struct entry needs an entry name and a type name");
131894
+ }
131889
131895
 
131890
131896
  auto entry_name_node = (duckdb_libpgquery::PGValue *)(type_val.head->data.ptr_value);
131891
131897
  D_ASSERT(entry_name_node->type == duckdb_libpgquery::T_PGString);
@@ -132046,7 +132052,7 @@ unique_ptr<AlterStatement> Transformer::TransformAlter(duckdb_libpgquery::PGNode
132046
132052
  break;
132047
132053
  }
132048
132054
  case duckdb_libpgquery::PG_AT_ColumnDefault: {
132049
- auto expr = TransformExpression(command->def, 0);
132055
+ auto expr = TransformExpression(command->def);
132050
132056
  result->info = make_unique<SetDefaultInfo>(qname.schema, qname.name, command->name, move(expr));
132051
132057
  break;
132052
132058
  }
@@ -132056,7 +132062,7 @@ unique_ptr<AlterStatement> Transformer::TransformAlter(duckdb_libpgquery::PGNode
132056
132062
 
132057
132063
  unique_ptr<ParsedExpression> expr;
132058
132064
  if (cdef->raw_default) {
132059
- expr = TransformExpression(cdef->raw_default, 0);
132065
+ expr = TransformExpression(cdef->raw_default);
132060
132066
  } else {
132061
132067
  auto colref = make_unique<ColumnRefExpression>(command->name);
132062
132068
  expr = make_unique<CastExpression>(column_definition.type, move(colref));
@@ -132086,7 +132092,7 @@ unique_ptr<CallStatement> Transformer::TransformCall(duckdb_libpgquery::PGNode *
132086
132092
  D_ASSERT(stmt);
132087
132093
 
132088
132094
  auto result = make_unique<CallStatement>();
132089
- result->function = TransformFuncCall((duckdb_libpgquery::PGFuncCall *)stmt->func, 0);
132095
+ result->function = TransformFuncCall((duckdb_libpgquery::PGFuncCall *)stmt->func);
132090
132096
  return result;
132091
132097
  }
132092
132098
 
@@ -132160,7 +132166,7 @@ void Transformer::TransformCopyOptions(CopyInfo &info, duckdb_libpgquery::PGList
132160
132166
  break;
132161
132167
  default:
132162
132168
  info.options[def_elem->defname].push_back(
132163
- TransformValue(*((duckdb_libpgquery::PGValue *)def_elem->arg), 0)->value);
132169
+ TransformValue(*((duckdb_libpgquery::PGValue *)def_elem->arg))->value);
132164
132170
  break;
132165
132171
  }
132166
132172
  }
@@ -132255,13 +132261,13 @@ unique_ptr<CreateStatement> Transformer::TransformCreateFunction(duckdb_libpgque
132255
132261
  info->schema = qname.schema;
132256
132262
  info->name = qname.name;
132257
132263
 
132258
- auto function = TransformExpression(stmt->function, 0);
132264
+ auto function = TransformExpression(stmt->function);
132259
132265
  D_ASSERT(function);
132260
132266
  auto macro_func = make_unique<MacroFunction>(move(function));
132261
132267
 
132262
132268
  if (stmt->params) {
132263
132269
  vector<unique_ptr<ParsedExpression>> parameters;
132264
- TransformExpressionList(*stmt->params, parameters, 0);
132270
+ TransformExpressionList(*stmt->params, parameters);
132265
132271
  for (auto &param : parameters) {
132266
132272
  if (param->type == ExpressionType::VALUE_CONSTANT) {
132267
132273
  // parameters with default value (must have an alias)
@@ -132335,7 +132341,7 @@ unique_ptr<CreateStatement> Transformer::TransformCreateIndex(duckdb_libpgquery:
132335
132341
  } else {
132336
132342
  // parse the index expression
132337
132343
  D_ASSERT(index_element->expr);
132338
- info->expressions.push_back(TransformExpression(index_element->expr, 0));
132344
+ info->expressions.push_back(TransformExpression(index_element->expr));
132339
132345
  }
132340
132346
  }
132341
132347
 
@@ -132517,9 +132523,8 @@ OnCreateConflict Transformer::TransformOnConflict(duckdb_libpgquery::PGOnCreateC
132517
132523
  }
132518
132524
  }
132519
132525
 
132520
- unique_ptr<ParsedExpression> Transformer::TransformCollateExpr(duckdb_libpgquery::PGCollateClause *collate,
132521
- idx_t depth) {
132522
- auto child = TransformExpression(collate->arg, depth + 1);
132526
+ unique_ptr<ParsedExpression> Transformer::TransformCollateExpr(duckdb_libpgquery::PGCollateClause *collate) {
132527
+ auto child = TransformExpression(collate->arg);
132523
132528
  auto collation = TransformCollation(collate);
132524
132529
  return make_unique<CollateExpression>(collation, move(child));
132525
132530
  }
@@ -132699,7 +132704,7 @@ unique_ptr<DeleteStatement> Transformer::TransformDelete(duckdb_libpgquery::PGNo
132699
132704
  D_ASSERT(stmt);
132700
132705
  auto result = make_unique<DeleteStatement>();
132701
132706
 
132702
- result->condition = TransformExpression(stmt->whereClause, 0);
132707
+ result->condition = TransformExpression(stmt->whereClause);
132703
132708
  result->table = TransformRangeVar(stmt->relation);
132704
132709
  if (result->table->type != TableReferenceType::BASE_TABLE) {
132705
132710
  throw Exception("Can only delete from base tables!");
@@ -132837,7 +132842,7 @@ unique_ptr<TableRef> Transformer::TransformValuesList(duckdb_libpgquery::PGList
132837
132842
  auto target = (duckdb_libpgquery::PGList *)(value_list->data.ptr_value);
132838
132843
 
132839
132844
  vector<unique_ptr<ParsedExpression>> insert_values;
132840
- TransformExpressionList(*target, insert_values, 0);
132845
+ TransformExpressionList(*target, insert_values);
132841
132846
  if (!result->values.empty()) {
132842
132847
  if (result->values[0].size() != insert_values.size()) {
132843
132848
  throw ParserException("VALUES lists must all be the same length");
@@ -132910,13 +132915,13 @@ unique_ptr<PragmaStatement> Transformer::TransformPragma(duckdb_libpgquery::PGNo
132910
132915
  if (stmt->args) {
132911
132916
  for (auto cell = stmt->args->head; cell != nullptr; cell = cell->next) {
132912
132917
  auto node = reinterpret_cast<duckdb_libpgquery::PGNode *>(cell->data.ptr_value);
132913
- auto expr = TransformExpression(node, 0);
132918
+ auto expr = TransformExpression(node);
132914
132919
 
132915
132920
  if (expr->type == ExpressionType::COMPARE_EQUAL) {
132916
132921
  auto &comp = (ComparisonExpression &)*expr;
132917
132922
  info.named_parameters[comp.left->ToString()] = Value(comp.right->ToString());
132918
132923
  } else if (node->type == duckdb_libpgquery::T_PGAConst) {
132919
- auto constant = TransformConstant((duckdb_libpgquery::PGAConst *)node, 0);
132924
+ auto constant = TransformConstant((duckdb_libpgquery::PGAConst *)node);
132920
132925
  info.parameters.push_back(((ConstantExpression &)*constant).value);
132921
132926
  } else {
132922
132927
  info.parameters.emplace_back(expr->ToString());
@@ -132979,7 +132984,7 @@ unique_ptr<ExecuteStatement> Transformer::TransformExecute(duckdb_libpgquery::PG
132979
132984
  result->name = string(stmt->name);
132980
132985
 
132981
132986
  if (stmt->params) {
132982
- TransformExpressionList(*stmt->params, result->values, 0);
132987
+ TransformExpressionList(*stmt->params, result->values);
132983
132988
  }
132984
132989
  for (auto &expr : result->values) {
132985
132990
  if (!expr->IsScalar()) {
@@ -132992,6 +132997,9 @@ unique_ptr<ExecuteStatement> Transformer::TransformExecute(duckdb_libpgquery::PG
132992
132997
  unique_ptr<DropStatement> Transformer::TransformDeallocate(duckdb_libpgquery::PGNode *node) {
132993
132998
  auto stmt = reinterpret_cast<duckdb_libpgquery::PGDeallocateStmt *>(node);
132994
132999
  D_ASSERT(stmt);
133000
+ if (!stmt->name) {
133001
+ throw ParserException("DEALLOCATE requires a name");
133002
+ }
132995
133003
 
132996
133004
  auto result = make_unique<DropStatement>();
132997
133005
  result->info->type = CatalogType::PREPARED_STATEMENT;
@@ -133144,7 +133152,7 @@ unique_ptr<QueryNode> Transformer::TransformSelectNode(duckdb_libpgquery::PGSele
133144
133152
  auto target = reinterpret_cast<duckdb_libpgquery::PGNode *>(stmt->distinctClause->head->data.ptr_value);
133145
133153
  if (target) {
133146
133154
  // add the columns defined in the ON clause to the select list
133147
- TransformExpressionList(*stmt->distinctClause, modifier->distinct_on_targets, 0);
133155
+ TransformExpressionList(*stmt->distinctClause, modifier->distinct_on_targets);
133148
133156
  }
133149
133157
  result->modifiers.push_back(move(modifier));
133150
133158
  }
@@ -133160,16 +133168,16 @@ unique_ptr<QueryNode> Transformer::TransformSelectNode(duckdb_libpgquery::PGSele
133160
133168
  throw ParserException("SELECT clause without selection list");
133161
133169
  }
133162
133170
  // select list
133163
- TransformExpressionList(*stmt->targetList, result->select_list, 0);
133171
+ TransformExpressionList(*stmt->targetList, result->select_list);
133164
133172
  result->from_table = TransformFrom(stmt->fromClause);
133165
133173
  }
133166
133174
 
133167
133175
  // where
133168
- result->where_clause = TransformExpression(stmt->whereClause, 0);
133176
+ result->where_clause = TransformExpression(stmt->whereClause);
133169
133177
  // group by
133170
133178
  TransformGroupBy(stmt->groupClause, result->groups);
133171
133179
  // having
133172
- result->having = TransformExpression(stmt->havingClause, 0);
133180
+ result->having = TransformExpression(stmt->havingClause);
133173
133181
  // sample
133174
133182
  result->sample = TransformSampleOptions(stmt->sampleOptions);
133175
133183
  break;
@@ -133226,10 +133234,10 @@ unique_ptr<QueryNode> Transformer::TransformSelectNode(duckdb_libpgquery::PGSele
133226
133234
  if (stmt->limitCount || stmt->limitOffset) {
133227
133235
  auto limit_modifier = make_unique<LimitModifier>();
133228
133236
  if (stmt->limitCount) {
133229
- limit_modifier->limit = TransformExpression(stmt->limitCount, 0);
133237
+ limit_modifier->limit = TransformExpression(stmt->limitCount);
133230
133238
  }
133231
133239
  if (stmt->limitOffset) {
133232
- limit_modifier->offset = TransformExpression(stmt->limitOffset, 0);
133240
+ limit_modifier->offset = TransformExpression(stmt->limitOffset);
133233
133241
  }
133234
133242
  node->modifiers.push_back(move(limit_modifier));
133235
133243
  }
@@ -133283,7 +133291,7 @@ unique_ptr<SetStatement> Transformer::TransformSet(duckdb_libpgquery::PGNode *no
133283
133291
  D_ASSERT(stmt->args->head && stmt->args->head->data.ptr_value);
133284
133292
  D_ASSERT(((duckdb_libpgquery::PGNode *)stmt->args->head->data.ptr_value)->type == duckdb_libpgquery::T_PGAConst);
133285
133293
 
133286
- auto value = TransformValue(((duckdb_libpgquery::PGAConst *)stmt->args->head->data.ptr_value)->val, 0)->value;
133294
+ auto value = TransformValue(((duckdb_libpgquery::PGAConst *)stmt->args->head->data.ptr_value)->val)->value;
133287
133295
 
133288
133296
  return make_unique<SetStatement>(name, value, ToSetScope(stmt->scope));
133289
133297
  }
@@ -133396,10 +133404,10 @@ unique_ptr<UpdateStatement> Transformer::TransformUpdate(duckdb_libpgquery::PGNo
133396
133404
  for (auto cell = root->head; cell != nullptr; cell = cell->next) {
133397
133405
  auto target = (duckdb_libpgquery::PGResTarget *)(cell->data.ptr_value);
133398
133406
  result->columns.emplace_back(target->name);
133399
- result->expressions.push_back(TransformExpression(target->val, 0));
133407
+ result->expressions.push_back(TransformExpression(target->val));
133400
133408
  }
133401
133409
 
133402
- result->condition = TransformExpression(stmt->whereClause, 0);
133410
+ result->condition = TransformExpression(stmt->whereClause);
133403
133411
  return result;
133404
133412
  }
133405
133413
 
@@ -133471,6 +133479,7 @@ unique_ptr<TableRef> Transformer::TransformFrom(duckdb_libpgquery::PGList *root)
133471
133479
  // Cross Product
133472
133480
  auto result = make_unique<CrossProductRef>();
133473
133481
  CrossProductRef *cur_root = result.get();
133482
+ idx_t list_size = 0;
133474
133483
  for (auto node = root->head; node != nullptr; node = node->next) {
133475
133484
  auto n = reinterpret_cast<duckdb_libpgquery::PGNode *>(node->data.ptr_value);
133476
133485
  unique_ptr<TableRef> next = TransformTableRefNode(n);
@@ -133485,6 +133494,8 @@ unique_ptr<TableRef> Transformer::TransformFrom(duckdb_libpgquery::PGList *root)
133485
133494
  result->right = move(next);
133486
133495
  cur_root = result.get();
133487
133496
  }
133497
+ list_size++;
133498
+ StackCheck(list_size * DEFAULT_ENTRY_STACK_SIZE);
133488
133499
  }
133489
133500
  return move(result);
133490
133501
  }
@@ -133553,7 +133564,7 @@ unique_ptr<TableRef> Transformer::TransformJoin(duckdb_libpgquery::PGJoinExpr *r
133553
133564
  cross->right = move(result->right);
133554
133565
  return move(cross);
133555
133566
  }
133556
- result->condition = TransformExpression(root->quals, 0);
133567
+ result->condition = TransformExpression(root->quals);
133557
133568
  return move(result);
133558
133569
  }
133559
133570
 
@@ -133610,12 +133621,12 @@ unique_ptr<TableRef> Transformer::TransformRangeFunction(duckdb_libpgquery::PGRa
133610
133621
  switch (call_tree->type) {
133611
133622
  case duckdb_libpgquery::T_PGFuncCall: {
133612
133623
  auto func_call = (duckdb_libpgquery::PGFuncCall *)call_tree;
133613
- result->function = TransformFuncCall(func_call, 0);
133624
+ result->function = TransformFuncCall(func_call);
133614
133625
  result->query_location = func_call->location;
133615
133626
  break;
133616
133627
  }
133617
133628
  case duckdb_libpgquery::T_PGSQLValueFunction:
133618
- result->function = TransformSQLValueFunction((duckdb_libpgquery::PGSQLValueFunction *)call_tree, 0);
133629
+ result->function = TransformSQLValueFunction((duckdb_libpgquery::PGSQLValueFunction *)call_tree);
133619
133630
  break;
133620
133631
  default:
133621
133632
  throw ParserException("Not a function call or value function");
@@ -133635,6 +133646,8 @@ unique_ptr<TableRef> Transformer::TransformRangeFunction(duckdb_libpgquery::PGRa
133635
133646
  namespace duckdb {
133636
133647
 
133637
133648
  unique_ptr<TableRef> Transformer::TransformTableRefNode(duckdb_libpgquery::PGNode *n) {
133649
+ StackCheck();
133650
+
133638
133651
  switch (n->type) {
133639
133652
  case duckdb_libpgquery::T_PGRangeVar:
133640
133653
  return TransformRangeVar(reinterpret_cast<duckdb_libpgquery::PGRangeVar *>(n));
@@ -133679,6 +133692,8 @@ unique_ptr<TableRef> Transformer::TransformTableRefNode(duckdb_libpgquery::PGNod
133679
133692
  namespace duckdb {
133680
133693
 
133681
133694
  bool Transformer::TransformParseTree(duckdb_libpgquery::PGList *tree, vector<unique_ptr<SQLStatement>> &statements) {
133695
+ int stack_check_var;
133696
+ InitializeStackCheck(&stack_check_var);
133682
133697
  for (auto entry = tree->head; entry != nullptr; entry = entry->next) {
133683
133698
  SetParamCount(0);
133684
133699
  auto stmt = TransformStatement((duckdb_libpgquery::PGNode *)entry->data.ptr_value);
@@ -133689,6 +133704,26 @@ bool Transformer::TransformParseTree(duckdb_libpgquery::PGList *tree, vector<uni
133689
133704
  return true;
133690
133705
  }
133691
133706
 
133707
+ void Transformer::InitializeStackCheck(int *stack_check_var) {
133708
+ this->root = stack_check_var;
133709
+ }
133710
+
133711
+ void Transformer::StackCheck(idx_t extra_stack) {
133712
+ if (!root) {
133713
+ return;
133714
+ }
133715
+ int current_stack_var;
133716
+ if (&current_stack_var > root) {
133717
+ throw InternalException("Transformer::StackCheck variables are incorrectly set up");
133718
+ }
133719
+ idx_t stack_size = root - &current_stack_var + extra_stack;
133720
+ if (stack_size > MAX_STACK_SIZE) {
133721
+ throw ParserException(
133722
+ "Stack usage in parsing is too high: the query tree is too deep (stack usage %lld, max stack usage %lld)",
133723
+ stack_size, MAX_STACK_SIZE);
133724
+ }
133725
+ }
133726
+
133692
133727
  unique_ptr<SQLStatement> Transformer::TransformStatement(duckdb_libpgquery::PGNode *stmt) {
133693
133728
  switch (stmt->type) {
133694
133729
  case duckdb_libpgquery::T_PGRawStmt: {
@@ -135988,6 +136023,40 @@ unique_ptr<BoundQueryNode> Binder::BindNode(RecursiveCTENode &statement) {
135988
136023
 
135989
136024
 
135990
136025
 
136026
+ //===----------------------------------------------------------------------===//
136027
+ // DuckDB
136028
+ //
136029
+ // duckdb/planner/expression_binder/column_alias_binder.hpp
136030
+ //
136031
+ //
136032
+ //===----------------------------------------------------------------------===//
136033
+
136034
+
136035
+
136036
+
136037
+
136038
+
136039
+ namespace duckdb {
136040
+
136041
+ class BoundSelectNode;
136042
+ class ColumnRefExpression;
136043
+
136044
+ //! A helper binder for WhereBinder and HavingBinder which support alias as a columnref.
136045
+ class ColumnAliasBinder {
136046
+ public:
136047
+ ColumnAliasBinder(BoundSelectNode &node, const unordered_map<string, idx_t> &alias_map);
136048
+
136049
+ BindResult BindAlias(ExpressionBinder &enclosing_binder, ColumnRefExpression &expr, idx_t depth,
136050
+ bool root_expression);
136051
+
136052
+ private:
136053
+ BoundSelectNode &node;
136054
+ const unordered_map<string, idx_t> &alias_map;
136055
+ bool in_alias;
136056
+ };
136057
+
136058
+ } // namespace duckdb
136059
+
135991
136060
  //===----------------------------------------------------------------------===//
135992
136061
  // DuckDB
135993
136062
  //
@@ -136078,6 +136147,7 @@ protected:
136078
136147
 
136079
136148
 
136080
136149
 
136150
+
136081
136151
  namespace duckdb {
136082
136152
 
136083
136153
  //! The HAVING binder is responsible for binding an expression within the HAVING clause of a SQL statement
@@ -136086,12 +136156,14 @@ public:
136086
136156
  HavingBinder(Binder &binder, ClientContext &context, BoundSelectNode &node, BoundGroupInformation &info,
136087
136157
  unordered_map<string, idx_t> &alias_map);
136088
136158
 
136089
- unordered_map<string, idx_t> &alias_map;
136090
- bool in_alias;
136091
-
136092
136159
  protected:
136093
136160
  BindResult BindExpression(unique_ptr<ParsedExpression> *expr_ptr, idx_t depth,
136094
136161
  bool root_expression = false) override;
136162
+
136163
+ private:
136164
+ BindResult BindColumnRef(unique_ptr<ParsedExpression> *expr_ptr, idx_t depth, bool root_expression);
136165
+
136166
+ ColumnAliasBinder column_alias_binder;
136095
136167
  };
136096
136168
 
136097
136169
  } // namespace duckdb
@@ -136154,16 +136226,23 @@ private:
136154
136226
 
136155
136227
  namespace duckdb {
136156
136228
 
136229
+ class ColumnAliasBinder;
136230
+
136157
136231
  //! The WHERE binder is responsible for binding an expression within the WHERE clause of a SQL statement
136158
136232
  class WhereBinder : public ExpressionBinder {
136159
136233
  public:
136160
- WhereBinder(Binder &binder, ClientContext &context);
136234
+ WhereBinder(Binder &binder, ClientContext &context, ColumnAliasBinder *column_alias_binder = nullptr);
136161
136235
 
136162
136236
  protected:
136163
136237
  BindResult BindExpression(unique_ptr<ParsedExpression> *expr_ptr, idx_t depth,
136164
136238
  bool root_expression = false) override;
136165
136239
 
136166
136240
  string UnsupportedAggregateMessage() override;
136241
+
136242
+ private:
136243
+ BindResult BindColumnRef(unique_ptr<ParsedExpression> *expr_ptr, idx_t depth, bool root_expression);
136244
+
136245
+ ColumnAliasBinder *column_alias_binder;
136167
136246
  };
136168
136247
 
136169
136248
  } // namespace duckdb
@@ -136172,10 +136251,6 @@ protected:
136172
136251
 
136173
136252
 
136174
136253
  namespace duckdb {
136175
- unique_ptr<Expression> Binder::BindFilter(unique_ptr<ParsedExpression> condition) {
136176
- WhereBinder where_binder(*this, context);
136177
- return where_binder.Bind(condition);
136178
- }
136179
136254
 
136180
136255
  unique_ptr<Expression> Binder::BindOrderExpression(OrderBinder &order_binder, unique_ptr<ParsedExpression> expr) {
136181
136256
  // we treat the Distinct list as a order by
@@ -136374,7 +136449,10 @@ unique_ptr<BoundQueryNode> Binder::BindNode(SelectNode &statement) {
136374
136449
  // first visit the WHERE clause
136375
136450
  // the WHERE clause happens before the GROUP BY, PROJECTION or HAVING clauses
136376
136451
  if (statement.where_clause) {
136377
- result->where_clause = BindFilter(move(statement.where_clause));
136452
+ ColumnAliasBinder alias_binder(*result, alias_map);
136453
+ WhereBinder where_binder(*this, context, &alias_binder);
136454
+ unique_ptr<ParsedExpression> condition = move(statement.where_clause);
136455
+ result->where_clause = where_binder.Bind(condition);
136378
136456
  }
136379
136457
 
136380
136458
  // now bind all the result modifiers; including DISTINCT and ORDER BY targets
@@ -141718,6 +141796,38 @@ BindResult CheckBinder::BindCheckColumn(ColumnRefExpression &colref) {
141718
141796
  } // namespace duckdb
141719
141797
 
141720
141798
 
141799
+
141800
+
141801
+
141802
+
141803
+ namespace duckdb {
141804
+
141805
+ ColumnAliasBinder::ColumnAliasBinder(BoundSelectNode &node, const unordered_map<string, idx_t> &alias_map)
141806
+ : node(node), alias_map(alias_map), in_alias(false) {
141807
+ }
141808
+
141809
+ BindResult ColumnAliasBinder::BindAlias(ExpressionBinder &enclosing_binder, ColumnRefExpression &expr, idx_t depth,
141810
+ bool root_expression) {
141811
+ if (!expr.table_name.empty()) {
141812
+ return BindResult(StringUtil::Format("Alias %s cannot be qualified.", expr.ToString()));
141813
+ }
141814
+
141815
+ auto alias_entry = alias_map.find(expr.column_name);
141816
+ if (alias_entry == alias_map.end()) {
141817
+ return BindResult(StringUtil::Format("Alias %s is not found.", expr.ToString()));
141818
+ }
141819
+
141820
+ // found an alias: bind the alias expression
141821
+ D_ASSERT(!in_alias);
141822
+ auto expression = node.original_expressions[alias_entry->second]->Copy();
141823
+ in_alias = true;
141824
+ auto result = enclosing_binder.BindExpression(&expression, depth, root_expression);
141825
+ in_alias = false;
141826
+ return result;
141827
+ }
141828
+
141829
+ } // namespace duckdb
141830
+
141721
141831
  namespace duckdb {
141722
141832
 
141723
141833
  ConstantBinder::ConstantBinder(Binder &binder, ClientContext &context, string clause)
@@ -141865,10 +141975,21 @@ namespace duckdb {
141865
141975
 
141866
141976
  HavingBinder::HavingBinder(Binder &binder, ClientContext &context, BoundSelectNode &node, BoundGroupInformation &info,
141867
141977
  unordered_map<string, idx_t> &alias_map)
141868
- : SelectBinder(binder, context, node, info), alias_map(alias_map), in_alias(false) {
141978
+ : SelectBinder(binder, context, node, info), column_alias_binder(node, alias_map) {
141869
141979
  target_type = LogicalType(LogicalTypeId::BOOLEAN);
141870
141980
  }
141871
141981
 
141982
+ BindResult HavingBinder::BindColumnRef(unique_ptr<ParsedExpression> *expr_ptr, idx_t depth, bool root_expression) {
141983
+ auto &expr = (ColumnRefExpression &)**expr_ptr;
141984
+ auto alias_result = column_alias_binder.BindAlias(*this, expr, depth, root_expression);
141985
+ if (!alias_result.HasError()) {
141986
+ return alias_result;
141987
+ }
141988
+
141989
+ return BindResult(StringUtil::Format(
141990
+ "column %s must appear in the GROUP BY clause or be used in an aggregate function", expr.ToString()));
141991
+ }
141992
+
141872
141993
  BindResult HavingBinder::BindExpression(unique_ptr<ParsedExpression> *expr_ptr, idx_t depth, bool root_expression) {
141873
141994
  auto &expr = **expr_ptr;
141874
141995
  // check if the expression binds to one of the groups
@@ -141880,22 +142001,7 @@ BindResult HavingBinder::BindExpression(unique_ptr<ParsedExpression> *expr_ptr,
141880
142001
  case ExpressionClass::WINDOW:
141881
142002
  return BindResult("HAVING clause cannot contain window functions!");
141882
142003
  case ExpressionClass::COLUMN_REF:
141883
- if (!in_alias) {
141884
- auto &colref = (ColumnRefExpression &)expr;
141885
- if (colref.table_name.empty()) {
141886
- auto alias_entry = alias_map.find(colref.column_name);
141887
- if (alias_entry != alias_map.end()) {
141888
- // found an alias: bind the alias expression
141889
- auto expression = node.original_expressions[alias_entry->second]->Copy();
141890
- in_alias = true;
141891
- auto result = BindExpression(&expression, depth, root_expression);
141892
- in_alias = false;
141893
- return result;
141894
- }
141895
- }
141896
- }
141897
- return BindResult(StringUtil::Format(
141898
- "column %s must appear in the GROUP BY clause or be used in an aggregate function", expr.ToString()));
142004
+ return BindColumnRef(expr_ptr, depth, root_expression);
141899
142005
  default:
141900
142006
  return duckdb::SelectBinder::BindExpression(expr_ptr, depth);
141901
142007
  }
@@ -142229,12 +142335,30 @@ string UpdateBinder::UnsupportedAggregateMessage() {
142229
142335
  } // namespace duckdb
142230
142336
 
142231
142337
 
142338
+
142232
142339
  namespace duckdb {
142233
142340
 
142234
- WhereBinder::WhereBinder(Binder &binder, ClientContext &context) : ExpressionBinder(binder, context) {
142341
+ WhereBinder::WhereBinder(Binder &binder, ClientContext &context, ColumnAliasBinder *column_alias_binder)
142342
+ : ExpressionBinder(binder, context), column_alias_binder(column_alias_binder) {
142235
142343
  target_type = LogicalType(LogicalTypeId::BOOLEAN);
142236
142344
  }
142237
142345
 
142346
+ BindResult WhereBinder::BindColumnRef(unique_ptr<ParsedExpression> *expr_ptr, idx_t depth, bool root_expression) {
142347
+ auto &expr = (ColumnRefExpression &)**expr_ptr;
142348
+ auto result = ExpressionBinder::BindExpression(expr_ptr, depth);
142349
+ if (!result.HasError() || !column_alias_binder) {
142350
+ return result;
142351
+ }
142352
+
142353
+ BindResult alias_result = column_alias_binder->BindAlias(*this, expr, depth, root_expression);
142354
+ // This code path cannot be exercised at thispoint. #1547 might change that.
142355
+ if (!alias_result.HasError()) {
142356
+ return alias_result;
142357
+ }
142358
+
142359
+ return result;
142360
+ }
142361
+
142238
142362
  BindResult WhereBinder::BindExpression(unique_ptr<ParsedExpression> *expr_ptr, idx_t depth, bool root_expression) {
142239
142363
  auto &expr = **expr_ptr;
142240
142364
  switch (expr.GetExpressionClass()) {
@@ -142242,6 +142366,8 @@ BindResult WhereBinder::BindExpression(unique_ptr<ParsedExpression> *expr_ptr, i
142242
142366
  return BindResult("WHERE clause cannot contain DEFAULT clause");
142243
142367
  case ExpressionClass::WINDOW:
142244
142368
  return BindResult("WHERE clause cannot contain window functions!");
142369
+ case ExpressionClass::COLUMN_REF:
142370
+ return BindColumnRef(expr_ptr, depth, root_expression);
142245
142371
  default:
142246
142372
  return ExpressionBinder::BindExpression(expr_ptr, depth);
142247
142373
  }
@@ -142503,8 +142629,6 @@ void ExpressionBinder::BindTableNames(Binder &binder, ParsedExpression &expr, un
142503
142629
  if (binder.macro_binding != nullptr && binder.macro_binding->HasMatchingBinding(colref.column_name)) {
142504
142630
  // macro parameters get priority
142505
142631
  colref.table_name = binder.macro_binding->alias;
142506
- } else if (alias_map && alias_map->find(colref.column_name) != alias_map->end()) {
142507
- // alias: leave unqualified
142508
142632
  } else {
142509
142633
  colref.table_name = binder.bind_context.GetMatchingBinding(colref.column_name);
142510
142634
  }