duckdb 0.6.1-dev15.0 → 0.6.1-dev151.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) {
@@ -155646,7 +155851,7 @@ unique_ptr<LogicalOperator> FilterPushdown::PushdownJoin(unique_ptr<LogicalOpera
155646
155851
  void FilterPushdown::PushFilters() {
155647
155852
  for (auto &f : filters) {
155648
155853
  auto result = combiner.AddFilter(move(f->filter));
155649
- D_ASSERT(result == FilterResult::SUCCESS);
155854
+ D_ASSERT(result != FilterResult::UNSUPPORTED);
155650
155855
  (void)result;
155651
155856
  }
155652
155857
  filters.clear();
@@ -157843,6 +158048,7 @@ public:
157843
158048
 
157844
158049
 
157845
158050
 
158051
+
157846
158052
  namespace duckdb {
157847
158053
 
157848
158054
  Optimizer::Optimizer(Binder &binder, ClientContext &context) : context(context), binder(binder), rewriter(context) {
@@ -157879,9 +158085,18 @@ void Optimizer::RunOptimizer(OptimizerType type, const std::function<void()> &ca
157879
158085
  profiler.StartPhase(OptimizerTypeToString(type));
157880
158086
  callback();
157881
158087
  profiler.EndPhase();
158088
+ if (plan) {
158089
+ Verify(*plan);
158090
+ }
157882
158091
  }
157883
158092
 
157884
- unique_ptr<LogicalOperator> Optimizer::Optimize(unique_ptr<LogicalOperator> plan) {
158093
+ void Optimizer::Verify(LogicalOperator &op) {
158094
+ ColumnBindingResolver::Verify(op);
158095
+ }
158096
+
158097
+ unique_ptr<LogicalOperator> Optimizer::Optimize(unique_ptr<LogicalOperator> plan_p) {
158098
+ Verify(*plan_p);
158099
+ this->plan = move(plan_p);
157885
158100
  // first we perform expression rewrites using the ExpressionRewriter
157886
158101
  // this does not change the logical plan structure, but only simplifies the expression trees
157887
158102
  RunOptimizer(OptimizerType::EXPRESSION_REWRITER, [&]() { rewriter.VisitOperator(*plan); });
@@ -157968,7 +158183,7 @@ unique_ptr<LogicalOperator> Optimizer::Optimize(unique_ptr<LogicalOperator> plan
157968
158183
 
157969
158184
  Planner::VerifyPlan(context, plan);
157970
158185
 
157971
- return plan;
158186
+ return move(plan);
157972
158187
  }
157973
158188
 
157974
158189
  } // namespace duckdb
@@ -158007,7 +158222,8 @@ namespace duckdb {
158007
158222
  unique_ptr<LogicalOperator> FilterPullup::PullupFilter(unique_ptr<LogicalOperator> op) {
158008
158223
  D_ASSERT(op->type == LogicalOperatorType::LOGICAL_FILTER);
158009
158224
 
158010
- if (can_pullup) {
158225
+ auto &filter = (LogicalFilter &)*op;
158226
+ if (can_pullup && filter.projection_map.empty()) {
158011
158227
  unique_ptr<LogicalOperator> child = move(op->children[0]);
158012
158228
  child = Rewrite(move(child));
158013
158229
  // moving filter's expressions
@@ -158314,6 +158530,9 @@ using Filter = FilterPushdown::Filter;
158314
158530
  unique_ptr<LogicalOperator> FilterPushdown::PushdownFilter(unique_ptr<LogicalOperator> op) {
158315
158531
  D_ASSERT(op->type == LogicalOperatorType::LOGICAL_FILTER);
158316
158532
  auto &filter = (LogicalFilter &)*op;
158533
+ if (!filter.projection_map.empty()) {
158534
+ return FinishPushdown(move(op));
158535
+ }
158317
158536
  // filter: gather the filters and remove the filter from the set of operations
158318
158537
  for (auto &expression : filter.expressions) {
158319
158538
  if (AddFilter(move(expression)) == FilterResult::UNSATISFIABLE) {
@@ -158617,8 +158836,8 @@ unique_ptr<LogicalOperator> FilterPushdown::PushdownMarkJoin(unique_ptr<LogicalO
158617
158836
 
158618
158837
  right_bindings.insert(comp_join.mark_index);
158619
158838
  FilterPushdown left_pushdown(optimizer), right_pushdown(optimizer);
158620
- #ifndef NDEBUG
158621
- bool found_mark_reference = false;
158839
+ #ifdef DEBUG
158840
+ bool simplified_mark_join = false;
158622
158841
  #endif
158623
158842
  // now check the set of filters
158624
158843
  for (idx_t i = 0; i < filters.size(); i++) {
@@ -158630,15 +158849,16 @@ unique_ptr<LogicalOperator> FilterPushdown::PushdownMarkJoin(unique_ptr<LogicalO
158630
158849
  filters.erase(filters.begin() + i);
158631
158850
  i--;
158632
158851
  } else if (side == JoinSide::RIGHT) {
158633
- // there can only be at most one filter referencing the marker
158634
- #ifndef NDEBUG
158635
- D_ASSERT(!found_mark_reference);
158636
- found_mark_reference = true;
158852
+ #ifdef DEBUG
158853
+ D_ASSERT(!simplified_mark_join);
158637
158854
  #endif
158638
158855
  // this filter references the marker
158639
158856
  // we can turn this into a SEMI join if the filter is on only the marker
158640
158857
  if (filters[i]->filter->type == ExpressionType::BOUND_COLUMN_REF) {
158641
158858
  // filter just references the marker: turn into semi join
158859
+ #ifdef DEBUG
158860
+ simplified_mark_join = true;
158861
+ #endif
158642
158862
  join.join_type = JoinType::SEMI;
158643
158863
  filters.erase(filters.begin() + i);
158644
158864
  i--;
@@ -158661,6 +158881,9 @@ unique_ptr<LogicalOperator> FilterPushdown::PushdownMarkJoin(unique_ptr<LogicalO
158661
158881
  }
158662
158882
  }
158663
158883
  if (all_null_values_are_equal) {
158884
+ #ifdef DEBUG
158885
+ simplified_mark_join = true;
158886
+ #endif
158664
158887
  // all null values are equal, convert to ANTI join
158665
158888
  join.join_type = JoinType::ANTI;
158666
158889
  filters.erase(filters.begin() + i);
@@ -159423,7 +159646,8 @@ unique_ptr<Expression> ComparisonSimplificationRule::Apply(LogicalOperator &op,
159423
159646
  }
159424
159647
 
159425
159648
  // Is the constant cast invertible?
159426
- if (!BoundCastExpression::CastIsInvertible(cast_expression->return_type, target_type)) {
159649
+ if (!cast_constant.IsNull() &&
159650
+ !BoundCastExpression::CastIsInvertible(cast_expression->return_type, target_type)) {
159427
159651
  // Is it actually invertible?
159428
159652
  Value uncast_constant;
159429
159653
  if (!cast_constant.DefaultTryCastAs(constant_value.type(), uncast_constant, &error_message, true) ||
@@ -182828,6 +183052,7 @@ bool Transformer::TransformGroupBy(duckdb_libpgquery::PGList *group, SelectNode
182828
183052
 
182829
183053
 
182830
183054
 
183055
+
182831
183056
  namespace duckdb {
182832
183057
 
182833
183058
  bool Transformer::TransformOrderBy(duckdb_libpgquery::PGList *order, vector<OrderByNode> &result) {
@@ -182861,6 +183086,13 @@ bool Transformer::TransformOrderBy(duckdb_libpgquery::PGList *order, vector<Orde
182861
183086
  throw NotImplementedException("Unimplemented order by type");
182862
183087
  }
182863
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
+ }
182864
183096
  result.emplace_back(type, null_order, move(order_expression));
182865
183097
  } else {
182866
183098
  throw NotImplementedException("ORDER BY list member type %d\n", temp->type);
@@ -188415,13 +188647,14 @@ protected:
188415
188647
 
188416
188648
 
188417
188649
 
188650
+
188418
188651
  namespace duckdb {
188419
188652
 
188420
188653
  //! The HAVING binder is responsible for binding an expression within the HAVING clause of a SQL statement
188421
188654
  class HavingBinder : public SelectBinder {
188422
188655
  public:
188423
188656
  HavingBinder(Binder &binder, ClientContext &context, BoundSelectNode &node, BoundGroupInformation &info,
188424
- case_insensitive_map_t<idx_t> &alias_map);
188657
+ case_insensitive_map_t<idx_t> &alias_map, AggregateHandling aggregate_handling);
188425
188658
 
188426
188659
  protected:
188427
188660
  BindResult BindExpression(unique_ptr<ParsedExpression> *expr_ptr, idx_t depth,
@@ -188431,6 +188664,7 @@ private:
188431
188664
  BindResult BindColumnRef(unique_ptr<ParsedExpression> *expr_ptr, idx_t depth, bool root_expression);
188432
188665
 
188433
188666
  ColumnAliasBinder column_alias_binder;
188667
+ AggregateHandling aggregate_handling;
188434
188668
  };
188435
188669
 
188436
188670
  } // namespace duckdb
@@ -188561,6 +188795,9 @@ unique_ptr<Expression> Binder::BindDelimiter(ClientContext &context, OrderBinder
188561
188795
  delimiter_value = ExpressionExecutor::EvaluateScalar(context, *expr).CastAs(context, type);
188562
188796
  return nullptr;
188563
188797
  }
188798
+ if (!new_binder->correlated_columns.empty()) {
188799
+ throw BinderException("Correlated columns not supported in LIMIT/OFFSET");
188800
+ }
188564
188801
  // move any correlated columns to this binder
188565
188802
  MoveCorrelatedExpressions(*new_binder);
188566
188803
  return expr;
@@ -188926,16 +189163,22 @@ unique_ptr<BoundQueryNode> Binder::BindNode(SelectNode &statement) {
188926
189163
 
188927
189164
  // bind the HAVING clause, if any
188928
189165
  if (statement.having) {
188929
- HavingBinder having_binder(*this, context, *result, info, alias_map);
189166
+ HavingBinder having_binder(*this, context, *result, info, alias_map, statement.aggregate_handling);
188930
189167
  ExpressionBinder::QualifyColumnNames(*this, statement.having);
188931
189168
  result->having = having_binder.Bind(statement.having);
188932
189169
  }
188933
189170
 
188934
189171
  // bind the QUALIFY clause, if any
188935
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
+ }
188936
189176
  QualifyBinder qualify_binder(*this, context, *result, info, alias_map);
188937
189177
  ExpressionBinder::QualifyColumnNames(*this, statement.qualify);
188938
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
+ }
188939
189182
  }
188940
189183
 
188941
189184
  // after that, we bind to the SELECT list
@@ -190138,7 +190381,7 @@ unique_ptr<Expression> Binder::PlanSubquery(BoundSubqueryExpression &expr, uniqu
190138
190381
  D_ASSERT(root);
190139
190382
  // first we translate the QueryNode of the subquery into a logical plan
190140
190383
  // note that we do not plan nested subqueries yet
190141
- auto sub_binder = Binder::CreateBinder(context);
190384
+ auto sub_binder = Binder::CreateBinder(context, this);
190142
190385
  sub_binder->plan_subquery = false;
190143
190386
  auto subquery_root = sub_binder->CreatePlan(*expr.subquery);
190144
190387
  D_ASSERT(subquery_root);
@@ -190354,8 +190597,8 @@ BoundStatement Binder::BindCopyFrom(CopyStatement &stmt) {
190354
190597
 
190355
190598
  auto function_data =
190356
190599
  copy_function->function.copy_from_bind(context, *stmt.info, expected_names, bound_insert.expected_types);
190357
- auto get = make_unique<LogicalGet>(0, copy_function->function.copy_from_function, move(function_data),
190358
- 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);
190359
190602
  for (idx_t i = 0; i < bound_insert.expected_types.size(); i++) {
190360
190603
  get->column_ids.push_back(i);
190361
190604
  }
@@ -191034,6 +191277,7 @@ protected:
191034
191277
 
191035
191278
 
191036
191279
 
191280
+
191037
191281
  #include <algorithm>
191038
191282
 
191039
191283
  namespace duckdb {
@@ -191234,6 +191478,31 @@ void Binder::BindDefaultValues(ColumnList &columns, vector<unique_ptr<Expression
191234
191478
  }
191235
191479
  }
191236
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
+
191237
191506
  unique_ptr<BoundCreateTableInfo> Binder::BindCreateTableInfo(unique_ptr<CreateInfo> info) {
191238
191507
  auto &base = (CreateTableInfo &)*info;
191239
191508
 
@@ -191264,6 +191533,8 @@ unique_ptr<BoundCreateTableInfo> Binder::BindCreateTableInfo(unique_ptr<CreateIn
191264
191533
  // bind the default values
191265
191534
  BindDefaultValues(base.columns, result->bound_defaults);
191266
191535
  }
191536
+ // extract dependencies from any default values or CHECK constraints
191537
+ ExtractDependencies(*result);
191267
191538
 
191268
191539
  if (base.columns.PhysicalColumnCount() == 0) {
191269
191540
  throw BinderException("Creating a table without physical (non-generated) columns is not supported");
@@ -191357,7 +191628,8 @@ BoundStatement Binder::Bind(DeleteStatement &stmt) {
191357
191628
  unique_ptr<LogicalOperator> child_operator;
191358
191629
  for (auto &using_clause : stmt.using_clauses) {
191359
191630
  // bind the using clause
191360
- auto bound_node = Bind(*using_clause);
191631
+ auto using_binder = Binder::CreateBinder(context, this);
191632
+ auto bound_node = using_binder->Bind(*using_clause);
191361
191633
  auto op = CreatePlan(*bound_node);
191362
191634
  if (child_operator) {
191363
191635
  // already bound a child: create a cross product to unify the two
@@ -191365,6 +191637,7 @@ BoundStatement Binder::Bind(DeleteStatement &stmt) {
191365
191637
  } else {
191366
191638
  child_operator = move(op);
191367
191639
  }
191640
+ bind_context.AddContext(move(using_binder->bind_context));
191368
191641
  }
191369
191642
  if (child_operator) {
191370
191643
  root = LogicalCrossProduct::Create(move(root), move(child_operator));
@@ -191383,7 +191656,7 @@ BoundStatement Binder::Bind(DeleteStatement &stmt) {
191383
191656
  root = move(filter);
191384
191657
  }
191385
191658
  // create the delete node
191386
- auto del = make_unique<LogicalDelete>(table);
191659
+ auto del = make_unique<LogicalDelete>(table, GenerateTableIndex());
191387
191660
  del->AddChild(move(root));
191388
191661
 
191389
191662
  // set up the delete expression
@@ -191511,6 +191784,7 @@ BoundStatement Binder::Bind(ExecuteStatement &stmt) {
191511
191784
  prepared = prepared_planner.PrepareSQLStatement(entry->second->unbound_statement->Copy());
191512
191785
  rebound_plan = move(prepared_planner.plan);
191513
191786
  D_ASSERT(prepared->properties.bound_all_parameters);
191787
+ this->bound_tables = prepared_planner.binder->bound_tables;
191514
191788
  }
191515
191789
  // copy the properties of the prepared statement into the planner
191516
191790
  this->properties = prepared->properties;
@@ -191732,7 +192006,7 @@ BoundStatement Binder::Bind(ExportStatement &stmt) {
191732
192006
  CopyStatement copy_stmt;
191733
192007
  copy_stmt.info = move(info);
191734
192008
 
191735
- auto copy_binder = Binder::CreateBinder(context);
192009
+ auto copy_binder = Binder::CreateBinder(context, this);
191736
192010
  auto bound_statement = copy_binder->Bind(copy_stmt);
191737
192011
  if (child_operator) {
191738
192012
  // use UNION ALL to combine the individual copy statements into a single node
@@ -191862,7 +192136,7 @@ BoundStatement Binder::Bind(InsertStatement &stmt) {
191862
192136
  properties.read_only = false;
191863
192137
  }
191864
192138
 
191865
- auto insert = make_unique<LogicalInsert>(table);
192139
+ auto insert = make_unique<LogicalInsert>(table, GenerateTableIndex());
191866
192140
 
191867
192141
  // Add CTEs as bindable
191868
192142
  AddCTEMap(stmt.cte_map);
@@ -192066,6 +192340,7 @@ namespace duckdb {
192066
192340
  BoundStatement Binder::Bind(PrepareStatement &stmt) {
192067
192341
  Planner prepared_planner(context);
192068
192342
  auto prepared_data = prepared_planner.PrepareSQLStatement(move(stmt.statement));
192343
+ this->bound_tables = prepared_planner.binder->bound_tables;
192069
192344
 
192070
192345
  auto prepare = make_unique<LogicalPrepare>(stmt.name, move(prepared_data), move(prepared_planner.plan));
192071
192346
  // we can prepare in read-only mode: prepared statements are not written to the catalog
@@ -192192,7 +192467,7 @@ BoundStatement Binder::Bind(AlterStatement &stmt) {
192192
192467
 
192193
192468
  BoundStatement Binder::Bind(TransactionStatement &stmt) {
192194
192469
  // transaction statements do not require a valid transaction
192195
- properties.requires_valid_transaction = false;
192470
+ properties.requires_valid_transaction = stmt.info->type == TransactionType::BEGIN_TRANSACTION;
192196
192471
 
192197
192472
  BoundStatement result;
192198
192473
  result.names = {"Success"};
@@ -192486,6 +192761,13 @@ static void BindUpdateConstraints(TableCatalogEntry &table, LogicalGet &get, Log
192486
192761
  BindExtraColumns(table, get, proj, update, check.bound_columns);
192487
192762
  }
192488
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
+ }
192489
192771
  // for index updates we always turn any update into an insert and a delete
192490
192772
  // we thus need all the columns to be available, hence we check if the update touches any index columns
192491
192773
  // If the returning keyword is used, we need access to the whole row in case the user requests it.
@@ -192508,7 +192790,7 @@ static void BindUpdateConstraints(TableCatalogEntry &table, LogicalGet &get, Log
192508
192790
  }
192509
192791
  }
192510
192792
 
192511
- if (update.update_is_del_and_insert || update.return_chunk) {
192793
+ if (update.update_is_del_and_insert) {
192512
192794
  // the update updates a column required by an index or requires returning the updated rows,
192513
192795
  // push projections for all columns
192514
192796
  physical_index_set_t all_columns;
@@ -192619,16 +192901,15 @@ BoundStatement Binder::Bind(UpdateStatement &stmt) {
192619
192901
  // set the projection as child of the update node and finalize the result
192620
192902
  update->AddChild(move(proj));
192621
192903
 
192904
+ auto update_table_index = GenerateTableIndex();
192905
+ update->table_index = update_table_index;
192622
192906
  if (!stmt.returning_list.empty()) {
192623
- auto update_table_index = GenerateTableIndex();
192624
- update->table_index = update_table_index;
192625
192907
  unique_ptr<LogicalOperator> update_as_logicaloperator = move(update);
192626
192908
 
192627
192909
  return BindReturning(move(stmt.returning_list), table, update_table_index, move(update_as_logicaloperator),
192628
192910
  move(result));
192629
192911
  }
192630
192912
 
192631
- update->table_index = 0;
192632
192913
  result.names = {"Count"};
192633
192914
  result.types = {LogicalType::BIGINT};
192634
192915
  result.plan = move(update);
@@ -192954,6 +193235,9 @@ unique_ptr<BoundTableRef> Binder::Bind(BaseTableRef &ref) {
192954
193235
  // bind the child subquery
192955
193236
  view_binder->AddBoundView(view_catalog_entry);
192956
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
+ }
192957
193241
 
192958
193242
  D_ASSERT(bound_child->type == TableReferenceType::SUBQUERY);
192959
193243
  // verify that the types and names match up with the expected types and names
@@ -193465,6 +193749,33 @@ unique_ptr<BoundTableRef> Binder::Bind(SubqueryRef &ref, CommonTableExpressionIn
193465
193749
 
193466
193750
 
193467
193751
 
193752
+ //===----------------------------------------------------------------------===//
193753
+ // DuckDB
193754
+ //
193755
+ // duckdb/planner/expression_binder/table_function_binder.hpp
193756
+ //
193757
+ //
193758
+ //===----------------------------------------------------------------------===//
193759
+
193760
+
193761
+
193762
+
193763
+
193764
+ namespace duckdb {
193765
+
193766
+ //! The Table function binder can bind standard table function parameters (i.e. non-table-in-out functions)
193767
+ class TableFunctionBinder : public ExpressionBinder {
193768
+ public:
193769
+ TableFunctionBinder(Binder &binder, ClientContext &context);
193770
+
193771
+ protected:
193772
+ BindResult BindColumnReference(ColumnRefExpression &expr);
193773
+ BindResult BindExpression(unique_ptr<ParsedExpression> *expr, idx_t depth, bool root_expression = false) override;
193774
+
193775
+ string UnsupportedAggregateMessage() override;
193776
+ };
193777
+
193778
+ } // namespace duckdb
193468
193779
 
193469
193780
 
193470
193781
 
@@ -193541,7 +193852,7 @@ bool Binder::BindTableFunctionParameters(TableFunctionCatalogEntry &table_functi
193541
193852
  continue;
193542
193853
  }
193543
193854
 
193544
- ConstantBinder binder(*this, context, "TABLE FUNCTION parameter");
193855
+ TableFunctionBinder binder(*this, context);
193545
193856
  LogicalType sql_type;
193546
193857
  auto expr = binder.Bind(child, &sql_type);
193547
193858
  if (expr->HasParameter()) {
@@ -196504,8 +196815,9 @@ BindResult GroupBinder::BindColumnRef(ColumnRefExpression &colref) {
196504
196815
  namespace duckdb {
196505
196816
 
196506
196817
  HavingBinder::HavingBinder(Binder &binder, ClientContext &context, BoundSelectNode &node, BoundGroupInformation &info,
196507
- case_insensitive_map_t<idx_t> &alias_map)
196508
- : SelectBinder(binder, context, node, info), column_alias_binder(node, alias_map) {
196818
+ case_insensitive_map_t<idx_t> &alias_map, AggregateHandling aggregate_handling)
196819
+ : SelectBinder(binder, context, node, info), column_alias_binder(node, alias_map),
196820
+ aggregate_handling(aggregate_handling) {
196509
196821
  target_type = LogicalType(LogicalTypeId::BOOLEAN);
196510
196822
  }
196511
196823
 
@@ -196515,7 +196827,16 @@ BindResult HavingBinder::BindColumnRef(unique_ptr<ParsedExpression> *expr_ptr, i
196515
196827
  if (!alias_result.HasError()) {
196516
196828
  return alias_result;
196517
196829
  }
196518
-
196830
+ if (aggregate_handling == AggregateHandling::FORCE_AGGREGATES) {
196831
+ auto expr = duckdb::SelectBinder::BindExpression(expr_ptr, depth);
196832
+ if (expr.HasError()) {
196833
+ return expr;
196834
+ }
196835
+ auto group_ref = make_unique<BoundColumnRefExpression>(
196836
+ expr.expression->return_type, ColumnBinding(node.group_index, node.groups.group_expressions.size()));
196837
+ node.groups.group_expressions.push_back(move(expr.expression));
196838
+ return BindResult(move(group_ref));
196839
+ }
196519
196840
  return BindResult(StringUtil::Format(
196520
196841
  "column %s must appear in the GROUP BY clause or be used in an aggregate function", expr.ToString()));
196521
196842
  }
@@ -196997,6 +197318,42 @@ BindResult SelectBinder::BindGroup(ParsedExpression &expr, idx_t depth, idx_t gr
196997
197318
  } // namespace duckdb
196998
197319
 
196999
197320
 
197321
+
197322
+
197323
+ namespace duckdb {
197324
+
197325
+ TableFunctionBinder::TableFunctionBinder(Binder &binder, ClientContext &context) : ExpressionBinder(binder, context) {
197326
+ }
197327
+
197328
+ BindResult TableFunctionBinder::BindColumnReference(ColumnRefExpression &expr) {
197329
+ auto result_name = StringUtil::Join(expr.column_names, ".");
197330
+ return BindResult(make_unique<BoundConstantExpression>(Value(result_name)));
197331
+ }
197332
+
197333
+ BindResult TableFunctionBinder::BindExpression(unique_ptr<ParsedExpression> *expr_ptr, idx_t depth,
197334
+ bool root_expression) {
197335
+ auto &expr = **expr_ptr;
197336
+ switch (expr.GetExpressionClass()) {
197337
+ case ExpressionClass::COLUMN_REF:
197338
+ return BindColumnReference((ColumnRefExpression &)expr);
197339
+ case ExpressionClass::SUBQUERY:
197340
+ throw BinderException("Table function cannot contain subqueries");
197341
+ case ExpressionClass::DEFAULT:
197342
+ return BindResult("Table function cannot contain DEFAULT clause");
197343
+ case ExpressionClass::WINDOW:
197344
+ return BindResult("Table function cannot contain window functions!");
197345
+ default:
197346
+ return ExpressionBinder::BindExpression(expr_ptr, depth);
197347
+ }
197348
+ }
197349
+
197350
+ string TableFunctionBinder::UnsupportedAggregateMessage() {
197351
+ return "Table function cannot contain aggregates!";
197352
+ }
197353
+
197354
+ } // namespace duckdb
197355
+
197356
+
197000
197357
  namespace duckdb {
197001
197358
 
197002
197359
  UpdateBinder::UpdateBinder(Binder &binder, ClientContext &context) : ExpressionBinder(binder, context) {
@@ -198274,6 +198631,10 @@ unique_ptr<LogicalOperator> LogicalOperator::Deserialize(Deserializer &deseriali
198274
198631
  return result;
198275
198632
  }
198276
198633
 
198634
+ vector<idx_t> LogicalOperator::GetTableIndex() const {
198635
+ return vector<idx_t> {};
198636
+ }
198637
+
198277
198638
  unique_ptr<LogicalOperator> LogicalOperator::Copy(ClientContext &context) const {
198278
198639
  BufferedSerializer logical_op_serializer;
198279
198640
  try {
@@ -198665,6 +199026,14 @@ idx_t LogicalAggregate::EstimateCardinality(ClientContext &context) {
198665
199026
  return LogicalOperator::EstimateCardinality(context);
198666
199027
  }
198667
199028
 
199029
+ vector<idx_t> LogicalAggregate::GetTableIndex() const {
199030
+ vector<idx_t> result {group_index, aggregate_index};
199031
+ if (groupings_index != DConstants::INVALID_INDEX) {
199032
+ result.push_back(groupings_index);
199033
+ }
199034
+ return result;
199035
+ }
199036
+
198668
199037
  } // namespace duckdb
198669
199038
 
198670
199039
 
@@ -198731,6 +199100,10 @@ unique_ptr<LogicalOperator> LogicalColumnDataGet::Deserialize(LogicalDeserializa
198731
199100
  return make_unique<LogicalColumnDataGet>(table_index, move(chunk_types), move(collection));
198732
199101
  }
198733
199102
 
199103
+ vector<idx_t> LogicalColumnDataGet::GetTableIndex() const {
199104
+ return vector<idx_t> {table_index};
199105
+ }
199106
+
198734
199107
  } // namespace duckdb
198735
199108
 
198736
199109
 
@@ -198995,6 +199368,10 @@ unique_ptr<LogicalOperator> LogicalCTERef::Deserialize(LogicalDeserializationSta
198995
199368
  return make_unique<LogicalCTERef>(table_index, cte_index, chunk_types, bound_columns);
198996
199369
  }
198997
199370
 
199371
+ vector<idx_t> LogicalCTERef::GetTableIndex() const {
199372
+ return vector<idx_t> {table_index};
199373
+ }
199374
+
198998
199375
  } // namespace duckdb
198999
199376
 
199000
199377
 
@@ -199015,8 +199392,8 @@ unique_ptr<LogicalOperator> LogicalDelete::Deserialize(LogicalDeserializationSta
199015
199392
 
199016
199393
  TableCatalogEntry *table_catalog_entry = catalog.GetEntry<TableCatalogEntry>(context, info->schema, info->table);
199017
199394
 
199018
- auto result = make_unique<LogicalDelete>(table_catalog_entry);
199019
- result->table_index = reader.ReadRequired<idx_t>();
199395
+ auto table_index = reader.ReadRequired<idx_t>();
199396
+ auto result = make_unique<LogicalDelete>(table_catalog_entry, table_index);
199020
199397
  result->return_chunk = reader.ReadRequired<bool>();
199021
199398
  return move(result);
199022
199399
  }
@@ -199025,6 +199402,10 @@ idx_t LogicalDelete::EstimateCardinality(ClientContext &context) {
199025
199402
  return return_chunk ? LogicalOperator::EstimateCardinality(context) : 1;
199026
199403
  }
199027
199404
 
199405
+ vector<idx_t> LogicalDelete::GetTableIndex() const {
199406
+ return vector<idx_t> {table_index};
199407
+ }
199408
+
199028
199409
  } // namespace duckdb
199029
199410
 
199030
199411
 
@@ -199042,6 +199423,10 @@ unique_ptr<LogicalOperator> LogicalDelimGet::Deserialize(LogicalDeserializationS
199042
199423
  return make_unique<LogicalDelimGet>(table_index, chunk_types);
199043
199424
  }
199044
199425
 
199426
+ vector<idx_t> LogicalDelimGet::GetTableIndex() const {
199427
+ return vector<idx_t> {table_index};
199428
+ }
199429
+
199045
199430
  } // namespace duckdb
199046
199431
 
199047
199432
 
@@ -199109,6 +199494,10 @@ unique_ptr<LogicalOperator> LogicalDummyScan::Deserialize(LogicalDeserialization
199109
199494
  return make_unique<LogicalDummyScan>(table_index);
199110
199495
  }
199111
199496
 
199497
+ vector<idx_t> LogicalDummyScan::GetTableIndex() const {
199498
+ return vector<idx_t> {table_index};
199499
+ }
199500
+
199112
199501
  } // namespace duckdb
199113
199502
 
199114
199503
 
@@ -199217,6 +199606,10 @@ unique_ptr<LogicalOperator> LogicalExpressionGet::Deserialize(LogicalDeserializa
199217
199606
  return make_unique<LogicalExpressionGet>(table_index, expr_types, move(expressions));
199218
199607
  }
199219
199608
 
199609
+ vector<idx_t> LogicalExpressionGet::GetTableIndex() const {
199610
+ return vector<idx_t> {table_index};
199611
+ }
199612
+
199220
199613
  } // namespace duckdb
199221
199614
 
199222
199615
 
@@ -199454,6 +199847,10 @@ unique_ptr<LogicalOperator> LogicalGet::Deserialize(LogicalDeserializationState
199454
199847
  return move(result);
199455
199848
  }
199456
199849
 
199850
+ vector<idx_t> LogicalGet::GetTableIndex() const {
199851
+ return vector<idx_t> {table_index};
199852
+ }
199853
+
199457
199854
  } // namespace duckdb
199458
199855
 
199459
199856
 
@@ -199499,10 +199896,9 @@ unique_ptr<LogicalOperator> LogicalInsert::Deserialize(LogicalDeserializationSta
199499
199896
  throw InternalException("Cant find catalog entry for table %s", info->table);
199500
199897
  }
199501
199898
 
199502
- auto result = make_unique<LogicalInsert>(table_catalog_entry);
199899
+ auto result = make_unique<LogicalInsert>(table_catalog_entry, table_index);
199503
199900
  result->type = state.type;
199504
199901
  result->table = table_catalog_entry;
199505
- result->table_index = table_index;
199506
199902
  result->return_chunk = return_chunk;
199507
199903
  result->insert_values = move(insert_values);
199508
199904
  result->column_index_map = column_index_map;
@@ -199515,6 +199911,10 @@ idx_t LogicalInsert::EstimateCardinality(ClientContext &context) {
199515
199911
  return return_chunk ? LogicalOperator::EstimateCardinality(context) : 1;
199516
199912
  }
199517
199913
 
199914
+ vector<idx_t> LogicalInsert::GetTableIndex() const {
199915
+ return vector<idx_t> {table_index};
199916
+ }
199917
+
199518
199918
  } // namespace duckdb
199519
199919
 
199520
199920
 
@@ -199751,6 +200151,10 @@ unique_ptr<LogicalOperator> LogicalProjection::Deserialize(LogicalDeserializatio
199751
200151
  return make_unique<LogicalProjection>(table_index, move(expressions));
199752
200152
  }
199753
200153
 
200154
+ vector<idx_t> LogicalProjection::GetTableIndex() const {
200155
+ return vector<idx_t> {table_index};
200156
+ }
200157
+
199754
200158
  } // namespace duckdb
199755
200159
 
199756
200160
 
@@ -199771,6 +200175,10 @@ unique_ptr<LogicalOperator> LogicalRecursiveCTE::Deserialize(LogicalDeserializat
199771
200175
  return unique_ptr<LogicalRecursiveCTE>(new LogicalRecursiveCTE(table_index, column_count, union_all, state.type));
199772
200176
  }
199773
200177
 
200178
+ vector<idx_t> LogicalRecursiveCTE::GetTableIndex() const {
200179
+ return vector<idx_t> {table_index};
200180
+ }
200181
+
199774
200182
  } // namespace duckdb
199775
200183
 
199776
200184
 
@@ -199789,7 +200197,12 @@ vector<ColumnBinding> LogicalSample::GetColumnBindings() {
199789
200197
  idx_t LogicalSample::EstimateCardinality(ClientContext &context) {
199790
200198
  auto child_cardinality = children[0]->EstimateCardinality(context);
199791
200199
  if (sample_options->is_percentage) {
199792
- return idx_t(child_cardinality * sample_options->sample_size.GetValue<double>());
200200
+ double sample_cardinality =
200201
+ double(child_cardinality) * (sample_options->sample_size.GetValue<double>() / 100.0);
200202
+ if (sample_cardinality > double(child_cardinality)) {
200203
+ return child_cardinality;
200204
+ }
200205
+ return idx_t(sample_cardinality);
199793
200206
  } else {
199794
200207
  auto sample_size = sample_options->sample_size.GetValue<uint64_t>();
199795
200208
  if (sample_size < child_cardinality) {
@@ -199853,6 +200266,11 @@ unique_ptr<LogicalOperator> LogicalSetOperation::Deserialize(LogicalDeserializat
199853
200266
  // TODO(stephwang): review if unique_ptr<LogicalOperator> plan is needed
199854
200267
  return unique_ptr<LogicalSetOperation>(new LogicalSetOperation(table_index, column_count, state.type));
199855
200268
  }
200269
+
200270
+ vector<idx_t> LogicalSetOperation::GetTableIndex() const {
200271
+ return vector<idx_t> {table_index};
200272
+ }
200273
+
199856
200274
  } // namespace duckdb
199857
200275
 
199858
200276
 
@@ -199951,6 +200369,11 @@ unique_ptr<LogicalOperator> LogicalUnnest::Deserialize(LogicalDeserializationSta
199951
200369
  result->expressions = move(expressions);
199952
200370
  return move(result);
199953
200371
  }
200372
+
200373
+ vector<idx_t> LogicalUnnest::GetTableIndex() const {
200374
+ return vector<idx_t> {unnest_index};
200375
+ }
200376
+
199954
200377
  } // namespace duckdb
199955
200378
 
199956
200379
 
@@ -200025,6 +200448,10 @@ unique_ptr<LogicalOperator> LogicalWindow::Deserialize(LogicalDeserializationSta
200025
200448
  return move(result);
200026
200449
  }
200027
200450
 
200451
+ vector<idx_t> LogicalWindow::GetTableIndex() const {
200452
+ return vector<idx_t> {window_index};
200453
+ }
200454
+
200028
200455
  } // namespace duckdb
200029
200456
 
200030
200457
 
@@ -200556,10 +200983,13 @@ unique_ptr<LogicalOperator> FlattenDependentJoins::PushDownDependentJoinInternal
200556
200983
  // we reached a node without correlated expressions
200557
200984
  // we can eliminate the dependent join now and create a simple cross product
200558
200985
  // now create the duplicate eliminated scan for this node
200986
+ auto left_columns = plan->GetColumnBindings().size();
200559
200987
  auto delim_index = binder.GenerateTableIndex();
200560
200988
  this->base_binding = ColumnBinding(delim_index, 0);
200989
+ this->delim_offset = 0;
200990
+ this->data_offset = left_columns;
200561
200991
  auto delim_scan = make_unique<LogicalDelimGet>(delim_index, delim_types);
200562
- return LogicalCrossProduct::Create(move(delim_scan), move(plan));
200992
+ return LogicalCrossProduct::Create(move(plan), move(delim_scan));
200563
200993
  }
200564
200994
  switch (plan->type) {
200565
200995
  case LogicalOperatorType::LOGICAL_UNNEST:
@@ -200923,8 +201353,19 @@ unique_ptr<LogicalOperator> FlattenDependentJoins::PushDownDependentJoinInternal
200923
201353
  case LogicalOperatorType::LOGICAL_UNION: {
200924
201354
  auto &setop = (LogicalSetOperation &)*plan;
200925
201355
  // set operator, push into both children
201356
+ #ifdef DEBUG
201357
+ plan->children[0]->ResolveOperatorTypes();
201358
+ plan->children[1]->ResolveOperatorTypes();
201359
+ D_ASSERT(plan->children[0]->types == plan->children[1]->types);
201360
+ #endif
200926
201361
  plan->children[0] = PushDownDependentJoin(move(plan->children[0]));
200927
201362
  plan->children[1] = PushDownDependentJoin(move(plan->children[1]));
201363
+ #ifdef DEBUG
201364
+ D_ASSERT(plan->children[0]->GetColumnBindings().size() == plan->children[1]->GetColumnBindings().size());
201365
+ plan->children[0]->ResolveOperatorTypes();
201366
+ plan->children[1]->ResolveOperatorTypes();
201367
+ D_ASSERT(plan->children[0]->types == plan->children[1]->types);
201368
+ #endif
200928
201369
  // we have to refer to the setop index now
200929
201370
  base_binding.table_index = setop.table_index;
200930
201371
  base_binding.column_index = setop.column_count;
@@ -201993,7 +202434,7 @@ void BufferManager::SetTemporaryDirectory(string new_dir) {
201993
202434
 
201994
202435
  BufferManager::BufferManager(DatabaseInstance &db, string tmp, idx_t maximum_memory)
201995
202436
  : db(db), current_memory(0), maximum_memory(maximum_memory), temp_directory(move(tmp)),
201996
- queue(make_unique<EvictionQueue>()), temporary_id(MAXIMUM_BLOCK),
202437
+ queue(make_unique<EvictionQueue>()), temporary_id(MAXIMUM_BLOCK), queue_insertions(0),
201997
202438
  buffer_allocator(BufferAllocatorAllocate, BufferAllocatorFree, BufferAllocatorRealloc,
201998
202439
  make_unique<BufferAllocatorData>(*this)) {
201999
202440
  temp_block_manager = make_unique<InMemoryBlockManager>(*this);
@@ -213321,7 +213762,12 @@ bool DataTable::AppendToIndexes(TableIndexList &indexes, DataChunk &chunk, row_t
213321
213762
  bool append_failed = false;
213322
213763
  // now append the entries to the indices
213323
213764
  indexes.Scan([&](Index &index) {
213324
- if (!index.Append(chunk, row_identifiers)) {
213765
+ try {
213766
+ if (!index.Append(chunk, row_identifiers)) {
213767
+ append_failed = true;
213768
+ return true;
213769
+ }
213770
+ } catch (...) {
213325
213771
  append_failed = true;
213326
213772
  return true;
213327
213773
  }
@@ -213335,7 +213781,6 @@ bool DataTable::AppendToIndexes(TableIndexList &indexes, DataChunk &chunk, row_t
213335
213781
  for (auto *index : already_appended) {
213336
213782
  index->Delete(chunk, row_identifiers);
213337
213783
  }
213338
-
213339
213784
  return false;
213340
213785
  }
213341
213786
  return true;
@@ -213978,12 +214423,21 @@ void LocalTableStorage::AppendToIndexes(Transaction &transaction, TableAppendSta
213978
214423
  append_state.current_row);
213979
214424
  }
213980
214425
  if (constraint_violated) {
214426
+ PreservedError error;
213981
214427
  // need to revert the append
213982
214428
  row_t current_row = append_state.row_start;
213983
214429
  // remove the data from the indexes, if there are any indexes
213984
214430
  row_groups->Scan(transaction, [&](DataChunk &chunk) -> bool {
213985
214431
  // append this chunk to the indexes of the table
213986
- table->RemoveFromIndexes(append_state, chunk, current_row);
214432
+ try {
214433
+ table->RemoveFromIndexes(append_state, chunk, current_row);
214434
+ } catch (Exception &ex) {
214435
+ error = PreservedError(ex);
214436
+ return false;
214437
+ } catch (std::exception &ex) {
214438
+ error = PreservedError(ex);
214439
+ return false;
214440
+ }
213987
214441
 
213988
214442
  current_row += chunk.size();
213989
214443
  if (current_row >= append_state.current_row) {
@@ -213995,6 +214449,9 @@ void LocalTableStorage::AppendToIndexes(Transaction &transaction, TableAppendSta
213995
214449
  if (append_to_table) {
213996
214450
  table->RevertAppendInternal(append_state.row_start, append_count);
213997
214451
  }
214452
+ if (error) {
214453
+ error.Throw();
214454
+ }
213998
214455
  throw ConstraintException("PRIMARY KEY or UNIQUE constraint violated: duplicated key");
213999
214456
  }
214000
214457
  }
@@ -214126,7 +214583,7 @@ void LocalStorage::InitializeAppend(LocalAppendState &state, DataTable *table) {
214126
214583
  void LocalStorage::Append(LocalAppendState &state, DataChunk &chunk) {
214127
214584
  // append to unique indices (if any)
214128
214585
  auto storage = state.storage;
214129
- idx_t base_id = MAX_ROW_ID + storage->row_groups->GetTotalRows();
214586
+ idx_t base_id = MAX_ROW_ID + storage->row_groups->GetTotalRows() + state.append_state.total_append_count;
214130
214587
  if (!DataTable::AppendToIndexes(storage->indexes, chunk, base_id)) {
214131
214588
  throw ConstraintException("PRIMARY KEY or UNIQUE constraint violated: duplicated key");
214132
214589
  }
@@ -223785,7 +224242,10 @@ void CleanupState::Flush() {
223785
224242
  Vector row_identifiers(LogicalType::ROW_TYPE, (data_ptr_t)row_numbers);
223786
224243
 
223787
224244
  // delete the tuples from all the indexes
223788
- current_table->RemoveFromIndexes(row_identifiers, count);
224245
+ try {
224246
+ current_table->RemoveFromIndexes(row_identifiers, count);
224247
+ } catch (...) {
224248
+ }
223789
224249
 
223790
224250
  count = 0;
223791
224251
  }