duckdb 0.3.2-dev528.0 → 0.3.2-dev556.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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/src/duckdb.cpp +267 -3125
  3. package/src/duckdb.hpp +3582 -479
package/src/duckdb.hpp CHANGED
@@ -10,8 +10,8 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
10
10
 
11
11
  #pragma once
12
12
  #define DUCKDB_AMALGAMATION 1
13
- #define DUCKDB_SOURCE_ID "0546552f6"
14
- #define DUCKDB_VERSION "0.3.2-dev528"
13
+ #define DUCKDB_SOURCE_ID "3c1a9e3b7"
14
+ #define DUCKDB_VERSION "0.3.2-dev556"
15
15
  //===----------------------------------------------------------------------===//
16
16
  // DuckDB
17
17
  //
@@ -7257,7 +7257,7 @@ public:
7257
7257
  atomic<transaction_t> timestamp;
7258
7258
  //! Child entry
7259
7259
  unique_ptr<CatalogEntry> child;
7260
- //! Parent entry (the node that owns this node)
7260
+ //! Parent entry (the node that dependents_map this node)
7261
7261
  CatalogEntry *parent;
7262
7262
 
7263
7263
  public:
@@ -7445,7 +7445,7 @@ public:
7445
7445
  DUCKDB_API CatalogEntry *CreateFunction(ClientContext &context, CreateFunctionInfo *info);
7446
7446
  //! Creates a table in the catalog.
7447
7447
  DUCKDB_API CatalogEntry *CreateView(ClientContext &context, CreateViewInfo *info);
7448
- //! Creates a table in the catalog.
7448
+ //! Creates a sequence in the catalog.
7449
7449
  DUCKDB_API CatalogEntry *CreateSequence(ClientContext &context, CreateSequenceInfo *info);
7450
7450
  //! Creates a Enum in the catalog.
7451
7451
  DUCKDB_API CatalogEntry *CreateType(ClientContext &context, CreateTypeInfo *info);
@@ -7493,6 +7493,9 @@ public:
7493
7493
  const string &name, bool if_exists = false,
7494
7494
  QueryErrorContext error_context = QueryErrorContext());
7495
7495
 
7496
+ //! Gets the "schema.name" entry without a specified type, if entry does not exist an exception is thrown
7497
+ DUCKDB_API CatalogEntry *GetEntry(ClientContext &context, const string &schema, const string &name);
7498
+
7496
7499
  template <class T>
7497
7500
  T *GetEntry(ClientContext &context, const string &schema_name, const string &name, bool if_exists = false,
7498
7501
  QueryErrorContext error_context = QueryErrorContext());
@@ -12422,274 +12425,42 @@ private:
12422
12425
  };
12423
12426
  } // namespace duckdb
12424
12427
 
12425
- #include <functional>
12426
- #include <memory>
12427
-
12428
- namespace duckdb {
12429
- struct AlterInfo;
12430
-
12431
- class ClientContext;
12432
-
12433
- typedef unordered_map<CatalogSet *, unique_lock<mutex>> set_lock_map_t;
12434
-
12435
- struct MappingValue {
12436
- explicit MappingValue(idx_t index_) : index(index_), timestamp(0), deleted(false), parent(nullptr) {
12437
- }
12438
-
12439
- idx_t index;
12440
- transaction_t timestamp;
12441
- bool deleted;
12442
- unique_ptr<MappingValue> child;
12443
- MappingValue *parent;
12444
- };
12445
-
12446
- //! The Catalog Set stores (key, value) map of a set of CatalogEntries
12447
- class CatalogSet {
12448
- friend class DependencyManager;
12449
-
12450
- public:
12451
- DUCKDB_API explicit CatalogSet(Catalog &catalog, unique_ptr<DefaultGenerator> defaults = nullptr);
12452
-
12453
- //! Create an entry in the catalog set. Returns whether or not it was
12454
- //! successful.
12455
- DUCKDB_API bool CreateEntry(ClientContext &context, const string &name, unique_ptr<CatalogEntry> value,
12456
- unordered_set<CatalogEntry *> &dependencies);
12457
-
12458
- DUCKDB_API bool AlterEntry(ClientContext &context, const string &name, AlterInfo *alter_info);
12459
-
12460
- DUCKDB_API bool DropEntry(ClientContext &context, const string &name, bool cascade);
12461
-
12462
- void CleanupEntry(CatalogEntry *catalog_entry);
12463
-
12464
- //! Returns the entry with the specified name
12465
- DUCKDB_API CatalogEntry *GetEntry(ClientContext &context, const string &name);
12466
-
12467
- //! Gets the entry that is most similar to the given name (i.e. smallest levenshtein distance), or empty string if
12468
- //! none is found. The returned pair consists of the entry name and the distance (smaller means closer).
12469
- pair<string, idx_t> SimilarEntry(ClientContext &context, const string &name);
12470
-
12471
- //! Rollback <entry> to be the currently valid entry for a certain catalog
12472
- //! entry
12473
- void Undo(CatalogEntry *entry);
12474
-
12475
- //! Scan the catalog set, invoking the callback method for every committed entry
12476
- DUCKDB_API void Scan(const std::function<void(CatalogEntry *)> &callback);
12477
- //! Scan the catalog set, invoking the callback method for every entry
12478
- DUCKDB_API void Scan(ClientContext &context, const std::function<void(CatalogEntry *)> &callback);
12479
-
12480
- template <class T>
12481
- vector<T *> GetEntries(ClientContext &context) {
12482
- vector<T *> result;
12483
- Scan(context, [&](CatalogEntry *entry) { result.push_back((T *)entry); });
12484
- return result;
12485
- }
12486
-
12487
- DUCKDB_API static bool HasConflict(ClientContext &context, transaction_t timestamp);
12488
- DUCKDB_API static bool UseTimestamp(ClientContext &context, transaction_t timestamp);
12489
-
12490
- CatalogEntry *GetEntryFromIndex(idx_t index);
12491
- void UpdateTimestamp(CatalogEntry *entry, transaction_t timestamp);
12492
-
12493
- private:
12494
- //! Adjusts table dependencies on the event of an UNDO
12495
- void AdjustTableDependencies(CatalogEntry *entry);
12496
- //! Adjust one dependency
12497
- void AdjustDependency(CatalogEntry *entry, TableCatalogEntry *table, ColumnDefinition &column, bool remove);
12498
- //! Adjust Enum dependency
12499
- void AdjustEnumDependency(CatalogEntry *entry, ColumnDefinition &column, bool remove);
12500
- //! Given a root entry, gets the entry valid for this transaction
12501
- CatalogEntry *GetEntryForTransaction(ClientContext &context, CatalogEntry *current);
12502
- CatalogEntry *GetCommittedEntry(CatalogEntry *current);
12503
- bool GetEntryInternal(ClientContext &context, const string &name, idx_t &entry_index, CatalogEntry *&entry);
12504
- bool GetEntryInternal(ClientContext &context, idx_t entry_index, CatalogEntry *&entry);
12505
- //! Drops an entry from the catalog set; must hold the catalog_lock to safely call this
12506
- void DropEntryInternal(ClientContext &context, idx_t entry_index, CatalogEntry &entry, bool cascade,
12507
- set_lock_map_t &lock_set);
12508
- CatalogEntry *CreateEntryInternal(ClientContext &context, unique_ptr<CatalogEntry> entry);
12509
- MappingValue *GetMapping(ClientContext &context, const string &name, bool get_latest = false);
12510
- void PutMapping(ClientContext &context, const string &name, idx_t entry_index);
12511
- void DeleteMapping(ClientContext &context, const string &name);
12512
-
12513
- private:
12514
- Catalog &catalog;
12515
- //! The catalog lock is used to make changes to the data
12516
- mutex catalog_lock;
12517
- //! Mapping of string to catalog entry
12518
- case_insensitive_map_t<unique_ptr<MappingValue>> mapping;
12519
- //! The set of catalog entries
12520
- unordered_map<idx_t, unique_ptr<CatalogEntry>> entries;
12521
- //! The current catalog entry index
12522
- idx_t current_entry = 0;
12523
- //! The generator used to generate default internal entries
12524
- unique_ptr<DefaultGenerator> defaults;
12525
- };
12526
-
12527
- } // namespace duckdb
12528
-
12529
-
12530
-
12531
- namespace duckdb {
12532
- class ClientContext;
12533
-
12534
- class StandardEntry;
12535
- class TableCatalogEntry;
12536
- class TableFunctionCatalogEntry;
12537
- class SequenceCatalogEntry;
12538
- class Serializer;
12539
- class Deserializer;
12540
-
12541
- enum class OnCreateConflict : uint8_t;
12542
-
12543
- struct AlterTableInfo;
12544
- struct CreateIndexInfo;
12545
- struct CreateFunctionInfo;
12546
- struct CreateCollationInfo;
12547
- struct CreateViewInfo;
12548
- struct BoundCreateTableInfo;
12549
- struct CreatePragmaFunctionInfo;
12550
- struct CreateSequenceInfo;
12551
- struct CreateSchemaInfo;
12552
- struct CreateTableFunctionInfo;
12553
- struct CreateCopyFunctionInfo;
12554
- struct CreateTypeInfo;
12555
-
12556
- struct DropInfo;
12557
-
12558
- //! A schema in the catalog
12559
- class SchemaCatalogEntry : public CatalogEntry {
12560
- friend class Catalog;
12561
-
12562
- public:
12563
- SchemaCatalogEntry(Catalog *catalog, string name, bool is_internal);
12564
-
12565
- private:
12566
- //! The catalog set holding the tables
12567
- CatalogSet tables;
12568
- //! The catalog set holding the indexes
12569
- CatalogSet indexes;
12570
- //! The catalog set holding the table functions
12571
- CatalogSet table_functions;
12572
- //! The catalog set holding the copy functions
12573
- CatalogSet copy_functions;
12574
- //! The catalog set holding the pragma functions
12575
- CatalogSet pragma_functions;
12576
- //! The catalog set holding the scalar and aggregate functions
12577
- CatalogSet functions;
12578
- //! The catalog set holding the sequences
12579
- CatalogSet sequences;
12580
- //! The catalog set holding the collations
12581
- CatalogSet collations;
12582
- //! The catalog set holding the types
12583
- CatalogSet types;
12584
-
12585
- public:
12586
- //! Scan the specified catalog set, invoking the callback method for every entry
12587
- void Scan(ClientContext &context, CatalogType type, const std::function<void(CatalogEntry *)> &callback);
12588
- //! Scan the specified catalog set, invoking the callback method for every committed entry
12589
- void Scan(CatalogType type, const std::function<void(CatalogEntry *)> &callback);
12590
-
12591
- //! Serialize the meta information of the SchemaCatalogEntry a serializer
12592
- virtual void Serialize(Serializer &serializer);
12593
- //! Deserializes to a CreateSchemaInfo
12594
- static unique_ptr<CreateSchemaInfo> Deserialize(Deserializer &source);
12595
-
12596
- string ToSQL() override;
12597
-
12598
- //! Creates an index with the given name in the schema
12599
- CatalogEntry *CreateIndex(ClientContext &context, CreateIndexInfo *info, TableCatalogEntry *table);
12600
-
12601
- private:
12602
- //! Create a scalar or aggregate function within the given schema
12603
- CatalogEntry *CreateFunction(ClientContext &context, CreateFunctionInfo *info);
12604
- //! Creates a table with the given name in the schema
12605
- CatalogEntry *CreateTable(ClientContext &context, BoundCreateTableInfo *info);
12606
- //! Creates a view with the given name in the schema
12607
- CatalogEntry *CreateView(ClientContext &context, CreateViewInfo *info);
12608
- //! Creates a sequence with the given name in the schema
12609
- CatalogEntry *CreateSequence(ClientContext &context, CreateSequenceInfo *info);
12610
- //! Create a table function within the given schema
12611
- CatalogEntry *CreateTableFunction(ClientContext &context, CreateTableFunctionInfo *info);
12612
- //! Create a copy function within the given schema
12613
- CatalogEntry *CreateCopyFunction(ClientContext &context, CreateCopyFunctionInfo *info);
12614
- //! Create a pragma function within the given schema
12615
- CatalogEntry *CreatePragmaFunction(ClientContext &context, CreatePragmaFunctionInfo *info);
12616
- //! Create a collation within the given schema
12617
- CatalogEntry *CreateCollation(ClientContext &context, CreateCollationInfo *info);
12618
- //! Create a enum within the given schema
12619
- CatalogEntry *CreateType(ClientContext &context, CreateTypeInfo *info);
12620
-
12621
- //! Drops an entry from the schema
12622
- void DropEntry(ClientContext &context, DropInfo *info);
12623
-
12624
- //! Append a scalar or aggregate function within the given schema
12625
- CatalogEntry *AddFunction(ClientContext &context, CreateFunctionInfo *info);
12626
-
12627
- //! Alters a catalog entry
12628
- void Alter(ClientContext &context, AlterInfo *info);
12629
-
12630
- //! Add a catalog entry to this schema
12631
- CatalogEntry *AddEntry(ClientContext &context, unique_ptr<StandardEntry> entry, OnCreateConflict on_conflict);
12632
- //! Add a catalog entry to this schema
12633
- CatalogEntry *AddEntry(ClientContext &context, unique_ptr<StandardEntry> entry, OnCreateConflict on_conflict,
12634
- unordered_set<CatalogEntry *> dependencies);
12635
-
12636
- //! Get the catalog set for the specified type
12637
- CatalogSet &GetCatalogSet(CatalogType type);
12638
- };
12639
- } // namespace duckdb
12640
-
12641
-
12642
12428
  //===----------------------------------------------------------------------===//
12643
12429
  // DuckDB
12644
12430
  //
12645
- // duckdb/common/deque.hpp
12431
+ // duckdb/catalog/catalog_entry/sequence_catalog_entry.hpp
12646
12432
  //
12647
12433
  //
12648
12434
  //===----------------------------------------------------------------------===//
12649
12435
 
12650
12436
 
12651
12437
 
12652
- #include <deque>
12653
12438
 
12654
- namespace duckdb {
12655
- using std::deque;
12656
- }
12657
12439
 
12658
12440
  //===----------------------------------------------------------------------===//
12659
12441
  // DuckDB
12660
12442
  //
12661
- // duckdb/common/progress_bar.hpp
12443
+ // duckdb/parser/parsed_data/create_sequence_info.hpp
12662
12444
  //
12663
12445
  //
12664
12446
  //===----------------------------------------------------------------------===//
12665
12447
 
12666
12448
 
12667
12449
 
12668
- #ifndef DUCKDB_NO_THREADS
12669
12450
  //===----------------------------------------------------------------------===//
12670
12451
  // DuckDB
12671
12452
  //
12672
- // duckdb/common/thread.hpp
12453
+ // duckdb/parser/parsed_data/create_info.hpp
12673
12454
  //
12674
12455
  //
12675
12456
  //===----------------------------------------------------------------------===//
12676
12457
 
12677
12458
 
12678
12459
 
12679
- #include <thread>
12680
-
12681
- namespace duckdb {
12682
- using std::thread;
12683
- }
12684
-
12685
- #include <future>
12686
- #endif
12687
-
12688
-
12689
12460
  //===----------------------------------------------------------------------===//
12690
12461
  // DuckDB
12691
12462
  //
12692
- // duckdb/execution/executor.hpp
12463
+ // duckdb/parser/parsed_data/parse_info.hpp
12693
12464
  //
12694
12465
  //
12695
12466
  //===----------------------------------------------------------------------===//
@@ -12698,22 +12469,3581 @@ using std::thread;
12698
12469
 
12699
12470
 
12700
12471
 
12701
- //===----------------------------------------------------------------------===//
12702
- // DuckDB
12703
- //
12704
- // duckdb/parallel/pipeline.hpp
12705
- //
12706
- //
12707
- //===----------------------------------------------------------------------===//
12472
+ namespace duckdb {
12708
12473
 
12474
+ struct ParseInfo {
12475
+ virtual ~ParseInfo() {
12476
+ }
12477
+ };
12709
12478
 
12479
+ } // namespace duckdb
12710
12480
 
12711
12481
 
12712
- //===----------------------------------------------------------------------===//
12713
- // DuckDB
12714
- //
12715
- // duckdb/execution/physical_operator.hpp
12716
- //
12482
+
12483
+ namespace duckdb {
12484
+
12485
+ enum class OnCreateConflict : uint8_t {
12486
+ // Standard: throw error
12487
+ ERROR_ON_CONFLICT,
12488
+ // CREATE IF NOT EXISTS, silently do nothing on conflict
12489
+ IGNORE_ON_CONFLICT,
12490
+ // CREATE OR REPLACE
12491
+ REPLACE_ON_CONFLICT
12492
+ };
12493
+
12494
+ struct CreateInfo : public ParseInfo {
12495
+ explicit CreateInfo(CatalogType type, string schema = DEFAULT_SCHEMA)
12496
+ : type(type), schema(schema), on_conflict(OnCreateConflict::ERROR_ON_CONFLICT), temporary(false),
12497
+ internal(false) {
12498
+ }
12499
+ ~CreateInfo() override {
12500
+ }
12501
+
12502
+ //! The to-be-created catalog type
12503
+ CatalogType type;
12504
+ //! The schema name of the entry
12505
+ string schema;
12506
+ //! What to do on create conflict
12507
+ OnCreateConflict on_conflict;
12508
+ //! Whether or not the entry is temporary
12509
+ bool temporary;
12510
+ //! Whether or not the entry is an internal entry
12511
+ bool internal;
12512
+ //! The SQL string of the CREATE statement
12513
+ string sql;
12514
+
12515
+ public:
12516
+ virtual unique_ptr<CreateInfo> Copy() const = 0;
12517
+ void CopyProperties(CreateInfo &other) const {
12518
+ other.type = type;
12519
+ other.schema = schema;
12520
+ other.on_conflict = on_conflict;
12521
+ other.temporary = temporary;
12522
+ other.internal = internal;
12523
+ other.sql = sql;
12524
+ }
12525
+ };
12526
+
12527
+ } // namespace duckdb
12528
+
12529
+
12530
+
12531
+ namespace duckdb {
12532
+
12533
+ struct CreateSequenceInfo : public CreateInfo {
12534
+ CreateSequenceInfo()
12535
+ : CreateInfo(CatalogType::SEQUENCE_ENTRY, INVALID_SCHEMA), name(string()), usage_count(0), increment(1),
12536
+ min_value(1), max_value(NumericLimits<int64_t>::Maximum()), start_value(1), cycle(false) {
12537
+ }
12538
+
12539
+ //! Sequence name to create
12540
+ string name;
12541
+ //! Usage count of the sequence
12542
+ uint64_t usage_count;
12543
+ //! The increment value
12544
+ int64_t increment;
12545
+ //! The minimum value of the sequence
12546
+ int64_t min_value;
12547
+ //! The maximum value of the sequence
12548
+ int64_t max_value;
12549
+ //! The start value of the sequence
12550
+ int64_t start_value;
12551
+ //! Whether or not the sequence cycles
12552
+ bool cycle;
12553
+
12554
+ public:
12555
+ unique_ptr<CreateInfo> Copy() const override {
12556
+ auto result = make_unique<CreateSequenceInfo>();
12557
+ CopyProperties(*result);
12558
+ result->name = name;
12559
+ result->schema = schema;
12560
+ result->usage_count = usage_count;
12561
+ result->increment = increment;
12562
+ result->min_value = min_value;
12563
+ result->max_value = max_value;
12564
+ result->start_value = start_value;
12565
+ result->cycle = cycle;
12566
+ return move(result);
12567
+ }
12568
+ };
12569
+
12570
+ } // namespace duckdb
12571
+
12572
+ //===----------------------------------------------------------------------===//
12573
+ // DuckDB
12574
+ //
12575
+ // duckdb/parser/parsed_data/alter_table_info.hpp
12576
+ //
12577
+ //
12578
+ //===----------------------------------------------------------------------===//
12579
+
12580
+
12581
+
12582
+
12583
+
12584
+
12585
+
12586
+ namespace duckdb {
12587
+
12588
+ enum class AlterType : uint8_t {
12589
+ INVALID = 0,
12590
+ ALTER_TABLE = 1,
12591
+ ALTER_VIEW = 2,
12592
+ ALTER_SEQUENCE = 3,
12593
+ CHANGE_OWNERSHIP = 4
12594
+ };
12595
+
12596
+ struct AlterInfo : public ParseInfo {
12597
+ AlterInfo(AlterType type, string schema, string name) : type(type), schema(move(schema)), name(move(name)) {
12598
+ }
12599
+ ~AlterInfo() override {
12600
+ }
12601
+
12602
+ AlterType type;
12603
+ //! Schema name to alter
12604
+ string schema;
12605
+ //! Entry name to alter
12606
+ string name;
12607
+
12608
+ public:
12609
+ virtual CatalogType GetCatalogType() = 0;
12610
+ virtual unique_ptr<AlterInfo> Copy() const = 0;
12611
+ virtual void Serialize(Serializer &serializer);
12612
+ static unique_ptr<AlterInfo> Deserialize(Deserializer &source);
12613
+ };
12614
+
12615
+ //===--------------------------------------------------------------------===//
12616
+ // Change Ownership
12617
+ //===--------------------------------------------------------------------===//
12618
+ struct ChangeOwnershipInfo : public AlterInfo {
12619
+ ChangeOwnershipInfo(CatalogType entry_catalog_type, string entry_schema, string entry_name, string owner_schema,
12620
+ string owner_name)
12621
+ : AlterInfo(AlterType::CHANGE_OWNERSHIP, entry_schema, entry_name), entry_catalog_type(entry_catalog_type),
12622
+ owner_schema(owner_schema), owner_name(owner_name) {
12623
+ }
12624
+
12625
+ // Catalog type refers to the entry type, since this struct is usually built from an
12626
+ // ALTER <TYPE> <schema>.<name> OWNED BY <owner_schema>.<owner_name> statement
12627
+ // here it is only possible to know the type of who is to be owned
12628
+ CatalogType entry_catalog_type;
12629
+
12630
+ string owner_schema;
12631
+ string owner_name;
12632
+
12633
+ public:
12634
+ CatalogType GetCatalogType() override;
12635
+ unique_ptr<AlterInfo> Copy() const override;
12636
+ };
12637
+
12638
+ //===--------------------------------------------------------------------===//
12639
+ // Alter Table
12640
+ //===--------------------------------------------------------------------===//
12641
+ enum class AlterTableType : uint8_t {
12642
+ INVALID = 0,
12643
+ RENAME_COLUMN = 1,
12644
+ RENAME_TABLE = 2,
12645
+ ADD_COLUMN = 3,
12646
+ REMOVE_COLUMN = 4,
12647
+ ALTER_COLUMN_TYPE = 5,
12648
+ SET_DEFAULT = 6
12649
+ };
12650
+
12651
+ struct AlterTableInfo : public AlterInfo {
12652
+ AlterTableInfo(AlterTableType type, string schema, string table)
12653
+ : AlterInfo(AlterType::ALTER_TABLE, schema, table), alter_table_type(type) {
12654
+ }
12655
+ ~AlterTableInfo() override {
12656
+ }
12657
+
12658
+ AlterTableType alter_table_type;
12659
+
12660
+ public:
12661
+ CatalogType GetCatalogType() override {
12662
+ return CatalogType::TABLE_ENTRY;
12663
+ }
12664
+ void Serialize(Serializer &serializer) override;
12665
+ static unique_ptr<AlterInfo> Deserialize(Deserializer &source);
12666
+ };
12667
+
12668
+ //===--------------------------------------------------------------------===//
12669
+ // RenameColumnInfo
12670
+ //===--------------------------------------------------------------------===//
12671
+ struct RenameColumnInfo : public AlterTableInfo {
12672
+ RenameColumnInfo(string schema, string table, string old_name_p, string new_name_p)
12673
+ : AlterTableInfo(AlterTableType::RENAME_COLUMN, move(schema), move(table)), old_name(move(old_name_p)),
12674
+ new_name(move(new_name_p)) {
12675
+ }
12676
+ ~RenameColumnInfo() override {
12677
+ }
12678
+
12679
+ //! Column old name
12680
+ string old_name;
12681
+ //! Column new name
12682
+ string new_name;
12683
+
12684
+ public:
12685
+ unique_ptr<AlterInfo> Copy() const override;
12686
+ void Serialize(Serializer &serializer) override;
12687
+ static unique_ptr<AlterInfo> Deserialize(Deserializer &source, string schema, string table);
12688
+ };
12689
+
12690
+ //===--------------------------------------------------------------------===//
12691
+ // RenameTableInfo
12692
+ //===--------------------------------------------------------------------===//
12693
+ struct RenameTableInfo : public AlterTableInfo {
12694
+ RenameTableInfo(string schema, string table, string new_name)
12695
+ : AlterTableInfo(AlterTableType::RENAME_TABLE, schema, table), new_table_name(new_name) {
12696
+ }
12697
+ ~RenameTableInfo() override {
12698
+ }
12699
+
12700
+ //! Relation new name
12701
+ string new_table_name;
12702
+
12703
+ public:
12704
+ unique_ptr<AlterInfo> Copy() const override;
12705
+ void Serialize(Serializer &serializer) override;
12706
+ static unique_ptr<AlterInfo> Deserialize(Deserializer &source, string schema, string table);
12707
+ };
12708
+
12709
+ //===--------------------------------------------------------------------===//
12710
+ // AddColumnInfo
12711
+ //===--------------------------------------------------------------------===//
12712
+ struct AddColumnInfo : public AlterTableInfo {
12713
+ AddColumnInfo(string schema, string table, ColumnDefinition new_column)
12714
+ : AlterTableInfo(AlterTableType::ADD_COLUMN, schema, table), new_column(move(new_column)) {
12715
+ }
12716
+ ~AddColumnInfo() override {
12717
+ }
12718
+
12719
+ //! New column
12720
+ ColumnDefinition new_column;
12721
+
12722
+ public:
12723
+ unique_ptr<AlterInfo> Copy() const override;
12724
+ void Serialize(Serializer &serializer) override;
12725
+ static unique_ptr<AlterInfo> Deserialize(Deserializer &source, string schema, string table);
12726
+ };
12727
+
12728
+ //===--------------------------------------------------------------------===//
12729
+ // RemoveColumnInfo
12730
+ //===--------------------------------------------------------------------===//
12731
+ struct RemoveColumnInfo : public AlterTableInfo {
12732
+ RemoveColumnInfo(string schema, string table, string removed_column, bool if_exists)
12733
+ : AlterTableInfo(AlterTableType::REMOVE_COLUMN, schema, table), removed_column(move(removed_column)),
12734
+ if_exists(if_exists) {
12735
+ }
12736
+ ~RemoveColumnInfo() override {
12737
+ }
12738
+
12739
+ //! The column to remove
12740
+ string removed_column;
12741
+ //! Whether or not an error should be thrown if the column does not exist
12742
+ bool if_exists;
12743
+
12744
+ public:
12745
+ unique_ptr<AlterInfo> Copy() const override;
12746
+ void Serialize(Serializer &serializer) override;
12747
+ static unique_ptr<AlterInfo> Deserialize(Deserializer &source, string schema, string table);
12748
+ };
12749
+
12750
+ //===--------------------------------------------------------------------===//
12751
+ // ChangeColumnTypeInfo
12752
+ //===--------------------------------------------------------------------===//
12753
+ struct ChangeColumnTypeInfo : public AlterTableInfo {
12754
+ ChangeColumnTypeInfo(string schema, string table, string column_name, LogicalType target_type,
12755
+ unique_ptr<ParsedExpression> expression)
12756
+ : AlterTableInfo(AlterTableType::ALTER_COLUMN_TYPE, schema, table), column_name(move(column_name)),
12757
+ target_type(move(target_type)), expression(move(expression)) {
12758
+ }
12759
+ ~ChangeColumnTypeInfo() override {
12760
+ }
12761
+
12762
+ //! The column name to alter
12763
+ string column_name;
12764
+ //! The target type of the column
12765
+ LogicalType target_type;
12766
+ //! The expression used for data conversion
12767
+ unique_ptr<ParsedExpression> expression;
12768
+
12769
+ public:
12770
+ unique_ptr<AlterInfo> Copy() const override;
12771
+ void Serialize(Serializer &serializer) override;
12772
+ static unique_ptr<AlterInfo> Deserialize(Deserializer &source, string schema, string table);
12773
+ };
12774
+
12775
+ //===--------------------------------------------------------------------===//
12776
+ // SetDefaultInfo
12777
+ //===--------------------------------------------------------------------===//
12778
+ struct SetDefaultInfo : public AlterTableInfo {
12779
+ SetDefaultInfo(string schema, string table, string column_name, unique_ptr<ParsedExpression> new_default)
12780
+ : AlterTableInfo(AlterTableType::SET_DEFAULT, schema, table), column_name(move(column_name)),
12781
+ expression(move(new_default)) {
12782
+ }
12783
+ ~SetDefaultInfo() override {
12784
+ }
12785
+
12786
+ //! The column name to alter
12787
+ string column_name;
12788
+ //! The expression used for data conversion
12789
+ unique_ptr<ParsedExpression> expression;
12790
+
12791
+ public:
12792
+ unique_ptr<AlterInfo> Copy() const override;
12793
+ void Serialize(Serializer &serializer) override;
12794
+ static unique_ptr<AlterInfo> Deserialize(Deserializer &source, string schema, string table);
12795
+ };
12796
+
12797
+ //===--------------------------------------------------------------------===//
12798
+ // Alter View
12799
+ //===--------------------------------------------------------------------===//
12800
+ enum class AlterViewType : uint8_t { INVALID = 0, RENAME_VIEW = 1 };
12801
+
12802
+ struct AlterViewInfo : public AlterInfo {
12803
+ AlterViewInfo(AlterViewType type, string schema, string view)
12804
+ : AlterInfo(AlterType::ALTER_VIEW, schema, view), alter_view_type(type) {
12805
+ }
12806
+ ~AlterViewInfo() override {
12807
+ }
12808
+
12809
+ AlterViewType alter_view_type;
12810
+
12811
+ public:
12812
+ CatalogType GetCatalogType() override {
12813
+ return CatalogType::VIEW_ENTRY;
12814
+ }
12815
+ void Serialize(Serializer &serializer) override;
12816
+ static unique_ptr<AlterInfo> Deserialize(Deserializer &source);
12817
+ };
12818
+
12819
+ //===--------------------------------------------------------------------===//
12820
+ // RenameViewInfo
12821
+ //===--------------------------------------------------------------------===//
12822
+ struct RenameViewInfo : public AlterViewInfo {
12823
+ RenameViewInfo(string schema, string view, string new_name)
12824
+ : AlterViewInfo(AlterViewType::RENAME_VIEW, schema, view), new_view_name(new_name) {
12825
+ }
12826
+ ~RenameViewInfo() override {
12827
+ }
12828
+
12829
+ //! Relation new name
12830
+ string new_view_name;
12831
+
12832
+ public:
12833
+ unique_ptr<AlterInfo> Copy() const override;
12834
+ void Serialize(Serializer &serializer) override;
12835
+ static unique_ptr<AlterInfo> Deserialize(Deserializer &source, string schema, string table);
12836
+ };
12837
+
12838
+ } // namespace duckdb
12839
+
12840
+
12841
+ namespace duckdb {
12842
+ class Serializer;
12843
+ class Deserializer;
12844
+
12845
+ struct SequenceValue {
12846
+ SequenceValue() : usage_count(0), counter(-1) {
12847
+ }
12848
+ SequenceValue(uint64_t usage_count, int64_t counter) : usage_count(usage_count), counter(counter) {
12849
+ }
12850
+
12851
+ uint64_t usage_count;
12852
+ int64_t counter;
12853
+ };
12854
+
12855
+ //! A sequence catalog entry
12856
+ class SequenceCatalogEntry : public StandardEntry {
12857
+ public:
12858
+ //! Create a real TableCatalogEntry and initialize storage for it
12859
+ SequenceCatalogEntry(Catalog *catalog, SchemaCatalogEntry *schema, CreateSequenceInfo *info);
12860
+
12861
+ //! Lock for getting a value on the sequence
12862
+ mutex lock;
12863
+ //! The amount of times the sequence has been used
12864
+ uint64_t usage_count;
12865
+ //! The sequence counter
12866
+ int64_t counter;
12867
+ //! The most recently returned value
12868
+ int64_t last_value;
12869
+ //! The increment value
12870
+ int64_t increment;
12871
+ //! The minimum value of the sequence
12872
+ int64_t start_value;
12873
+ //! The minimum value of the sequence
12874
+ int64_t min_value;
12875
+ //! The maximum value of the sequence
12876
+ int64_t max_value;
12877
+ //! Whether or not the sequence cycles
12878
+ bool cycle;
12879
+
12880
+ public:
12881
+ //! Serialize the meta information of the SequenceCatalogEntry a serializer
12882
+ virtual void Serialize(Serializer &serializer);
12883
+ //! Deserializes to a CreateTableInfo
12884
+ static unique_ptr<CreateSequenceInfo> Deserialize(Deserializer &source);
12885
+
12886
+ string ToSQL() override;
12887
+
12888
+ CatalogEntry *AlterOwnership(ClientContext &context, AlterInfo *info);
12889
+ };
12890
+ } // namespace duckdb
12891
+
12892
+ //===----------------------------------------------------------------------===//
12893
+ // DuckDB
12894
+ //
12895
+ // duckdb/transaction/transaction.hpp
12896
+ //
12897
+ //
12898
+ //===----------------------------------------------------------------------===//
12899
+
12900
+
12901
+
12902
+
12903
+
12904
+
12905
+ //===----------------------------------------------------------------------===//
12906
+ // DuckDB
12907
+ //
12908
+ // duckdb/transaction/undo_buffer.hpp
12909
+ //
12910
+ //
12911
+ //===----------------------------------------------------------------------===//
12912
+
12913
+
12914
+
12915
+
12916
+ //===----------------------------------------------------------------------===//
12917
+ // DuckDB
12918
+ //
12919
+ // duckdb/common/enums/undo_flags.hpp
12920
+ //
12921
+ //
12922
+ //===----------------------------------------------------------------------===//
12923
+
12924
+
12925
+
12926
+
12927
+
12928
+ namespace duckdb {
12929
+
12930
+ enum class UndoFlags : uint32_t { // far to big but aligned (TM)
12931
+ EMPTY_ENTRY = 0,
12932
+ CATALOG_ENTRY = 1,
12933
+ INSERT_TUPLE = 2,
12934
+ DELETE_TUPLE = 3,
12935
+ UPDATE_TUPLE = 4
12936
+ };
12937
+
12938
+ } // namespace duckdb
12939
+
12940
+
12941
+ namespace duckdb {
12942
+
12943
+ class WriteAheadLog;
12944
+
12945
+ struct UndoChunk {
12946
+ explicit UndoChunk(idx_t size);
12947
+ ~UndoChunk();
12948
+
12949
+ data_ptr_t WriteEntry(UndoFlags type, uint32_t len);
12950
+
12951
+ unique_ptr<data_t[]> data;
12952
+ idx_t current_position;
12953
+ idx_t maximum_size;
12954
+ unique_ptr<UndoChunk> next;
12955
+ UndoChunk *prev;
12956
+ };
12957
+
12958
+ //! The undo buffer of a transaction is used to hold previous versions of tuples
12959
+ //! that might be required in the future (because of rollbacks or previous
12960
+ //! transactions accessing them)
12961
+ class UndoBuffer {
12962
+ public:
12963
+ struct IteratorState {
12964
+ UndoChunk *current;
12965
+ data_ptr_t start;
12966
+ data_ptr_t end;
12967
+ };
12968
+
12969
+ public:
12970
+ UndoBuffer();
12971
+
12972
+ //! Reserve space for an entry of the specified type and length in the undo
12973
+ //! buffer
12974
+ data_ptr_t CreateEntry(UndoFlags type, idx_t len);
12975
+
12976
+ bool ChangesMade();
12977
+ idx_t EstimatedSize();
12978
+
12979
+ //! Cleanup the undo buffer
12980
+ void Cleanup();
12981
+ //! Commit the changes made in the UndoBuffer: should be called on commit
12982
+ void Commit(UndoBuffer::IteratorState &iterator_state, WriteAheadLog *log, transaction_t commit_id);
12983
+ //! Revert committed changes made in the UndoBuffer up until the currently committed state
12984
+ void RevertCommit(UndoBuffer::IteratorState &iterator_state, transaction_t transaction_id);
12985
+ //! Rollback the changes made in this UndoBuffer: should be called on
12986
+ //! rollback
12987
+ void Rollback() noexcept;
12988
+
12989
+ private:
12990
+ unique_ptr<UndoChunk> head;
12991
+ UndoChunk *tail;
12992
+
12993
+ private:
12994
+ template <class T>
12995
+ void IterateEntries(UndoBuffer::IteratorState &state, T &&callback);
12996
+ template <class T>
12997
+ void IterateEntries(UndoBuffer::IteratorState &state, UndoBuffer::IteratorState &end_state, T &&callback);
12998
+ template <class T>
12999
+ void ReverseIterateEntries(T &&callback);
13000
+ };
13001
+
13002
+ } // namespace duckdb
13003
+
13004
+ //===----------------------------------------------------------------------===//
13005
+ // DuckDB
13006
+ //
13007
+ // duckdb/transaction/local_storage.hpp
13008
+ //
13009
+ //
13010
+ //===----------------------------------------------------------------------===//
13011
+
13012
+
13013
+
13014
+
13015
+ //===----------------------------------------------------------------------===//
13016
+ // DuckDB
13017
+ //
13018
+ // duckdb/storage/table/scan_state.hpp
13019
+ //
13020
+ //
13021
+ //===----------------------------------------------------------------------===//
13022
+
13023
+
13024
+
13025
+
13026
+ //===----------------------------------------------------------------------===//
13027
+ // DuckDB
13028
+ //
13029
+ // duckdb/storage/buffer/buffer_handle.hpp
13030
+ //
13031
+ //
13032
+ //===----------------------------------------------------------------------===//
13033
+
13034
+
13035
+
13036
+ //===----------------------------------------------------------------------===//
13037
+ // DuckDB
13038
+ //
13039
+ // duckdb/storage/storage_info.hpp
13040
+ //
13041
+ //
13042
+ //===----------------------------------------------------------------------===//
13043
+
13044
+
13045
+
13046
+
13047
+
13048
+ namespace duckdb {
13049
+ class Serializer;
13050
+ class Deserializer;
13051
+ struct FileHandle;
13052
+
13053
+ //! The version number of the database storage format
13054
+ extern const uint64_t VERSION_NUMBER;
13055
+
13056
+ using block_id_t = int64_t;
13057
+
13058
+ #define INVALID_BLOCK (-1)
13059
+
13060
+ // maximum block id, 2^62
13061
+ #define MAXIMUM_BLOCK 4611686018427388000LL
13062
+
13063
+ //! The MainHeader is the first header in the storage file. The MainHeader is typically written only once for a database
13064
+ //! file.
13065
+ struct MainHeader {
13066
+ static constexpr idx_t MAGIC_BYTE_SIZE = 4;
13067
+ static constexpr idx_t MAGIC_BYTE_OFFSET = sizeof(uint64_t);
13068
+ static constexpr idx_t FLAG_COUNT = 4;
13069
+ // the magic bytes in front of the file
13070
+ // should be "DUCK"
13071
+ static const char MAGIC_BYTES[];
13072
+ //! The version of the database
13073
+ uint64_t version_number;
13074
+ //! The set of flags used by the database
13075
+ uint64_t flags[FLAG_COUNT];
13076
+
13077
+ static void CheckMagicBytes(FileHandle &handle);
13078
+
13079
+ void Serialize(Serializer &ser);
13080
+ static MainHeader Deserialize(Deserializer &source);
13081
+ };
13082
+
13083
+ //! The DatabaseHeader contains information about the current state of the database. Every storage file has two
13084
+ //! DatabaseHeaders. On startup, the DatabaseHeader with the highest iteration count is used as the active header. When
13085
+ //! a checkpoint is performed, the active DatabaseHeader is switched by increasing the iteration count of the
13086
+ //! DatabaseHeader.
13087
+ struct DatabaseHeader {
13088
+ //! The iteration count, increases by 1 every time the storage is checkpointed.
13089
+ uint64_t iteration;
13090
+ //! A pointer to the initial meta block
13091
+ block_id_t meta_block;
13092
+ //! A pointer to the block containing the free list
13093
+ block_id_t free_list;
13094
+ //! The number of blocks that is in the file as of this database header. If the file is larger than BLOCK_SIZE *
13095
+ //! block_count any blocks appearing AFTER block_count are implicitly part of the free_list.
13096
+ uint64_t block_count;
13097
+
13098
+ void Serialize(Serializer &ser);
13099
+ static DatabaseHeader Deserialize(Deserializer &source);
13100
+ };
13101
+
13102
+ } // namespace duckdb
13103
+
13104
+
13105
+ namespace duckdb {
13106
+ class BlockHandle;
13107
+ class FileBuffer;
13108
+
13109
+ class BufferHandle {
13110
+ public:
13111
+ BufferHandle(shared_ptr<BlockHandle> handle, FileBuffer *node);
13112
+ ~BufferHandle();
13113
+
13114
+ //! The block handle
13115
+ shared_ptr<BlockHandle> handle;
13116
+ //! The managed buffer node
13117
+ FileBuffer *node;
13118
+ data_ptr_t Ptr();
13119
+ };
13120
+
13121
+ } // namespace duckdb
13122
+
13123
+ //===----------------------------------------------------------------------===//
13124
+ // DuckDB
13125
+ //
13126
+ // duckdb/storage/storage_lock.hpp
13127
+ //
13128
+ //
13129
+ //===----------------------------------------------------------------------===//
13130
+
13131
+
13132
+
13133
+
13134
+
13135
+
13136
+
13137
+ namespace duckdb {
13138
+ class StorageLock;
13139
+
13140
+ enum class StorageLockType { SHARED = 0, EXCLUSIVE = 1 };
13141
+
13142
+ class StorageLockKey {
13143
+ public:
13144
+ StorageLockKey(StorageLock &lock, StorageLockType type);
13145
+ ~StorageLockKey();
13146
+
13147
+ private:
13148
+ StorageLock &lock;
13149
+ StorageLockType type;
13150
+ };
13151
+
13152
+ class StorageLock {
13153
+ friend class StorageLockKey;
13154
+
13155
+ public:
13156
+ StorageLock();
13157
+
13158
+ //! Get an exclusive lock
13159
+ unique_ptr<StorageLockKey> GetExclusiveLock();
13160
+ //! Get a shared lock
13161
+ unique_ptr<StorageLockKey> GetSharedLock();
13162
+
13163
+ private:
13164
+ mutex exclusive_lock;
13165
+ atomic<idx_t> read_count;
13166
+
13167
+ private:
13168
+ //! Release an exclusive lock
13169
+ void ReleaseExclusiveLock();
13170
+ //! Release a shared lock
13171
+ void ReleaseSharedLock();
13172
+ };
13173
+
13174
+ } // namespace duckdb
13175
+
13176
+
13177
+ //===----------------------------------------------------------------------===//
13178
+ // DuckDB
13179
+ //
13180
+ // duckdb/execution/adaptive_filter.hpp
13181
+ //
13182
+ //
13183
+ //===----------------------------------------------------------------------===//
13184
+
13185
+
13186
+
13187
+ //===----------------------------------------------------------------------===//
13188
+ // DuckDB
13189
+ //
13190
+ // duckdb/planner/expression/bound_aggregate_expression.hpp
13191
+ //
13192
+ //
13193
+ //===----------------------------------------------------------------------===//
13194
+
13195
+
13196
+
13197
+
13198
+
13199
+ #include <memory>
13200
+
13201
+ namespace duckdb {
13202
+ class BoundAggregateExpression : public Expression {
13203
+ public:
13204
+ BoundAggregateExpression(AggregateFunction function, vector<unique_ptr<Expression>> children,
13205
+ unique_ptr<Expression> filter, unique_ptr<FunctionData> bind_info, bool distinct);
13206
+
13207
+ //! The bound function expression
13208
+ AggregateFunction function;
13209
+ //! List of arguments to the function
13210
+ vector<unique_ptr<Expression>> children;
13211
+ //! The bound function data (if any)
13212
+ unique_ptr<FunctionData> bind_info;
13213
+ //! True to aggregate on distinct values
13214
+ bool distinct;
13215
+
13216
+ //! Filter for this aggregate
13217
+ unique_ptr<Expression> filter;
13218
+
13219
+ public:
13220
+ bool IsAggregate() const override {
13221
+ return true;
13222
+ }
13223
+ bool IsFoldable() const override {
13224
+ return false;
13225
+ }
13226
+
13227
+ string ToString() const override;
13228
+
13229
+ hash_t Hash() const override;
13230
+ bool Equals(const BaseExpression *other) const override;
13231
+ unique_ptr<Expression> Copy() override;
13232
+ };
13233
+ } // namespace duckdb
13234
+
13235
+ //===----------------------------------------------------------------------===//
13236
+ // DuckDB
13237
+ //
13238
+ // duckdb/planner/expression/bound_between_expression.hpp
13239
+ //
13240
+ //
13241
+ //===----------------------------------------------------------------------===//
13242
+
13243
+
13244
+
13245
+
13246
+
13247
+ namespace duckdb {
13248
+
13249
+ class BoundBetweenExpression : public Expression {
13250
+ public:
13251
+ BoundBetweenExpression(unique_ptr<Expression> input, unique_ptr<Expression> lower, unique_ptr<Expression> upper,
13252
+ bool lower_inclusive, bool upper_inclusive);
13253
+
13254
+ unique_ptr<Expression> input;
13255
+ unique_ptr<Expression> lower;
13256
+ unique_ptr<Expression> upper;
13257
+ bool lower_inclusive;
13258
+ bool upper_inclusive;
13259
+
13260
+ public:
13261
+ string ToString() const override;
13262
+
13263
+ bool Equals(const BaseExpression *other) const override;
13264
+
13265
+ unique_ptr<Expression> Copy() override;
13266
+
13267
+ public:
13268
+ ExpressionType LowerComparisonType() {
13269
+ return lower_inclusive ? ExpressionType::COMPARE_GREATERTHANOREQUALTO : ExpressionType::COMPARE_GREATERTHAN;
13270
+ }
13271
+ ExpressionType UpperComparisonType() {
13272
+ return upper_inclusive ? ExpressionType::COMPARE_LESSTHANOREQUALTO : ExpressionType::COMPARE_LESSTHAN;
13273
+ }
13274
+ };
13275
+ } // namespace duckdb
13276
+
13277
+ //===----------------------------------------------------------------------===//
13278
+ // DuckDB
13279
+ //
13280
+ // duckdb/planner/expression/bound_case_expression.hpp
13281
+ //
13282
+ //
13283
+ //===----------------------------------------------------------------------===//
13284
+
13285
+
13286
+
13287
+
13288
+
13289
+ namespace duckdb {
13290
+
13291
+ struct BoundCaseCheck {
13292
+ unique_ptr<Expression> when_expr;
13293
+ unique_ptr<Expression> then_expr;
13294
+ };
13295
+
13296
+ class BoundCaseExpression : public Expression {
13297
+ public:
13298
+ BoundCaseExpression(LogicalType type);
13299
+ BoundCaseExpression(unique_ptr<Expression> when_expr, unique_ptr<Expression> then_expr,
13300
+ unique_ptr<Expression> else_expr);
13301
+
13302
+ vector<BoundCaseCheck> case_checks;
13303
+ unique_ptr<Expression> else_expr;
13304
+
13305
+ public:
13306
+ string ToString() const override;
13307
+
13308
+ bool Equals(const BaseExpression *other) const override;
13309
+
13310
+ unique_ptr<Expression> Copy() override;
13311
+ };
13312
+ } // namespace duckdb
13313
+
13314
+ //===----------------------------------------------------------------------===//
13315
+ // DuckDB
13316
+ //
13317
+ // duckdb/planner/expression/bound_cast_expression.hpp
13318
+ //
13319
+ //
13320
+ //===----------------------------------------------------------------------===//
13321
+
13322
+
13323
+
13324
+
13325
+
13326
+ namespace duckdb {
13327
+
13328
+ class BoundCastExpression : public Expression {
13329
+ public:
13330
+ BoundCastExpression(unique_ptr<Expression> child, LogicalType target_type, bool try_cast = false);
13331
+
13332
+ //! The child type
13333
+ unique_ptr<Expression> child;
13334
+ //! Whether to use try_cast or not. try_cast converts cast failures into NULLs instead of throwing an error.
13335
+ bool try_cast;
13336
+
13337
+ public:
13338
+ LogicalType source_type() {
13339
+ return child->return_type;
13340
+ }
13341
+
13342
+ //! Cast an expression to the specified SQL type if required
13343
+ static unique_ptr<Expression> AddCastToType(unique_ptr<Expression> expr, const LogicalType &target_type);
13344
+ //! Returns true if a cast is invertible (i.e. CAST(s -> t -> s) = s for all values of s). This is not true for e.g.
13345
+ //! boolean casts, because that can be e.g. -1 -> TRUE -> 1. This is necessary to prevent some optimizer bugs.
13346
+ static bool CastIsInvertible(const LogicalType &source_type, const LogicalType &target_type);
13347
+
13348
+ string ToString() const override;
13349
+
13350
+ bool Equals(const BaseExpression *other) const override;
13351
+
13352
+ unique_ptr<Expression> Copy() override;
13353
+ };
13354
+ } // namespace duckdb
13355
+
13356
+ //===----------------------------------------------------------------------===//
13357
+ // DuckDB
13358
+ //
13359
+ // duckdb/planner/expression/bound_columnref_expression.hpp
13360
+ //
13361
+ //
13362
+ //===----------------------------------------------------------------------===//
13363
+
13364
+
13365
+
13366
+
13367
+
13368
+
13369
+ namespace duckdb {
13370
+
13371
+ //! A BoundColumnRef expression represents a ColumnRef expression that was bound to an actual table and column index. It
13372
+ //! is not yet executable, however. The ColumnBindingResolver transforms the BoundColumnRefExpressions into
13373
+ //! BoundExpressions, which refer to indexes into the physical chunks that pass through the executor.
13374
+ class BoundColumnRefExpression : public Expression {
13375
+ public:
13376
+ BoundColumnRefExpression(LogicalType type, ColumnBinding binding, idx_t depth = 0);
13377
+ BoundColumnRefExpression(string alias, LogicalType type, ColumnBinding binding, idx_t depth = 0);
13378
+
13379
+ //! Column index set by the binder, used to generate the final BoundExpression
13380
+ ColumnBinding binding;
13381
+ //! The subquery depth (i.e. depth 0 = current query, depth 1 = parent query, depth 2 = parent of parent, etc...).
13382
+ //! This is only non-zero for correlated expressions inside subqueries.
13383
+ idx_t depth;
13384
+
13385
+ public:
13386
+ bool IsScalar() const override {
13387
+ return false;
13388
+ }
13389
+ bool IsFoldable() const override {
13390
+ return false;
13391
+ }
13392
+
13393
+ string ToString() const override;
13394
+
13395
+ bool Equals(const BaseExpression *other) const override;
13396
+ hash_t Hash() const override;
13397
+
13398
+ unique_ptr<Expression> Copy() override;
13399
+ };
13400
+ } // namespace duckdb
13401
+
13402
+ //===----------------------------------------------------------------------===//
13403
+ // DuckDB
13404
+ //
13405
+ // duckdb/planner/expression/bound_comparison_expression.hpp
13406
+ //
13407
+ //
13408
+ //===----------------------------------------------------------------------===//
13409
+
13410
+
13411
+
13412
+
13413
+
13414
+ namespace duckdb {
13415
+
13416
+ class BoundComparisonExpression : public Expression {
13417
+ public:
13418
+ BoundComparisonExpression(ExpressionType type, unique_ptr<Expression> left, unique_ptr<Expression> right);
13419
+
13420
+ unique_ptr<Expression> left;
13421
+ unique_ptr<Expression> right;
13422
+
13423
+ public:
13424
+ string ToString() const override;
13425
+
13426
+ bool Equals(const BaseExpression *other) const override;
13427
+
13428
+ unique_ptr<Expression> Copy() override;
13429
+
13430
+ public:
13431
+ static LogicalType BindComparison(LogicalType left_type, LogicalType right_type);
13432
+ };
13433
+ } // namespace duckdb
13434
+
13435
+ //===----------------------------------------------------------------------===//
13436
+ // DuckDB
13437
+ //
13438
+ // duckdb/planner/expression/bound_conjunction_expression.hpp
13439
+ //
13440
+ //
13441
+ //===----------------------------------------------------------------------===//
13442
+
13443
+
13444
+
13445
+
13446
+
13447
+ namespace duckdb {
13448
+
13449
+ class BoundConjunctionExpression : public Expression {
13450
+ public:
13451
+ explicit BoundConjunctionExpression(ExpressionType type);
13452
+ BoundConjunctionExpression(ExpressionType type, unique_ptr<Expression> left, unique_ptr<Expression> right);
13453
+
13454
+ vector<unique_ptr<Expression>> children;
13455
+
13456
+ public:
13457
+ string ToString() const override;
13458
+
13459
+ bool Equals(const BaseExpression *other) const override;
13460
+
13461
+ unique_ptr<Expression> Copy() override;
13462
+ };
13463
+ } // namespace duckdb
13464
+
13465
+ //===----------------------------------------------------------------------===//
13466
+ // DuckDB
13467
+ //
13468
+ // duckdb/planner/expression/bound_constant_expression.hpp
13469
+ //
13470
+ //
13471
+ //===----------------------------------------------------------------------===//
13472
+
13473
+
13474
+
13475
+
13476
+
13477
+
13478
+ namespace duckdb {
13479
+
13480
+ class BoundConstantExpression : public Expression {
13481
+ public:
13482
+ explicit BoundConstantExpression(Value value);
13483
+
13484
+ Value value;
13485
+
13486
+ public:
13487
+ string ToString() const override;
13488
+
13489
+ bool Equals(const BaseExpression *other) const override;
13490
+ hash_t Hash() const override;
13491
+
13492
+ unique_ptr<Expression> Copy() override;
13493
+ };
13494
+ } // namespace duckdb
13495
+
13496
+ //===----------------------------------------------------------------------===//
13497
+ // DuckDB
13498
+ //
13499
+ // duckdb/planner/expression/bound_default_expression.hpp
13500
+ //
13501
+ //
13502
+ //===----------------------------------------------------------------------===//
13503
+
13504
+
13505
+
13506
+
13507
+
13508
+ namespace duckdb {
13509
+
13510
+ class BoundDefaultExpression : public Expression {
13511
+ public:
13512
+ explicit BoundDefaultExpression(LogicalType type = LogicalType())
13513
+ : Expression(ExpressionType::VALUE_DEFAULT, ExpressionClass::BOUND_DEFAULT, type) {
13514
+ }
13515
+
13516
+ public:
13517
+ bool IsScalar() const override {
13518
+ return false;
13519
+ }
13520
+ bool IsFoldable() const override {
13521
+ return false;
13522
+ }
13523
+
13524
+ string ToString() const override {
13525
+ return "DEFAULT";
13526
+ }
13527
+
13528
+ unique_ptr<Expression> Copy() override {
13529
+ return make_unique<BoundDefaultExpression>(return_type);
13530
+ }
13531
+ };
13532
+ } // namespace duckdb
13533
+
13534
+ //===----------------------------------------------------------------------===//
13535
+ // DuckDB
13536
+ //
13537
+ // duckdb/planner/expression/bound_function_expression.hpp
13538
+ //
13539
+ //
13540
+ //===----------------------------------------------------------------------===//
13541
+
13542
+
13543
+
13544
+
13545
+
13546
+
13547
+ namespace duckdb {
13548
+ class ScalarFunctionCatalogEntry;
13549
+
13550
+ //! Represents a function call that has been bound to a base function
13551
+ class BoundFunctionExpression : public Expression {
13552
+ public:
13553
+ BoundFunctionExpression(LogicalType return_type, ScalarFunction bound_function,
13554
+ vector<unique_ptr<Expression>> arguments, unique_ptr<FunctionData> bind_info,
13555
+ bool is_operator = false);
13556
+
13557
+ // The bound function expression
13558
+ ScalarFunction function;
13559
+ //! List of child-expressions of the function
13560
+ vector<unique_ptr<Expression>> children;
13561
+ //! The bound function data (if any)
13562
+ unique_ptr<FunctionData> bind_info;
13563
+ //! Whether or not the function is an operator, only used for rendering
13564
+ bool is_operator;
13565
+
13566
+ public:
13567
+ bool HasSideEffects() const override;
13568
+ bool IsFoldable() const override;
13569
+ string ToString() const override;
13570
+
13571
+ hash_t Hash() const override;
13572
+ bool Equals(const BaseExpression *other) const override;
13573
+
13574
+ unique_ptr<Expression> Copy() override;
13575
+ };
13576
+ } // namespace duckdb
13577
+
13578
+ //===----------------------------------------------------------------------===//
13579
+ // DuckDB
13580
+ //
13581
+ // duckdb/planner/expression/bound_operator_expression.hpp
13582
+ //
13583
+ //
13584
+ //===----------------------------------------------------------------------===//
13585
+
13586
+
13587
+
13588
+
13589
+
13590
+ namespace duckdb {
13591
+
13592
+ class BoundOperatorExpression : public Expression {
13593
+ public:
13594
+ BoundOperatorExpression(ExpressionType type, LogicalType return_type);
13595
+
13596
+ vector<unique_ptr<Expression>> children;
13597
+
13598
+ public:
13599
+ string ToString() const override;
13600
+
13601
+ bool Equals(const BaseExpression *other) const override;
13602
+
13603
+ unique_ptr<Expression> Copy() override;
13604
+ };
13605
+ } // namespace duckdb
13606
+
13607
+ //===----------------------------------------------------------------------===//
13608
+ // DuckDB
13609
+ //
13610
+ // duckdb/planner/expression/bound_parameter_expression.hpp
13611
+ //
13612
+ //
13613
+ //===----------------------------------------------------------------------===//
13614
+
13615
+
13616
+
13617
+
13618
+
13619
+
13620
+ namespace duckdb {
13621
+
13622
+ class BoundParameterExpression : public Expression {
13623
+ public:
13624
+ explicit BoundParameterExpression(idx_t parameter_nr);
13625
+
13626
+ idx_t parameter_nr;
13627
+ Value *value;
13628
+
13629
+ public:
13630
+ bool IsScalar() const override;
13631
+ bool HasParameter() const override;
13632
+ bool IsFoldable() const override;
13633
+
13634
+ string ToString() const override;
13635
+
13636
+ bool Equals(const BaseExpression *other) const override;
13637
+ hash_t Hash() const override;
13638
+
13639
+ unique_ptr<Expression> Copy() override;
13640
+ };
13641
+ } // namespace duckdb
13642
+
13643
+ //===----------------------------------------------------------------------===//
13644
+ // DuckDB
13645
+ //
13646
+ // duckdb/planner/expression/bound_reference_expression.hpp
13647
+ //
13648
+ //
13649
+ //===----------------------------------------------------------------------===//
13650
+
13651
+
13652
+
13653
+
13654
+
13655
+ namespace duckdb {
13656
+
13657
+ //! A BoundReferenceExpression represents a physical index into a DataChunk
13658
+ class BoundReferenceExpression : public Expression {
13659
+ public:
13660
+ BoundReferenceExpression(string alias, LogicalType type, idx_t index);
13661
+ BoundReferenceExpression(LogicalType type, idx_t index);
13662
+
13663
+ //! Index used to access data in the chunks
13664
+ idx_t index;
13665
+
13666
+ public:
13667
+ bool IsScalar() const override {
13668
+ return false;
13669
+ }
13670
+ bool IsFoldable() const override {
13671
+ return false;
13672
+ }
13673
+
13674
+ string ToString() const override;
13675
+
13676
+ hash_t Hash() const override;
13677
+ bool Equals(const BaseExpression *other) const override;
13678
+
13679
+ unique_ptr<Expression> Copy() override;
13680
+ };
13681
+ } // namespace duckdb
13682
+
13683
+ //===----------------------------------------------------------------------===//
13684
+ // DuckDB
13685
+ //
13686
+ // duckdb/planner/expression/bound_subquery_expression.hpp
13687
+ //
13688
+ //
13689
+ //===----------------------------------------------------------------------===//
13690
+
13691
+
13692
+
13693
+ //===----------------------------------------------------------------------===//
13694
+ // DuckDB
13695
+ //
13696
+ // duckdb/common/enums/subquery_type.hpp
13697
+ //
13698
+ //
13699
+ //===----------------------------------------------------------------------===//
13700
+
13701
+
13702
+
13703
+
13704
+
13705
+ namespace duckdb {
13706
+
13707
+ //===--------------------------------------------------------------------===//
13708
+ // Subquery Types
13709
+ //===--------------------------------------------------------------------===//
13710
+ enum class SubqueryType : uint8_t {
13711
+ INVALID = 0,
13712
+ SCALAR = 1, // Regular scalar subquery
13713
+ EXISTS = 2, // EXISTS (SELECT...)
13714
+ NOT_EXISTS = 3, // NOT EXISTS(SELECT...)
13715
+ ANY = 4, // x = ANY(SELECT...) OR x IN (SELECT...)
13716
+ };
13717
+
13718
+ } // namespace duckdb
13719
+
13720
+ //===----------------------------------------------------------------------===//
13721
+ // DuckDB
13722
+ //
13723
+ // duckdb/planner/binder.hpp
13724
+ //
13725
+ //
13726
+ //===----------------------------------------------------------------------===//
13727
+
13728
+
13729
+
13730
+
13731
+
13732
+ //===----------------------------------------------------------------------===//
13733
+ // DuckDB
13734
+ //
13735
+ // duckdb/parser/tokens.hpp
13736
+ //
13737
+ //
13738
+ //===----------------------------------------------------------------------===//
13739
+
13740
+
13741
+
13742
+ namespace duckdb {
13743
+
13744
+ //===--------------------------------------------------------------------===//
13745
+ // Statements
13746
+ //===--------------------------------------------------------------------===//
13747
+ class SQLStatement;
13748
+
13749
+ class AlterStatement;
13750
+ class CallStatement;
13751
+ class CopyStatement;
13752
+ class CreateStatement;
13753
+ class DeleteStatement;
13754
+ class DropStatement;
13755
+ class InsertStatement;
13756
+ class SelectStatement;
13757
+ class TransactionStatement;
13758
+ class UpdateStatement;
13759
+ class PrepareStatement;
13760
+ class ExecuteStatement;
13761
+ class PragmaStatement;
13762
+ class ShowStatement;
13763
+ class ExplainStatement;
13764
+ class ExportStatement;
13765
+ class VacuumStatement;
13766
+ class RelationStatement;
13767
+ class SetStatement;
13768
+ class LoadStatement;
13769
+
13770
+ //===--------------------------------------------------------------------===//
13771
+ // Query Node
13772
+ //===--------------------------------------------------------------------===//
13773
+ class QueryNode;
13774
+ class SelectNode;
13775
+ class SetOperationNode;
13776
+ class RecursiveCTENode;
13777
+
13778
+ //===--------------------------------------------------------------------===//
13779
+ // Expressions
13780
+ //===--------------------------------------------------------------------===//
13781
+ class ParsedExpression;
13782
+
13783
+ class BetweenExpression;
13784
+ class CaseExpression;
13785
+ class CastExpression;
13786
+ class CollateExpression;
13787
+ class ColumnRefExpression;
13788
+ class ComparisonExpression;
13789
+ class ConjunctionExpression;
13790
+ class ConstantExpression;
13791
+ class DefaultExpression;
13792
+ class FunctionExpression;
13793
+ class LambdaExpression;
13794
+ class OperatorExpression;
13795
+ class ParameterExpression;
13796
+ class PositionalReferenceExpression;
13797
+ class StarExpression;
13798
+ class SubqueryExpression;
13799
+ class WindowExpression;
13800
+
13801
+ //===--------------------------------------------------------------------===//
13802
+ // Constraints
13803
+ //===--------------------------------------------------------------------===//
13804
+ class Constraint;
13805
+
13806
+ class NotNullConstraint;
13807
+ class CheckConstraint;
13808
+ class UniqueConstraint;
13809
+
13810
+ //===--------------------------------------------------------------------===//
13811
+ // TableRefs
13812
+ //===--------------------------------------------------------------------===//
13813
+ class TableRef;
13814
+
13815
+ class BaseTableRef;
13816
+ class CrossProductRef;
13817
+ class JoinRef;
13818
+ class SubqueryRef;
13819
+ class TableFunctionRef;
13820
+ class EmptyTableRef;
13821
+ class ExpressionListRef;
13822
+
13823
+ //===--------------------------------------------------------------------===//
13824
+ // Other
13825
+ //===--------------------------------------------------------------------===//
13826
+ struct SampleOptions;
13827
+
13828
+ } // namespace duckdb
13829
+
13830
+ //===----------------------------------------------------------------------===//
13831
+ // DuckDB
13832
+ //
13833
+ // duckdb/planner/bind_context.hpp
13834
+ //
13835
+ //
13836
+ //===----------------------------------------------------------------------===//
13837
+
13838
+
13839
+
13840
+
13841
+
13842
+ //===----------------------------------------------------------------------===//
13843
+ // DuckDB
13844
+ //
13845
+ // duckdb/catalog/catalog_entry/table_function_catalog_entry.hpp
13846
+ //
13847
+ //
13848
+ //===----------------------------------------------------------------------===//
13849
+
13850
+
13851
+
13852
+
13853
+ //===----------------------------------------------------------------------===//
13854
+ // DuckDB
13855
+ //
13856
+ // duckdb/function/table_function.hpp
13857
+ //
13858
+ //
13859
+ //===----------------------------------------------------------------------===//
13860
+
13861
+
13862
+
13863
+
13864
+
13865
+
13866
+
13867
+ #include <functional>
13868
+
13869
+ namespace duckdb {
13870
+ class BaseStatistics;
13871
+ class LogicalGet;
13872
+ struct ParallelState;
13873
+ class TableFilterSet;
13874
+
13875
+ struct FunctionOperatorData {
13876
+ DUCKDB_API virtual ~FunctionOperatorData();
13877
+ };
13878
+
13879
+ struct TableFilterCollection {
13880
+ DUCKDB_API explicit TableFilterCollection(TableFilterSet *table_filters);
13881
+
13882
+ TableFilterSet *table_filters;
13883
+ };
13884
+
13885
+ typedef unique_ptr<FunctionData> (*table_function_bind_t)(ClientContext &context, vector<Value> &inputs,
13886
+ unordered_map<string, Value> &named_parameters,
13887
+ vector<LogicalType> &input_table_types,
13888
+ vector<string> &input_table_names,
13889
+ vector<LogicalType> &return_types, vector<string> &names);
13890
+ typedef unique_ptr<FunctionOperatorData> (*table_function_init_t)(ClientContext &context, const FunctionData *bind_data,
13891
+ const vector<column_t> &column_ids,
13892
+ TableFilterCollection *filters);
13893
+ typedef unique_ptr<BaseStatistics> (*table_statistics_t)(ClientContext &context, const FunctionData *bind_data,
13894
+ column_t column_index);
13895
+ typedef void (*table_function_t)(ClientContext &context, const FunctionData *bind_data,
13896
+ FunctionOperatorData *operator_state, DataChunk *input, DataChunk &output);
13897
+
13898
+ typedef void (*table_function_parallel_t)(ClientContext &context, const FunctionData *bind_data,
13899
+ FunctionOperatorData *operator_state, DataChunk *input, DataChunk &output,
13900
+ ParallelState *parallel_state);
13901
+
13902
+ typedef void (*table_function_cleanup_t)(ClientContext &context, const FunctionData *bind_data,
13903
+ FunctionOperatorData *operator_state);
13904
+ typedef idx_t (*table_function_max_threads_t)(ClientContext &context, const FunctionData *bind_data);
13905
+ typedef unique_ptr<ParallelState> (*table_function_init_parallel_state_t)(ClientContext &context,
13906
+ const FunctionData *bind_data,
13907
+ const vector<column_t> &column_ids,
13908
+ TableFilterCollection *filters);
13909
+ typedef unique_ptr<FunctionOperatorData> (*table_function_init_parallel_t)(ClientContext &context,
13910
+ const FunctionData *bind_data,
13911
+ ParallelState *state,
13912
+ const vector<column_t> &column_ids,
13913
+ TableFilterCollection *filters);
13914
+ typedef bool (*table_function_parallel_state_next_t)(ClientContext &context, const FunctionData *bind_data,
13915
+ FunctionOperatorData *state, ParallelState *parallel_state);
13916
+ typedef int (*table_function_progress_t)(ClientContext &context, const FunctionData *bind_data);
13917
+ typedef void (*table_function_dependency_t)(unordered_set<CatalogEntry *> &dependencies, const FunctionData *bind_data);
13918
+ typedef unique_ptr<NodeStatistics> (*table_function_cardinality_t)(ClientContext &context,
13919
+ const FunctionData *bind_data);
13920
+ typedef void (*table_function_pushdown_complex_filter_t)(ClientContext &context, LogicalGet &get,
13921
+ FunctionData *bind_data,
13922
+ vector<unique_ptr<Expression>> &filters);
13923
+ typedef string (*table_function_to_string_t)(const FunctionData *bind_data);
13924
+
13925
+ class TableFunction : public SimpleNamedParameterFunction {
13926
+ public:
13927
+ DUCKDB_API
13928
+ TableFunction(string name, vector<LogicalType> arguments, table_function_t function,
13929
+ table_function_bind_t bind = nullptr, table_function_init_t init = nullptr,
13930
+ table_statistics_t statistics = nullptr, table_function_cleanup_t cleanup = nullptr,
13931
+ table_function_dependency_t dependency = nullptr, table_function_cardinality_t cardinality = nullptr,
13932
+ table_function_pushdown_complex_filter_t pushdown_complex_filter = nullptr,
13933
+ table_function_to_string_t to_string = nullptr, table_function_max_threads_t max_threads = nullptr,
13934
+ table_function_init_parallel_state_t init_parallel_state = nullptr,
13935
+ table_function_parallel_t parallel_function = nullptr,
13936
+ table_function_init_parallel_t parallel_init = nullptr,
13937
+ table_function_parallel_state_next_t parallel_state_next = nullptr, bool projection_pushdown = false,
13938
+ bool filter_pushdown = false, table_function_progress_t query_progress = nullptr);
13939
+ DUCKDB_API
13940
+ TableFunction(const vector<LogicalType> &arguments, table_function_t function, table_function_bind_t bind = nullptr,
13941
+ table_function_init_t init = nullptr, table_statistics_t statistics = nullptr,
13942
+ table_function_cleanup_t cleanup = nullptr, table_function_dependency_t dependency = nullptr,
13943
+ table_function_cardinality_t cardinality = nullptr,
13944
+ table_function_pushdown_complex_filter_t pushdown_complex_filter = nullptr,
13945
+ table_function_to_string_t to_string = nullptr, table_function_max_threads_t max_threads = nullptr,
13946
+ table_function_init_parallel_state_t init_parallel_state = nullptr,
13947
+ table_function_parallel_t parallel_function = nullptr,
13948
+ table_function_init_parallel_t parallel_init = nullptr,
13949
+ table_function_parallel_state_next_t parallel_state_next = nullptr, bool projection_pushdown = false,
13950
+ bool filter_pushdown = false, table_function_progress_t query_progress = nullptr);
13951
+ DUCKDB_API TableFunction();
13952
+
13953
+ //! Bind function
13954
+ //! This function is used for determining the return type of a table producing function and returning bind data
13955
+ //! The returned FunctionData object should be constant and should not be changed during execution.
13956
+ table_function_bind_t bind;
13957
+ //! (Optional) init function
13958
+ //! Initialize the operator state of the function. The operator state is used to keep track of the progress in the
13959
+ //! table function.
13960
+ table_function_init_t init;
13961
+ //! The main function
13962
+ table_function_t function;
13963
+ //! (Optional) statistics function
13964
+ //! Returns the statistics of a specified column
13965
+ table_statistics_t statistics;
13966
+ //! (Optional) cleanup function
13967
+ //! The final cleanup function, called after all data is exhausted from the main function
13968
+ table_function_cleanup_t cleanup;
13969
+ //! (Optional) dependency function
13970
+ //! Sets up which catalog entries this table function depend on
13971
+ table_function_dependency_t dependency;
13972
+ //! (Optional) cardinality function
13973
+ //! Returns the expected cardinality of this scan
13974
+ table_function_cardinality_t cardinality;
13975
+ //! (Optional) pushdown a set of arbitrary filter expressions, rather than only simple comparisons with a constant
13976
+ //! Any functions remaining in the expression list will be pushed as a regular filter after the scan
13977
+ table_function_pushdown_complex_filter_t pushdown_complex_filter;
13978
+ //! (Optional) function for rendering the operator to a string in profiling output
13979
+ table_function_to_string_t to_string;
13980
+ //! (Optional) function that returns the maximum amount of threads that can work on this task
13981
+ table_function_max_threads_t max_threads;
13982
+ //! (Optional) initialize the parallel scan state, called once in total.
13983
+ table_function_init_parallel_state_t init_parallel_state;
13984
+ //! (Optional) Parallel version of the main function
13985
+ table_function_parallel_t parallel_function;
13986
+ //! (Optional) initialize the parallel scan given the parallel state. Called once per task. Return nullptr if there
13987
+ //! is nothing left to scan.
13988
+ table_function_init_parallel_t parallel_init;
13989
+ //! (Optional) return the next chunk to process in the parallel scan, or return nullptr if there is none
13990
+ table_function_parallel_state_next_t parallel_state_next;
13991
+ //! (Optional) return how much of the table we have scanned up to this point (% of the data)
13992
+ table_function_progress_t table_scan_progress;
13993
+ //! Whether or not the table function supports projection pushdown. If not supported a projection will be added
13994
+ //! that filters out unused columns.
13995
+ bool projection_pushdown;
13996
+ //! Whether or not the table function supports filter pushdown. If not supported a filter will be added
13997
+ //! that applies the table filter directly.
13998
+ bool filter_pushdown;
13999
+ };
14000
+
14001
+ } // namespace duckdb
14002
+
14003
+
14004
+
14005
+ namespace duckdb {
14006
+
14007
+ class Catalog;
14008
+ class Constraint;
14009
+
14010
+ struct CreateTableFunctionInfo;
14011
+
14012
+ //! A table function in the catalog
14013
+ class TableFunctionCatalogEntry : public StandardEntry {
14014
+ public:
14015
+ TableFunctionCatalogEntry(Catalog *catalog, SchemaCatalogEntry *schema, CreateTableFunctionInfo *info);
14016
+
14017
+ //! The table function
14018
+ vector<TableFunction> functions;
14019
+ };
14020
+ } // namespace duckdb
14021
+
14022
+
14023
+
14024
+
14025
+ //===----------------------------------------------------------------------===//
14026
+ // DuckDB
14027
+ //
14028
+ // duckdb/parser/expression/columnref_expression.hpp
14029
+ //
14030
+ //
14031
+ //===----------------------------------------------------------------------===//
14032
+
14033
+
14034
+
14035
+
14036
+
14037
+
14038
+ namespace duckdb {
14039
+
14040
+ //! Represents a reference to a column from either the FROM clause or from an
14041
+ //! alias
14042
+ class ColumnRefExpression : public ParsedExpression {
14043
+ public:
14044
+ //! Specify both the column and table name
14045
+ ColumnRefExpression(string column_name, string table_name);
14046
+ //! Only specify the column name, the table name will be derived later
14047
+ explicit ColumnRefExpression(string column_name);
14048
+ //! Specify a set of names
14049
+ explicit ColumnRefExpression(vector<string> column_names);
14050
+
14051
+ //! The stack of names in order of which they appear (column_names[0].column_names[1].column_names[2]....)
14052
+ vector<string> column_names;
14053
+
14054
+ public:
14055
+ bool IsQualified() const;
14056
+ const string &GetColumnName() const;
14057
+ const string &GetTableName() const;
14058
+ bool IsScalar() const override {
14059
+ return false;
14060
+ }
14061
+
14062
+ string GetName() const override;
14063
+ string ToString() const override;
14064
+
14065
+ static bool Equals(const ColumnRefExpression *a, const ColumnRefExpression *b);
14066
+ hash_t Hash() const override;
14067
+
14068
+ unique_ptr<ParsedExpression> Copy() const override;
14069
+
14070
+ void Serialize(Serializer &serializer) override;
14071
+ static unique_ptr<ParsedExpression> Deserialize(ExpressionType type, Deserializer &source);
14072
+ };
14073
+ } // namespace duckdb
14074
+
14075
+
14076
+ //===----------------------------------------------------------------------===//
14077
+ // DuckDB
14078
+ //
14079
+ // duckdb/parser/qualified_name_set.hpp
14080
+ //
14081
+ //
14082
+ //===----------------------------------------------------------------------===//
14083
+
14084
+
14085
+
14086
+ //===----------------------------------------------------------------------===//
14087
+ // DuckDB
14088
+ //
14089
+ // duckdb/parser/qualified_name.hpp
14090
+ //
14091
+ //
14092
+ //===----------------------------------------------------------------------===//
14093
+
14094
+
14095
+
14096
+
14097
+
14098
+
14099
+ namespace duckdb {
14100
+
14101
+ struct QualifiedName {
14102
+ string schema;
14103
+ string name;
14104
+
14105
+ //! Parse the (optional) schema and a name from a string in the format of e.g. "schema"."table"; if there is no dot
14106
+ //! the schema will be set to INVALID_SCHEMA
14107
+ static QualifiedName Parse(string input) {
14108
+ string schema;
14109
+ string name;
14110
+ idx_t idx = 0;
14111
+ vector<string> entries;
14112
+ string entry;
14113
+ normal:
14114
+ //! quote
14115
+ for (; idx < input.size(); idx++) {
14116
+ if (input[idx] == '"') {
14117
+ idx++;
14118
+ goto quoted;
14119
+ } else if (input[idx] == '.') {
14120
+ goto separator;
14121
+ }
14122
+ entry += input[idx];
14123
+ }
14124
+ goto end;
14125
+ separator:
14126
+ entries.push_back(entry);
14127
+ entry = "";
14128
+ idx++;
14129
+ goto normal;
14130
+ quoted:
14131
+ //! look for another quote
14132
+ for (; idx < input.size(); idx++) {
14133
+ if (input[idx] == '"') {
14134
+ //! unquote
14135
+ idx++;
14136
+ goto normal;
14137
+ }
14138
+ entry += input[idx];
14139
+ }
14140
+ throw ParserException("Unterminated quote in qualified name!");
14141
+ end:
14142
+ if (entries.empty()) {
14143
+ schema = INVALID_SCHEMA;
14144
+ name = entry;
14145
+ } else if (entries.size() == 1) {
14146
+ schema = entries[0];
14147
+ name = entry;
14148
+ } else {
14149
+ throw ParserException("Expected schema.entry or entry: too many entries found");
14150
+ }
14151
+ return QualifiedName {schema, name};
14152
+ }
14153
+ };
14154
+
14155
+ struct QualifiedColumnName {
14156
+ QualifiedColumnName() {
14157
+ }
14158
+ QualifiedColumnName(string table_p, string column_p) : table(move(table_p)), column(move(column_p)) {
14159
+ }
14160
+
14161
+ string schema;
14162
+ string table;
14163
+ string column;
14164
+ };
14165
+
14166
+ } // namespace duckdb
14167
+
14168
+ //===----------------------------------------------------------------------===//
14169
+ // DuckDB
14170
+ //
14171
+ // duckdb/common/types/hash.hpp
14172
+ //
14173
+ //
14174
+ //===----------------------------------------------------------------------===//
14175
+
14176
+
14177
+
14178
+
14179
+
14180
+
14181
+ namespace duckdb {
14182
+
14183
+ struct string_t;
14184
+
14185
+ // efficient hash function that maximizes the avalanche effect and minimizes
14186
+ // bias
14187
+ // see: https://nullprogram.com/blog/2018/07/31/
14188
+
14189
+ inline hash_t murmurhash64(uint64_t x) {
14190
+ return x * UINT64_C(0xbf58476d1ce4e5b9);
14191
+ }
14192
+
14193
+ inline hash_t murmurhash32(uint32_t x) {
14194
+ return murmurhash64(x);
14195
+ }
14196
+
14197
+ template <class T>
14198
+ hash_t Hash(T value) {
14199
+ return murmurhash32(value);
14200
+ }
14201
+
14202
+ //! Combine two hashes by XORing them
14203
+ inline hash_t CombineHash(hash_t left, hash_t right) {
14204
+ return left ^ right;
14205
+ }
14206
+
14207
+ template <>
14208
+ hash_t Hash(uint64_t val);
14209
+ template <>
14210
+ hash_t Hash(int64_t val);
14211
+ template <>
14212
+ hash_t Hash(hugeint_t val);
14213
+ template <>
14214
+ hash_t Hash(float val);
14215
+ template <>
14216
+ hash_t Hash(double val);
14217
+ template <>
14218
+ hash_t Hash(const char *val);
14219
+ template <>
14220
+ hash_t Hash(char *val);
14221
+ template <>
14222
+ hash_t Hash(string_t val);
14223
+ template <>
14224
+ hash_t Hash(interval_t val);
14225
+ hash_t Hash(const char *val, size_t size);
14226
+ hash_t Hash(uint8_t *val, size_t size);
14227
+
14228
+ } // namespace duckdb
14229
+
14230
+
14231
+
14232
+ namespace duckdb {
14233
+
14234
+ struct QualifiedColumnHashFunction {
14235
+ uint64_t operator()(const QualifiedColumnName &a) const {
14236
+ std::hash<std::string> str_hasher;
14237
+ return str_hasher(a.schema) ^ str_hasher(a.table) ^ str_hasher(a.column);
14238
+ }
14239
+ };
14240
+
14241
+ struct QualifiedColumnEquality {
14242
+ bool operator()(const QualifiedColumnName &a, const QualifiedColumnName &b) const {
14243
+ return a.schema == b.schema && a.table == b.table && a.column == b.column;
14244
+ }
14245
+ };
14246
+
14247
+ using qualified_column_set_t = unordered_set<QualifiedColumnName, QualifiedColumnHashFunction, QualifiedColumnEquality>;
14248
+
14249
+ } // namespace duckdb
14250
+
14251
+
14252
+ //===----------------------------------------------------------------------===//
14253
+ // DuckDB
14254
+ //
14255
+ // duckdb/planner/expression_binder.hpp
14256
+ //
14257
+ //
14258
+ //===----------------------------------------------------------------------===//
14259
+
14260
+
14261
+
14262
+
14263
+ //===----------------------------------------------------------------------===//
14264
+ // DuckDB
14265
+ //
14266
+ // duckdb/parser/expression/bound_expression.hpp
14267
+ //
14268
+ //
14269
+ //===----------------------------------------------------------------------===//
14270
+
14271
+
14272
+
14273
+
14274
+
14275
+
14276
+
14277
+ namespace duckdb {
14278
+
14279
+ //! BoundExpression is an intermediate dummy class used by the binder. It is a ParsedExpression but holds an Expression.
14280
+ //! It represents a successfully bound expression. It is used in the Binder to prevent re-binding of already bound parts
14281
+ //! when dealing with subqueries.
14282
+ class BoundExpression : public ParsedExpression {
14283
+ public:
14284
+ BoundExpression(unique_ptr<Expression> expr)
14285
+ : ParsedExpression(ExpressionType::INVALID, ExpressionClass::BOUND_EXPRESSION), expr(move(expr)) {
14286
+ }
14287
+
14288
+ unique_ptr<Expression> expr;
14289
+
14290
+ public:
14291
+ string ToString() const override {
14292
+ return expr->ToString();
14293
+ }
14294
+
14295
+ bool Equals(const BaseExpression *other) const override {
14296
+ return false;
14297
+ }
14298
+ hash_t Hash() const override {
14299
+ return 0;
14300
+ }
14301
+
14302
+ unique_ptr<ParsedExpression> Copy() const override {
14303
+ throw SerializationException("Cannot copy or serialize bound expression");
14304
+ }
14305
+ };
14306
+
14307
+ } // namespace duckdb
14308
+
14309
+
14310
+
14311
+
14312
+
14313
+
14314
+ namespace duckdb {
14315
+
14316
+ class Binder;
14317
+ class ClientContext;
14318
+ class QueryNode;
14319
+
14320
+ class ScalarFunctionCatalogEntry;
14321
+ class AggregateFunctionCatalogEntry;
14322
+ class MacroCatalogEntry;
14323
+ class CatalogEntry;
14324
+ class SimpleFunction;
14325
+
14326
+ struct MacroBinding;
14327
+
14328
+ struct BoundColumnReferenceInfo {
14329
+ string name;
14330
+ idx_t query_location;
14331
+ };
14332
+
14333
+ struct BindResult {
14334
+ BindResult() {
14335
+ }
14336
+ explicit BindResult(string error) : error(error) {
14337
+ }
14338
+ explicit BindResult(unique_ptr<Expression> expr) : expression(move(expr)) {
14339
+ }
14340
+
14341
+ bool HasError() {
14342
+ return !error.empty();
14343
+ }
14344
+
14345
+ unique_ptr<Expression> expression;
14346
+ string error;
14347
+ };
14348
+
14349
+ class ExpressionBinder {
14350
+ public:
14351
+ ExpressionBinder(Binder &binder, ClientContext &context, bool replace_binder = false);
14352
+ virtual ~ExpressionBinder();
14353
+
14354
+ //! The target type that should result from the binder. If the result is not of this type, a cast to this type will
14355
+ //! be added. Defaults to INVALID.
14356
+ LogicalType target_type;
14357
+
14358
+ public:
14359
+ unique_ptr<Expression> Bind(unique_ptr<ParsedExpression> &expr, LogicalType *result_type = nullptr,
14360
+ bool root_expression = true);
14361
+
14362
+ //! Returns whether or not any columns have been bound by the expression binder
14363
+ bool HasBoundColumns() {
14364
+ return !bound_columns.empty();
14365
+ }
14366
+ const vector<BoundColumnReferenceInfo> &GetBoundColumns() {
14367
+ return bound_columns;
14368
+ }
14369
+
14370
+ string Bind(unique_ptr<ParsedExpression> *expr, idx_t depth, bool root_expression = false);
14371
+
14372
+ unique_ptr<ParsedExpression> CreateStructExtract(unique_ptr<ParsedExpression> base, string field_name);
14373
+ BindResult BindQualifiedColumnName(ColumnRefExpression &colref, const string &table_name);
14374
+
14375
+ unique_ptr<ParsedExpression> QualifyColumnName(const string &column_name, string &error_message);
14376
+ unique_ptr<ParsedExpression> QualifyColumnName(ColumnRefExpression &colref, string &error_message);
14377
+
14378
+ // Bind table names to ColumnRefExpressions
14379
+ void QualifyColumnNames(unique_ptr<ParsedExpression> &expr);
14380
+ static void QualifyColumnNames(Binder &binder, unique_ptr<ParsedExpression> &expr);
14381
+
14382
+ static unique_ptr<Expression> PushCollation(ClientContext &context, unique_ptr<Expression> source,
14383
+ const string &collation, bool equality_only = false);
14384
+ static void TestCollation(ClientContext &context, const string &collation);
14385
+
14386
+ bool BindCorrelatedColumns(unique_ptr<ParsedExpression> &expr);
14387
+
14388
+ void BindChild(unique_ptr<ParsedExpression> &expr, idx_t depth, string &error);
14389
+ static void ExtractCorrelatedExpressions(Binder &binder, Expression &expr);
14390
+
14391
+ static bool ContainsNullType(const LogicalType &type);
14392
+ static LogicalType ExchangeNullType(const LogicalType &type);
14393
+ static bool ContainsType(const LogicalType &type, LogicalTypeId target);
14394
+ static LogicalType ExchangeType(const LogicalType &type, LogicalTypeId target, LogicalType new_type);
14395
+
14396
+ static void ResolveParameterType(LogicalType &type);
14397
+ static void ResolveParameterType(unique_ptr<Expression> &expr);
14398
+
14399
+ //! Bind the given expresion. Unlike Bind(), this does *not* mute the given ParsedExpression.
14400
+ //! Exposed to be used from sub-binders that aren't subclasses of ExpressionBinder.
14401
+ virtual BindResult BindExpression(unique_ptr<ParsedExpression> *expr_ptr, idx_t depth,
14402
+ bool root_expression = false);
14403
+
14404
+ protected:
14405
+ BindResult BindExpression(BetweenExpression &expr, idx_t depth);
14406
+ BindResult BindExpression(CaseExpression &expr, idx_t depth);
14407
+ BindResult BindExpression(CollateExpression &expr, idx_t depth);
14408
+ BindResult BindExpression(CastExpression &expr, idx_t depth);
14409
+ BindResult BindExpression(ColumnRefExpression &expr, idx_t depth);
14410
+ BindResult BindExpression(ComparisonExpression &expr, idx_t depth);
14411
+ BindResult BindExpression(ConjunctionExpression &expr, idx_t depth);
14412
+ BindResult BindExpression(ConstantExpression &expr, idx_t depth);
14413
+ BindResult BindExpression(FunctionExpression &expr, idx_t depth, unique_ptr<ParsedExpression> *expr_ptr);
14414
+ BindResult BindExpression(LambdaExpression &expr, idx_t depth);
14415
+ BindResult BindExpression(OperatorExpression &expr, idx_t depth);
14416
+ BindResult BindExpression(ParameterExpression &expr, idx_t depth);
14417
+ BindResult BindExpression(PositionalReferenceExpression &ref, idx_t depth);
14418
+ BindResult BindExpression(StarExpression &expr, idx_t depth);
14419
+ BindResult BindExpression(SubqueryExpression &expr, idx_t depth);
14420
+
14421
+ protected:
14422
+ virtual BindResult BindGroupingFunction(OperatorExpression &op, idx_t depth);
14423
+ virtual BindResult BindFunction(FunctionExpression &expr, ScalarFunctionCatalogEntry *function, idx_t depth);
14424
+ virtual BindResult BindAggregate(FunctionExpression &expr, AggregateFunctionCatalogEntry *function, idx_t depth);
14425
+ virtual BindResult BindUnnest(FunctionExpression &expr, idx_t depth);
14426
+ virtual BindResult BindMacro(FunctionExpression &expr, MacroCatalogEntry *macro, idx_t depth,
14427
+ unique_ptr<ParsedExpression> *expr_ptr);
14428
+
14429
+ void ReplaceMacroParametersRecursive(unique_ptr<ParsedExpression> &expr);
14430
+
14431
+ virtual string UnsupportedAggregateMessage();
14432
+ virtual string UnsupportedUnnestMessage();
14433
+
14434
+ Binder &binder;
14435
+ ClientContext &context;
14436
+ ExpressionBinder *stored_binder;
14437
+ MacroBinding *macro_binding;
14438
+ vector<BoundColumnReferenceInfo> bound_columns;
14439
+ };
14440
+
14441
+ } // namespace duckdb
14442
+
14443
+ //===----------------------------------------------------------------------===//
14444
+ // DuckDB
14445
+ //
14446
+ // duckdb/planner/table_binding.hpp
14447
+ //
14448
+ //
14449
+ //===----------------------------------------------------------------------===//
14450
+
14451
+
14452
+
14453
+
14454
+
14455
+
14456
+
14457
+
14458
+
14459
+ namespace duckdb {
14460
+ class BindContext;
14461
+ class BoundQueryNode;
14462
+ class ColumnRefExpression;
14463
+ class SubqueryRef;
14464
+ class LogicalGet;
14465
+ class TableCatalogEntry;
14466
+ class TableFunctionCatalogEntry;
14467
+ class BoundTableFunction;
14468
+
14469
+ //! A Binding represents a binding to a table, table-producing function or subquery with a specified table index.
14470
+ struct Binding {
14471
+ Binding(const string &alias, vector<LogicalType> types, vector<string> names, idx_t index);
14472
+ virtual ~Binding() = default;
14473
+
14474
+ //! The alias of the binding
14475
+ string alias;
14476
+ //! The table index of the binding
14477
+ idx_t index;
14478
+ vector<LogicalType> types;
14479
+ //! Column names of the subquery
14480
+ vector<string> names;
14481
+ //! Name -> index for the names
14482
+ case_insensitive_map_t<column_t> name_map;
14483
+
14484
+ public:
14485
+ bool TryGetBindingIndex(const string &column_name, column_t &column_index);
14486
+ column_t GetBindingIndex(const string &column_name);
14487
+ bool HasMatchingBinding(const string &column_name);
14488
+ virtual string ColumnNotFoundError(const string &column_name) const;
14489
+ virtual BindResult Bind(ColumnRefExpression &colref, idx_t depth);
14490
+ virtual TableCatalogEntry *GetTableEntry();
14491
+ };
14492
+
14493
+ //! TableBinding is exactly like the Binding, except it keeps track of which columns were bound in the linked LogicalGet
14494
+ //! node for projection pushdown purposes.
14495
+ struct TableBinding : public Binding {
14496
+ TableBinding(const string &alias, vector<LogicalType> types, vector<string> names, LogicalGet &get, idx_t index,
14497
+ bool add_row_id = false);
14498
+
14499
+ //! the underlying LogicalGet
14500
+ LogicalGet &get;
14501
+
14502
+ public:
14503
+ BindResult Bind(ColumnRefExpression &colref, idx_t depth) override;
14504
+ TableCatalogEntry *GetTableEntry() override;
14505
+ string ColumnNotFoundError(const string &column_name) const override;
14506
+ };
14507
+
14508
+ //! MacroBinding is like the Binding, except the alias and index are set by default. Used for binding Macro
14509
+ //! Params/Arguments.
14510
+ struct MacroBinding : public Binding {
14511
+ static constexpr const char *MACRO_NAME = "0_macro_parameters";
14512
+
14513
+ public:
14514
+ MacroBinding(vector<LogicalType> types_p, vector<string> names_p, string macro_name);
14515
+
14516
+ //! Arguments
14517
+ vector<unique_ptr<ParsedExpression>> arguments;
14518
+ //! The name of the macro
14519
+ string macro_name;
14520
+
14521
+ public:
14522
+ BindResult Bind(ColumnRefExpression &colref, idx_t depth) override;
14523
+
14524
+ //! Given the parameter colref, returns a copy of the argument that was supplied for this parameter
14525
+ unique_ptr<ParsedExpression> ParamToArg(ColumnRefExpression &colref);
14526
+ };
14527
+
14528
+ } // namespace duckdb
14529
+
14530
+
14531
+ namespace duckdb {
14532
+ class Binder;
14533
+ class LogicalGet;
14534
+ class BoundQueryNode;
14535
+
14536
+ class StarExpression;
14537
+
14538
+ struct UsingColumnSet {
14539
+ string primary_binding;
14540
+ unordered_set<string> bindings;
14541
+ };
14542
+
14543
+ //! The BindContext object keeps track of all the tables and columns that are
14544
+ //! encountered during the binding process.
14545
+ class BindContext {
14546
+ public:
14547
+ //! Keep track of recursive CTE references
14548
+ case_insensitive_map_t<std::shared_ptr<idx_t>> cte_references;
14549
+
14550
+ public:
14551
+ //! Given a column name, find the matching table it belongs to. Throws an
14552
+ //! exception if no table has a column of the given name.
14553
+ string GetMatchingBinding(const string &column_name);
14554
+ //! Like GetMatchingBinding, but instead of throwing an error if multiple tables have the same binding it will
14555
+ //! return a list of all the matching ones
14556
+ unordered_set<string> GetMatchingBindings(const string &column_name);
14557
+ //! Like GetMatchingBindings, but returns the top 3 most similar bindings (in levenshtein distance) instead of the
14558
+ //! matching ones
14559
+ vector<string> GetSimilarBindings(const string &column_name);
14560
+
14561
+ Binding *GetCTEBinding(const string &ctename);
14562
+ //! Binds a column expression to the base table. Returns the bound expression
14563
+ //! or throws an exception if the column could not be bound.
14564
+ BindResult BindColumn(ColumnRefExpression &colref, idx_t depth);
14565
+ string BindColumn(PositionalReferenceExpression &ref, string &table_name, string &column_name);
14566
+ BindResult BindColumn(PositionalReferenceExpression &ref, idx_t depth);
14567
+
14568
+ unique_ptr<ParsedExpression> CreateColumnReference(const string &table_name, const string &column_name);
14569
+ unique_ptr<ParsedExpression> CreateColumnReference(const string &schema_name, const string &table_name,
14570
+ const string &column_name);
14571
+
14572
+ //! Generate column expressions for all columns that are present in the
14573
+ //! referenced tables. This is used to resolve the * expression in a
14574
+ //! selection list.
14575
+ void GenerateAllColumnExpressions(StarExpression &expr, vector<unique_ptr<ParsedExpression>> &new_select_list);
14576
+ //! Check if the given (binding, column_name) is in the exclusion/replacement lists.
14577
+ //! Returns true if it is in one of these lists, and should therefore be skipped.
14578
+ bool CheckExclusionList(StarExpression &expr, Binding *binding, const string &column_name,
14579
+ vector<unique_ptr<ParsedExpression>> &new_select_list,
14580
+ case_insensitive_set_t &excluded_columns);
14581
+
14582
+ const vector<std::pair<string, Binding *>> &GetBindingsList() {
14583
+ return bindings_list;
14584
+ }
14585
+
14586
+ //! Adds a base table with the given alias to the BindContext.
14587
+ void AddBaseTable(idx_t index, const string &alias, const vector<string> &names, const vector<LogicalType> &types,
14588
+ LogicalGet &get);
14589
+ //! Adds a call to a table function with the given alias to the BindContext.
14590
+ void AddTableFunction(idx_t index, const string &alias, const vector<string> &names,
14591
+ const vector<LogicalType> &types, LogicalGet &get);
14592
+ //! Adds a subquery with a given alias to the BindContext.
14593
+ void AddSubquery(idx_t index, const string &alias, SubqueryRef &ref, BoundQueryNode &subquery);
14594
+ //! Adds a base table with the given alias to the BindContext.
14595
+ void AddGenericBinding(idx_t index, const string &alias, const vector<string> &names,
14596
+ const vector<LogicalType> &types);
14597
+
14598
+ //! Adds a base table with the given alias to the CTE BindContext.
14599
+ //! We need this to correctly bind recursive CTEs with multiple references.
14600
+ void AddCTEBinding(idx_t index, const string &alias, const vector<string> &names, const vector<LogicalType> &types);
14601
+
14602
+ //! Add an implicit join condition (e.g. USING (x))
14603
+ void AddUsingBinding(const string &column_name, UsingColumnSet *set);
14604
+
14605
+ void AddUsingBindingSet(unique_ptr<UsingColumnSet> set);
14606
+
14607
+ //! Returns any using column set for the given column name, or nullptr if there is none. On conflict (multiple using
14608
+ //! column sets with the same name) throw an exception.
14609
+ UsingColumnSet *GetUsingBinding(const string &column_name);
14610
+ //! Returns any using column set for the given column name, or nullptr if there is none
14611
+ UsingColumnSet *GetUsingBinding(const string &column_name, const string &binding_name);
14612
+ //! Erase a using binding from the set of using bindings
14613
+ void RemoveUsingBinding(const string &column_name, UsingColumnSet *set);
14614
+ //! Finds the using bindings for a given column. Returns true if any exists, false otherwise.
14615
+ bool FindUsingBinding(const string &column_name, unordered_set<UsingColumnSet *> **using_columns);
14616
+ //! Transfer a using binding from one bind context to this bind context
14617
+ void TransferUsingBinding(BindContext &current_context, UsingColumnSet *current_set, UsingColumnSet *new_set,
14618
+ const string &binding, const string &using_column);
14619
+
14620
+ //! Fetch the actual column name from the given binding, or throws if none exists
14621
+ //! This can be different from "column_name" because of case insensitivity
14622
+ //! (e.g. "column_name" might return "COLUMN_NAME")
14623
+ string GetActualColumnName(const string &binding, const string &column_name);
14624
+
14625
+ case_insensitive_map_t<std::shared_ptr<Binding>> GetCTEBindings() {
14626
+ return cte_bindings;
14627
+ }
14628
+ void SetCTEBindings(case_insensitive_map_t<std::shared_ptr<Binding>> bindings) {
14629
+ cte_bindings = bindings;
14630
+ }
14631
+
14632
+ //! Alias a set of column names for the specified table, using the original names if there are not enough aliases
14633
+ //! specified.
14634
+ static vector<string> AliasColumnNames(const string &table_name, const vector<string> &names,
14635
+ const vector<string> &column_aliases);
14636
+
14637
+ //! Add all the bindings from a BindContext to this BindContext. The other BindContext is destroyed in the process.
14638
+ void AddContext(BindContext other);
14639
+
14640
+ //! Gets a binding of the specified name. Returns a nullptr and sets the out_error if the binding could not be
14641
+ //! found.
14642
+ Binding *GetBinding(const string &name, string &out_error);
14643
+
14644
+ private:
14645
+ void AddBinding(const string &alias, unique_ptr<Binding> binding);
14646
+
14647
+ private:
14648
+ //! The set of bindings
14649
+ case_insensitive_map_t<unique_ptr<Binding>> bindings;
14650
+ //! The list of bindings in insertion order
14651
+ vector<std::pair<string, Binding *>> bindings_list;
14652
+ //! The set of columns used in USING join conditions
14653
+ case_insensitive_map_t<unordered_set<UsingColumnSet *>> using_columns;
14654
+ //! Using column sets
14655
+ vector<unique_ptr<UsingColumnSet>> using_column_sets;
14656
+
14657
+ //! The set of CTE bindings
14658
+ case_insensitive_map_t<std::shared_ptr<Binding>> cte_bindings;
14659
+ };
14660
+ } // namespace duckdb
14661
+
14662
+
14663
+
14664
+
14665
+
14666
+
14667
+
14668
+
14669
+ namespace duckdb {
14670
+ class BoundResultModifier;
14671
+ class BoundSelectNode;
14672
+ class ClientContext;
14673
+ class ExpressionBinder;
14674
+ class LimitModifier;
14675
+ class OrderBinder;
14676
+ class TableCatalogEntry;
14677
+ class ViewCatalogEntry;
14678
+
14679
+ struct CreateInfo;
14680
+ struct BoundCreateTableInfo;
14681
+ struct BoundCreateFunctionInfo;
14682
+ struct CommonTableExpressionInfo;
14683
+
14684
+ struct CorrelatedColumnInfo {
14685
+ ColumnBinding binding;
14686
+ LogicalType type;
14687
+ string name;
14688
+ idx_t depth;
14689
+
14690
+ explicit CorrelatedColumnInfo(BoundColumnRefExpression &expr)
14691
+ : binding(expr.binding), type(expr.return_type), name(expr.GetName()), depth(expr.depth) {
14692
+ }
14693
+
14694
+ bool operator==(const CorrelatedColumnInfo &rhs) const {
14695
+ return binding == rhs.binding;
14696
+ }
14697
+ };
14698
+
14699
+ //! Bind the parsed query tree to the actual columns present in the catalog.
14700
+ /*!
14701
+ The binder is responsible for binding tables and columns to actual physical
14702
+ tables and columns in the catalog. In the process, it also resolves types of
14703
+ all expressions.
14704
+ */
14705
+ class Binder : public std::enable_shared_from_this<Binder> {
14706
+ friend class ExpressionBinder;
14707
+ friend class SelectBinder;
14708
+ friend class RecursiveSubqueryPlanner;
14709
+
14710
+ public:
14711
+ static shared_ptr<Binder> CreateBinder(ClientContext &context, Binder *parent = nullptr, bool inherit_ctes = true);
14712
+
14713
+ //! The client context
14714
+ ClientContext &context;
14715
+ //! A mapping of names to common table expressions
14716
+ case_insensitive_map_t<CommonTableExpressionInfo *> CTE_bindings;
14717
+ //! The CTEs that have already been bound
14718
+ unordered_set<CommonTableExpressionInfo *> bound_ctes;
14719
+ //! The bind context
14720
+ BindContext bind_context;
14721
+ //! The set of correlated columns bound by this binder (FIXME: this should probably be an unordered_set and not a
14722
+ //! vector)
14723
+ vector<CorrelatedColumnInfo> correlated_columns;
14724
+ //! The set of parameter expressions bound by this binder
14725
+ vector<BoundParameterExpression *> *parameters;
14726
+ //! Whether or not the bound statement is read-only
14727
+ bool read_only;
14728
+ //! Whether or not the statement requires a valid transaction to run
14729
+ bool requires_valid_transaction;
14730
+ //! Whether or not the statement can be streamed to the client
14731
+ bool allow_stream_result;
14732
+ //! The alias for the currently processing subquery, if it exists
14733
+ string alias;
14734
+ //! Macro parameter bindings (if any)
14735
+ MacroBinding *macro_binding = nullptr;
14736
+
14737
+ public:
14738
+ BoundStatement Bind(SQLStatement &statement);
14739
+ BoundStatement Bind(QueryNode &node);
14740
+
14741
+ unique_ptr<BoundCreateTableInfo> BindCreateTableInfo(unique_ptr<CreateInfo> info);
14742
+ void BindCreateViewInfo(CreateViewInfo &base);
14743
+ SchemaCatalogEntry *BindSchema(CreateInfo &info);
14744
+ SchemaCatalogEntry *BindCreateFunctionInfo(CreateInfo &info);
14745
+
14746
+ //! Check usage, and cast named parameters to their types
14747
+ static void BindNamedParameters(unordered_map<string, LogicalType> &types, unordered_map<string, Value> &values,
14748
+ QueryErrorContext &error_context, string &func_name);
14749
+
14750
+ unique_ptr<BoundTableRef> Bind(TableRef &ref);
14751
+ unique_ptr<LogicalOperator> CreatePlan(BoundTableRef &ref);
14752
+
14753
+ //! Generates an unused index for a table
14754
+ idx_t GenerateTableIndex();
14755
+
14756
+ //! Add a common table expression to the binder
14757
+ void AddCTE(const string &name, CommonTableExpressionInfo *cte);
14758
+ //! Find a common table expression by name; returns nullptr if none exists
14759
+ CommonTableExpressionInfo *FindCTE(const string &name, bool skip = false);
14760
+
14761
+ bool CTEIsAlreadyBound(CommonTableExpressionInfo *cte);
14762
+
14763
+ void PushExpressionBinder(ExpressionBinder *binder);
14764
+ void PopExpressionBinder();
14765
+ void SetActiveBinder(ExpressionBinder *binder);
14766
+ ExpressionBinder *GetActiveBinder();
14767
+ bool HasActiveBinder();
14768
+
14769
+ vector<ExpressionBinder *> &GetActiveBinders();
14770
+
14771
+ void MergeCorrelatedColumns(vector<CorrelatedColumnInfo> &other);
14772
+ //! Add a correlated column to this binder (if it does not exist)
14773
+ void AddCorrelatedColumn(const CorrelatedColumnInfo &info);
14774
+
14775
+ string FormatError(ParsedExpression &expr_context, const string &message);
14776
+ string FormatError(TableRef &ref_context, const string &message);
14777
+
14778
+ string FormatErrorRecursive(idx_t query_location, const string &message, vector<ExceptionFormatValue> &values);
14779
+ template <class T, typename... Args>
14780
+ string FormatErrorRecursive(idx_t query_location, const string &msg, vector<ExceptionFormatValue> &values, T param,
14781
+ Args... params) {
14782
+ values.push_back(ExceptionFormatValue::CreateFormatValue<T>(param));
14783
+ return FormatErrorRecursive(query_location, msg, values, params...);
14784
+ }
14785
+
14786
+ template <typename... Args>
14787
+ string FormatError(idx_t query_location, const string &msg, Args... params) {
14788
+ vector<ExceptionFormatValue> values;
14789
+ return FormatErrorRecursive(query_location, msg, values, params...);
14790
+ }
14791
+
14792
+ static void BindLogicalType(ClientContext &context, LogicalType &type, const string &schema = "");
14793
+
14794
+ bool HasMatchingBinding(const string &table_name, const string &column_name, string &error_message);
14795
+ bool HasMatchingBinding(const string &schema_name, const string &table_name, const string &column_name,
14796
+ string &error_message);
14797
+
14798
+ private:
14799
+ //! The parent binder (if any)
14800
+ shared_ptr<Binder> parent;
14801
+ //! The vector of active binders
14802
+ vector<ExpressionBinder *> active_binders;
14803
+ //! The count of bound_tables
14804
+ idx_t bound_tables;
14805
+ //! Whether or not the binder has any unplanned subqueries that still need to be planned
14806
+ bool has_unplanned_subqueries = false;
14807
+ //! Whether or not subqueries should be planned already
14808
+ bool plan_subquery = true;
14809
+ //! Whether CTEs should reference the parent binder (if it exists)
14810
+ bool inherit_ctes = true;
14811
+ //! Whether or not the binder can contain NULLs as the root of expressions
14812
+ bool can_contain_nulls = false;
14813
+ //! The root statement of the query that is currently being parsed
14814
+ SQLStatement *root_statement = nullptr;
14815
+
14816
+ private:
14817
+ //! Bind the default values of the columns of a table
14818
+ void BindDefaultValues(vector<ColumnDefinition> &columns, vector<unique_ptr<Expression>> &bound_defaults);
14819
+ //! Bind a delimiter value (LIMIT or OFFSET)
14820
+ unique_ptr<Expression> BindDelimiter(ClientContext &context, unique_ptr<ParsedExpression> delimiter,
14821
+ const LogicalType &type, Value &delimiter_value);
14822
+
14823
+ //! Move correlated expressions from the child binder to this binder
14824
+ void MoveCorrelatedExpressions(Binder &other);
14825
+
14826
+ BoundStatement Bind(SelectStatement &stmt);
14827
+ BoundStatement Bind(InsertStatement &stmt);
14828
+ BoundStatement Bind(CopyStatement &stmt);
14829
+ BoundStatement Bind(DeleteStatement &stmt);
14830
+ BoundStatement Bind(UpdateStatement &stmt);
14831
+ BoundStatement Bind(CreateStatement &stmt);
14832
+ BoundStatement Bind(DropStatement &stmt);
14833
+ BoundStatement Bind(AlterStatement &stmt);
14834
+ BoundStatement Bind(TransactionStatement &stmt);
14835
+ BoundStatement Bind(PragmaStatement &stmt);
14836
+ BoundStatement Bind(ExplainStatement &stmt);
14837
+ BoundStatement Bind(VacuumStatement &stmt);
14838
+ BoundStatement Bind(RelationStatement &stmt);
14839
+ BoundStatement Bind(ShowStatement &stmt);
14840
+ BoundStatement Bind(CallStatement &stmt);
14841
+ BoundStatement Bind(ExportStatement &stmt);
14842
+ BoundStatement Bind(SetStatement &stmt);
14843
+ BoundStatement Bind(LoadStatement &stmt);
14844
+
14845
+ unique_ptr<BoundQueryNode> BindNode(SelectNode &node);
14846
+ unique_ptr<BoundQueryNode> BindNode(SetOperationNode &node);
14847
+ unique_ptr<BoundQueryNode> BindNode(RecursiveCTENode &node);
14848
+ unique_ptr<BoundQueryNode> BindNode(QueryNode &node);
14849
+
14850
+ unique_ptr<LogicalOperator> VisitQueryNode(BoundQueryNode &node, unique_ptr<LogicalOperator> root);
14851
+ unique_ptr<LogicalOperator> CreatePlan(BoundRecursiveCTENode &node);
14852
+ unique_ptr<LogicalOperator> CreatePlan(BoundSelectNode &statement);
14853
+ unique_ptr<LogicalOperator> CreatePlan(BoundSetOperationNode &node);
14854
+ unique_ptr<LogicalOperator> CreatePlan(BoundQueryNode &node);
14855
+
14856
+ unique_ptr<BoundTableRef> Bind(BaseTableRef &ref);
14857
+ unique_ptr<BoundTableRef> Bind(CrossProductRef &ref);
14858
+ unique_ptr<BoundTableRef> Bind(JoinRef &ref);
14859
+ unique_ptr<BoundTableRef> Bind(SubqueryRef &ref, CommonTableExpressionInfo *cte = nullptr);
14860
+ unique_ptr<BoundTableRef> Bind(TableFunctionRef &ref);
14861
+ unique_ptr<BoundTableRef> Bind(EmptyTableRef &ref);
14862
+ unique_ptr<BoundTableRef> Bind(ExpressionListRef &ref);
14863
+
14864
+ bool BindFunctionParameters(vector<unique_ptr<ParsedExpression>> &expressions, vector<LogicalType> &arguments,
14865
+ vector<Value> &parameters, unordered_map<string, Value> &named_parameters,
14866
+ unique_ptr<BoundSubqueryRef> &subquery, string &error);
14867
+
14868
+ unique_ptr<LogicalOperator> CreatePlan(BoundBaseTableRef &ref);
14869
+ unique_ptr<LogicalOperator> CreatePlan(BoundCrossProductRef &ref);
14870
+ unique_ptr<LogicalOperator> CreatePlan(BoundJoinRef &ref);
14871
+ unique_ptr<LogicalOperator> CreatePlan(BoundSubqueryRef &ref);
14872
+ unique_ptr<LogicalOperator> CreatePlan(BoundTableFunction &ref);
14873
+ unique_ptr<LogicalOperator> CreatePlan(BoundEmptyTableRef &ref);
14874
+ unique_ptr<LogicalOperator> CreatePlan(BoundExpressionListRef &ref);
14875
+ unique_ptr<LogicalOperator> CreatePlan(BoundCTERef &ref);
14876
+
14877
+ unique_ptr<LogicalOperator> BindTable(TableCatalogEntry &table, BaseTableRef &ref);
14878
+ unique_ptr<LogicalOperator> BindView(ViewCatalogEntry &view, BaseTableRef &ref);
14879
+ unique_ptr<LogicalOperator> BindTableOrView(BaseTableRef &ref);
14880
+
14881
+ BoundStatement BindCopyTo(CopyStatement &stmt);
14882
+ BoundStatement BindCopyFrom(CopyStatement &stmt);
14883
+
14884
+ void BindModifiers(OrderBinder &order_binder, QueryNode &statement, BoundQueryNode &result);
14885
+ void BindModifierTypes(BoundQueryNode &result, const vector<LogicalType> &sql_types, idx_t projection_index);
14886
+
14887
+ BoundStatement BindSummarize(ShowStatement &stmt);
14888
+ unique_ptr<BoundResultModifier> BindLimit(LimitModifier &limit_mod);
14889
+ unique_ptr<BoundResultModifier> BindLimitPercent(LimitPercentModifier &limit_mod);
14890
+ unique_ptr<Expression> BindOrderExpression(OrderBinder &order_binder, unique_ptr<ParsedExpression> expr);
14891
+
14892
+ unique_ptr<LogicalOperator> PlanFilter(unique_ptr<Expression> condition, unique_ptr<LogicalOperator> root);
14893
+
14894
+ void PlanSubqueries(unique_ptr<Expression> *expr, unique_ptr<LogicalOperator> *root);
14895
+ unique_ptr<Expression> PlanSubquery(BoundSubqueryExpression &expr, unique_ptr<LogicalOperator> &root);
14896
+
14897
+ unique_ptr<LogicalOperator> CastLogicalOperatorToTypes(vector<LogicalType> &source_types,
14898
+ vector<LogicalType> &target_types,
14899
+ unique_ptr<LogicalOperator> op);
14900
+
14901
+ string FindBinding(const string &using_column, const string &join_side);
14902
+ bool TryFindBinding(const string &using_column, const string &join_side, string &result);
14903
+
14904
+ void AddUsingBindingSet(unique_ptr<UsingColumnSet> set);
14905
+ string RetrieveUsingBinding(Binder &current_binder, UsingColumnSet *current_set, const string &column_name,
14906
+ const string &join_side, UsingColumnSet *new_set);
14907
+
14908
+ public:
14909
+ // This should really be a private constructor, but make_shared does not allow it...
14910
+ // If you are thinking about calling this, you should probably call Binder::CreateBinder
14911
+ Binder(bool I_know_what_I_am_doing, ClientContext &context, shared_ptr<Binder> parent, bool inherit_ctes);
14912
+ };
14913
+
14914
+ } // namespace duckdb
14915
+
14916
+ //===----------------------------------------------------------------------===//
14917
+ // DuckDB
14918
+ //
14919
+ // duckdb/planner/bound_query_node.hpp
14920
+ //
14921
+ //
14922
+ //===----------------------------------------------------------------------===//
14923
+
14924
+
14925
+
14926
+
14927
+
14928
+ //===----------------------------------------------------------------------===//
14929
+ // DuckDB
14930
+ //
14931
+ // duckdb/parser/query_node.hpp
14932
+ //
14933
+ //
14934
+ //===----------------------------------------------------------------------===//
14935
+
14936
+
14937
+
14938
+
14939
+
14940
+
14941
+
14942
+ //===----------------------------------------------------------------------===//
14943
+ // DuckDB
14944
+ //
14945
+ // duckdb/parser/common_table_expression_info.hpp
14946
+ //
14947
+ //
14948
+ //===----------------------------------------------------------------------===//
14949
+
14950
+
14951
+
14952
+ //===----------------------------------------------------------------------===//
14953
+ // DuckDB
14954
+ //
14955
+ // duckdb/parser/statement/select_statement.hpp
14956
+ //
14957
+ //
14958
+ //===----------------------------------------------------------------------===//
14959
+
14960
+
14961
+
14962
+
14963
+
14964
+
14965
+
14966
+ //===----------------------------------------------------------------------===//
14967
+ // DuckDB
14968
+ //
14969
+ // duckdb/parser/tableref.hpp
14970
+ //
14971
+ //
14972
+ //===----------------------------------------------------------------------===//
14973
+
14974
+
14975
+
14976
+
14977
+ //===----------------------------------------------------------------------===//
14978
+ // DuckDB
14979
+ //
14980
+ // duckdb/common/enums/tableref_type.hpp
14981
+ //
14982
+ //
14983
+ //===----------------------------------------------------------------------===//
14984
+
14985
+
14986
+
14987
+
14988
+
14989
+ namespace duckdb {
14990
+
14991
+ //===--------------------------------------------------------------------===//
14992
+ // Table Reference Types
14993
+ //===--------------------------------------------------------------------===//
14994
+ enum class TableReferenceType : uint8_t {
14995
+ INVALID = 0, // invalid table reference type
14996
+ BASE_TABLE = 1, // base table reference
14997
+ SUBQUERY = 2, // output of a subquery
14998
+ JOIN = 3, // output of join
14999
+ CROSS_PRODUCT = 4, // out of cartesian product
15000
+ TABLE_FUNCTION = 5, // table producing function
15001
+ EXPRESSION_LIST = 6, // expression list
15002
+ CTE = 7, // Recursive CTE
15003
+ EMPTY = 8 // placeholder for empty FROM
15004
+ };
15005
+
15006
+ } // namespace duckdb
15007
+
15008
+ //===----------------------------------------------------------------------===//
15009
+ // DuckDB
15010
+ //
15011
+ // duckdb/parser/parsed_data/sample_options.hpp
15012
+ //
15013
+ //
15014
+ //===----------------------------------------------------------------------===//
15015
+
15016
+
15017
+
15018
+
15019
+
15020
+
15021
+
15022
+
15023
+ namespace duckdb {
15024
+
15025
+ enum class SampleMethod : uint8_t { SYSTEM_SAMPLE = 0, BERNOULLI_SAMPLE = 1, RESERVOIR_SAMPLE = 2 };
15026
+
15027
+ string SampleMethodToString(SampleMethod method);
15028
+
15029
+ struct SampleOptions {
15030
+ Value sample_size;
15031
+ bool is_percentage;
15032
+ SampleMethod method;
15033
+ int64_t seed = -1;
15034
+
15035
+ unique_ptr<SampleOptions> Copy();
15036
+ void Serialize(Serializer &serializer);
15037
+ static unique_ptr<SampleOptions> Deserialize(Deserializer &source);
15038
+ static bool Equals(SampleOptions *a, SampleOptions *b);
15039
+ };
15040
+
15041
+ } // namespace duckdb
15042
+
15043
+
15044
+ namespace duckdb {
15045
+ class Deserializer;
15046
+ class Serializer;
15047
+
15048
+ //! Represents a generic expression that returns a table.
15049
+ class TableRef {
15050
+ public:
15051
+ explicit TableRef(TableReferenceType type) : type(type) {
15052
+ }
15053
+ virtual ~TableRef() {
15054
+ }
15055
+
15056
+ TableReferenceType type;
15057
+ string alias;
15058
+ //! Sample options (if any)
15059
+ unique_ptr<SampleOptions> sample;
15060
+ //! The location in the query (if any)
15061
+ idx_t query_location = DConstants::INVALID_INDEX;
15062
+
15063
+ public:
15064
+ //! Convert the object to a string
15065
+ virtual string ToString() const;
15066
+ void Print();
15067
+
15068
+ virtual bool Equals(const TableRef *other) const;
15069
+
15070
+ virtual unique_ptr<TableRef> Copy() = 0;
15071
+
15072
+ //! Serializes a TableRef to a stand-alone binary blob
15073
+ virtual void Serialize(Serializer &serializer);
15074
+ //! Deserializes a blob back into a TableRef
15075
+ static unique_ptr<TableRef> Deserialize(Deserializer &source);
15076
+
15077
+ //! Copy the properties of this table ref to the target
15078
+ void CopyProperties(TableRef &target) const;
15079
+ };
15080
+ } // namespace duckdb
15081
+
15082
+
15083
+ namespace duckdb {
15084
+
15085
+ class QueryNode;
15086
+
15087
+ //! SelectStatement is a typical SELECT clause
15088
+ class SelectStatement : public SQLStatement {
15089
+ public:
15090
+ SelectStatement() : SQLStatement(StatementType::SELECT_STATEMENT) {
15091
+ }
15092
+
15093
+ //! The main query node
15094
+ unique_ptr<QueryNode> node;
15095
+
15096
+ public:
15097
+ //! Create a copy of this SelectStatement
15098
+ unique_ptr<SQLStatement> Copy() const override;
15099
+ //! Serializes a SelectStatement to a stand-alone binary blob
15100
+ void Serialize(Serializer &serializer);
15101
+ //! Deserializes a blob back into a SelectStatement, returns nullptr if
15102
+ //! deserialization is not possible
15103
+ static unique_ptr<SelectStatement> Deserialize(Deserializer &source);
15104
+ //! Whether or not the statements are equivalent
15105
+ bool Equals(const SQLStatement *other) const;
15106
+ };
15107
+ } // namespace duckdb
15108
+
15109
+
15110
+ namespace duckdb {
15111
+
15112
+ class SelectStatement;
15113
+
15114
+ struct CommonTableExpressionInfo {
15115
+ vector<string> aliases;
15116
+ unique_ptr<SelectStatement> query;
15117
+ };
15118
+
15119
+ } // namespace duckdb
15120
+
15121
+
15122
+ namespace duckdb {
15123
+
15124
+ enum QueryNodeType : uint8_t {
15125
+ SELECT_NODE = 1,
15126
+ SET_OPERATION_NODE = 2,
15127
+ BOUND_SUBQUERY_NODE = 3,
15128
+ RECURSIVE_CTE_NODE = 4
15129
+ };
15130
+
15131
+ class QueryNode {
15132
+ public:
15133
+ explicit QueryNode(QueryNodeType type) : type(type) {
15134
+ }
15135
+ virtual ~QueryNode() {
15136
+ }
15137
+
15138
+ //! The type of the query node, either SetOperation or Select
15139
+ QueryNodeType type;
15140
+ //! The set of result modifiers associated with this query node
15141
+ vector<unique_ptr<ResultModifier>> modifiers;
15142
+ //! CTEs (used by SelectNode and SetOperationNode)
15143
+ unordered_map<string, unique_ptr<CommonTableExpressionInfo>> cte_map;
15144
+
15145
+ virtual const vector<unique_ptr<ParsedExpression>> &GetSelectList() const = 0;
15146
+
15147
+ public:
15148
+ virtual bool Equals(const QueryNode *other) const;
15149
+
15150
+ //! Create a copy of this QueryNode
15151
+ virtual unique_ptr<QueryNode> Copy() = 0;
15152
+ //! Serializes a QueryNode to a stand-alone binary blob
15153
+ virtual void Serialize(Serializer &serializer);
15154
+ //! Deserializes a blob back into a QueryNode, returns nullptr if
15155
+ //! deserialization is not possible
15156
+ static unique_ptr<QueryNode> Deserialize(Deserializer &source);
15157
+
15158
+ protected:
15159
+ //! Copy base QueryNode properties from another expression to this one,
15160
+ //! used in Copy method
15161
+ void CopyProperties(QueryNode &other) const;
15162
+ };
15163
+
15164
+ } // namespace duckdb
15165
+
15166
+
15167
+ namespace duckdb {
15168
+
15169
+ //! Bound equivalent of QueryNode
15170
+ class BoundQueryNode {
15171
+ public:
15172
+ explicit BoundQueryNode(QueryNodeType type) : type(type) {
15173
+ }
15174
+ virtual ~BoundQueryNode() {
15175
+ }
15176
+
15177
+ //! The type of the query node, either SetOperation or Select
15178
+ QueryNodeType type;
15179
+ //! The result modifiers that should be applied to this query node
15180
+ vector<unique_ptr<BoundResultModifier>> modifiers;
15181
+
15182
+ //! The names returned by this QueryNode.
15183
+ vector<string> names;
15184
+ //! The types returned by this QueryNode.
15185
+ vector<LogicalType> types;
15186
+
15187
+ public:
15188
+ virtual idx_t GetRootIndex() = 0;
15189
+ };
15190
+
15191
+ } // namespace duckdb
15192
+
15193
+
15194
+
15195
+ namespace duckdb {
15196
+
15197
+ class BoundSubqueryExpression : public Expression {
15198
+ public:
15199
+ explicit BoundSubqueryExpression(LogicalType return_type);
15200
+
15201
+ bool IsCorrelated() {
15202
+ return binder->correlated_columns.size() > 0;
15203
+ }
15204
+
15205
+ //! The binder used to bind the subquery node
15206
+ shared_ptr<Binder> binder;
15207
+ //! The bound subquery node
15208
+ unique_ptr<BoundQueryNode> subquery;
15209
+ //! The subquery type
15210
+ SubqueryType subquery_type;
15211
+ //! the child expression to compare with (in case of IN, ANY, ALL operators)
15212
+ unique_ptr<Expression> child;
15213
+ //! The comparison type of the child expression with the subquery (in case of ANY, ALL operators)
15214
+ ExpressionType comparison_type;
15215
+ //! The LogicalType of the subquery result. Only used for ANY expressions.
15216
+ LogicalType child_type;
15217
+ //! The target LogicalType of the subquery result (i.e. to which type it should be casted, if child_type <>
15218
+ //! child_target). Only used for ANY expressions.
15219
+ LogicalType child_target;
15220
+
15221
+ public:
15222
+ bool HasSubquery() const override {
15223
+ return true;
15224
+ }
15225
+ bool IsScalar() const override {
15226
+ return false;
15227
+ }
15228
+ bool IsFoldable() const override {
15229
+ return false;
15230
+ }
15231
+
15232
+ string ToString() const override;
15233
+
15234
+ bool Equals(const BaseExpression *other) const override;
15235
+
15236
+ unique_ptr<Expression> Copy() override;
15237
+ };
15238
+ } // namespace duckdb
15239
+
15240
+ //===----------------------------------------------------------------------===//
15241
+ // DuckDB
15242
+ //
15243
+ // duckdb/planner/expression/bound_unnest_expression.hpp
15244
+ //
15245
+ //
15246
+ //===----------------------------------------------------------------------===//
15247
+
15248
+
15249
+
15250
+
15251
+
15252
+ namespace duckdb {
15253
+
15254
+ //! Represents a function call that has been bound to a base function
15255
+ class BoundUnnestExpression : public Expression {
15256
+ public:
15257
+ explicit BoundUnnestExpression(LogicalType return_type);
15258
+
15259
+ unique_ptr<Expression> child;
15260
+
15261
+ public:
15262
+ bool IsFoldable() const override;
15263
+ string ToString() const override;
15264
+
15265
+ hash_t Hash() const override;
15266
+ bool Equals(const BaseExpression *other) const override;
15267
+
15268
+ unique_ptr<Expression> Copy() override;
15269
+ };
15270
+ } // namespace duckdb
15271
+
15272
+ //===----------------------------------------------------------------------===//
15273
+ // DuckDB
15274
+ //
15275
+ // duckdb/planner/expression/bound_window_expression.hpp
15276
+ //
15277
+ //
15278
+ //===----------------------------------------------------------------------===//
15279
+
15280
+
15281
+
15282
+ //===----------------------------------------------------------------------===//
15283
+ // DuckDB
15284
+ //
15285
+ // duckdb/parser/expression/window_expression.hpp
15286
+ //
15287
+ //
15288
+ //===----------------------------------------------------------------------===//
15289
+
15290
+
15291
+
15292
+
15293
+
15294
+
15295
+ namespace duckdb {
15296
+
15297
+ enum class WindowBoundary : uint8_t {
15298
+ INVALID = 0,
15299
+ UNBOUNDED_PRECEDING = 1,
15300
+ UNBOUNDED_FOLLOWING = 2,
15301
+ CURRENT_ROW_RANGE = 3,
15302
+ CURRENT_ROW_ROWS = 4,
15303
+ EXPR_PRECEDING_ROWS = 5,
15304
+ EXPR_FOLLOWING_ROWS = 6,
15305
+ EXPR_PRECEDING_RANGE = 7,
15306
+ EXPR_FOLLOWING_RANGE = 8
15307
+ };
15308
+
15309
+ //! The WindowExpression represents a window function in the query. They are a special case of aggregates which is why
15310
+ //! they inherit from them.
15311
+ class WindowExpression : public ParsedExpression {
15312
+ public:
15313
+ WindowExpression(ExpressionType type, string schema_name, const string &function_name);
15314
+
15315
+ //! Schema of the aggregate function
15316
+ string schema;
15317
+ //! Name of the aggregate function
15318
+ string function_name;
15319
+ //! The child expression of the main window aggregate
15320
+ vector<unique_ptr<ParsedExpression>> children;
15321
+ //! The set of expressions to partition by
15322
+ vector<unique_ptr<ParsedExpression>> partitions;
15323
+ //! The set of ordering clauses
15324
+ vector<OrderByNode> orders;
15325
+ //! True to ignore NULL values
15326
+ bool ignore_nulls;
15327
+ //! The window boundaries
15328
+ WindowBoundary start = WindowBoundary::INVALID;
15329
+ WindowBoundary end = WindowBoundary::INVALID;
15330
+
15331
+ unique_ptr<ParsedExpression> start_expr;
15332
+ unique_ptr<ParsedExpression> end_expr;
15333
+ //! Offset and default expressions for WINDOW_LEAD and WINDOW_LAG functions
15334
+ unique_ptr<ParsedExpression> offset_expr;
15335
+ unique_ptr<ParsedExpression> default_expr;
15336
+
15337
+ public:
15338
+ bool IsWindow() const override {
15339
+ return true;
15340
+ }
15341
+
15342
+ //! Get the name of the expression
15343
+ string GetName() const override;
15344
+ //! Convert the Expression to a String
15345
+ string ToString() const override;
15346
+
15347
+ static bool Equals(const WindowExpression *a, const WindowExpression *b);
15348
+
15349
+ unique_ptr<ParsedExpression> Copy() const override;
15350
+
15351
+ void Serialize(Serializer &serializer) override;
15352
+ static unique_ptr<ParsedExpression> Deserialize(ExpressionType type, Deserializer &source);
15353
+ };
15354
+ } // namespace duckdb
15355
+
15356
+
15357
+
15358
+
15359
+
15360
+ namespace duckdb {
15361
+ class AggregateFunction;
15362
+
15363
+ class BoundWindowExpression : public Expression {
15364
+ public:
15365
+ BoundWindowExpression(ExpressionType type, LogicalType return_type, unique_ptr<AggregateFunction> aggregate,
15366
+ unique_ptr<FunctionData> bind_info);
15367
+
15368
+ //! The bound aggregate function
15369
+ unique_ptr<AggregateFunction> aggregate;
15370
+ //! The bound function info
15371
+ unique_ptr<FunctionData> bind_info;
15372
+ //! The child expressions of the main window aggregate
15373
+ vector<unique_ptr<Expression>> children;
15374
+ //! The set of expressions to partition by
15375
+ vector<unique_ptr<Expression>> partitions;
15376
+ //! Statistics belonging to the partitions expressions
15377
+ vector<unique_ptr<BaseStatistics>> partitions_stats;
15378
+ //! The set of ordering clauses
15379
+ vector<BoundOrderByNode> orders;
15380
+ //! True to ignore NULL values
15381
+ bool ignore_nulls;
15382
+ //! The window boundaries
15383
+ WindowBoundary start = WindowBoundary::INVALID;
15384
+ WindowBoundary end = WindowBoundary::INVALID;
15385
+
15386
+ unique_ptr<Expression> start_expr;
15387
+ unique_ptr<Expression> end_expr;
15388
+ //! Offset and default expressions for WINDOW_LEAD and WINDOW_LAG functions
15389
+ unique_ptr<Expression> offset_expr;
15390
+ unique_ptr<Expression> default_expr;
15391
+
15392
+ public:
15393
+ bool IsWindow() const override {
15394
+ return true;
15395
+ }
15396
+ bool IsFoldable() const override {
15397
+ return false;
15398
+ }
15399
+
15400
+ string ToString() const override;
15401
+
15402
+ bool KeysAreCompatible(const BoundWindowExpression *other) const;
15403
+ bool Equals(const BaseExpression *other) const override;
15404
+
15405
+ unique_ptr<Expression> Copy() override;
15406
+ };
15407
+ } // namespace duckdb
15408
+
15409
+
15410
+
15411
+ #include <random>
15412
+ namespace duckdb {
15413
+
15414
+ class AdaptiveFilter {
15415
+ public:
15416
+ explicit AdaptiveFilter(const Expression &expr);
15417
+ explicit AdaptiveFilter(TableFilterSet *table_filters);
15418
+ void AdaptRuntimeStatistics(double duration);
15419
+ vector<idx_t> permutation;
15420
+
15421
+ private:
15422
+ //! used for adaptive expression reordering
15423
+ idx_t iteration_count;
15424
+ idx_t swap_idx;
15425
+ idx_t right_random_border;
15426
+ idx_t observe_interval;
15427
+ idx_t execute_interval;
15428
+ double runtime_sum;
15429
+ double prev_mean;
15430
+ bool observe;
15431
+ bool warmup;
15432
+ vector<idx_t> swap_likeliness;
15433
+ std::default_random_engine generator;
15434
+ };
15435
+ } // namespace duckdb
15436
+
15437
+
15438
+ namespace duckdb {
15439
+ class ColumnSegment;
15440
+ class LocalTableStorage;
15441
+ class Index;
15442
+ class RowGroup;
15443
+ class UpdateSegment;
15444
+ class TableScanState;
15445
+ class ColumnSegment;
15446
+ class ValiditySegment;
15447
+ class TableFilterSet;
15448
+
15449
+ struct SegmentScanState {
15450
+ virtual ~SegmentScanState() {
15451
+ }
15452
+ };
15453
+
15454
+ struct IndexScanState {
15455
+ virtual ~IndexScanState() {
15456
+ }
15457
+ };
15458
+
15459
+ typedef unordered_map<block_id_t, unique_ptr<BufferHandle>> buffer_handle_set_t;
15460
+
15461
+ struct ColumnScanState {
15462
+ //! The column segment that is currently being scanned
15463
+ ColumnSegment *current;
15464
+ //! The current row index of the scan
15465
+ idx_t row_index;
15466
+ //! The internal row index (i.e. the position of the SegmentScanState)
15467
+ idx_t internal_index;
15468
+ //! Segment scan state
15469
+ unique_ptr<SegmentScanState> scan_state;
15470
+ //! Child states of the vector
15471
+ vector<ColumnScanState> child_states;
15472
+ //! Whether or not InitializeState has been called for this segment
15473
+ bool initialized = false;
15474
+ //! If this segment has already been checked for skipping purposes
15475
+ bool segment_checked = false;
15476
+
15477
+ public:
15478
+ //! Move the scan state forward by "count" rows (including all child states)
15479
+ void Next(idx_t count);
15480
+ //! Move ONLY this state forward by "count" rows (i.e. not the child states)
15481
+ void NextInternal(idx_t count);
15482
+ //! Move the scan state forward by STANDARD_VECTOR_SIZE rows
15483
+ void NextVector();
15484
+ };
15485
+
15486
+ struct ColumnFetchState {
15487
+ //! The set of pinned block handles for this set of fetches
15488
+ buffer_handle_set_t handles;
15489
+ //! Any child states of the fetch
15490
+ vector<unique_ptr<ColumnFetchState>> child_states;
15491
+ };
15492
+
15493
+ struct LocalScanState {
15494
+ ~LocalScanState();
15495
+
15496
+ void SetStorage(LocalTableStorage *storage);
15497
+ LocalTableStorage *GetStorage() {
15498
+ return storage;
15499
+ }
15500
+
15501
+ idx_t chunk_index;
15502
+ idx_t max_index;
15503
+ idx_t last_chunk_count;
15504
+ TableFilterSet *table_filters;
15505
+
15506
+ private:
15507
+ LocalTableStorage *storage = nullptr;
15508
+ };
15509
+
15510
+ class RowGroupScanState {
15511
+ public:
15512
+ RowGroupScanState(TableScanState &parent_p) : parent(parent_p), vector_index(0), max_row(0) {
15513
+ }
15514
+
15515
+ //! The parent scan state
15516
+ TableScanState &parent;
15517
+ //! The current row_group we are scanning
15518
+ RowGroup *row_group;
15519
+ //! The vector index within the row_group
15520
+ idx_t vector_index;
15521
+ //! The maximum row index of this row_group scan
15522
+ idx_t max_row;
15523
+ //! Child column scans
15524
+ unique_ptr<ColumnScanState[]> column_scans;
15525
+
15526
+ public:
15527
+ //! Move to the next vector, skipping past the current one
15528
+ void NextVector();
15529
+ };
15530
+
15531
+ class TableScanState {
15532
+ public:
15533
+ TableScanState() : row_group_scan_state(*this), max_row(0) {};
15534
+
15535
+ //! The row_group scan state
15536
+ RowGroupScanState row_group_scan_state;
15537
+ //! The total maximum row index
15538
+ idx_t max_row;
15539
+ //! The column identifiers of the scan
15540
+ vector<column_t> column_ids;
15541
+ //! The table filters (if any)
15542
+ TableFilterSet *table_filters = nullptr;
15543
+ //! Adaptive filter info (if any)
15544
+ unique_ptr<AdaptiveFilter> adaptive_filter;
15545
+ //! Transaction-local scan state
15546
+ LocalScanState local_state;
15547
+
15548
+ public:
15549
+ //! Move to the next vector
15550
+ void NextVector();
15551
+ };
15552
+
15553
+ class CreateIndexScanState : public TableScanState {
15554
+ public:
15555
+ vector<unique_ptr<StorageLockKey>> locks;
15556
+ unique_lock<mutex> append_lock;
15557
+ unique_lock<mutex> delete_lock;
15558
+ };
15559
+
15560
+ } // namespace duckdb
15561
+
15562
+
15563
+ namespace duckdb {
15564
+ class DataTable;
15565
+ class WriteAheadLog;
15566
+ struct TableAppendState;
15567
+
15568
+ class LocalTableStorage {
15569
+ public:
15570
+ explicit LocalTableStorage(DataTable &table);
15571
+ ~LocalTableStorage();
15572
+
15573
+ DataTable &table;
15574
+ //! The main chunk collection holding the data
15575
+ ChunkCollection collection;
15576
+ //! The set of unique indexes
15577
+ vector<unique_ptr<Index>> indexes;
15578
+ //! The set of deleted entries
15579
+ unordered_map<idx_t, unique_ptr<bool[]>> deleted_entries;
15580
+ //! The number of deleted rows
15581
+ idx_t deleted_rows;
15582
+ //! The number of active scans
15583
+ atomic<idx_t> active_scans;
15584
+
15585
+ public:
15586
+ void InitializeScan(LocalScanState &state, TableFilterSet *table_filters = nullptr);
15587
+ idx_t EstimatedSize();
15588
+
15589
+ void Clear();
15590
+ };
15591
+
15592
+ //! The LocalStorage class holds appends that have not been committed yet
15593
+ class LocalStorage {
15594
+ public:
15595
+ struct CommitState {
15596
+ unordered_map<DataTable *, unique_ptr<TableAppendState>> append_states;
15597
+ };
15598
+
15599
+ public:
15600
+ explicit LocalStorage(Transaction &transaction) : transaction(transaction) {
15601
+ }
15602
+
15603
+ //! Initialize a scan of the local storage
15604
+ void InitializeScan(DataTable *table, LocalScanState &state, TableFilterSet *table_filters);
15605
+ //! Scan
15606
+ void Scan(LocalScanState &state, const vector<column_t> &column_ids, DataChunk &result);
15607
+
15608
+ //! Append a chunk to the local storage
15609
+ void Append(DataTable *table, DataChunk &chunk);
15610
+ //! Delete a set of rows from the local storage
15611
+ idx_t Delete(DataTable *table, Vector &row_ids, idx_t count);
15612
+ //! Update a set of rows in the local storage
15613
+ void Update(DataTable *table, Vector &row_ids, const vector<column_t> &column_ids, DataChunk &data);
15614
+
15615
+ //! Commits the local storage, writing it to the WAL and completing the commit
15616
+ void Commit(LocalStorage::CommitState &commit_state, Transaction &transaction, WriteAheadLog *log,
15617
+ transaction_t commit_id);
15618
+
15619
+ bool ChangesMade() noexcept {
15620
+ return table_storage.size() > 0;
15621
+ }
15622
+ idx_t EstimatedSize();
15623
+
15624
+ bool Find(DataTable *table) {
15625
+ return table_storage.find(table) != table_storage.end();
15626
+ }
15627
+
15628
+ idx_t AddedRows(DataTable *table) {
15629
+ auto entry = table_storage.find(table);
15630
+ if (entry == table_storage.end()) {
15631
+ return 0;
15632
+ }
15633
+ return entry->second->collection.Count() - entry->second->deleted_rows;
15634
+ }
15635
+
15636
+ void AddColumn(DataTable *old_dt, DataTable *new_dt, ColumnDefinition &new_column, Expression *default_value);
15637
+ void ChangeType(DataTable *old_dt, DataTable *new_dt, idx_t changed_idx, const LogicalType &target_type,
15638
+ const vector<column_t> &bound_columns, Expression &cast_expr);
15639
+
15640
+ private:
15641
+ LocalTableStorage *GetStorage(DataTable *table);
15642
+
15643
+ template <class T>
15644
+ bool ScanTableStorage(DataTable &table, LocalTableStorage &storage, T &&fun);
15645
+
15646
+ private:
15647
+ Transaction &transaction;
15648
+ unordered_map<DataTable *, unique_ptr<LocalTableStorage>> table_storage;
15649
+
15650
+ void Flush(DataTable &table, LocalTableStorage &storage);
15651
+ };
15652
+
15653
+ } // namespace duckdb
15654
+
15655
+
15656
+
15657
+ namespace duckdb {
15658
+ class SequenceCatalogEntry;
15659
+
15660
+ class ColumnData;
15661
+ class ClientContext;
15662
+ class CatalogEntry;
15663
+ class DataTable;
15664
+ class DatabaseInstance;
15665
+ class WriteAheadLog;
15666
+
15667
+ class ChunkVectorInfo;
15668
+
15669
+ struct DeleteInfo;
15670
+ struct UpdateInfo;
15671
+
15672
+ //! The transaction object holds information about a currently running or past
15673
+ //! transaction
15674
+
15675
+ class Transaction {
15676
+ public:
15677
+ Transaction(weak_ptr<ClientContext> context, transaction_t start_time, transaction_t transaction_id,
15678
+ timestamp_t start_timestamp, idx_t catalog_version)
15679
+ : context(move(context)), start_time(start_time), transaction_id(transaction_id), commit_id(0),
15680
+ highest_active_query(0), active_query(MAXIMUM_QUERY_ID), start_timestamp(start_timestamp),
15681
+ catalog_version(catalog_version), storage(*this), is_invalidated(false) {
15682
+ }
15683
+
15684
+ weak_ptr<ClientContext> context;
15685
+ //! The start timestamp of this transaction
15686
+ transaction_t start_time;
15687
+ //! The transaction id of this transaction
15688
+ transaction_t transaction_id;
15689
+ //! The commit id of this transaction, if it has successfully been committed
15690
+ transaction_t commit_id;
15691
+ //! Highest active query when the transaction finished, used for cleaning up
15692
+ transaction_t highest_active_query;
15693
+ //! The current active query for the transaction. Set to MAXIMUM_QUERY_ID if
15694
+ //! no query is active.
15695
+ atomic<transaction_t> active_query;
15696
+ //! The timestamp when the transaction started
15697
+ timestamp_t start_timestamp;
15698
+ //! The catalog version when the transaction was started
15699
+ idx_t catalog_version;
15700
+ //! The set of uncommitted appends for the transaction
15701
+ LocalStorage storage;
15702
+ //! Map of all sequences that were used during the transaction and the value they had in this transaction
15703
+ unordered_map<SequenceCatalogEntry *, SequenceValue> sequence_usage;
15704
+ //! Whether or not the transaction has been invalidated
15705
+ bool is_invalidated;
15706
+
15707
+ public:
15708
+ static Transaction &GetTransaction(ClientContext &context);
15709
+
15710
+ void PushCatalogEntry(CatalogEntry *entry, data_ptr_t extra_data = nullptr, idx_t extra_data_size = 0);
15711
+
15712
+ //! Commit the current transaction with the given commit identifier. Returns an error message if the transaction
15713
+ //! commit failed, or an empty string if the commit was sucessful
15714
+ string Commit(DatabaseInstance &db, transaction_t commit_id, bool checkpoint) noexcept;
15715
+ //! Returns whether or not a commit of this transaction should trigger an automatic checkpoint
15716
+ bool AutomaticCheckpoint(DatabaseInstance &db);
15717
+
15718
+ //! Rollback
15719
+ void Rollback() noexcept {
15720
+ undo_buffer.Rollback();
15721
+ }
15722
+ //! Cleanup the undo buffer
15723
+ void Cleanup() {
15724
+ undo_buffer.Cleanup();
15725
+ }
15726
+
15727
+ void Invalidate() {
15728
+ is_invalidated = true;
15729
+ }
15730
+ bool IsInvalidated() {
15731
+ return is_invalidated;
15732
+ }
15733
+ bool ChangesMade();
15734
+
15735
+ timestamp_t GetCurrentTransactionStartTimestamp() {
15736
+ return start_timestamp;
15737
+ }
15738
+
15739
+ void PushDelete(DataTable *table, ChunkVectorInfo *vinfo, row_t rows[], idx_t count, idx_t base_row);
15740
+ void PushAppend(DataTable *table, idx_t row_start, idx_t row_count);
15741
+ UpdateInfo *CreateUpdateInfo(idx_t type_size, idx_t entries);
15742
+
15743
+ private:
15744
+ //! The undo buffer is used to store old versions of rows that are updated
15745
+ //! or deleted
15746
+ UndoBuffer undo_buffer;
15747
+
15748
+ Transaction(const Transaction &) = delete;
15749
+ };
15750
+
15751
+ } // namespace duckdb
15752
+
15753
+ #include <functional>
15754
+ #include <memory>
15755
+
15756
+ namespace duckdb {
15757
+ struct AlterInfo;
15758
+
15759
+ class ClientContext;
15760
+
15761
+ typedef unordered_map<CatalogSet *, unique_lock<mutex>> set_lock_map_t;
15762
+
15763
+ struct MappingValue {
15764
+ explicit MappingValue(idx_t index_) : index(index_), timestamp(0), deleted(false), parent(nullptr) {
15765
+ }
15766
+
15767
+ idx_t index;
15768
+ transaction_t timestamp;
15769
+ bool deleted;
15770
+ unique_ptr<MappingValue> child;
15771
+ MappingValue *parent;
15772
+ };
15773
+
15774
+ //! The Catalog Set stores (key, value) map of a set of CatalogEntries
15775
+ class CatalogSet {
15776
+ friend class DependencyManager;
15777
+ friend class EntryDropper;
15778
+
15779
+ public:
15780
+ DUCKDB_API explicit CatalogSet(Catalog &catalog, unique_ptr<DefaultGenerator> defaults = nullptr);
15781
+
15782
+ //! Create an entry in the catalog set. Returns whether or not it was
15783
+ //! successful.
15784
+ DUCKDB_API bool CreateEntry(ClientContext &context, const string &name, unique_ptr<CatalogEntry> value,
15785
+ unordered_set<CatalogEntry *> &dependencies);
15786
+
15787
+ DUCKDB_API bool AlterEntry(ClientContext &context, const string &name, AlterInfo *alter_info);
15788
+
15789
+ DUCKDB_API bool DropEntry(ClientContext &context, const string &name, bool cascade);
15790
+
15791
+ bool AlterOwnership(ClientContext &context, ChangeOwnershipInfo *info);
15792
+
15793
+ void CleanupEntry(CatalogEntry *catalog_entry);
15794
+
15795
+ //! Returns the entry with the specified name
15796
+ DUCKDB_API CatalogEntry *GetEntry(ClientContext &context, const string &name);
15797
+
15798
+ //! Gets the entry that is most similar to the given name (i.e. smallest levenshtein distance), or empty string if
15799
+ //! none is found. The returned pair consists of the entry name and the distance (smaller means closer).
15800
+ pair<string, idx_t> SimilarEntry(ClientContext &context, const string &name);
15801
+
15802
+ //! Rollback <entry> to be the currently valid entry for a certain catalog
15803
+ //! entry
15804
+ void Undo(CatalogEntry *entry);
15805
+
15806
+ //! Scan the catalog set, invoking the callback method for every committed entry
15807
+ DUCKDB_API void Scan(const std::function<void(CatalogEntry *)> &callback);
15808
+ //! Scan the catalog set, invoking the callback method for every entry
15809
+ DUCKDB_API void Scan(ClientContext &context, const std::function<void(CatalogEntry *)> &callback);
15810
+
15811
+ template <class T>
15812
+ vector<T *> GetEntries(ClientContext &context) {
15813
+ vector<T *> result;
15814
+ Scan(context, [&](CatalogEntry *entry) { result.push_back((T *)entry); });
15815
+ return result;
15816
+ }
15817
+
15818
+ DUCKDB_API static bool HasConflict(ClientContext &context, transaction_t timestamp);
15819
+ DUCKDB_API static bool UseTimestamp(ClientContext &context, transaction_t timestamp);
15820
+
15821
+ CatalogEntry *GetEntryFromIndex(idx_t index);
15822
+ void UpdateTimestamp(CatalogEntry *entry, transaction_t timestamp);
15823
+
15824
+ private:
15825
+ //! Adjusts table dependencies on the event of an UNDO
15826
+ void AdjustTableDependencies(CatalogEntry *entry);
15827
+ //! Adjust one dependency
15828
+ void AdjustDependency(CatalogEntry *entry, TableCatalogEntry *table, ColumnDefinition &column, bool remove);
15829
+ //! Adjust Enum dependency
15830
+ void AdjustEnumDependency(CatalogEntry *entry, ColumnDefinition &column, bool remove);
15831
+ //! Given a root entry, gets the entry valid for this transaction
15832
+ CatalogEntry *GetEntryForTransaction(ClientContext &context, CatalogEntry *current);
15833
+ CatalogEntry *GetCommittedEntry(CatalogEntry *current);
15834
+ bool GetEntryInternal(ClientContext &context, const string &name, idx_t &entry_index, CatalogEntry *&entry);
15835
+ bool GetEntryInternal(ClientContext &context, idx_t entry_index, CatalogEntry *&entry);
15836
+ //! Drops an entry from the catalog set; must hold the catalog_lock to safely call this
15837
+ void DropEntryInternal(ClientContext &context, idx_t entry_index, CatalogEntry &entry, bool cascade);
15838
+ CatalogEntry *CreateEntryInternal(ClientContext &context, unique_ptr<CatalogEntry> entry);
15839
+ MappingValue *GetMapping(ClientContext &context, const string &name, bool get_latest = false);
15840
+ void PutMapping(ClientContext &context, const string &name, idx_t entry_index);
15841
+ void DeleteMapping(ClientContext &context, const string &name);
15842
+ void DropEntryDependencies(ClientContext &context, idx_t entry_index, CatalogEntry &entry, bool cascade);
15843
+
15844
+ private:
15845
+ Catalog &catalog;
15846
+ //! The catalog lock is used to make changes to the data
15847
+ mutex catalog_lock;
15848
+ //! Mapping of string to catalog entry
15849
+ case_insensitive_map_t<unique_ptr<MappingValue>> mapping;
15850
+ //! The set of catalog entries
15851
+ unordered_map<idx_t, unique_ptr<CatalogEntry>> entries;
15852
+ //! The current catalog entry index
15853
+ idx_t current_entry = 0;
15854
+ //! The generator used to generate default internal entries
15855
+ unique_ptr<DefaultGenerator> defaults;
15856
+ };
15857
+ } // namespace duckdb
15858
+
15859
+
15860
+
15861
+ namespace duckdb {
15862
+ class ClientContext;
15863
+
15864
+ class StandardEntry;
15865
+ class TableCatalogEntry;
15866
+ class TableFunctionCatalogEntry;
15867
+ class SequenceCatalogEntry;
15868
+ class Serializer;
15869
+ class Deserializer;
15870
+
15871
+ enum class OnCreateConflict : uint8_t;
15872
+
15873
+ struct AlterTableInfo;
15874
+ struct CreateIndexInfo;
15875
+ struct CreateFunctionInfo;
15876
+ struct CreateCollationInfo;
15877
+ struct CreateViewInfo;
15878
+ struct BoundCreateTableInfo;
15879
+ struct CreatePragmaFunctionInfo;
15880
+ struct CreateSequenceInfo;
15881
+ struct CreateSchemaInfo;
15882
+ struct CreateTableFunctionInfo;
15883
+ struct CreateCopyFunctionInfo;
15884
+ struct CreateTypeInfo;
15885
+
15886
+ struct DropInfo;
15887
+
15888
+ //! A schema in the catalog
15889
+ class SchemaCatalogEntry : public CatalogEntry {
15890
+ friend class Catalog;
15891
+
15892
+ public:
15893
+ SchemaCatalogEntry(Catalog *catalog, string name, bool is_internal);
15894
+
15895
+ private:
15896
+ //! The catalog set holding the tables
15897
+ CatalogSet tables;
15898
+ //! The catalog set holding the indexes
15899
+ CatalogSet indexes;
15900
+ //! The catalog set holding the table functions
15901
+ CatalogSet table_functions;
15902
+ //! The catalog set holding the copy functions
15903
+ CatalogSet copy_functions;
15904
+ //! The catalog set holding the pragma functions
15905
+ CatalogSet pragma_functions;
15906
+ //! The catalog set holding the scalar and aggregate functions
15907
+ CatalogSet functions;
15908
+ //! The catalog set holding the sequences
15909
+ CatalogSet sequences;
15910
+ //! The catalog set holding the collations
15911
+ CatalogSet collations;
15912
+ //! The catalog set holding the types
15913
+ CatalogSet types;
15914
+
15915
+ public:
15916
+ //! Scan the specified catalog set, invoking the callback method for every entry
15917
+ void Scan(ClientContext &context, CatalogType type, const std::function<void(CatalogEntry *)> &callback);
15918
+ //! Scan the specified catalog set, invoking the callback method for every committed entry
15919
+ void Scan(CatalogType type, const std::function<void(CatalogEntry *)> &callback);
15920
+
15921
+ //! Serialize the meta information of the SchemaCatalogEntry a serializer
15922
+ virtual void Serialize(Serializer &serializer);
15923
+ //! Deserializes to a CreateSchemaInfo
15924
+ static unique_ptr<CreateSchemaInfo> Deserialize(Deserializer &source);
15925
+
15926
+ string ToSQL() override;
15927
+
15928
+ //! Creates an index with the given name in the schema
15929
+ CatalogEntry *CreateIndex(ClientContext &context, CreateIndexInfo *info, TableCatalogEntry *table);
15930
+
15931
+ private:
15932
+ //! Create a scalar or aggregate function within the given schema
15933
+ CatalogEntry *CreateFunction(ClientContext &context, CreateFunctionInfo *info);
15934
+ //! Creates a table with the given name in the schema
15935
+ CatalogEntry *CreateTable(ClientContext &context, BoundCreateTableInfo *info);
15936
+ //! Creates a view with the given name in the schema
15937
+ CatalogEntry *CreateView(ClientContext &context, CreateViewInfo *info);
15938
+ //! Creates a sequence with the given name in the schema
15939
+ CatalogEntry *CreateSequence(ClientContext &context, CreateSequenceInfo *info);
15940
+ //! Create a table function within the given schema
15941
+ CatalogEntry *CreateTableFunction(ClientContext &context, CreateTableFunctionInfo *info);
15942
+ //! Create a copy function within the given schema
15943
+ CatalogEntry *CreateCopyFunction(ClientContext &context, CreateCopyFunctionInfo *info);
15944
+ //! Create a pragma function within the given schema
15945
+ CatalogEntry *CreatePragmaFunction(ClientContext &context, CreatePragmaFunctionInfo *info);
15946
+ //! Create a collation within the given schema
15947
+ CatalogEntry *CreateCollation(ClientContext &context, CreateCollationInfo *info);
15948
+ //! Create a enum within the given schema
15949
+ CatalogEntry *CreateType(ClientContext &context, CreateTypeInfo *info);
15950
+
15951
+ //! Drops an entry from the schema
15952
+ void DropEntry(ClientContext &context, DropInfo *info);
15953
+
15954
+ //! Append a scalar or aggregate function within the given schema
15955
+ CatalogEntry *AddFunction(ClientContext &context, CreateFunctionInfo *info);
15956
+
15957
+ //! Alters a catalog entry
15958
+ void Alter(ClientContext &context, AlterInfo *info);
15959
+
15960
+ //! Add a catalog entry to this schema
15961
+ CatalogEntry *AddEntry(ClientContext &context, unique_ptr<StandardEntry> entry, OnCreateConflict on_conflict);
15962
+ //! Add a catalog entry to this schema
15963
+ CatalogEntry *AddEntry(ClientContext &context, unique_ptr<StandardEntry> entry, OnCreateConflict on_conflict,
15964
+ unordered_set<CatalogEntry *> dependencies);
15965
+
15966
+ //! Get the catalog set for the specified type
15967
+ CatalogSet &GetCatalogSet(CatalogType type);
15968
+ };
15969
+ } // namespace duckdb
15970
+
15971
+
15972
+ //===----------------------------------------------------------------------===//
15973
+ // DuckDB
15974
+ //
15975
+ // duckdb/common/deque.hpp
15976
+ //
15977
+ //
15978
+ //===----------------------------------------------------------------------===//
15979
+
15980
+
15981
+
15982
+ #include <deque>
15983
+
15984
+ namespace duckdb {
15985
+ using std::deque;
15986
+ }
15987
+
15988
+ //===----------------------------------------------------------------------===//
15989
+ // DuckDB
15990
+ //
15991
+ // duckdb/common/progress_bar.hpp
15992
+ //
15993
+ //
15994
+ //===----------------------------------------------------------------------===//
15995
+
15996
+
15997
+
15998
+ #ifndef DUCKDB_NO_THREADS
15999
+ //===----------------------------------------------------------------------===//
16000
+ // DuckDB
16001
+ //
16002
+ // duckdb/common/thread.hpp
16003
+ //
16004
+ //
16005
+ //===----------------------------------------------------------------------===//
16006
+
16007
+
16008
+
16009
+ #include <thread>
16010
+
16011
+ namespace duckdb {
16012
+ using std::thread;
16013
+ }
16014
+
16015
+ #include <future>
16016
+ #endif
16017
+
16018
+
16019
+ //===----------------------------------------------------------------------===//
16020
+ // DuckDB
16021
+ //
16022
+ // duckdb/execution/executor.hpp
16023
+ //
16024
+ //
16025
+ //===----------------------------------------------------------------------===//
16026
+
16027
+
16028
+
16029
+
16030
+
16031
+ //===----------------------------------------------------------------------===//
16032
+ // DuckDB
16033
+ //
16034
+ // duckdb/parallel/pipeline.hpp
16035
+ //
16036
+ //
16037
+ //===----------------------------------------------------------------------===//
16038
+
16039
+
16040
+
16041
+
16042
+ //===----------------------------------------------------------------------===//
16043
+ // DuckDB
16044
+ //
16045
+ // duckdb/execution/physical_operator.hpp
16046
+ //
12717
16047
  //
12718
16048
  //===----------------------------------------------------------------------===//
12719
16049
 
@@ -13042,155 +16372,6 @@ public:
13042
16372
 
13043
16373
  } // namespace duckdb
13044
16374
 
13045
- //===----------------------------------------------------------------------===//
13046
- // DuckDB
13047
- //
13048
- // duckdb/function/table_function.hpp
13049
- //
13050
- //
13051
- //===----------------------------------------------------------------------===//
13052
-
13053
-
13054
-
13055
-
13056
-
13057
-
13058
-
13059
- #include <functional>
13060
-
13061
- namespace duckdb {
13062
- class BaseStatistics;
13063
- class LogicalGet;
13064
- struct ParallelState;
13065
- class TableFilterSet;
13066
-
13067
- struct FunctionOperatorData {
13068
- DUCKDB_API virtual ~FunctionOperatorData();
13069
- };
13070
-
13071
- struct TableFilterCollection {
13072
- DUCKDB_API explicit TableFilterCollection(TableFilterSet *table_filters);
13073
-
13074
- TableFilterSet *table_filters;
13075
- };
13076
-
13077
- typedef unique_ptr<FunctionData> (*table_function_bind_t)(ClientContext &context, vector<Value> &inputs,
13078
- unordered_map<string, Value> &named_parameters,
13079
- vector<LogicalType> &input_table_types,
13080
- vector<string> &input_table_names,
13081
- vector<LogicalType> &return_types, vector<string> &names);
13082
- typedef unique_ptr<FunctionOperatorData> (*table_function_init_t)(ClientContext &context, const FunctionData *bind_data,
13083
- const vector<column_t> &column_ids,
13084
- TableFilterCollection *filters);
13085
- typedef unique_ptr<BaseStatistics> (*table_statistics_t)(ClientContext &context, const FunctionData *bind_data,
13086
- column_t column_index);
13087
- typedef void (*table_function_t)(ClientContext &context, const FunctionData *bind_data,
13088
- FunctionOperatorData *operator_state, DataChunk *input, DataChunk &output);
13089
-
13090
- typedef void (*table_function_parallel_t)(ClientContext &context, const FunctionData *bind_data,
13091
- FunctionOperatorData *operator_state, DataChunk *input, DataChunk &output,
13092
- ParallelState *parallel_state);
13093
-
13094
- typedef void (*table_function_cleanup_t)(ClientContext &context, const FunctionData *bind_data,
13095
- FunctionOperatorData *operator_state);
13096
- typedef idx_t (*table_function_max_threads_t)(ClientContext &context, const FunctionData *bind_data);
13097
- typedef unique_ptr<ParallelState> (*table_function_init_parallel_state_t)(ClientContext &context,
13098
- const FunctionData *bind_data,
13099
- const vector<column_t> &column_ids,
13100
- TableFilterCollection *filters);
13101
- typedef unique_ptr<FunctionOperatorData> (*table_function_init_parallel_t)(ClientContext &context,
13102
- const FunctionData *bind_data,
13103
- ParallelState *state,
13104
- const vector<column_t> &column_ids,
13105
- TableFilterCollection *filters);
13106
- typedef bool (*table_function_parallel_state_next_t)(ClientContext &context, const FunctionData *bind_data,
13107
- FunctionOperatorData *state, ParallelState *parallel_state);
13108
- typedef int (*table_function_progress_t)(ClientContext &context, const FunctionData *bind_data);
13109
- typedef void (*table_function_dependency_t)(unordered_set<CatalogEntry *> &dependencies, const FunctionData *bind_data);
13110
- typedef unique_ptr<NodeStatistics> (*table_function_cardinality_t)(ClientContext &context,
13111
- const FunctionData *bind_data);
13112
- typedef void (*table_function_pushdown_complex_filter_t)(ClientContext &context, LogicalGet &get,
13113
- FunctionData *bind_data,
13114
- vector<unique_ptr<Expression>> &filters);
13115
- typedef string (*table_function_to_string_t)(const FunctionData *bind_data);
13116
-
13117
- class TableFunction : public SimpleNamedParameterFunction {
13118
- public:
13119
- DUCKDB_API
13120
- TableFunction(string name, vector<LogicalType> arguments, table_function_t function,
13121
- table_function_bind_t bind = nullptr, table_function_init_t init = nullptr,
13122
- table_statistics_t statistics = nullptr, table_function_cleanup_t cleanup = nullptr,
13123
- table_function_dependency_t dependency = nullptr, table_function_cardinality_t cardinality = nullptr,
13124
- table_function_pushdown_complex_filter_t pushdown_complex_filter = nullptr,
13125
- table_function_to_string_t to_string = nullptr, table_function_max_threads_t max_threads = nullptr,
13126
- table_function_init_parallel_state_t init_parallel_state = nullptr,
13127
- table_function_parallel_t parallel_function = nullptr,
13128
- table_function_init_parallel_t parallel_init = nullptr,
13129
- table_function_parallel_state_next_t parallel_state_next = nullptr, bool projection_pushdown = false,
13130
- bool filter_pushdown = false, table_function_progress_t query_progress = nullptr);
13131
- DUCKDB_API
13132
- TableFunction(const vector<LogicalType> &arguments, table_function_t function, table_function_bind_t bind = nullptr,
13133
- table_function_init_t init = nullptr, table_statistics_t statistics = nullptr,
13134
- table_function_cleanup_t cleanup = nullptr, table_function_dependency_t dependency = nullptr,
13135
- table_function_cardinality_t cardinality = nullptr,
13136
- table_function_pushdown_complex_filter_t pushdown_complex_filter = nullptr,
13137
- table_function_to_string_t to_string = nullptr, table_function_max_threads_t max_threads = nullptr,
13138
- table_function_init_parallel_state_t init_parallel_state = nullptr,
13139
- table_function_parallel_t parallel_function = nullptr,
13140
- table_function_init_parallel_t parallel_init = nullptr,
13141
- table_function_parallel_state_next_t parallel_state_next = nullptr, bool projection_pushdown = false,
13142
- bool filter_pushdown = false, table_function_progress_t query_progress = nullptr);
13143
- DUCKDB_API TableFunction();
13144
-
13145
- //! Bind function
13146
- //! This function is used for determining the return type of a table producing function and returning bind data
13147
- //! The returned FunctionData object should be constant and should not be changed during execution.
13148
- table_function_bind_t bind;
13149
- //! (Optional) init function
13150
- //! Initialize the operator state of the function. The operator state is used to keep track of the progress in the
13151
- //! table function.
13152
- table_function_init_t init;
13153
- //! The main function
13154
- table_function_t function;
13155
- //! (Optional) statistics function
13156
- //! Returns the statistics of a specified column
13157
- table_statistics_t statistics;
13158
- //! (Optional) cleanup function
13159
- //! The final cleanup function, called after all data is exhausted from the main function
13160
- table_function_cleanup_t cleanup;
13161
- //! (Optional) dependency function
13162
- //! Sets up which catalog entries this table function depend on
13163
- table_function_dependency_t dependency;
13164
- //! (Optional) cardinality function
13165
- //! Returns the expected cardinality of this scan
13166
- table_function_cardinality_t cardinality;
13167
- //! (Optional) pushdown a set of arbitrary filter expressions, rather than only simple comparisons with a constant
13168
- //! Any functions remaining in the expression list will be pushed as a regular filter after the scan
13169
- table_function_pushdown_complex_filter_t pushdown_complex_filter;
13170
- //! (Optional) function for rendering the operator to a string in profiling output
13171
- table_function_to_string_t to_string;
13172
- //! (Optional) function that returns the maximum amount of threads that can work on this task
13173
- table_function_max_threads_t max_threads;
13174
- //! (Optional) initialize the parallel scan state, called once in total.
13175
- table_function_init_parallel_state_t init_parallel_state;
13176
- //! (Optional) Parallel version of the main function
13177
- table_function_parallel_t parallel_function;
13178
- //! (Optional) initialize the parallel scan given the parallel state. Called once per task. Return nullptr if there
13179
- //! is nothing left to scan.
13180
- table_function_init_parallel_t parallel_init;
13181
- //! (Optional) return the next chunk to process in the parallel scan, or return nullptr if there is none
13182
- table_function_parallel_state_next_t parallel_state_next;
13183
- //! (Optional) return how much of the table we have scanned up to this point (% of the data)
13184
- table_function_progress_t table_scan_progress;
13185
- //! Whether or not the table function supports projection pushdown. If not supported a projection will be added
13186
- //! that filters out unused columns.
13187
- bool projection_pushdown;
13188
- //! Whether or not the table function supports filter pushdown. If not supported a filter will be added
13189
- //! that applies the table filter directly.
13190
- bool filter_pushdown;
13191
- };
13192
-
13193
- } // namespace duckdb
13194
16375
 
13195
16376
  //===----------------------------------------------------------------------===//
13196
16377
  // DuckDB
@@ -13911,84 +17092,6 @@ private:
13911
17092
 
13912
17093
 
13913
17094
 
13914
- //===----------------------------------------------------------------------===//
13915
- // DuckDB
13916
- //
13917
- // duckdb/parser/parsed_data/create_info.hpp
13918
- //
13919
- //
13920
- //===----------------------------------------------------------------------===//
13921
-
13922
-
13923
-
13924
- //===----------------------------------------------------------------------===//
13925
- // DuckDB
13926
- //
13927
- // duckdb/parser/parsed_data/parse_info.hpp
13928
- //
13929
- //
13930
- //===----------------------------------------------------------------------===//
13931
-
13932
-
13933
-
13934
-
13935
-
13936
- namespace duckdb {
13937
-
13938
- struct ParseInfo {
13939
- virtual ~ParseInfo() {
13940
- }
13941
- };
13942
-
13943
- } // namespace duckdb
13944
-
13945
-
13946
-
13947
- namespace duckdb {
13948
-
13949
- enum class OnCreateConflict : uint8_t {
13950
- // Standard: throw error
13951
- ERROR_ON_CONFLICT,
13952
- // CREATE IF NOT EXISTS, silently do nothing on conflict
13953
- IGNORE_ON_CONFLICT,
13954
- // CREATE OR REPLACE
13955
- REPLACE_ON_CONFLICT
13956
- };
13957
-
13958
- struct CreateInfo : public ParseInfo {
13959
- explicit CreateInfo(CatalogType type, string schema = DEFAULT_SCHEMA)
13960
- : type(type), schema(schema), on_conflict(OnCreateConflict::ERROR_ON_CONFLICT), temporary(false),
13961
- internal(false) {
13962
- }
13963
- ~CreateInfo() override {
13964
- }
13965
-
13966
- //! The to-be-created catalog type
13967
- CatalogType type;
13968
- //! The schema name of the entry
13969
- string schema;
13970
- //! What to do on create conflict
13971
- OnCreateConflict on_conflict;
13972
- //! Whether or not the entry is temporary
13973
- bool temporary;
13974
- //! Whether or not the entry is an internal entry
13975
- bool internal;
13976
- //! The SQL string of the CREATE statement
13977
- string sql;
13978
-
13979
- public:
13980
- virtual unique_ptr<CreateInfo> Copy() const = 0;
13981
- void CopyProperties(CreateInfo &other) const {
13982
- other.type = type;
13983
- other.schema = schema;
13984
- other.on_conflict = on_conflict;
13985
- other.temporary = temporary;
13986
- other.internal = internal;
13987
- other.sql = sql;
13988
- }
13989
- };
13990
-
13991
- } // namespace duckdb
13992
17095
 
13993
17096
 
13994
17097