duckdb 0.6.1-dev12.0 → 0.6.1-dev131.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
@@ -4121,6 +4121,20 @@ unique_ptr<CatalogEntry> TableCatalogEntry::AlterEntry(ClientContext &context, A
4121
4121
  }
4122
4122
  }
4123
4123
 
4124
+ void TableCatalogEntry::UndoAlter(ClientContext &context, AlterInfo *info) {
4125
+ D_ASSERT(!internal);
4126
+ D_ASSERT(info->type == AlterType::ALTER_TABLE);
4127
+ auto table_info = (AlterTableInfo *)info;
4128
+ switch (table_info->alter_table_type) {
4129
+ case AlterTableType::RENAME_TABLE: {
4130
+ storage->info->table = this->name;
4131
+ break;
4132
+ default:
4133
+ break;
4134
+ }
4135
+ }
4136
+ }
4137
+
4124
4138
  static void RenameExpression(ParsedExpression &expr, RenameColumnInfo &info) {
4125
4139
  if (expr.type == ExpressionType::COLUMN_REF) {
4126
4140
  auto &colref = (ColumnRefExpression &)expr;
@@ -4219,6 +4233,8 @@ unique_ptr<CatalogEntry> TableCatalogEntry::AddColumn(ClientContext &context, Ad
4219
4233
  create_info->constraints.push_back(constraint->Copy());
4220
4234
  }
4221
4235
  Binder::BindLogicalType(context, info.new_column.TypeMutable(), schema->name);
4236
+ info.new_column.SetOid(columns.LogicalColumnCount());
4237
+ info.new_column.SetStorageOid(columns.PhysicalColumnCount());
4222
4238
  auto col = info.new_column.Copy();
4223
4239
 
4224
4240
  create_info->columns.AddColumn(move(col));
@@ -4966,6 +4982,9 @@ unique_ptr<CatalogEntry> CatalogEntry::AlterEntry(ClientContext &context, AlterI
4966
4982
  throw InternalException("Unsupported alter type for catalog entry!");
4967
4983
  }
4968
4984
 
4985
+ void CatalogEntry::UndoAlter(ClientContext &context, AlterInfo *info) {
4986
+ }
4987
+
4969
4988
  unique_ptr<CatalogEntry> CatalogEntry::Copy(ClientContext &context) {
4970
4989
  throw InternalException("Unsupported copy type for catalog entry!");
4971
4990
  }
@@ -5318,8 +5337,9 @@ bool CatalogSet::AlterEntry(ClientContext &context, const string &name, AlterInf
5318
5337
  if (value->name != original_name) {
5319
5338
  auto mapping_value = GetMapping(context, value->name);
5320
5339
  if (mapping_value && !mapping_value->deleted) {
5321
- auto entry = GetEntryForTransaction(context, entries[mapping_value->index].get());
5322
- if (!entry->deleted) {
5340
+ auto original_entry = GetEntryForTransaction(context, entries[mapping_value->index].get());
5341
+ if (!original_entry->deleted) {
5342
+ entry->UndoAlter(context, alter_info);
5323
5343
  string rename_err_msg =
5324
5344
  "Could not rename \"%s\" to \"%s\": another entry with this name already exists!";
5325
5345
  throw CatalogException(rename_err_msg, original_name, value->name);
@@ -5408,6 +5428,7 @@ bool CatalogSet::DropEntry(ClientContext &context, const string &name, bool casc
5408
5428
  throw CatalogException("Cannot drop entry \"%s\" because it is an internal system entry", entry->name);
5409
5429
  }
5410
5430
 
5431
+ lock_guard<mutex> read_lock(catalog_lock);
5411
5432
  DropEntryInternal(context, entry_index, *entry, cascade);
5412
5433
  return true;
5413
5434
  }
@@ -6493,6 +6514,9 @@ AllocatedData::AllocatedData() : allocator(nullptr), pointer(nullptr), allocated
6493
6514
 
6494
6515
  AllocatedData::AllocatedData(Allocator &allocator, data_ptr_t pointer, idx_t allocated_size)
6495
6516
  : allocator(&allocator), pointer(pointer), allocated_size(allocated_size) {
6517
+ if (!pointer) {
6518
+ throw InternalException("AllocatedData object constructed with nullptr");
6519
+ }
6496
6520
  }
6497
6521
  AllocatedData::~AllocatedData() {
6498
6522
  Reset();
@@ -6584,11 +6608,19 @@ Allocator::~Allocator() {
6584
6608
 
6585
6609
  data_ptr_t Allocator::AllocateData(idx_t size) {
6586
6610
  D_ASSERT(size > 0);
6611
+ if (size >= MAXIMUM_ALLOC_SIZE) {
6612
+ D_ASSERT(false);
6613
+ throw InternalException("Requested allocation size of %llu is out of range - maximum allocation size is %llu",
6614
+ size, MAXIMUM_ALLOC_SIZE);
6615
+ }
6587
6616
  auto result = allocate_function(private_data.get(), size);
6588
6617
  #ifdef DEBUG
6589
6618
  D_ASSERT(private_data);
6590
6619
  private_data->debug_info->AllocateData(result, size);
6591
6620
  #endif
6621
+ if (!result) {
6622
+ throw std::bad_alloc();
6623
+ }
6592
6624
  return result;
6593
6625
  }
6594
6626
 
@@ -6608,11 +6640,20 @@ data_ptr_t Allocator::ReallocateData(data_ptr_t pointer, idx_t old_size, idx_t s
6608
6640
  if (!pointer) {
6609
6641
  return nullptr;
6610
6642
  }
6643
+ if (size >= MAXIMUM_ALLOC_SIZE) {
6644
+ D_ASSERT(false);
6645
+ throw InternalException(
6646
+ "Requested re-allocation size of %llu is out of range - maximum allocation size is %llu", size,
6647
+ MAXIMUM_ALLOC_SIZE);
6648
+ }
6611
6649
  auto new_pointer = reallocate_function(private_data.get(), pointer, old_size, size);
6612
6650
  #ifdef DEBUG
6613
6651
  D_ASSERT(private_data);
6614
6652
  private_data->debug_info->ReallocateData(pointer, new_pointer, old_size, size);
6615
6653
  #endif
6654
+ if (!new_pointer) {
6655
+ throw std::bad_alloc();
6656
+ }
6616
6657
  return new_pointer;
6617
6658
  }
6618
6659
 
@@ -18593,6 +18634,7 @@ FileType FileHandle::GetType() {
18593
18634
 
18594
18635
 
18595
18636
 
18637
+
18596
18638
  #include <limits>
18597
18639
  #include <cstring>
18598
18640
  #include <cmath>
@@ -22487,6 +22529,7 @@ unique_ptr<FileSystem> FileSystem::CreateLocal() {
22487
22529
 
22488
22530
 
22489
22531
 
22532
+
22490
22533
  namespace duckdb {
22491
22534
 
22492
22535
  struct ConvertToString {
@@ -24109,8 +24152,11 @@ string_t StringCastFromDecimal::Operation(hugeint_t input, uint8_t width, uint8_
24109
24152
 
24110
24153
 
24111
24154
 
24155
+
24112
24156
  namespace duckdb {
24113
24157
 
24158
+ struct interval_t;
24159
+
24114
24160
  struct MultiplyOperator {
24115
24161
  template <class TA, class TB, class TR>
24116
24162
  static inline TR Operation(TA left, TB right) {
@@ -45487,6 +45533,7 @@ string Decimal::ToString(hugeint_t value, uint8_t width, uint8_t scale) {
45487
45533
 
45488
45534
 
45489
45535
 
45536
+
45490
45537
  #include <functional>
45491
45538
  #include <cmath>
45492
45539
 
@@ -45627,6 +45674,7 @@ hash_t Hash(uint8_t *val, size_t size) {
45627
45674
 
45628
45675
 
45629
45676
 
45677
+
45630
45678
  #include <cmath>
45631
45679
  #include <limits>
45632
45680
 
@@ -46121,6 +46169,13 @@ bool Hugeint::TryConvert(int8_t value, hugeint_t &result) {
46121
46169
  return true;
46122
46170
  }
46123
46171
 
46172
+ template <>
46173
+ bool Hugeint::TryConvert(const char *value, hugeint_t &result) {
46174
+ auto len = strlen(value);
46175
+ string_t string_val(value, len);
46176
+ return TryCast::Operation<string_t, hugeint_t>(string_val, result, true);
46177
+ }
46178
+
46124
46179
  template <>
46125
46180
  bool Hugeint::TryConvert(int16_t value, hugeint_t &result) {
46126
46181
  result = HugeintConvertInteger<int16_t>(value);
@@ -46720,6 +46775,7 @@ DUCKDB_API DatePartSpecifier GetDatePartSpecifier(const string &specifier);
46720
46775
 
46721
46776
 
46722
46777
 
46778
+
46723
46779
  namespace duckdb {
46724
46780
 
46725
46781
  struct AddOperator {
@@ -46847,8 +46903,14 @@ dtime_t AddTimeOperator::Operation(interval_t left, dtime_t right);
46847
46903
 
46848
46904
 
46849
46905
 
46906
+
46850
46907
  namespace duckdb {
46851
46908
 
46909
+ struct interval_t;
46910
+ struct date_t;
46911
+ struct timestamp_t;
46912
+ struct dtime_t;
46913
+
46852
46914
  struct SubtractOperator {
46853
46915
  template <class TA, class TB, class TR>
46854
46916
  static inline TR Operation(TA left, TB right) {
@@ -57199,7 +57261,9 @@ static inline void ListLoopHash(Vector &input, Vector &hashes, const SelectionVe
57199
57261
  const auto child_count = ListVector::GetListSize(input);
57200
57262
 
57201
57263
  Vector child_hashes(LogicalType::HASH, child_count);
57202
- VectorOperations::Hash(child, child_hashes, child_count);
57264
+ if (child_count > 0) {
57265
+ VectorOperations::Hash(child, child_hashes, child_count);
57266
+ }
57203
57267
  auto chdata = FlatVector::GetData<hash_t>(child_hashes);
57204
57268
 
57205
57269
  // Reduce the number of entries to check to the non-empty ones
@@ -58599,11 +58663,13 @@ public:
58599
58663
  ColumnBindingResolver();
58600
58664
 
58601
58665
  void VisitOperator(LogicalOperator &op) override;
58666
+ static void Verify(LogicalOperator &op);
58602
58667
 
58603
58668
  protected:
58604
58669
  vector<ColumnBinding> bindings;
58605
58670
 
58606
58671
  unique_ptr<Expression> VisitReplace(BoundColumnRefExpression &expr, unique_ptr<Expression> *expr_ptr) override;
58672
+ static unordered_set<idx_t> VerifyInternal(LogicalOperator &op);
58607
58673
  };
58608
58674
  } // namespace duckdb
58609
58675
 
@@ -58945,6 +59011,35 @@ unique_ptr<Expression> ColumnBindingResolver::VisitReplace(BoundColumnRefExpress
58945
59011
  // LCOV_EXCL_STOP
58946
59012
  }
58947
59013
 
59014
+ unordered_set<idx_t> ColumnBindingResolver::VerifyInternal(LogicalOperator &op) {
59015
+ unordered_set<idx_t> result;
59016
+ for (auto &child : op.children) {
59017
+ auto child_indexes = VerifyInternal(*child);
59018
+ for (auto index : child_indexes) {
59019
+ D_ASSERT(index != DConstants::INVALID_INDEX);
59020
+ if (result.find(index) != result.end()) {
59021
+ throw InternalException("Duplicate table index \"%lld\" found", index);
59022
+ }
59023
+ result.insert(index);
59024
+ }
59025
+ }
59026
+ auto indexes = op.GetTableIndex();
59027
+ for (auto index : indexes) {
59028
+ D_ASSERT(index != DConstants::INVALID_INDEX);
59029
+ if (result.find(index) != result.end()) {
59030
+ throw InternalException("Duplicate table index \"%lld\" found", index);
59031
+ }
59032
+ result.insert(index);
59033
+ }
59034
+ return result;
59035
+ }
59036
+
59037
+ void ColumnBindingResolver::Verify(LogicalOperator &op) {
59038
+ #ifdef DEBUG
59039
+ VerifyInternal(op);
59040
+ #endif
59041
+ }
59042
+
58948
59043
  } // namespace duckdb
58949
59044
 
58950
59045
 
@@ -65144,6 +65239,44 @@ static void TemplatedMarkJoin(Vector &left, Vector &right, idx_t lcount, idx_t r
65144
65239
  }
65145
65240
  }
65146
65241
 
65242
+ static void MarkJoinNested(Vector &left, Vector &right, idx_t lcount, idx_t rcount, bool found_match[],
65243
+ ExpressionType comparison_type) {
65244
+ Vector left_reference(left.GetType());
65245
+ SelectionVector true_sel(rcount);
65246
+ for (idx_t i = 0; i < lcount; i++) {
65247
+ if (found_match[i]) {
65248
+ continue;
65249
+ }
65250
+ ConstantVector::Reference(left_reference, left, i, rcount);
65251
+ idx_t count;
65252
+ switch (comparison_type) {
65253
+ case ExpressionType::COMPARE_EQUAL:
65254
+ count = VectorOperations::Equals(left_reference, right, nullptr, rcount, nullptr, nullptr);
65255
+ break;
65256
+ case ExpressionType::COMPARE_NOTEQUAL:
65257
+ count = VectorOperations::NotEquals(left_reference, right, nullptr, rcount, nullptr, nullptr);
65258
+ break;
65259
+ case ExpressionType::COMPARE_LESSTHAN:
65260
+ count = VectorOperations::LessThan(left_reference, right, nullptr, rcount, nullptr, nullptr);
65261
+ break;
65262
+ case ExpressionType::COMPARE_GREATERTHAN:
65263
+ count = VectorOperations::GreaterThan(left_reference, right, nullptr, rcount, nullptr, nullptr);
65264
+ break;
65265
+ case ExpressionType::COMPARE_LESSTHANOREQUALTO:
65266
+ count = VectorOperations::LessThanEquals(left_reference, right, nullptr, rcount, nullptr, nullptr);
65267
+ break;
65268
+ case ExpressionType::COMPARE_GREATERTHANOREQUALTO:
65269
+ count = VectorOperations::GreaterThanEquals(left_reference, right, nullptr, rcount, nullptr, nullptr);
65270
+ break;
65271
+ default:
65272
+ throw InternalException("Unsupported comparison type for MarkJoinNested");
65273
+ }
65274
+ if (count > 0) {
65275
+ found_match[i] = true;
65276
+ }
65277
+ }
65278
+ }
65279
+
65147
65280
  template <class OP>
65148
65281
  static void MarkJoinSwitch(Vector &left, Vector &right, idx_t lcount, idx_t rcount, bool found_match[]) {
65149
65282
  switch (left.GetType().InternalType()) {
@@ -65179,6 +65312,13 @@ static void MarkJoinSwitch(Vector &left, Vector &right, idx_t lcount, idx_t rcou
65179
65312
 
65180
65313
  static void MarkJoinComparisonSwitch(Vector &left, Vector &right, idx_t lcount, idx_t rcount, bool found_match[],
65181
65314
  ExpressionType comparison_type) {
65315
+ switch (left.GetType().InternalType()) {
65316
+ case PhysicalType::STRUCT:
65317
+ case PhysicalType::LIST:
65318
+ return MarkJoinNested(left, right, lcount, rcount, found_match, comparison_type);
65319
+ default:
65320
+ break;
65321
+ }
65182
65322
  D_ASSERT(left.GetType() == right.GetType());
65183
65323
  switch (comparison_type) {
65184
65324
  case ExpressionType::COMPARE_EQUAL:
@@ -71209,6 +71349,7 @@ class LimitPercentOperatorState : public GlobalSourceState {
71209
71349
  public:
71210
71350
  explicit LimitPercentOperatorState(const PhysicalLimitPercent &op)
71211
71351
  : limit(DConstants::INVALID_INDEX), current_offset(0) {
71352
+ D_ASSERT(op.sink_state);
71212
71353
  auto &gstate = (LimitPercentGlobalState &)*op.sink_state;
71213
71354
  gstate.data.InitializeScan(scan_state);
71214
71355
  }
@@ -72230,7 +72371,12 @@ void PhysicalTransaction::GetData(ExecutionContext &context, DataChunk &chunk, G
72230
72371
  LocalSourceState &lstate) const {
72231
72372
  auto &client = context.client;
72232
72373
 
72233
- switch (info->type) {
72374
+ auto type = info->type;
72375
+ if (type == TransactionType::COMMIT && ValidChecker::IsInvalidated(client.ActiveTransaction())) {
72376
+ // transaction is invalidated - turn COMMIT into ROLLBACK
72377
+ type = TransactionType::ROLLBACK;
72378
+ }
72379
+ switch (type) {
72234
72380
  case TransactionType::BEGIN_TRANSACTION: {
72235
72381
  if (client.transaction.IsAutoCommit()) {
72236
72382
  // start the active transaction
@@ -72452,6 +72598,7 @@ public:
72452
72598
  public:
72453
72599
  bool EmptyResultIfRHSIsEmpty() const;
72454
72600
 
72601
+ static bool HasNullValues(DataChunk &chunk);
72455
72602
  static void ConstructSemiJoinResult(DataChunk &left, DataChunk &result, bool found_match[]);
72456
72603
  static void ConstructAntiJoinResult(DataChunk &left, DataChunk &result, bool found_match[]);
72457
72604
  static void ConstructMarkJoinResult(DataChunk &join_keys, DataChunk &left, DataChunk &result, bool found_match[],
@@ -76821,7 +76968,7 @@ public:
76821
76968
  return true;
76822
76969
  }
76823
76970
 
76824
- static bool IsSupported(const vector<JoinCondition> &conditions);
76971
+ static bool IsSupported(const vector<JoinCondition> &conditions, JoinType join_type);
76825
76972
 
76826
76973
  public:
76827
76974
  //! Returns a list of the types of the join conditions
@@ -76855,7 +77002,7 @@ PhysicalNestedLoopJoin::PhysicalNestedLoopJoin(LogicalOperator &op, unique_ptr<P
76855
77002
  children.push_back(move(right));
76856
77003
  }
76857
77004
 
76858
- static bool HasNullValues(DataChunk &chunk) {
77005
+ bool PhysicalJoin::HasNullValues(DataChunk &chunk) {
76859
77006
  for (idx_t col_idx = 0; col_idx < chunk.ColumnCount(); col_idx++) {
76860
77007
  UnifiedVectorFormat vdata;
76861
77008
  chunk.data[col_idx].ToUnifiedFormat(chunk.size(), vdata);
@@ -76944,7 +77091,10 @@ void PhysicalJoin::ConstructMarkJoinResult(DataChunk &join_keys, DataChunk &left
76944
77091
  }
76945
77092
  }
76946
77093
 
76947
- bool PhysicalNestedLoopJoin::IsSupported(const vector<JoinCondition> &conditions) {
77094
+ bool PhysicalNestedLoopJoin::IsSupported(const vector<JoinCondition> &conditions, JoinType join_type) {
77095
+ if (join_type == JoinType::MARK) {
77096
+ return true;
77097
+ }
76948
77098
  for (auto &cond : conditions) {
76949
77099
  if (cond.left->return_type.InternalType() == PhysicalType::STRUCT ||
76950
77100
  cond.left->return_type.InternalType() == PhysicalType::LIST) {
@@ -76988,7 +77138,7 @@ public:
76988
77138
  //! Materialized join condition of the RHS
76989
77139
  ColumnDataCollection right_condition_data;
76990
77140
  //! Whether or not the RHS of the nested loop join has NULL values
76991
- bool has_null;
77141
+ atomic<bool> has_null;
76992
77142
  //! A bool indicating for each tuple in the RHS if they found a match (only used in FULL OUTER JOIN)
76993
77143
  OuterJoinMarker right_outer;
76994
77144
  };
@@ -82307,6 +82457,7 @@ public:
82307
82457
 
82308
82458
 
82309
82459
 
82460
+
82310
82461
  namespace duckdb {
82311
82462
 
82312
82463
  PhysicalBatchInsert::PhysicalBatchInsert(vector<LogicalType> types, TableCatalogEntry *table,
@@ -82349,22 +82500,9 @@ public:
82349
82500
  if (Empty()) {
82350
82501
  return nullptr;
82351
82502
  }
82352
- unique_ptr<RowGroupCollection> new_collection;
82353
- if (current_collections.size() == 1) {
82354
- // we have gathered only one row group collection: merge it directly
82355
- new_collection = move(current_collections[0]);
82356
- } else {
82503
+ unique_ptr<RowGroupCollection> new_collection = move(current_collections[0]);
82504
+ if (current_collections.size() > 1) {
82357
82505
  // we have gathered multiple collections: create one big collection and merge that
82358
- // find the biggest collection
82359
- idx_t biggest_index = 0;
82360
- for (idx_t i = 1; i < current_collections.size(); i++) {
82361
- D_ASSERT(current_collections[i]);
82362
- if (current_collections[i]->GetTotalRows() > current_collections[biggest_index]->GetTotalRows()) {
82363
- biggest_index = i;
82364
- }
82365
- }
82366
- // now append all the other collections to this collection
82367
- new_collection = move(current_collections[biggest_index]);
82368
82506
  auto &types = new_collection->GetTypes();
82369
82507
  TableAppendState append_state;
82370
82508
  new_collection->InitializeAppend(append_state);
@@ -85798,15 +85936,14 @@ void PhysicalRecursiveCTE::ExecuteRecursivePipelines(ExecutionContext &context)
85798
85936
  for (auto &pipeline : pipelines) {
85799
85937
  auto sink = pipeline->GetSink();
85800
85938
  if (sink != this) {
85801
- // reset the sink state for any intermediate sinks
85802
- sink->sink_state = sink->GetGlobalSinkState(context.client);
85939
+ sink->sink_state.reset();
85803
85940
  }
85804
85941
  for (auto &op : pipeline->GetOperators()) {
85805
85942
  if (op) {
85806
- op->op_state = op->GetGlobalOperatorState(context.client);
85943
+ op->op_state.reset();
85807
85944
  }
85808
85945
  }
85809
- pipeline->ResetSource(true);
85946
+ pipeline->ClearSource();
85810
85947
  }
85811
85948
 
85812
85949
  // get the MetaPipelines in the recursive_meta_pipeline and reschedule them
@@ -86781,6 +86918,7 @@ public:
86781
86918
  void Serialize(FieldWriter &writer) const override;
86782
86919
  static unique_ptr<LogicalOperator> Deserialize(LogicalDeserializationState &state, FieldReader &reader);
86783
86920
  idx_t EstimateCardinality(ClientContext &context) override;
86921
+ vector<idx_t> GetTableIndex() const override;
86784
86922
 
86785
86923
  protected:
86786
86924
  void ResolveTypes() override;
@@ -87063,6 +87201,7 @@ public:
87063
87201
 
87064
87202
  void Serialize(FieldWriter &writer) const override;
87065
87203
  static unique_ptr<LogicalOperator> Deserialize(LogicalDeserializationState &state, FieldReader &reader);
87204
+ vector<idx_t> GetTableIndex() const override;
87066
87205
 
87067
87206
  protected:
87068
87207
  void ResolveTypes() override {
@@ -87351,7 +87490,7 @@ unique_ptr<PhysicalOperator> PhysicalPlanGenerator::CreatePlan(LogicalComparison
87351
87490
  // range join: use piecewise merge join
87352
87491
  plan = make_unique<PhysicalPiecewiseMergeJoin>(op, move(left), move(right), move(op.conditions),
87353
87492
  op.join_type, op.estimated_cardinality);
87354
- } else if (PhysicalNestedLoopJoin::IsSupported(op.conditions)) {
87493
+ } else if (PhysicalNestedLoopJoin::IsSupported(op.conditions, op.join_type)) {
87355
87494
  // inequality join: use nested loop
87356
87495
  plan = make_unique<PhysicalNestedLoopJoin>(op, move(left), move(right), move(op.conditions), op.join_type,
87357
87496
  op.estimated_cardinality);
@@ -87575,7 +87714,6 @@ unique_ptr<PhysicalOperator> PhysicalPlanGenerator::CreatePlan(LogicalCreateInde
87575
87714
 
87576
87715
 
87577
87716
 
87578
-
87579
87717
  //===----------------------------------------------------------------------===//
87580
87718
  // DuckDB
87581
87719
  //
@@ -87617,25 +87755,10 @@ protected:
87617
87755
 
87618
87756
 
87619
87757
 
87620
- namespace duckdb {
87621
87758
 
87622
- static void ExtractDependencies(Expression &expr, unordered_set<CatalogEntry *> &dependencies) {
87623
- if (expr.type == ExpressionType::BOUND_FUNCTION) {
87624
- auto &function = (BoundFunctionExpression &)expr;
87625
- if (function.function.dependency) {
87626
- function.function.dependency(function, dependencies);
87627
- }
87628
- }
87629
- ExpressionIterator::EnumerateChildren(expr, [&](Expression &child) { ExtractDependencies(child, dependencies); });
87630
- }
87759
+ namespace duckdb {
87631
87760
 
87632
87761
  unique_ptr<PhysicalOperator> PhysicalPlanGenerator::CreatePlan(LogicalCreateTable &op) {
87633
- // extract dependencies from any default values
87634
- for (auto &default_value : op.info->bound_defaults) {
87635
- if (default_value) {
87636
- ExtractDependencies(*default_value, op.info->dependencies);
87637
- }
87638
- }
87639
87762
  auto &create_info = (CreateTableInfo &)*op.info->base;
87640
87763
  auto &catalog = Catalog::GetCatalog(context);
87641
87764
  auto existing_entry =
@@ -87734,8 +87857,9 @@ namespace duckdb {
87734
87857
 
87735
87858
  class LogicalDelete : public LogicalOperator {
87736
87859
  public:
87737
- explicit LogicalDelete(TableCatalogEntry *table)
87738
- : LogicalOperator(LogicalOperatorType::LOGICAL_DELETE), table(table), table_index(0), return_chunk(false) {
87860
+ explicit LogicalDelete(TableCatalogEntry *table, idx_t table_index)
87861
+ : LogicalOperator(LogicalOperatorType::LOGICAL_DELETE), table(table), table_index(table_index),
87862
+ return_chunk(false) {
87739
87863
  }
87740
87864
 
87741
87865
  TableCatalogEntry *table;
@@ -87746,6 +87870,7 @@ public:
87746
87870
  void Serialize(FieldWriter &writer) const override;
87747
87871
  static unique_ptr<LogicalOperator> Deserialize(LogicalDeserializationState &state, FieldReader &reader);
87748
87872
  idx_t EstimateCardinality(ClientContext &context) override;
87873
+ vector<idx_t> GetTableIndex() const override;
87749
87874
 
87750
87875
  protected:
87751
87876
  vector<ColumnBinding> GetColumnBindings() override {
@@ -87822,6 +87947,7 @@ public:
87822
87947
  }
87823
87948
  void Serialize(FieldWriter &writer) const override;
87824
87949
  static unique_ptr<LogicalOperator> Deserialize(LogicalDeserializationState &state, FieldReader &reader);
87950
+ vector<idx_t> GetTableIndex() const override;
87825
87951
 
87826
87952
  protected:
87827
87953
  void ResolveTypes() override {
@@ -88062,6 +88188,7 @@ public:
88062
88188
  }
88063
88189
  void Serialize(FieldWriter &writer) const override;
88064
88190
  static unique_ptr<LogicalOperator> Deserialize(LogicalDeserializationState &state, FieldReader &reader);
88191
+ vector<idx_t> GetTableIndex() const override;
88065
88192
 
88066
88193
  protected:
88067
88194
  void ResolveTypes() override {
@@ -88445,6 +88572,7 @@ public:
88445
88572
  idx_t EstimateCardinality(ClientContext &context) override {
88446
88573
  return expressions.size();
88447
88574
  }
88575
+ vector<idx_t> GetTableIndex() const override;
88448
88576
 
88449
88577
  protected:
88450
88578
  void ResolveTypes() override {
@@ -89116,8 +89244,9 @@ namespace duckdb {
89116
89244
  //! LogicalInsert represents an insertion of data into a base table
89117
89245
  class LogicalInsert : public LogicalOperator {
89118
89246
  public:
89119
- explicit LogicalInsert(TableCatalogEntry *table)
89120
- : LogicalOperator(LogicalOperatorType::LOGICAL_INSERT), table(table), table_index(0), return_chunk(false) {
89247
+ LogicalInsert(TableCatalogEntry *table, idx_t table_index)
89248
+ : LogicalOperator(LogicalOperatorType::LOGICAL_INSERT), table(table), table_index(table_index),
89249
+ return_chunk(false) {
89121
89250
  }
89122
89251
 
89123
89252
  vector<vector<unique_ptr<Expression>>> insert_values;
@@ -89154,6 +89283,7 @@ protected:
89154
89283
  }
89155
89284
 
89156
89285
  idx_t EstimateCardinality(ClientContext &context) override;
89286
+ vector<idx_t> GetTableIndex() const override;
89157
89287
  };
89158
89288
  } // namespace duckdb
89159
89289
 
@@ -89562,6 +89692,7 @@ public:
89562
89692
  vector<ColumnBinding> GetColumnBindings() override;
89563
89693
  void Serialize(FieldWriter &writer) const override;
89564
89694
  static unique_ptr<LogicalOperator> Deserialize(LogicalDeserializationState &state, FieldReader &reader);
89695
+ vector<idx_t> GetTableIndex() const override;
89565
89696
 
89566
89697
  protected:
89567
89698
  void ResolveTypes() override;
@@ -89651,6 +89782,7 @@ public:
89651
89782
  }
89652
89783
  void Serialize(FieldWriter &writer) const override;
89653
89784
  static unique_ptr<LogicalOperator> Deserialize(LogicalDeserializationState &state, FieldReader &reader);
89785
+ vector<idx_t> GetTableIndex() const override;
89654
89786
 
89655
89787
  protected:
89656
89788
  void ResolveTypes() override {
@@ -89698,6 +89830,7 @@ public:
89698
89830
  }
89699
89831
  void Serialize(FieldWriter &writer) const override;
89700
89832
  static unique_ptr<LogicalOperator> Deserialize(LogicalDeserializationState &state, FieldReader &reader);
89833
+ vector<idx_t> GetTableIndex() const override;
89701
89834
 
89702
89835
  protected:
89703
89836
  void ResolveTypes() override {
@@ -89910,6 +90043,7 @@ public:
89910
90043
 
89911
90044
  void Serialize(FieldWriter &writer) const override;
89912
90045
  static unique_ptr<LogicalOperator> Deserialize(LogicalDeserializationState &state, FieldReader &reader);
90046
+ vector<idx_t> GetTableIndex() const override;
89913
90047
 
89914
90048
  protected:
89915
90049
  void ResolveTypes() override {
@@ -90214,6 +90348,7 @@ public:
90214
90348
  vector<ColumnBinding> GetColumnBindings() override;
90215
90349
  void Serialize(FieldWriter &writer) const override;
90216
90350
  static unique_ptr<LogicalOperator> Deserialize(LogicalDeserializationState &state, FieldReader &reader);
90351
+ vector<idx_t> GetTableIndex() const override;
90217
90352
 
90218
90353
  protected:
90219
90354
  void ResolveTypes() override;
@@ -90341,6 +90476,7 @@ public:
90341
90476
  vector<ColumnBinding> GetColumnBindings() override;
90342
90477
  void Serialize(FieldWriter &writer) const override;
90343
90478
  static unique_ptr<LogicalOperator> Deserialize(LogicalDeserializationState &state, FieldReader &reader);
90479
+ vector<idx_t> GetTableIndex() const override;
90344
90480
 
90345
90481
  protected:
90346
90482
  void ResolveTypes() override;
@@ -96950,7 +97086,8 @@ struct Interpolator {
96950
97086
  template <>
96951
97087
  struct Interpolator<true> {
96952
97088
  Interpolator(const double q, const idx_t n_p)
96953
- : n(n_p), RN((double)(n_p - 1) * q), FRN(floor(RN)), CRN(FRN), begin(0), end(n_p) {
97089
+ : n(n_p), RN((double)(n_p * q)), FRN(MaxValue<idx_t>(1, n_p - floor(n_p - RN)) - 1), CRN(FRN), begin(0),
97090
+ end(n_p) {
96954
97091
  }
96955
97092
 
96956
97093
  template <class INPUT_TYPE, class TARGET_TYPE, typename ACCESSOR = QuantileDirect<INPUT_TYPE>>
@@ -99792,10 +99929,11 @@ struct RegrCountFunction {
99792
99929
  namespace duckdb {
99793
99930
 
99794
99931
  void RegrCountFun::RegisterFunction(BuiltinFunctions &set) {
99795
- AggregateFunctionSet corr("regr_count");
99796
- corr.AddFunction(AggregateFunction::BinaryAggregate<size_t, double, double, uint32_t, RegrCountFunction>(
99797
- LogicalType::DOUBLE, LogicalType::DOUBLE, LogicalType::UINTEGER));
99798
- set.AddFunction(corr);
99932
+ auto regr_count = AggregateFunction::BinaryAggregate<size_t, double, double, uint32_t, RegrCountFunction>(
99933
+ LogicalType::DOUBLE, LogicalType::DOUBLE, LogicalType::UINTEGER);
99934
+ regr_count.name = "regr_count";
99935
+ regr_count.null_handling = FunctionNullHandling::SPECIAL_HANDLING;
99936
+ set.AddFunction(regr_count);
99799
99937
  }
99800
99938
 
99801
99939
  } // namespace duckdb
@@ -108901,15 +109039,16 @@ string StrTimeFormat::ParseFormatSpecifier(const string &format_string, StrTimeF
108901
109039
  }
108902
109040
 
108903
109041
  struct StrfTimeBindData : public FunctionData {
108904
- explicit StrfTimeBindData(StrfTimeFormat format_p, string format_string_p)
108905
- : format(move(format_p)), format_string(move(format_string_p)) {
109042
+ explicit StrfTimeBindData(StrfTimeFormat format_p, string format_string_p, bool is_null)
109043
+ : format(move(format_p)), format_string(move(format_string_p)), is_null(is_null) {
108906
109044
  }
108907
109045
 
108908
109046
  StrfTimeFormat format;
108909
109047
  string format_string;
109048
+ bool is_null;
108910
109049
 
108911
109050
  unique_ptr<FunctionData> Copy() const override {
108912
- return make_unique<StrfTimeBindData>(format, format_string);
109051
+ return make_unique<StrfTimeBindData>(format, format_string, is_null);
108913
109052
  }
108914
109053
 
108915
109054
  bool Equals(const FunctionData &other_p) const override {
@@ -108932,13 +109071,14 @@ static unique_ptr<FunctionData> StrfTimeBindFunction(ClientContext &context, Sca
108932
109071
  Value options_str = ExpressionExecutor::EvaluateScalar(context, *format_arg);
108933
109072
  auto format_string = options_str.GetValue<string>();
108934
109073
  StrfTimeFormat format;
108935
- if (!options_str.IsNull()) {
109074
+ bool is_null = options_str.IsNull();
109075
+ if (!is_null) {
108936
109076
  string error = StrTimeFormat::ParseFormatSpecifier(format_string, format);
108937
109077
  if (!error.empty()) {
108938
109078
  throw InvalidInputException("Failed to parse format specifier %s: %s", format_string, error);
108939
109079
  }
108940
109080
  }
108941
- return make_unique<StrfTimeBindData>(format, format_string);
109081
+ return make_unique<StrfTimeBindData>(format, format_string, is_null);
108942
109082
  }
108943
109083
 
108944
109084
  void StrfTimeFormat::ConvertDateVector(Vector &input, Vector &result, idx_t count) {
@@ -108965,7 +109105,7 @@ static void StrfTimeFunctionDate(DataChunk &args, ExpressionState &state, Vector
108965
109105
  auto &func_expr = (BoundFunctionExpression &)state.expr;
108966
109106
  auto &info = (StrfTimeBindData &)*func_expr.bind_info;
108967
109107
 
108968
- if (ConstantVector::IsNull(args.data[REVERSED ? 0 : 1])) {
109108
+ if (info.is_null) {
108969
109109
  result.SetVectorType(VectorType::CONSTANT_VECTOR);
108970
109110
  ConstantVector::SetNull(result, true);
108971
109111
  return;
@@ -108999,7 +109139,7 @@ static void StrfTimeFunctionTimestamp(DataChunk &args, ExpressionState &state, V
108999
109139
  auto &func_expr = (BoundFunctionExpression &)state.expr;
109000
109140
  auto &info = (StrfTimeBindData &)*func_expr.bind_info;
109001
109141
 
109002
- if (ConstantVector::IsNull(args.data[REVERSED ? 0 : 1])) {
109142
+ if (info.is_null) {
109003
109143
  result.SetVectorType(VectorType::CONSTANT_VECTOR);
109004
109144
  ConstantVector::SetNull(result, true);
109005
109145
  return;
@@ -122497,7 +122637,9 @@ static unique_ptr<FunctionData> StructInsertBind(ClientContext &context, ScalarF
122497
122637
  unique_ptr<BaseStatistics> StructInsertStats(ClientContext &context, FunctionStatisticsInput &input) {
122498
122638
  auto &child_stats = input.child_stats;
122499
122639
  auto &expr = input.expr;
122500
-
122640
+ if (child_stats.empty() || !child_stats[0]) {
122641
+ return nullptr;
122642
+ }
122501
122643
  auto &existing_struct_stats = (StructStatistics &)*child_stats[0];
122502
122644
  auto new_struct_stats = make_unique<StructStatistics>(expr.return_type);
122503
122645
 
@@ -131298,14 +131440,16 @@ void UDFWrapper::RegisterAggrFunction(AggregateFunction aggr_function, ClientCon
131298
131440
 
131299
131441
 
131300
131442
 
131443
+
131301
131444
  namespace duckdb {
131302
131445
 
131303
- BaseAppender::BaseAppender(Allocator &allocator) : allocator(allocator), column(0) {
131446
+ BaseAppender::BaseAppender(Allocator &allocator, AppenderType type_p)
131447
+ : allocator(allocator), column(0), appender_type(type_p) {
131304
131448
  }
131305
131449
 
131306
- BaseAppender::BaseAppender(Allocator &allocator_p, vector<LogicalType> types_p)
131450
+ BaseAppender::BaseAppender(Allocator &allocator_p, vector<LogicalType> types_p, AppenderType type_p)
131307
131451
  : allocator(allocator_p), types(move(types_p)), collection(make_unique<ColumnDataCollection>(allocator, types)),
131308
- column(0) {
131452
+ column(0), appender_type(type_p) {
131309
131453
  InitializeChunk();
131310
131454
  }
131311
131455
 
@@ -131325,7 +131469,8 @@ void BaseAppender::Destructor() {
131325
131469
  }
131326
131470
 
131327
131471
  InternalAppender::InternalAppender(ClientContext &context_p, TableCatalogEntry &table_p)
131328
- : BaseAppender(Allocator::DefaultAllocator(), table_p.GetTypes()), context(context_p), table(table_p) {
131472
+ : BaseAppender(Allocator::DefaultAllocator(), table_p.GetTypes(), AppenderType::PHYSICAL), context(context_p),
131473
+ table(table_p) {
131329
131474
  }
131330
131475
 
131331
131476
  InternalAppender::~InternalAppender() {
@@ -131333,7 +131478,7 @@ InternalAppender::~InternalAppender() {
131333
131478
  }
131334
131479
 
131335
131480
  Appender::Appender(Connection &con, const string &schema_name, const string &table_name)
131336
- : BaseAppender(Allocator::DefaultAllocator()), context(con.context) {
131481
+ : BaseAppender(Allocator::DefaultAllocator(), AppenderType::LOGICAL), context(con.context) {
131337
131482
  description = con.TableInfo(schema_name, table_name);
131338
131483
  if (!description) {
131339
131484
  // table could not be found
@@ -131377,6 +131522,27 @@ void BaseAppender::AppendValueInternal(Vector &col, SRC input) {
131377
131522
  FlatVector::GetData<DST>(col)[chunk.size()] = Cast::Operation<SRC, DST>(input);
131378
131523
  }
131379
131524
 
131525
+ template <class SRC, class DST>
131526
+ void BaseAppender::AppendDecimalValueInternal(Vector &col, SRC input) {
131527
+ switch (appender_type) {
131528
+ case AppenderType::LOGICAL: {
131529
+ auto &type = col.GetType();
131530
+ D_ASSERT(type.id() == LogicalTypeId::DECIMAL);
131531
+ auto width = DecimalType::GetWidth(type);
131532
+ auto scale = DecimalType::GetScale(type);
131533
+ TryCastToDecimal::Operation<SRC, DST>(input, FlatVector::GetData<DST>(col)[chunk.size()], nullptr, width,
131534
+ scale);
131535
+ return;
131536
+ }
131537
+ case AppenderType::PHYSICAL: {
131538
+ AppendValueInternal<SRC, DST>(col, input);
131539
+ return;
131540
+ }
131541
+ default:
131542
+ throw InternalException("Type not implemented for AppenderType");
131543
+ }
131544
+ }
131545
+
131380
131546
  template <class T>
131381
131547
  void BaseAppender::AppendValueInternal(T input) {
131382
131548
  if (column >= types.size()) {
@@ -131422,18 +131588,20 @@ void BaseAppender::AppendValueInternal(T input) {
131422
131588
  break;
131423
131589
  case LogicalTypeId::DECIMAL:
131424
131590
  switch (col.GetType().InternalType()) {
131425
- case PhysicalType::INT8:
131426
- AppendValueInternal<T, int8_t>(col, input);
131427
- break;
131428
131591
  case PhysicalType::INT16:
131429
- AppendValueInternal<T, int16_t>(col, input);
131592
+ AppendDecimalValueInternal<T, int16_t>(col, input);
131430
131593
  break;
131431
131594
  case PhysicalType::INT32:
131432
- AppendValueInternal<T, int32_t>(col, input);
131595
+ AppendDecimalValueInternal<T, int32_t>(col, input);
131433
131596
  break;
131434
- default:
131435
- AppendValueInternal<T, int64_t>(col, input);
131597
+ case PhysicalType::INT64:
131598
+ AppendDecimalValueInternal<T, int64_t>(col, input);
131599
+ break;
131600
+ case PhysicalType::INT128:
131601
+ AppendDecimalValueInternal<T, hugeint_t>(col, input);
131436
131602
  break;
131603
+ default:
131604
+ throw InternalException("Internal type not recognized for Decimal");
131437
131605
  }
131438
131606
  break;
131439
131607
  case LogicalTypeId::DATE:
@@ -135291,6 +135459,10 @@ public:
135291
135459
 
135292
135460
  private:
135293
135461
  void RunOptimizer(OptimizerType type, const std::function<void()> &callback);
135462
+ void Verify(LogicalOperator &op);
135463
+
135464
+ private:
135465
+ unique_ptr<LogicalOperator> plan;
135294
135466
  };
135295
135467
 
135296
135468
  } // namespace duckdb
@@ -135995,6 +136167,7 @@ unique_ptr<LogicalOperator> ClientContext::ExtractPlan(const string &query) {
135995
136167
  }
135996
136168
 
135997
136169
  ColumnBindingResolver resolver;
136170
+ resolver.Verify(*plan);
135998
136171
  resolver.VisitOperator(*plan);
135999
136172
 
136000
136173
  plan->ResolveOperatorTypes();
@@ -138304,12 +138477,19 @@ public:
138304
138477
  //! Creates and caches a new DB Instance (Fails if a cached instance already exists)
138305
138478
  shared_ptr<DuckDB> CreateInstance(const string &database, DBConfig &config_dict, bool cache_instance = true);
138306
138479
 
138480
+ //! Creates and caches a new DB Instance (Fails if a cached instance already exists)
138481
+ shared_ptr<DuckDB> GetOrCreateInstance(const string &database, DBConfig &config_dict, bool cache_instance);
138482
+
138307
138483
  private:
138308
138484
  //! A map with the cached instances <absolute_path/instance>
138309
138485
  unordered_map<string, weak_ptr<DuckDB>> db_instances;
138310
138486
 
138311
138487
  //! Lock to alter cache
138312
138488
  mutex cache_lock;
138489
+
138490
+ private:
138491
+ shared_ptr<DuckDB> GetInstanceInternal(const string &database, const DBConfig &config_dict);
138492
+ shared_ptr<DuckDB> CreateInstanceInternal(const string &database, DBConfig &config_dict, bool cache_instance);
138313
138493
  };
138314
138494
  } // namespace duckdb
138315
138495
 
@@ -138330,8 +138510,7 @@ string GetDBAbsolutePath(const string &database) {
138330
138510
  return FileSystem::JoinPath(FileSystem::GetWorkingDirectory(), database);
138331
138511
  }
138332
138512
 
138333
- shared_ptr<DuckDB> DBInstanceCache::GetInstance(const string &database, const DBConfig &config) {
138334
- lock_guard<mutex> l(cache_lock);
138513
+ shared_ptr<DuckDB> DBInstanceCache::GetInstanceInternal(const string &database, const DBConfig &config) {
138335
138514
  shared_ptr<DuckDB> db_instance;
138336
138515
  auto abs_database_path = GetDBAbsolutePath(database);
138337
138516
  if (db_instances.find(abs_database_path) != db_instances.end()) {
@@ -138350,8 +138529,13 @@ shared_ptr<DuckDB> DBInstanceCache::GetInstance(const string &database, const DB
138350
138529
  return db_instance;
138351
138530
  }
138352
138531
 
138353
- shared_ptr<DuckDB> DBInstanceCache::CreateInstance(const string &database, DBConfig &config, bool cache_instance) {
138532
+ shared_ptr<DuckDB> DBInstanceCache::GetInstance(const string &database, const DBConfig &config) {
138354
138533
  lock_guard<mutex> l(cache_lock);
138534
+ return GetInstanceInternal(database, config);
138535
+ }
138536
+
138537
+ shared_ptr<DuckDB> DBInstanceCache::CreateInstanceInternal(const string &database, DBConfig &config,
138538
+ bool cache_instance) {
138355
138539
  auto abs_database_path = GetDBAbsolutePath(database);
138356
138540
  if (db_instances.find(abs_database_path) != db_instances.end()) {
138357
138541
  throw duckdb::Exception(ExceptionType::CONNECTION,
@@ -138369,6 +138553,23 @@ shared_ptr<DuckDB> DBInstanceCache::CreateInstance(const string &database, DBCon
138369
138553
  return db_instance;
138370
138554
  }
138371
138555
 
138556
+ shared_ptr<DuckDB> DBInstanceCache::CreateInstance(const string &database, DBConfig &config, bool cache_instance) {
138557
+ lock_guard<mutex> l(cache_lock);
138558
+ return CreateInstanceInternal(database, config, cache_instance);
138559
+ }
138560
+
138561
+ shared_ptr<DuckDB> DBInstanceCache::GetOrCreateInstance(const string &database, DBConfig &config_dict,
138562
+ bool cache_instance) {
138563
+ lock_guard<mutex> l(cache_lock);
138564
+ if (cache_instance) {
138565
+ auto instance = GetInstanceInternal(database, config_dict);
138566
+ if (instance) {
138567
+ return instance;
138568
+ }
138569
+ }
138570
+ return CreateInstanceInternal(database, config_dict, cache_instance);
138571
+ }
138572
+
138372
138573
  } // namespace duckdb
138373
138574
 
138374
138575
 
@@ -153723,7 +153924,8 @@ bool Deliminator::RemoveInequalityCandidate(unique_ptr<LogicalOperator> *plan, u
153723
153924
  }
153724
153925
  parent_expr =
153725
153926
  make_unique<BoundColumnRefExpression>(parent_expr->alias, parent_expr->return_type, it->first);
153726
- parent_cond.comparison = child_cond.comparison;
153927
+ parent_cond.comparison =
153928
+ parent_delim_get_side == 0 ? child_cond.comparison : FlipComparisionExpression(child_cond.comparison);
153727
153929
  break;
153728
153930
  }
153729
153931
  }
@@ -154182,6 +154384,9 @@ idx_t FilterCombiner::GetEquivalenceSet(Expression *expr) {
154182
154384
 
154183
154385
  FilterResult FilterCombiner::AddConstantComparison(vector<ExpressionValueInformation> &info_list,
154184
154386
  ExpressionValueInformation info) {
154387
+ if (info.constant.IsNull()) {
154388
+ return FilterResult::UNSATISFIABLE;
154389
+ }
154185
154390
  for (idx_t i = 0; i < info_list.size(); i++) {
154186
154391
  auto comparison = CompareValueInformation(info_list[i], info);
154187
154392
  switch (comparison) {
@@ -155444,7 +155649,6 @@ unique_ptr<LogicalOperator> FilterPullup::PullupJoin(unique_ptr<LogicalOperator>
155444
155649
  case JoinType::LEFT:
155445
155650
  case JoinType::ANTI:
155446
155651
  case JoinType::SEMI: {
155447
- can_add_column = true;
155448
155652
  return PullupFromLeft(move(op));
155449
155653
  }
155450
155654
  default:
@@ -155647,7 +155851,7 @@ unique_ptr<LogicalOperator> FilterPushdown::PushdownJoin(unique_ptr<LogicalOpera
155647
155851
  void FilterPushdown::PushFilters() {
155648
155852
  for (auto &f : filters) {
155649
155853
  auto result = combiner.AddFilter(move(f->filter));
155650
- D_ASSERT(result == FilterResult::SUCCESS);
155854
+ D_ASSERT(result != FilterResult::UNSUPPORTED);
155651
155855
  (void)result;
155652
155856
  }
155653
155857
  filters.clear();
@@ -157844,6 +158048,7 @@ public:
157844
158048
 
157845
158049
 
157846
158050
 
158051
+
157847
158052
  namespace duckdb {
157848
158053
 
157849
158054
  Optimizer::Optimizer(Binder &binder, ClientContext &context) : context(context), binder(binder), rewriter(context) {
@@ -157880,9 +158085,18 @@ void Optimizer::RunOptimizer(OptimizerType type, const std::function<void()> &ca
157880
158085
  profiler.StartPhase(OptimizerTypeToString(type));
157881
158086
  callback();
157882
158087
  profiler.EndPhase();
158088
+ if (plan) {
158089
+ Verify(*plan);
158090
+ }
158091
+ }
158092
+
158093
+ void Optimizer::Verify(LogicalOperator &op) {
158094
+ ColumnBindingResolver::Verify(op);
157883
158095
  }
157884
158096
 
157885
- unique_ptr<LogicalOperator> Optimizer::Optimize(unique_ptr<LogicalOperator> plan) {
158097
+ unique_ptr<LogicalOperator> Optimizer::Optimize(unique_ptr<LogicalOperator> plan_p) {
158098
+ Verify(*plan_p);
158099
+ this->plan = move(plan_p);
157886
158100
  // first we perform expression rewrites using the ExpressionRewriter
157887
158101
  // this does not change the logical plan structure, but only simplifies the expression trees
157888
158102
  RunOptimizer(OptimizerType::EXPRESSION_REWRITER, [&]() { rewriter.VisitOperator(*plan); });
@@ -157969,7 +158183,7 @@ unique_ptr<LogicalOperator> Optimizer::Optimize(unique_ptr<LogicalOperator> plan
157969
158183
 
157970
158184
  Planner::VerifyPlan(context, plan);
157971
158185
 
157972
- return plan;
158186
+ return move(plan);
157973
158187
  }
157974
158188
 
157975
158189
  } // namespace duckdb
@@ -157982,6 +158196,8 @@ unique_ptr<LogicalOperator> FilterPullup::PullupBothSide(unique_ptr<LogicalOpera
157982
158196
  FilterPullup right_pullup(true, can_add_column);
157983
158197
  op->children[0] = left_pullup.Rewrite(move(op->children[0]));
157984
158198
  op->children[1] = right_pullup.Rewrite(move(op->children[1]));
158199
+ D_ASSERT(left_pullup.can_add_column == can_add_column);
158200
+ D_ASSERT(right_pullup.can_add_column == can_add_column);
157985
158201
 
157986
158202
  // merging filter expressions
157987
158203
  for (idx_t i = 0; i < right_pullup.filters_expr_pullup.size(); ++i) {
@@ -158006,7 +158222,8 @@ namespace duckdb {
158006
158222
  unique_ptr<LogicalOperator> FilterPullup::PullupFilter(unique_ptr<LogicalOperator> op) {
158007
158223
  D_ASSERT(op->type == LogicalOperatorType::LOGICAL_FILTER);
158008
158224
 
158009
- if (can_pullup) {
158225
+ auto &filter = (LogicalFilter &)*op;
158226
+ if (can_pullup && filter.projection_map.empty()) {
158010
158227
  unique_ptr<LogicalOperator> child = move(op->children[0]);
158011
158228
  child = Rewrite(move(child));
158012
158229
  // moving filter's expressions
@@ -158313,6 +158530,9 @@ using Filter = FilterPushdown::Filter;
158313
158530
  unique_ptr<LogicalOperator> FilterPushdown::PushdownFilter(unique_ptr<LogicalOperator> op) {
158314
158531
  D_ASSERT(op->type == LogicalOperatorType::LOGICAL_FILTER);
158315
158532
  auto &filter = (LogicalFilter &)*op;
158533
+ if (!filter.projection_map.empty()) {
158534
+ return FinishPushdown(move(op));
158535
+ }
158316
158536
  // filter: gather the filters and remove the filter from the set of operations
158317
158537
  for (auto &expression : filter.expressions) {
158318
158538
  if (AddFilter(move(expression)) == FilterResult::UNSATISFIABLE) {
@@ -158616,8 +158836,8 @@ unique_ptr<LogicalOperator> FilterPushdown::PushdownMarkJoin(unique_ptr<LogicalO
158616
158836
 
158617
158837
  right_bindings.insert(comp_join.mark_index);
158618
158838
  FilterPushdown left_pushdown(optimizer), right_pushdown(optimizer);
158619
- #ifndef NDEBUG
158620
- bool found_mark_reference = false;
158839
+ #ifdef DEBUG
158840
+ bool simplified_mark_join = false;
158621
158841
  #endif
158622
158842
  // now check the set of filters
158623
158843
  for (idx_t i = 0; i < filters.size(); i++) {
@@ -158629,15 +158849,16 @@ unique_ptr<LogicalOperator> FilterPushdown::PushdownMarkJoin(unique_ptr<LogicalO
158629
158849
  filters.erase(filters.begin() + i);
158630
158850
  i--;
158631
158851
  } else if (side == JoinSide::RIGHT) {
158632
- // there can only be at most one filter referencing the marker
158633
- #ifndef NDEBUG
158634
- D_ASSERT(!found_mark_reference);
158635
- found_mark_reference = true;
158852
+ #ifdef DEBUG
158853
+ D_ASSERT(!simplified_mark_join);
158636
158854
  #endif
158637
158855
  // this filter references the marker
158638
158856
  // we can turn this into a SEMI join if the filter is on only the marker
158639
158857
  if (filters[i]->filter->type == ExpressionType::BOUND_COLUMN_REF) {
158640
158858
  // filter just references the marker: turn into semi join
158859
+ #ifdef DEBUG
158860
+ simplified_mark_join = true;
158861
+ #endif
158641
158862
  join.join_type = JoinType::SEMI;
158642
158863
  filters.erase(filters.begin() + i);
158643
158864
  i--;
@@ -158660,6 +158881,9 @@ unique_ptr<LogicalOperator> FilterPushdown::PushdownMarkJoin(unique_ptr<LogicalO
158660
158881
  }
158661
158882
  }
158662
158883
  if (all_null_values_are_equal) {
158884
+ #ifdef DEBUG
158885
+ simplified_mark_join = true;
158886
+ #endif
158663
158887
  // all null values are equal, convert to ANTI join
158664
158888
  join.join_type = JoinType::ANTI;
158665
158889
  filters.erase(filters.begin() + i);
@@ -158764,6 +158988,9 @@ unique_ptr<LogicalOperator> FilterPushdown::PushdownSetOperation(unique_ptr<Logi
158764
158988
  D_ASSERT(op->children.size() == 2);
158765
158989
  auto left_bindings = op->children[0]->GetColumnBindings();
158766
158990
  auto right_bindings = op->children[1]->GetColumnBindings();
158991
+ if (left_bindings.size() != right_bindings.size()) {
158992
+ throw InternalException("Filter pushdown - set operation LHS and RHS have incompatible counts");
158993
+ }
158767
158994
 
158768
158995
  // pushdown into set operation, we can duplicate the condition and pushdown the expressions into both sides
158769
158996
  FilterPushdown left_pushdown(optimizer), right_pushdown(optimizer);
@@ -159419,7 +159646,8 @@ unique_ptr<Expression> ComparisonSimplificationRule::Apply(LogicalOperator &op,
159419
159646
  }
159420
159647
 
159421
159648
  // Is the constant cast invertible?
159422
- if (!BoundCastExpression::CastIsInvertible(cast_expression->return_type, target_type)) {
159649
+ if (!cast_constant.IsNull() &&
159650
+ !BoundCastExpression::CastIsInvertible(cast_expression->return_type, target_type)) {
159423
159651
  // Is it actually invertible?
159424
159652
  Value uncast_constant;
159425
159653
  if (!cast_constant.DefaultTryCastAs(constant_value.type(), uncast_constant, &error_message, true) ||
@@ -182824,6 +183052,7 @@ bool Transformer::TransformGroupBy(duckdb_libpgquery::PGList *group, SelectNode
182824
183052
 
182825
183053
 
182826
183054
 
183055
+
182827
183056
  namespace duckdb {
182828
183057
 
182829
183058
  bool Transformer::TransformOrderBy(duckdb_libpgquery::PGList *order, vector<OrderByNode> &result) {
@@ -182857,6 +183086,13 @@ bool Transformer::TransformOrderBy(duckdb_libpgquery::PGList *order, vector<Orde
182857
183086
  throw NotImplementedException("Unimplemented order by type");
182858
183087
  }
182859
183088
  auto order_expression = TransformExpression(target);
183089
+ if (order_expression->GetExpressionClass() == ExpressionClass::STAR) {
183090
+ auto &star_expr = (StarExpression &)*order_expression;
183091
+ D_ASSERT(star_expr.relation_name.empty());
183092
+ if (star_expr.columns) {
183093
+ throw ParserException("COLUMNS expr is not supported in ORDER BY");
183094
+ }
183095
+ }
182860
183096
  result.emplace_back(type, null_order, move(order_expression));
182861
183097
  } else {
182862
183098
  throw NotImplementedException("ORDER BY list member type %d\n", temp->type);
@@ -188411,13 +188647,14 @@ protected:
188411
188647
 
188412
188648
 
188413
188649
 
188650
+
188414
188651
  namespace duckdb {
188415
188652
 
188416
188653
  //! The HAVING binder is responsible for binding an expression within the HAVING clause of a SQL statement
188417
188654
  class HavingBinder : public SelectBinder {
188418
188655
  public:
188419
188656
  HavingBinder(Binder &binder, ClientContext &context, BoundSelectNode &node, BoundGroupInformation &info,
188420
- case_insensitive_map_t<idx_t> &alias_map);
188657
+ case_insensitive_map_t<idx_t> &alias_map, AggregateHandling aggregate_handling);
188421
188658
 
188422
188659
  protected:
188423
188660
  BindResult BindExpression(unique_ptr<ParsedExpression> *expr_ptr, idx_t depth,
@@ -188427,6 +188664,7 @@ private:
188427
188664
  BindResult BindColumnRef(unique_ptr<ParsedExpression> *expr_ptr, idx_t depth, bool root_expression);
188428
188665
 
188429
188666
  ColumnAliasBinder column_alias_binder;
188667
+ AggregateHandling aggregate_handling;
188430
188668
  };
188431
188669
 
188432
188670
  } // namespace duckdb
@@ -188557,6 +188795,9 @@ unique_ptr<Expression> Binder::BindDelimiter(ClientContext &context, OrderBinder
188557
188795
  delimiter_value = ExpressionExecutor::EvaluateScalar(context, *expr).CastAs(context, type);
188558
188796
  return nullptr;
188559
188797
  }
188798
+ if (!new_binder->correlated_columns.empty()) {
188799
+ throw BinderException("Correlated columns not supported in LIMIT/OFFSET");
188800
+ }
188560
188801
  // move any correlated columns to this binder
188561
188802
  MoveCorrelatedExpressions(*new_binder);
188562
188803
  return expr;
@@ -188922,16 +189163,22 @@ unique_ptr<BoundQueryNode> Binder::BindNode(SelectNode &statement) {
188922
189163
 
188923
189164
  // bind the HAVING clause, if any
188924
189165
  if (statement.having) {
188925
- HavingBinder having_binder(*this, context, *result, info, alias_map);
189166
+ HavingBinder having_binder(*this, context, *result, info, alias_map, statement.aggregate_handling);
188926
189167
  ExpressionBinder::QualifyColumnNames(*this, statement.having);
188927
189168
  result->having = having_binder.Bind(statement.having);
188928
189169
  }
188929
189170
 
188930
189171
  // bind the QUALIFY clause, if any
188931
189172
  if (statement.qualify) {
189173
+ if (statement.aggregate_handling == AggregateHandling::FORCE_AGGREGATES) {
189174
+ throw BinderException("Combining QUALIFY with GROUP BY ALL is not supported yet");
189175
+ }
188932
189176
  QualifyBinder qualify_binder(*this, context, *result, info, alias_map);
188933
189177
  ExpressionBinder::QualifyColumnNames(*this, statement.qualify);
188934
189178
  result->qualify = qualify_binder.Bind(statement.qualify);
189179
+ if (qualify_binder.HasBoundColumns() && qualify_binder.BoundAggregates()) {
189180
+ throw BinderException("Cannot mix aggregates with non-aggregated columns!");
189181
+ }
188935
189182
  }
188936
189183
 
188937
189184
  // after that, we bind to the SELECT list
@@ -190134,7 +190381,7 @@ unique_ptr<Expression> Binder::PlanSubquery(BoundSubqueryExpression &expr, uniqu
190134
190381
  D_ASSERT(root);
190135
190382
  // first we translate the QueryNode of the subquery into a logical plan
190136
190383
  // note that we do not plan nested subqueries yet
190137
- auto sub_binder = Binder::CreateBinder(context);
190384
+ auto sub_binder = Binder::CreateBinder(context, this);
190138
190385
  sub_binder->plan_subquery = false;
190139
190386
  auto subquery_root = sub_binder->CreatePlan(*expr.subquery);
190140
190387
  D_ASSERT(subquery_root);
@@ -190350,8 +190597,8 @@ BoundStatement Binder::BindCopyFrom(CopyStatement &stmt) {
190350
190597
 
190351
190598
  auto function_data =
190352
190599
  copy_function->function.copy_from_bind(context, *stmt.info, expected_names, bound_insert.expected_types);
190353
- auto get = make_unique<LogicalGet>(0, copy_function->function.copy_from_function, move(function_data),
190354
- bound_insert.expected_types, expected_names);
190600
+ auto get = make_unique<LogicalGet>(GenerateTableIndex(), copy_function->function.copy_from_function,
190601
+ move(function_data), bound_insert.expected_types, expected_names);
190355
190602
  for (idx_t i = 0; i < bound_insert.expected_types.size(); i++) {
190356
190603
  get->column_ids.push_back(i);
190357
190604
  }
@@ -191030,6 +191277,7 @@ protected:
191030
191277
 
191031
191278
 
191032
191279
 
191280
+
191033
191281
  #include <algorithm>
191034
191282
 
191035
191283
  namespace duckdb {
@@ -191230,6 +191478,31 @@ void Binder::BindDefaultValues(ColumnList &columns, vector<unique_ptr<Expression
191230
191478
  }
191231
191479
  }
191232
191480
 
191481
+ static void ExtractExpressionDependencies(Expression &expr, unordered_set<CatalogEntry *> &dependencies) {
191482
+ if (expr.type == ExpressionType::BOUND_FUNCTION) {
191483
+ auto &function = (BoundFunctionExpression &)expr;
191484
+ if (function.function.dependency) {
191485
+ function.function.dependency(function, dependencies);
191486
+ }
191487
+ }
191488
+ ExpressionIterator::EnumerateChildren(
191489
+ expr, [&](Expression &child) { ExtractExpressionDependencies(child, dependencies); });
191490
+ }
191491
+
191492
+ static void ExtractDependencies(BoundCreateTableInfo &info) {
191493
+ for (auto &default_value : info.bound_defaults) {
191494
+ if (default_value) {
191495
+ ExtractExpressionDependencies(*default_value, info.dependencies);
191496
+ }
191497
+ }
191498
+ for (auto &constraint : info.bound_constraints) {
191499
+ if (constraint->type == ConstraintType::CHECK) {
191500
+ auto &bound_check = (BoundCheckConstraint &)*constraint;
191501
+ ExtractExpressionDependencies(*bound_check.expression, info.dependencies);
191502
+ }
191503
+ }
191504
+ }
191505
+
191233
191506
  unique_ptr<BoundCreateTableInfo> Binder::BindCreateTableInfo(unique_ptr<CreateInfo> info) {
191234
191507
  auto &base = (CreateTableInfo &)*info;
191235
191508
 
@@ -191260,6 +191533,8 @@ unique_ptr<BoundCreateTableInfo> Binder::BindCreateTableInfo(unique_ptr<CreateIn
191260
191533
  // bind the default values
191261
191534
  BindDefaultValues(base.columns, result->bound_defaults);
191262
191535
  }
191536
+ // extract dependencies from any default values or CHECK constraints
191537
+ ExtractDependencies(*result);
191263
191538
 
191264
191539
  if (base.columns.PhysicalColumnCount() == 0) {
191265
191540
  throw BinderException("Creating a table without physical (non-generated) columns is not supported");
@@ -191353,7 +191628,8 @@ BoundStatement Binder::Bind(DeleteStatement &stmt) {
191353
191628
  unique_ptr<LogicalOperator> child_operator;
191354
191629
  for (auto &using_clause : stmt.using_clauses) {
191355
191630
  // bind the using clause
191356
- auto bound_node = Bind(*using_clause);
191631
+ auto using_binder = Binder::CreateBinder(context, this);
191632
+ auto bound_node = using_binder->Bind(*using_clause);
191357
191633
  auto op = CreatePlan(*bound_node);
191358
191634
  if (child_operator) {
191359
191635
  // already bound a child: create a cross product to unify the two
@@ -191361,6 +191637,7 @@ BoundStatement Binder::Bind(DeleteStatement &stmt) {
191361
191637
  } else {
191362
191638
  child_operator = move(op);
191363
191639
  }
191640
+ bind_context.AddContext(move(using_binder->bind_context));
191364
191641
  }
191365
191642
  if (child_operator) {
191366
191643
  root = LogicalCrossProduct::Create(move(root), move(child_operator));
@@ -191379,7 +191656,7 @@ BoundStatement Binder::Bind(DeleteStatement &stmt) {
191379
191656
  root = move(filter);
191380
191657
  }
191381
191658
  // create the delete node
191382
- auto del = make_unique<LogicalDelete>(table);
191659
+ auto del = make_unique<LogicalDelete>(table, GenerateTableIndex());
191383
191660
  del->AddChild(move(root));
191384
191661
 
191385
191662
  // set up the delete expression
@@ -191507,6 +191784,7 @@ BoundStatement Binder::Bind(ExecuteStatement &stmt) {
191507
191784
  prepared = prepared_planner.PrepareSQLStatement(entry->second->unbound_statement->Copy());
191508
191785
  rebound_plan = move(prepared_planner.plan);
191509
191786
  D_ASSERT(prepared->properties.bound_all_parameters);
191787
+ this->bound_tables = prepared_planner.binder->bound_tables;
191510
191788
  }
191511
191789
  // copy the properties of the prepared statement into the planner
191512
191790
  this->properties = prepared->properties;
@@ -191728,7 +192006,7 @@ BoundStatement Binder::Bind(ExportStatement &stmt) {
191728
192006
  CopyStatement copy_stmt;
191729
192007
  copy_stmt.info = move(info);
191730
192008
 
191731
- auto copy_binder = Binder::CreateBinder(context);
192009
+ auto copy_binder = Binder::CreateBinder(context, this);
191732
192010
  auto bound_statement = copy_binder->Bind(copy_stmt);
191733
192011
  if (child_operator) {
191734
192012
  // use UNION ALL to combine the individual copy statements into a single node
@@ -191858,7 +192136,7 @@ BoundStatement Binder::Bind(InsertStatement &stmt) {
191858
192136
  properties.read_only = false;
191859
192137
  }
191860
192138
 
191861
- auto insert = make_unique<LogicalInsert>(table);
192139
+ auto insert = make_unique<LogicalInsert>(table, GenerateTableIndex());
191862
192140
 
191863
192141
  // Add CTEs as bindable
191864
192142
  AddCTEMap(stmt.cte_map);
@@ -192062,6 +192340,7 @@ namespace duckdb {
192062
192340
  BoundStatement Binder::Bind(PrepareStatement &stmt) {
192063
192341
  Planner prepared_planner(context);
192064
192342
  auto prepared_data = prepared_planner.PrepareSQLStatement(move(stmt.statement));
192343
+ this->bound_tables = prepared_planner.binder->bound_tables;
192065
192344
 
192066
192345
  auto prepare = make_unique<LogicalPrepare>(stmt.name, move(prepared_data), move(prepared_planner.plan));
192067
192346
  // we can prepare in read-only mode: prepared statements are not written to the catalog
@@ -192188,7 +192467,7 @@ BoundStatement Binder::Bind(AlterStatement &stmt) {
192188
192467
 
192189
192468
  BoundStatement Binder::Bind(TransactionStatement &stmt) {
192190
192469
  // transaction statements do not require a valid transaction
192191
- properties.requires_valid_transaction = false;
192470
+ properties.requires_valid_transaction = stmt.info->type == TransactionType::BEGIN_TRANSACTION;
192192
192471
 
192193
192472
  BoundStatement result;
192194
192473
  result.names = {"Success"};
@@ -192482,6 +192761,13 @@ static void BindUpdateConstraints(TableCatalogEntry &table, LogicalGet &get, Log
192482
192761
  BindExtraColumns(table, get, proj, update, check.bound_columns);
192483
192762
  }
192484
192763
  }
192764
+ if (update.return_chunk) {
192765
+ physical_index_set_t all_columns;
192766
+ for (idx_t i = 0; i < table.storage->column_definitions.size(); i++) {
192767
+ all_columns.insert(PhysicalIndex(i));
192768
+ }
192769
+ BindExtraColumns(table, get, proj, update, all_columns);
192770
+ }
192485
192771
  // for index updates we always turn any update into an insert and a delete
192486
192772
  // we thus need all the columns to be available, hence we check if the update touches any index columns
192487
192773
  // If the returning keyword is used, we need access to the whole row in case the user requests it.
@@ -192504,7 +192790,7 @@ static void BindUpdateConstraints(TableCatalogEntry &table, LogicalGet &get, Log
192504
192790
  }
192505
192791
  }
192506
192792
 
192507
- if (update.update_is_del_and_insert || update.return_chunk) {
192793
+ if (update.update_is_del_and_insert) {
192508
192794
  // the update updates a column required by an index or requires returning the updated rows,
192509
192795
  // push projections for all columns
192510
192796
  physical_index_set_t all_columns;
@@ -192615,16 +192901,15 @@ BoundStatement Binder::Bind(UpdateStatement &stmt) {
192615
192901
  // set the projection as child of the update node and finalize the result
192616
192902
  update->AddChild(move(proj));
192617
192903
 
192904
+ auto update_table_index = GenerateTableIndex();
192905
+ update->table_index = update_table_index;
192618
192906
  if (!stmt.returning_list.empty()) {
192619
- auto update_table_index = GenerateTableIndex();
192620
- update->table_index = update_table_index;
192621
192907
  unique_ptr<LogicalOperator> update_as_logicaloperator = move(update);
192622
192908
 
192623
192909
  return BindReturning(move(stmt.returning_list), table, update_table_index, move(update_as_logicaloperator),
192624
192910
  move(result));
192625
192911
  }
192626
192912
 
192627
- update->table_index = 0;
192628
192913
  result.names = {"Count"};
192629
192914
  result.types = {LogicalType::BIGINT};
192630
192915
  result.plan = move(update);
@@ -192950,6 +193235,9 @@ unique_ptr<BoundTableRef> Binder::Bind(BaseTableRef &ref) {
192950
193235
  // bind the child subquery
192951
193236
  view_binder->AddBoundView(view_catalog_entry);
192952
193237
  auto bound_child = view_binder->Bind(subquery);
193238
+ if (!view_binder->correlated_columns.empty()) {
193239
+ throw BinderException("Contents of view were altered - view bound correlated columns");
193240
+ }
192953
193241
 
192954
193242
  D_ASSERT(bound_child->type == TableReferenceType::SUBQUERY);
192955
193243
  // verify that the types and names match up with the expected types and names
@@ -196500,8 +196788,9 @@ BindResult GroupBinder::BindColumnRef(ColumnRefExpression &colref) {
196500
196788
  namespace duckdb {
196501
196789
 
196502
196790
  HavingBinder::HavingBinder(Binder &binder, ClientContext &context, BoundSelectNode &node, BoundGroupInformation &info,
196503
- case_insensitive_map_t<idx_t> &alias_map)
196504
- : SelectBinder(binder, context, node, info), column_alias_binder(node, alias_map) {
196791
+ case_insensitive_map_t<idx_t> &alias_map, AggregateHandling aggregate_handling)
196792
+ : SelectBinder(binder, context, node, info), column_alias_binder(node, alias_map),
196793
+ aggregate_handling(aggregate_handling) {
196505
196794
  target_type = LogicalType(LogicalTypeId::BOOLEAN);
196506
196795
  }
196507
196796
 
@@ -196511,7 +196800,16 @@ BindResult HavingBinder::BindColumnRef(unique_ptr<ParsedExpression> *expr_ptr, i
196511
196800
  if (!alias_result.HasError()) {
196512
196801
  return alias_result;
196513
196802
  }
196514
-
196803
+ if (aggregate_handling == AggregateHandling::FORCE_AGGREGATES) {
196804
+ auto expr = duckdb::SelectBinder::BindExpression(expr_ptr, depth);
196805
+ if (expr.HasError()) {
196806
+ return expr;
196807
+ }
196808
+ auto group_ref = make_unique<BoundColumnRefExpression>(
196809
+ expr.expression->return_type, ColumnBinding(node.group_index, node.groups.group_expressions.size()));
196810
+ node.groups.group_expressions.push_back(move(expr.expression));
196811
+ return BindResult(move(group_ref));
196812
+ }
196515
196813
  return BindResult(StringUtil::Format(
196516
196814
  "column %s must appear in the GROUP BY clause or be used in an aggregate function", expr.ToString()));
196517
196815
  }
@@ -198270,6 +198568,10 @@ unique_ptr<LogicalOperator> LogicalOperator::Deserialize(Deserializer &deseriali
198270
198568
  return result;
198271
198569
  }
198272
198570
 
198571
+ vector<idx_t> LogicalOperator::GetTableIndex() const {
198572
+ return vector<idx_t> {};
198573
+ }
198574
+
198273
198575
  unique_ptr<LogicalOperator> LogicalOperator::Copy(ClientContext &context) const {
198274
198576
  BufferedSerializer logical_op_serializer;
198275
198577
  try {
@@ -198661,6 +198963,14 @@ idx_t LogicalAggregate::EstimateCardinality(ClientContext &context) {
198661
198963
  return LogicalOperator::EstimateCardinality(context);
198662
198964
  }
198663
198965
 
198966
+ vector<idx_t> LogicalAggregate::GetTableIndex() const {
198967
+ vector<idx_t> result {group_index, aggregate_index};
198968
+ if (groupings_index != DConstants::INVALID_INDEX) {
198969
+ result.push_back(groupings_index);
198970
+ }
198971
+ return result;
198972
+ }
198973
+
198664
198974
  } // namespace duckdb
198665
198975
 
198666
198976
 
@@ -198727,6 +199037,10 @@ unique_ptr<LogicalOperator> LogicalColumnDataGet::Deserialize(LogicalDeserializa
198727
199037
  return make_unique<LogicalColumnDataGet>(table_index, move(chunk_types), move(collection));
198728
199038
  }
198729
199039
 
199040
+ vector<idx_t> LogicalColumnDataGet::GetTableIndex() const {
199041
+ return vector<idx_t> {table_index};
199042
+ }
199043
+
198730
199044
  } // namespace duckdb
198731
199045
 
198732
199046
 
@@ -198991,6 +199305,10 @@ unique_ptr<LogicalOperator> LogicalCTERef::Deserialize(LogicalDeserializationSta
198991
199305
  return make_unique<LogicalCTERef>(table_index, cte_index, chunk_types, bound_columns);
198992
199306
  }
198993
199307
 
199308
+ vector<idx_t> LogicalCTERef::GetTableIndex() const {
199309
+ return vector<idx_t> {table_index};
199310
+ }
199311
+
198994
199312
  } // namespace duckdb
198995
199313
 
198996
199314
 
@@ -199011,8 +199329,8 @@ unique_ptr<LogicalOperator> LogicalDelete::Deserialize(LogicalDeserializationSta
199011
199329
 
199012
199330
  TableCatalogEntry *table_catalog_entry = catalog.GetEntry<TableCatalogEntry>(context, info->schema, info->table);
199013
199331
 
199014
- auto result = make_unique<LogicalDelete>(table_catalog_entry);
199015
- result->table_index = reader.ReadRequired<idx_t>();
199332
+ auto table_index = reader.ReadRequired<idx_t>();
199333
+ auto result = make_unique<LogicalDelete>(table_catalog_entry, table_index);
199016
199334
  result->return_chunk = reader.ReadRequired<bool>();
199017
199335
  return move(result);
199018
199336
  }
@@ -199021,6 +199339,10 @@ idx_t LogicalDelete::EstimateCardinality(ClientContext &context) {
199021
199339
  return return_chunk ? LogicalOperator::EstimateCardinality(context) : 1;
199022
199340
  }
199023
199341
 
199342
+ vector<idx_t> LogicalDelete::GetTableIndex() const {
199343
+ return vector<idx_t> {table_index};
199344
+ }
199345
+
199024
199346
  } // namespace duckdb
199025
199347
 
199026
199348
 
@@ -199038,6 +199360,10 @@ unique_ptr<LogicalOperator> LogicalDelimGet::Deserialize(LogicalDeserializationS
199038
199360
  return make_unique<LogicalDelimGet>(table_index, chunk_types);
199039
199361
  }
199040
199362
 
199363
+ vector<idx_t> LogicalDelimGet::GetTableIndex() const {
199364
+ return vector<idx_t> {table_index};
199365
+ }
199366
+
199041
199367
  } // namespace duckdb
199042
199368
 
199043
199369
 
@@ -199105,6 +199431,10 @@ unique_ptr<LogicalOperator> LogicalDummyScan::Deserialize(LogicalDeserialization
199105
199431
  return make_unique<LogicalDummyScan>(table_index);
199106
199432
  }
199107
199433
 
199434
+ vector<idx_t> LogicalDummyScan::GetTableIndex() const {
199435
+ return vector<idx_t> {table_index};
199436
+ }
199437
+
199108
199438
  } // namespace duckdb
199109
199439
 
199110
199440
 
@@ -199213,6 +199543,10 @@ unique_ptr<LogicalOperator> LogicalExpressionGet::Deserialize(LogicalDeserializa
199213
199543
  return make_unique<LogicalExpressionGet>(table_index, expr_types, move(expressions));
199214
199544
  }
199215
199545
 
199546
+ vector<idx_t> LogicalExpressionGet::GetTableIndex() const {
199547
+ return vector<idx_t> {table_index};
199548
+ }
199549
+
199216
199550
  } // namespace duckdb
199217
199551
 
199218
199552
 
@@ -199450,6 +199784,10 @@ unique_ptr<LogicalOperator> LogicalGet::Deserialize(LogicalDeserializationState
199450
199784
  return move(result);
199451
199785
  }
199452
199786
 
199787
+ vector<idx_t> LogicalGet::GetTableIndex() const {
199788
+ return vector<idx_t> {table_index};
199789
+ }
199790
+
199453
199791
  } // namespace duckdb
199454
199792
 
199455
199793
 
@@ -199495,10 +199833,9 @@ unique_ptr<LogicalOperator> LogicalInsert::Deserialize(LogicalDeserializationSta
199495
199833
  throw InternalException("Cant find catalog entry for table %s", info->table);
199496
199834
  }
199497
199835
 
199498
- auto result = make_unique<LogicalInsert>(table_catalog_entry);
199836
+ auto result = make_unique<LogicalInsert>(table_catalog_entry, table_index);
199499
199837
  result->type = state.type;
199500
199838
  result->table = table_catalog_entry;
199501
- result->table_index = table_index;
199502
199839
  result->return_chunk = return_chunk;
199503
199840
  result->insert_values = move(insert_values);
199504
199841
  result->column_index_map = column_index_map;
@@ -199511,6 +199848,10 @@ idx_t LogicalInsert::EstimateCardinality(ClientContext &context) {
199511
199848
  return return_chunk ? LogicalOperator::EstimateCardinality(context) : 1;
199512
199849
  }
199513
199850
 
199851
+ vector<idx_t> LogicalInsert::GetTableIndex() const {
199852
+ return vector<idx_t> {table_index};
199853
+ }
199854
+
199514
199855
  } // namespace duckdb
199515
199856
 
199516
199857
 
@@ -199747,6 +200088,10 @@ unique_ptr<LogicalOperator> LogicalProjection::Deserialize(LogicalDeserializatio
199747
200088
  return make_unique<LogicalProjection>(table_index, move(expressions));
199748
200089
  }
199749
200090
 
200091
+ vector<idx_t> LogicalProjection::GetTableIndex() const {
200092
+ return vector<idx_t> {table_index};
200093
+ }
200094
+
199750
200095
  } // namespace duckdb
199751
200096
 
199752
200097
 
@@ -199767,6 +200112,10 @@ unique_ptr<LogicalOperator> LogicalRecursiveCTE::Deserialize(LogicalDeserializat
199767
200112
  return unique_ptr<LogicalRecursiveCTE>(new LogicalRecursiveCTE(table_index, column_count, union_all, state.type));
199768
200113
  }
199769
200114
 
200115
+ vector<idx_t> LogicalRecursiveCTE::GetTableIndex() const {
200116
+ return vector<idx_t> {table_index};
200117
+ }
200118
+
199770
200119
  } // namespace duckdb
199771
200120
 
199772
200121
 
@@ -199785,7 +200134,12 @@ vector<ColumnBinding> LogicalSample::GetColumnBindings() {
199785
200134
  idx_t LogicalSample::EstimateCardinality(ClientContext &context) {
199786
200135
  auto child_cardinality = children[0]->EstimateCardinality(context);
199787
200136
  if (sample_options->is_percentage) {
199788
- return idx_t(child_cardinality * sample_options->sample_size.GetValue<double>());
200137
+ double sample_cardinality =
200138
+ double(child_cardinality) * (sample_options->sample_size.GetValue<double>() / 100.0);
200139
+ if (sample_cardinality > double(child_cardinality)) {
200140
+ return child_cardinality;
200141
+ }
200142
+ return idx_t(sample_cardinality);
199789
200143
  } else {
199790
200144
  auto sample_size = sample_options->sample_size.GetValue<uint64_t>();
199791
200145
  if (sample_size < child_cardinality) {
@@ -199849,6 +200203,11 @@ unique_ptr<LogicalOperator> LogicalSetOperation::Deserialize(LogicalDeserializat
199849
200203
  // TODO(stephwang): review if unique_ptr<LogicalOperator> plan is needed
199850
200204
  return unique_ptr<LogicalSetOperation>(new LogicalSetOperation(table_index, column_count, state.type));
199851
200205
  }
200206
+
200207
+ vector<idx_t> LogicalSetOperation::GetTableIndex() const {
200208
+ return vector<idx_t> {table_index};
200209
+ }
200210
+
199852
200211
  } // namespace duckdb
199853
200212
 
199854
200213
 
@@ -199947,6 +200306,11 @@ unique_ptr<LogicalOperator> LogicalUnnest::Deserialize(LogicalDeserializationSta
199947
200306
  result->expressions = move(expressions);
199948
200307
  return move(result);
199949
200308
  }
200309
+
200310
+ vector<idx_t> LogicalUnnest::GetTableIndex() const {
200311
+ return vector<idx_t> {unnest_index};
200312
+ }
200313
+
199950
200314
  } // namespace duckdb
199951
200315
 
199952
200316
 
@@ -200021,6 +200385,10 @@ unique_ptr<LogicalOperator> LogicalWindow::Deserialize(LogicalDeserializationSta
200021
200385
  return move(result);
200022
200386
  }
200023
200387
 
200388
+ vector<idx_t> LogicalWindow::GetTableIndex() const {
200389
+ return vector<idx_t> {window_index};
200390
+ }
200391
+
200024
200392
  } // namespace duckdb
200025
200393
 
200026
200394
 
@@ -200554,8 +200922,10 @@ unique_ptr<LogicalOperator> FlattenDependentJoins::PushDownDependentJoinInternal
200554
200922
  // now create the duplicate eliminated scan for this node
200555
200923
  auto delim_index = binder.GenerateTableIndex();
200556
200924
  this->base_binding = ColumnBinding(delim_index, 0);
200925
+ this->delim_offset = 0;
200926
+ this->data_offset = 0;
200557
200927
  auto delim_scan = make_unique<LogicalDelimGet>(delim_index, delim_types);
200558
- return LogicalCrossProduct::Create(move(delim_scan), move(plan));
200928
+ return LogicalCrossProduct::Create(move(plan), move(delim_scan));
200559
200929
  }
200560
200930
  switch (plan->type) {
200561
200931
  case LogicalOperatorType::LOGICAL_UNNEST:
@@ -200919,8 +201289,19 @@ unique_ptr<LogicalOperator> FlattenDependentJoins::PushDownDependentJoinInternal
200919
201289
  case LogicalOperatorType::LOGICAL_UNION: {
200920
201290
  auto &setop = (LogicalSetOperation &)*plan;
200921
201291
  // set operator, push into both children
201292
+ #ifdef DEBUG
201293
+ plan->children[0]->ResolveOperatorTypes();
201294
+ plan->children[1]->ResolveOperatorTypes();
201295
+ D_ASSERT(plan->children[0]->types == plan->children[1]->types);
201296
+ #endif
200922
201297
  plan->children[0] = PushDownDependentJoin(move(plan->children[0]));
200923
201298
  plan->children[1] = PushDownDependentJoin(move(plan->children[1]));
201299
+ #ifdef DEBUG
201300
+ D_ASSERT(plan->children[0]->GetColumnBindings().size() == plan->children[1]->GetColumnBindings().size());
201301
+ plan->children[0]->ResolveOperatorTypes();
201302
+ plan->children[1]->ResolveOperatorTypes();
201303
+ D_ASSERT(plan->children[0]->types == plan->children[1]->types);
201304
+ #endif
200924
201305
  // we have to refer to the setop index now
200925
201306
  base_binding.table_index = setop.table_index;
200926
201307
  base_binding.column_index = setop.column_count;
@@ -213317,7 +213698,12 @@ bool DataTable::AppendToIndexes(TableIndexList &indexes, DataChunk &chunk, row_t
213317
213698
  bool append_failed = false;
213318
213699
  // now append the entries to the indices
213319
213700
  indexes.Scan([&](Index &index) {
213320
- if (!index.Append(chunk, row_identifiers)) {
213701
+ try {
213702
+ if (!index.Append(chunk, row_identifiers)) {
213703
+ append_failed = true;
213704
+ return true;
213705
+ }
213706
+ } catch (...) {
213321
213707
  append_failed = true;
213322
213708
  return true;
213323
213709
  }
@@ -213331,7 +213717,6 @@ bool DataTable::AppendToIndexes(TableIndexList &indexes, DataChunk &chunk, row_t
213331
213717
  for (auto *index : already_appended) {
213332
213718
  index->Delete(chunk, row_identifiers);
213333
213719
  }
213334
-
213335
213720
  return false;
213336
213721
  }
213337
213722
  return true;
@@ -213974,12 +214359,21 @@ void LocalTableStorage::AppendToIndexes(Transaction &transaction, TableAppendSta
213974
214359
  append_state.current_row);
213975
214360
  }
213976
214361
  if (constraint_violated) {
214362
+ PreservedError error;
213977
214363
  // need to revert the append
213978
214364
  row_t current_row = append_state.row_start;
213979
214365
  // remove the data from the indexes, if there are any indexes
213980
214366
  row_groups->Scan(transaction, [&](DataChunk &chunk) -> bool {
213981
214367
  // append this chunk to the indexes of the table
213982
- table->RemoveFromIndexes(append_state, chunk, current_row);
214368
+ try {
214369
+ table->RemoveFromIndexes(append_state, chunk, current_row);
214370
+ } catch (Exception &ex) {
214371
+ error = PreservedError(ex);
214372
+ return false;
214373
+ } catch (std::exception &ex) {
214374
+ error = PreservedError(ex);
214375
+ return false;
214376
+ }
213983
214377
 
213984
214378
  current_row += chunk.size();
213985
214379
  if (current_row >= append_state.current_row) {
@@ -213991,6 +214385,9 @@ void LocalTableStorage::AppendToIndexes(Transaction &transaction, TableAppendSta
213991
214385
  if (append_to_table) {
213992
214386
  table->RevertAppendInternal(append_state.row_start, append_count);
213993
214387
  }
214388
+ if (error) {
214389
+ error.Throw();
214390
+ }
213994
214391
  throw ConstraintException("PRIMARY KEY or UNIQUE constraint violated: duplicated key");
213995
214392
  }
213996
214393
  }
@@ -214122,7 +214519,7 @@ void LocalStorage::InitializeAppend(LocalAppendState &state, DataTable *table) {
214122
214519
  void LocalStorage::Append(LocalAppendState &state, DataChunk &chunk) {
214123
214520
  // append to unique indices (if any)
214124
214521
  auto storage = state.storage;
214125
- idx_t base_id = MAX_ROW_ID + storage->row_groups->GetTotalRows();
214522
+ idx_t base_id = MAX_ROW_ID + storage->row_groups->GetTotalRows() + state.append_state.total_append_count;
214126
214523
  if (!DataTable::AppendToIndexes(storage->indexes, chunk, base_id)) {
214127
214524
  throw ConstraintException("PRIMARY KEY or UNIQUE constraint violated: duplicated key");
214128
214525
  }
@@ -223781,7 +224178,10 @@ void CleanupState::Flush() {
223781
224178
  Vector row_identifiers(LogicalType::ROW_TYPE, (data_ptr_t)row_numbers);
223782
224179
 
223783
224180
  // delete the tuples from all the indexes
223784
- current_table->RemoveFromIndexes(row_identifiers, count);
224181
+ try {
224182
+ current_table->RemoveFromIndexes(row_identifiers, count);
224183
+ } catch (...) {
224184
+ }
223785
224185
 
223786
224186
  count = 0;
223787
224187
  }