duckdb 0.4.1-dev188.0 → 0.4.1-dev204.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/duckdb.hpp CHANGED
@@ -11,8 +11,8 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
11
11
  #pragma once
12
12
  #define DUCKDB_AMALGAMATION 1
13
13
  #define DUCKDB_AMALGAMATION_EXTENDED 1
14
- #define DUCKDB_SOURCE_ID "b5a0b2595"
15
- #define DUCKDB_VERSION "v0.4.1-dev188"
14
+ #define DUCKDB_SOURCE_ID "78baad646"
15
+ #define DUCKDB_VERSION "v0.4.1-dev204"
16
16
  //===----------------------------------------------------------------------===//
17
17
  // DuckDB
18
18
  //
@@ -13844,297 +13844,6 @@ private:
13844
13844
 
13845
13845
 
13846
13846
 
13847
-
13848
-
13849
-
13850
- namespace duckdb {
13851
- class BoundResultModifier;
13852
- class BoundSelectNode;
13853
- class ClientContext;
13854
- class ExpressionBinder;
13855
- class LimitModifier;
13856
- class OrderBinder;
13857
- class TableCatalogEntry;
13858
- class ViewCatalogEntry;
13859
- class TableMacroCatalogEntry;
13860
-
13861
- struct CreateInfo;
13862
- struct BoundCreateTableInfo;
13863
- struct BoundCreateFunctionInfo;
13864
- struct CommonTableExpressionInfo;
13865
-
13866
- enum class BindingMode : uint8_t { STANDARD_BINDING, EXTRACT_NAMES };
13867
-
13868
- struct CorrelatedColumnInfo {
13869
- ColumnBinding binding;
13870
- LogicalType type;
13871
- string name;
13872
- idx_t depth;
13873
-
13874
- explicit CorrelatedColumnInfo(BoundColumnRefExpression &expr)
13875
- : binding(expr.binding), type(expr.return_type), name(expr.GetName()), depth(expr.depth) {
13876
- }
13877
-
13878
- bool operator==(const CorrelatedColumnInfo &rhs) const {
13879
- return binding == rhs.binding;
13880
- }
13881
- };
13882
-
13883
- //! Bind the parsed query tree to the actual columns present in the catalog.
13884
- /*!
13885
- The binder is responsible for binding tables and columns to actual physical
13886
- tables and columns in the catalog. In the process, it also resolves types of
13887
- all expressions.
13888
- */
13889
- class Binder : public std::enable_shared_from_this<Binder> {
13890
- friend class ExpressionBinder;
13891
- friend class SelectBinder;
13892
- friend class RecursiveSubqueryPlanner;
13893
-
13894
- public:
13895
- static shared_ptr<Binder> CreateBinder(ClientContext &context, Binder *parent = nullptr, bool inherit_ctes = true);
13896
-
13897
- //! The client context
13898
- ClientContext &context;
13899
- //! A mapping of names to common table expressions
13900
- case_insensitive_map_t<CommonTableExpressionInfo *> CTE_bindings;
13901
- //! The CTEs that have already been bound
13902
- unordered_set<CommonTableExpressionInfo *> bound_ctes;
13903
- //! The bind context
13904
- BindContext bind_context;
13905
- //! The set of correlated columns bound by this binder (FIXME: this should probably be an unordered_set and not a
13906
- //! vector)
13907
- vector<CorrelatedColumnInfo> correlated_columns;
13908
- //! The set of parameter expressions bound by this binder
13909
- vector<BoundParameterExpression *> *parameters;
13910
- //! The types of the prepared statement parameters, if any
13911
- vector<LogicalType> *parameter_types;
13912
- //! Statement properties
13913
- StatementProperties properties;
13914
- //! The alias for the currently processing subquery, if it exists
13915
- string alias;
13916
- //! Macro parameter bindings (if any)
13917
- MacroBinding *macro_binding = nullptr;
13918
-
13919
- public:
13920
- BoundStatement Bind(SQLStatement &statement);
13921
- BoundStatement Bind(QueryNode &node);
13922
-
13923
- unique_ptr<BoundCreateTableInfo> BindCreateTableInfo(unique_ptr<CreateInfo> info);
13924
- void BindCreateViewInfo(CreateViewInfo &base);
13925
- SchemaCatalogEntry *BindSchema(CreateInfo &info);
13926
- SchemaCatalogEntry *BindCreateFunctionInfo(CreateInfo &info);
13927
-
13928
- //! Check usage, and cast named parameters to their types
13929
- static void BindNamedParameters(named_parameter_type_map_t &types, named_parameter_map_t &values,
13930
- QueryErrorContext &error_context, string &func_name);
13931
-
13932
- unique_ptr<BoundTableRef> Bind(TableRef &ref);
13933
- unique_ptr<LogicalOperator> CreatePlan(BoundTableRef &ref);
13934
-
13935
- //! Generates an unused index for a table
13936
- idx_t GenerateTableIndex();
13937
-
13938
- //! Add a common table expression to the binder
13939
- void AddCTE(const string &name, CommonTableExpressionInfo *cte);
13940
- //! Find a common table expression by name; returns nullptr if none exists
13941
- CommonTableExpressionInfo *FindCTE(const string &name, bool skip = false);
13942
-
13943
- bool CTEIsAlreadyBound(CommonTableExpressionInfo *cte);
13944
-
13945
- //! Add the view to the set of currently bound views - used for detecting recursive view definitions
13946
- void AddBoundView(ViewCatalogEntry *view);
13947
-
13948
- void PushExpressionBinder(ExpressionBinder *binder);
13949
- void PopExpressionBinder();
13950
- void SetActiveBinder(ExpressionBinder *binder);
13951
- ExpressionBinder *GetActiveBinder();
13952
- bool HasActiveBinder();
13953
-
13954
- vector<ExpressionBinder *> &GetActiveBinders();
13955
-
13956
- void MergeCorrelatedColumns(vector<CorrelatedColumnInfo> &other);
13957
- //! Add a correlated column to this binder (if it does not exist)
13958
- void AddCorrelatedColumn(const CorrelatedColumnInfo &info);
13959
-
13960
- string FormatError(ParsedExpression &expr_context, const string &message);
13961
- string FormatError(TableRef &ref_context, const string &message);
13962
-
13963
- string FormatErrorRecursive(idx_t query_location, const string &message, vector<ExceptionFormatValue> &values);
13964
- template <class T, typename... Args>
13965
- string FormatErrorRecursive(idx_t query_location, const string &msg, vector<ExceptionFormatValue> &values, T param,
13966
- Args... params) {
13967
- values.push_back(ExceptionFormatValue::CreateFormatValue<T>(param));
13968
- return FormatErrorRecursive(query_location, msg, values, params...);
13969
- }
13970
-
13971
- template <typename... Args>
13972
- string FormatError(idx_t query_location, const string &msg, Args... params) {
13973
- vector<ExceptionFormatValue> values;
13974
- return FormatErrorRecursive(query_location, msg, values, params...);
13975
- }
13976
-
13977
- static void BindLogicalType(ClientContext &context, LogicalType &type, const string &schema = "");
13978
-
13979
- bool HasMatchingBinding(const string &table_name, const string &column_name, string &error_message);
13980
- bool HasMatchingBinding(const string &schema_name, const string &table_name, const string &column_name,
13981
- string &error_message);
13982
-
13983
- void SetBindingMode(BindingMode mode);
13984
- BindingMode GetBindingMode();
13985
- void AddTableName(string table_name);
13986
- const unordered_set<string> &GetTableNames();
13987
-
13988
- private:
13989
- //! The parent binder (if any)
13990
- shared_ptr<Binder> parent;
13991
- //! The vector of active binders
13992
- vector<ExpressionBinder *> active_binders;
13993
- //! The count of bound_tables
13994
- idx_t bound_tables;
13995
- //! Whether or not the binder has any unplanned subqueries that still need to be planned
13996
- bool has_unplanned_subqueries = false;
13997
- //! Whether or not subqueries should be planned already
13998
- bool plan_subquery = true;
13999
- //! Whether CTEs should reference the parent binder (if it exists)
14000
- bool inherit_ctes = true;
14001
- //! Whether or not the binder can contain NULLs as the root of expressions
14002
- bool can_contain_nulls = false;
14003
- //! The root statement of the query that is currently being parsed
14004
- SQLStatement *root_statement = nullptr;
14005
- //! Binding mode
14006
- BindingMode mode = BindingMode::STANDARD_BINDING;
14007
- //! Table names extracted for BindingMode::EXTRACT_NAMES
14008
- unordered_set<string> table_names;
14009
- //! The set of bound views
14010
- unordered_set<ViewCatalogEntry *> bound_views;
14011
-
14012
- private:
14013
- //! Bind the expressions of generated columns to check for errors
14014
- void BindGeneratedColumns(BoundCreateTableInfo &info);
14015
- //! Bind the default values of the columns of a table
14016
- void BindDefaultValues(vector<ColumnDefinition> &columns, vector<unique_ptr<Expression>> &bound_defaults);
14017
- //! Bind a limit value (LIMIT or OFFSET)
14018
- unique_ptr<Expression> BindDelimiter(ClientContext &context, OrderBinder &order_binder,
14019
- unique_ptr<ParsedExpression> delimiter, const LogicalType &type,
14020
- Value &delimiter_value);
14021
-
14022
- //! Move correlated expressions from the child binder to this binder
14023
- void MoveCorrelatedExpressions(Binder &other);
14024
-
14025
- BoundStatement Bind(SelectStatement &stmt);
14026
- BoundStatement Bind(InsertStatement &stmt);
14027
- BoundStatement Bind(CopyStatement &stmt);
14028
- BoundStatement Bind(DeleteStatement &stmt);
14029
- BoundStatement Bind(UpdateStatement &stmt);
14030
- BoundStatement Bind(CreateStatement &stmt);
14031
- BoundStatement Bind(DropStatement &stmt);
14032
- BoundStatement Bind(AlterStatement &stmt);
14033
- BoundStatement Bind(TransactionStatement &stmt);
14034
- BoundStatement Bind(PragmaStatement &stmt);
14035
- BoundStatement Bind(ExplainStatement &stmt);
14036
- BoundStatement Bind(VacuumStatement &stmt);
14037
- BoundStatement Bind(RelationStatement &stmt);
14038
- BoundStatement Bind(ShowStatement &stmt);
14039
- BoundStatement Bind(CallStatement &stmt);
14040
- BoundStatement Bind(ExportStatement &stmt);
14041
- BoundStatement Bind(ExtensionStatement &stmt);
14042
- BoundStatement Bind(SetStatement &stmt);
14043
- BoundStatement Bind(LoadStatement &stmt);
14044
- BoundStatement BindReturning(vector<unique_ptr<ParsedExpression>> returning_list, TableCatalogEntry *table,
14045
- idx_t update_table_index, unique_ptr<LogicalOperator> child_operator,
14046
- BoundStatement result);
14047
-
14048
- unique_ptr<QueryNode> BindTableMacro(FunctionExpression &function, TableMacroCatalogEntry *macro_func, idx_t depth);
14049
-
14050
- unique_ptr<BoundQueryNode> BindNode(SelectNode &node);
14051
- unique_ptr<BoundQueryNode> BindNode(SetOperationNode &node);
14052
- unique_ptr<BoundQueryNode> BindNode(RecursiveCTENode &node);
14053
- unique_ptr<BoundQueryNode> BindNode(QueryNode &node);
14054
-
14055
- unique_ptr<LogicalOperator> VisitQueryNode(BoundQueryNode &node, unique_ptr<LogicalOperator> root);
14056
- unique_ptr<LogicalOperator> CreatePlan(BoundRecursiveCTENode &node);
14057
- unique_ptr<LogicalOperator> CreatePlan(BoundSelectNode &statement);
14058
- unique_ptr<LogicalOperator> CreatePlan(BoundSetOperationNode &node);
14059
- unique_ptr<LogicalOperator> CreatePlan(BoundQueryNode &node);
14060
-
14061
- unique_ptr<BoundTableRef> Bind(BaseTableRef &ref);
14062
- unique_ptr<BoundTableRef> Bind(CrossProductRef &ref);
14063
- unique_ptr<BoundTableRef> Bind(JoinRef &ref);
14064
- unique_ptr<BoundTableRef> Bind(SubqueryRef &ref, CommonTableExpressionInfo *cte = nullptr);
14065
- unique_ptr<BoundTableRef> Bind(TableFunctionRef &ref);
14066
- unique_ptr<BoundTableRef> Bind(EmptyTableRef &ref);
14067
- unique_ptr<BoundTableRef> Bind(ExpressionListRef &ref);
14068
-
14069
- bool BindTableFunctionParameters(TableFunctionCatalogEntry &table_function,
14070
- vector<unique_ptr<ParsedExpression>> &expressions, vector<LogicalType> &arguments,
14071
- vector<Value> &parameters, named_parameter_map_t &named_parameters,
14072
- unique_ptr<BoundSubqueryRef> &subquery, string &error);
14073
- bool BindTableInTableOutFunction(vector<unique_ptr<ParsedExpression>> &expressions,
14074
- unique_ptr<BoundSubqueryRef> &subquery, string &error);
14075
- unique_ptr<LogicalOperator> BindTableFunction(TableFunction &function, vector<Value> parameters);
14076
- unique_ptr<LogicalOperator>
14077
- BindTableFunctionInternal(TableFunction &table_function, const string &function_name, vector<Value> parameters,
14078
- named_parameter_map_t named_parameters, vector<LogicalType> input_table_types,
14079
- vector<string> input_table_names, const vector<string> &column_name_alias,
14080
- unique_ptr<ExternalDependency> external_dependency);
14081
-
14082
- unique_ptr<LogicalOperator> CreatePlan(BoundBaseTableRef &ref);
14083
- unique_ptr<LogicalOperator> CreatePlan(BoundCrossProductRef &ref);
14084
- unique_ptr<LogicalOperator> CreatePlan(BoundJoinRef &ref);
14085
- unique_ptr<LogicalOperator> CreatePlan(BoundSubqueryRef &ref);
14086
- unique_ptr<LogicalOperator> CreatePlan(BoundTableFunction &ref);
14087
- unique_ptr<LogicalOperator> CreatePlan(BoundEmptyTableRef &ref);
14088
- unique_ptr<LogicalOperator> CreatePlan(BoundExpressionListRef &ref);
14089
- unique_ptr<LogicalOperator> CreatePlan(BoundCTERef &ref);
14090
-
14091
- BoundStatement BindCopyTo(CopyStatement &stmt);
14092
- BoundStatement BindCopyFrom(CopyStatement &stmt);
14093
-
14094
- void BindModifiers(OrderBinder &order_binder, QueryNode &statement, BoundQueryNode &result);
14095
- void BindModifierTypes(BoundQueryNode &result, const vector<LogicalType> &sql_types, idx_t projection_index);
14096
-
14097
- BoundStatement BindSummarize(ShowStatement &stmt);
14098
- unique_ptr<BoundResultModifier> BindLimit(OrderBinder &order_binder, LimitModifier &limit_mod);
14099
- unique_ptr<BoundResultModifier> BindLimitPercent(OrderBinder &order_binder, LimitPercentModifier &limit_mod);
14100
- unique_ptr<Expression> BindOrderExpression(OrderBinder &order_binder, unique_ptr<ParsedExpression> expr);
14101
-
14102
- unique_ptr<LogicalOperator> PlanFilter(unique_ptr<Expression> condition, unique_ptr<LogicalOperator> root);
14103
-
14104
- void PlanSubqueries(unique_ptr<Expression> *expr, unique_ptr<LogicalOperator> *root);
14105
- unique_ptr<Expression> PlanSubquery(BoundSubqueryExpression &expr, unique_ptr<LogicalOperator> &root);
14106
-
14107
- unique_ptr<LogicalOperator> CastLogicalOperatorToTypes(vector<LogicalType> &source_types,
14108
- vector<LogicalType> &target_types,
14109
- unique_ptr<LogicalOperator> op);
14110
-
14111
- string FindBinding(const string &using_column, const string &join_side);
14112
- bool TryFindBinding(const string &using_column, const string &join_side, string &result);
14113
-
14114
- void AddUsingBindingSet(unique_ptr<UsingColumnSet> set);
14115
- string RetrieveUsingBinding(Binder &current_binder, UsingColumnSet *current_set, const string &column_name,
14116
- const string &join_side, UsingColumnSet *new_set);
14117
-
14118
- public:
14119
- // This should really be a private constructor, but make_shared does not allow it...
14120
- // If you are thinking about calling this, you should probably call Binder::CreateBinder
14121
- Binder(bool I_know_what_I_am_doing, ClientContext &context, shared_ptr<Binder> parent, bool inherit_ctes);
14122
- };
14123
-
14124
- } // namespace duckdb
14125
-
14126
- //===----------------------------------------------------------------------===//
14127
- // DuckDB
14128
- //
14129
- // duckdb/planner/bound_query_node.hpp
14130
- //
14131
- //
14132
- //===----------------------------------------------------------------------===//
14133
-
14134
-
14135
-
14136
-
14137
-
14138
13847
  //===----------------------------------------------------------------------===//
14139
13848
  // DuckDB
14140
13849
  //
@@ -14321,146 +14030,452 @@ struct SampleOptions {
14321
14030
  static bool Equals(SampleOptions *a, SampleOptions *b);
14322
14031
  };
14323
14032
 
14324
- } // namespace duckdb
14033
+ } // namespace duckdb
14034
+
14035
+
14036
+ namespace duckdb {
14037
+ class Deserializer;
14038
+ class Serializer;
14039
+
14040
+ //! Represents a generic expression that returns a table.
14041
+ class TableRef {
14042
+ public:
14043
+ explicit TableRef(TableReferenceType type) : type(type) {
14044
+ }
14045
+ virtual ~TableRef() {
14046
+ }
14047
+
14048
+ TableReferenceType type;
14049
+ string alias;
14050
+ //! Sample options (if any)
14051
+ unique_ptr<SampleOptions> sample;
14052
+ //! The location in the query (if any)
14053
+ idx_t query_location = DConstants::INVALID_INDEX;
14054
+
14055
+ public:
14056
+ //! Convert the object to a string
14057
+ virtual string ToString() const = 0;
14058
+ string BaseToString(string result) const;
14059
+ string BaseToString(string result, const vector<string> &column_name_alias) const;
14060
+ void Print();
14061
+
14062
+ virtual bool Equals(const TableRef *other) const;
14063
+
14064
+ virtual unique_ptr<TableRef> Copy() = 0;
14065
+
14066
+ //! Serializes a TableRef to a stand-alone binary blob
14067
+ DUCKDB_API void Serialize(Serializer &serializer) const;
14068
+ //! Serializes a TableRef to a stand-alone binary blob
14069
+ DUCKDB_API virtual void Serialize(FieldWriter &writer) const = 0;
14070
+ //! Deserializes a blob back into a TableRef
14071
+ DUCKDB_API static unique_ptr<TableRef> Deserialize(Deserializer &source);
14072
+
14073
+ //! Copy the properties of this table ref to the target
14074
+ void CopyProperties(TableRef &target) const;
14075
+ };
14076
+ } // namespace duckdb
14077
+
14078
+
14079
+ namespace duckdb {
14080
+
14081
+ class QueryNode;
14082
+
14083
+ //! SelectStatement is a typical SELECT clause
14084
+ class SelectStatement : public SQLStatement {
14085
+ public:
14086
+ SelectStatement() : SQLStatement(StatementType::SELECT_STATEMENT) {
14087
+ }
14088
+
14089
+ //! The main query node
14090
+ unique_ptr<QueryNode> node;
14091
+
14092
+ protected:
14093
+ SelectStatement(const SelectStatement &other);
14094
+
14095
+ public:
14096
+ //! Convert the SELECT statement to a string
14097
+ string ToString() const override;
14098
+ //! Create a copy of this SelectStatement
14099
+ unique_ptr<SQLStatement> Copy() const override;
14100
+ //! Serializes a SelectStatement to a stand-alone binary blob
14101
+ void Serialize(Serializer &serializer) const;
14102
+ //! Deserializes a blob back into a SelectStatement, returns nullptr if
14103
+ //! deserialization is not possible
14104
+ static unique_ptr<SelectStatement> Deserialize(Deserializer &source);
14105
+ //! Whether or not the statements are equivalent
14106
+ bool Equals(const SQLStatement *other) const;
14107
+ };
14108
+ } // namespace duckdb
14109
+
14110
+
14111
+ namespace duckdb {
14112
+
14113
+ class SelectStatement;
14114
+
14115
+ struct CommonTableExpressionInfo {
14116
+ vector<string> aliases;
14117
+ unique_ptr<SelectStatement> query;
14118
+ };
14119
+
14120
+ } // namespace duckdb
14121
+
14122
+
14123
+ namespace duckdb {
14124
+
14125
+ enum QueryNodeType : uint8_t {
14126
+ SELECT_NODE = 1,
14127
+ SET_OPERATION_NODE = 2,
14128
+ BOUND_SUBQUERY_NODE = 3,
14129
+ RECURSIVE_CTE_NODE = 4
14130
+ };
14131
+
14132
+ struct CommonTableExpressionInfo;
14133
+
14134
+ class CommonTableExpressionMap {
14135
+ public:
14136
+ CommonTableExpressionMap();
14137
+
14138
+ unordered_map<string, unique_ptr<CommonTableExpressionInfo>> map;
14139
+
14140
+ public:
14141
+ string ToString() const;
14142
+ CommonTableExpressionMap Copy() const;
14143
+ };
14144
+
14145
+ class QueryNode {
14146
+ public:
14147
+ explicit QueryNode(QueryNodeType type) : type(type) {
14148
+ }
14149
+ virtual ~QueryNode() {
14150
+ }
14151
+
14152
+ //! The type of the query node, either SetOperation or Select
14153
+ QueryNodeType type;
14154
+ //! The set of result modifiers associated with this query node
14155
+ vector<unique_ptr<ResultModifier>> modifiers;
14156
+ //! CTEs (used by SelectNode and SetOperationNode)
14157
+ CommonTableExpressionMap cte_map;
14158
+
14159
+ virtual const vector<unique_ptr<ParsedExpression>> &GetSelectList() const = 0;
14160
+
14161
+ public:
14162
+ //! Convert the query node to a string
14163
+ virtual string ToString() const = 0;
14164
+
14165
+ virtual bool Equals(const QueryNode *other) const;
14166
+
14167
+ //! Create a copy of this QueryNode
14168
+ virtual unique_ptr<QueryNode> Copy() const = 0;
14169
+ //! Serializes a QueryNode to a stand-alone binary blob
14170
+ DUCKDB_API void Serialize(Serializer &serializer) const;
14171
+ //! Serializes a QueryNode to a stand-alone binary blob
14172
+ DUCKDB_API virtual void Serialize(FieldWriter &writer) const = 0;
14173
+ //! Deserializes a blob back into a QueryNode
14174
+ DUCKDB_API static unique_ptr<QueryNode> Deserialize(Deserializer &source);
14175
+
14176
+ string ResultModifiersToString() const;
14177
+
14178
+ protected:
14179
+ //! Copy base QueryNode properties from another expression to this one,
14180
+ //! used in Copy method
14181
+ void CopyProperties(QueryNode &other) const;
14182
+ };
14183
+
14184
+ } // namespace duckdb
14185
+
14186
+
14187
+
14188
+
14189
+ namespace duckdb {
14190
+ class BoundResultModifier;
14191
+ class BoundSelectNode;
14192
+ class ClientContext;
14193
+ class ExpressionBinder;
14194
+ class LimitModifier;
14195
+ class OrderBinder;
14196
+ class TableCatalogEntry;
14197
+ class ViewCatalogEntry;
14198
+ class TableMacroCatalogEntry;
14199
+
14200
+ struct CreateInfo;
14201
+ struct BoundCreateTableInfo;
14202
+ struct BoundCreateFunctionInfo;
14203
+ struct CommonTableExpressionInfo;
14204
+
14205
+ enum class BindingMode : uint8_t { STANDARD_BINDING, EXTRACT_NAMES };
14206
+
14207
+ struct CorrelatedColumnInfo {
14208
+ ColumnBinding binding;
14209
+ LogicalType type;
14210
+ string name;
14211
+ idx_t depth;
14212
+
14213
+ explicit CorrelatedColumnInfo(BoundColumnRefExpression &expr)
14214
+ : binding(expr.binding), type(expr.return_type), name(expr.GetName()), depth(expr.depth) {
14215
+ }
14216
+
14217
+ bool operator==(const CorrelatedColumnInfo &rhs) const {
14218
+ return binding == rhs.binding;
14219
+ }
14220
+ };
14221
+
14222
+ //! Bind the parsed query tree to the actual columns present in the catalog.
14223
+ /*!
14224
+ The binder is responsible for binding tables and columns to actual physical
14225
+ tables and columns in the catalog. In the process, it also resolves types of
14226
+ all expressions.
14227
+ */
14228
+ class Binder : public std::enable_shared_from_this<Binder> {
14229
+ friend class ExpressionBinder;
14230
+ friend class SelectBinder;
14231
+ friend class RecursiveSubqueryPlanner;
14232
+
14233
+ public:
14234
+ static shared_ptr<Binder> CreateBinder(ClientContext &context, Binder *parent = nullptr, bool inherit_ctes = true);
14235
+
14236
+ //! The client context
14237
+ ClientContext &context;
14238
+ //! A mapping of names to common table expressions
14239
+ case_insensitive_map_t<CommonTableExpressionInfo *> CTE_bindings;
14240
+ //! The CTEs that have already been bound
14241
+ unordered_set<CommonTableExpressionInfo *> bound_ctes;
14242
+ //! The bind context
14243
+ BindContext bind_context;
14244
+ //! The set of correlated columns bound by this binder (FIXME: this should probably be an unordered_set and not a
14245
+ //! vector)
14246
+ vector<CorrelatedColumnInfo> correlated_columns;
14247
+ //! The set of parameter expressions bound by this binder
14248
+ vector<BoundParameterExpression *> *parameters;
14249
+ //! The types of the prepared statement parameters, if any
14250
+ vector<LogicalType> *parameter_types;
14251
+ //! Statement properties
14252
+ StatementProperties properties;
14253
+ //! The alias for the currently processing subquery, if it exists
14254
+ string alias;
14255
+ //! Macro parameter bindings (if any)
14256
+ MacroBinding *macro_binding = nullptr;
14257
+
14258
+ public:
14259
+ BoundStatement Bind(SQLStatement &statement);
14260
+ BoundStatement Bind(QueryNode &node);
14261
+
14262
+ unique_ptr<BoundCreateTableInfo> BindCreateTableInfo(unique_ptr<CreateInfo> info);
14263
+ void BindCreateViewInfo(CreateViewInfo &base);
14264
+ SchemaCatalogEntry *BindSchema(CreateInfo &info);
14265
+ SchemaCatalogEntry *BindCreateFunctionInfo(CreateInfo &info);
14266
+
14267
+ //! Check usage, and cast named parameters to their types
14268
+ static void BindNamedParameters(named_parameter_type_map_t &types, named_parameter_map_t &values,
14269
+ QueryErrorContext &error_context, string &func_name);
14270
+
14271
+ unique_ptr<BoundTableRef> Bind(TableRef &ref);
14272
+ unique_ptr<LogicalOperator> CreatePlan(BoundTableRef &ref);
14273
+
14274
+ //! Generates an unused index for a table
14275
+ idx_t GenerateTableIndex();
14276
+
14277
+ //! Add a common table expression to the binder
14278
+ void AddCTE(const string &name, CommonTableExpressionInfo *cte);
14279
+ //! Find a common table expression by name; returns nullptr if none exists
14280
+ CommonTableExpressionInfo *FindCTE(const string &name, bool skip = false);
14281
+
14282
+ bool CTEIsAlreadyBound(CommonTableExpressionInfo *cte);
14283
+
14284
+ //! Add the view to the set of currently bound views - used for detecting recursive view definitions
14285
+ void AddBoundView(ViewCatalogEntry *view);
14286
+
14287
+ void PushExpressionBinder(ExpressionBinder *binder);
14288
+ void PopExpressionBinder();
14289
+ void SetActiveBinder(ExpressionBinder *binder);
14290
+ ExpressionBinder *GetActiveBinder();
14291
+ bool HasActiveBinder();
14292
+
14293
+ vector<ExpressionBinder *> &GetActiveBinders();
14325
14294
 
14295
+ void MergeCorrelatedColumns(vector<CorrelatedColumnInfo> &other);
14296
+ //! Add a correlated column to this binder (if it does not exist)
14297
+ void AddCorrelatedColumn(const CorrelatedColumnInfo &info);
14326
14298
 
14327
- namespace duckdb {
14328
- class Deserializer;
14329
- class Serializer;
14299
+ string FormatError(ParsedExpression &expr_context, const string &message);
14300
+ string FormatError(TableRef &ref_context, const string &message);
14330
14301
 
14331
- //! Represents a generic expression that returns a table.
14332
- class TableRef {
14333
- public:
14334
- explicit TableRef(TableReferenceType type) : type(type) {
14302
+ string FormatErrorRecursive(idx_t query_location, const string &message, vector<ExceptionFormatValue> &values);
14303
+ template <class T, typename... Args>
14304
+ string FormatErrorRecursive(idx_t query_location, const string &msg, vector<ExceptionFormatValue> &values, T param,
14305
+ Args... params) {
14306
+ values.push_back(ExceptionFormatValue::CreateFormatValue<T>(param));
14307
+ return FormatErrorRecursive(query_location, msg, values, params...);
14335
14308
  }
14336
- virtual ~TableRef() {
14309
+
14310
+ template <typename... Args>
14311
+ string FormatError(idx_t query_location, const string &msg, Args... params) {
14312
+ vector<ExceptionFormatValue> values;
14313
+ return FormatErrorRecursive(query_location, msg, values, params...);
14337
14314
  }
14338
14315
 
14339
- TableReferenceType type;
14340
- string alias;
14341
- //! Sample options (if any)
14342
- unique_ptr<SampleOptions> sample;
14343
- //! The location in the query (if any)
14344
- idx_t query_location = DConstants::INVALID_INDEX;
14316
+ static void BindLogicalType(ClientContext &context, LogicalType &type, const string &schema = "");
14345
14317
 
14346
- public:
14347
- //! Convert the object to a string
14348
- virtual string ToString() const = 0;
14349
- string BaseToString(string result) const;
14350
- string BaseToString(string result, const vector<string> &column_name_alias) const;
14351
- void Print();
14318
+ bool HasMatchingBinding(const string &table_name, const string &column_name, string &error_message);
14319
+ bool HasMatchingBinding(const string &schema_name, const string &table_name, const string &column_name,
14320
+ string &error_message);
14352
14321
 
14353
- virtual bool Equals(const TableRef *other) const;
14322
+ void SetBindingMode(BindingMode mode);
14323
+ BindingMode GetBindingMode();
14324
+ void AddTableName(string table_name);
14325
+ const unordered_set<string> &GetTableNames();
14354
14326
 
14355
- virtual unique_ptr<TableRef> Copy() = 0;
14327
+ private:
14328
+ //! The parent binder (if any)
14329
+ shared_ptr<Binder> parent;
14330
+ //! The vector of active binders
14331
+ vector<ExpressionBinder *> active_binders;
14332
+ //! The count of bound_tables
14333
+ idx_t bound_tables;
14334
+ //! Whether or not the binder has any unplanned subqueries that still need to be planned
14335
+ bool has_unplanned_subqueries = false;
14336
+ //! Whether or not subqueries should be planned already
14337
+ bool plan_subquery = true;
14338
+ //! Whether CTEs should reference the parent binder (if it exists)
14339
+ bool inherit_ctes = true;
14340
+ //! Whether or not the binder can contain NULLs as the root of expressions
14341
+ bool can_contain_nulls = false;
14342
+ //! The root statement of the query that is currently being parsed
14343
+ SQLStatement *root_statement = nullptr;
14344
+ //! Binding mode
14345
+ BindingMode mode = BindingMode::STANDARD_BINDING;
14346
+ //! Table names extracted for BindingMode::EXTRACT_NAMES
14347
+ unordered_set<string> table_names;
14348
+ //! The set of bound views
14349
+ unordered_set<ViewCatalogEntry *> bound_views;
14356
14350
 
14357
- //! Serializes a TableRef to a stand-alone binary blob
14358
- DUCKDB_API void Serialize(Serializer &serializer) const;
14359
- //! Serializes a TableRef to a stand-alone binary blob
14360
- DUCKDB_API virtual void Serialize(FieldWriter &writer) const = 0;
14361
- //! Deserializes a blob back into a TableRef
14362
- DUCKDB_API static unique_ptr<TableRef> Deserialize(Deserializer &source);
14351
+ private:
14352
+ //! Bind the expressions of generated columns to check for errors
14353
+ void BindGeneratedColumns(BoundCreateTableInfo &info);
14354
+ //! Bind the default values of the columns of a table
14355
+ void BindDefaultValues(vector<ColumnDefinition> &columns, vector<unique_ptr<Expression>> &bound_defaults);
14356
+ //! Bind a limit value (LIMIT or OFFSET)
14357
+ unique_ptr<Expression> BindDelimiter(ClientContext &context, OrderBinder &order_binder,
14358
+ unique_ptr<ParsedExpression> delimiter, const LogicalType &type,
14359
+ Value &delimiter_value);
14363
14360
 
14364
- //! Copy the properties of this table ref to the target
14365
- void CopyProperties(TableRef &target) const;
14366
- };
14367
- } // namespace duckdb
14361
+ //! Move correlated expressions from the child binder to this binder
14362
+ void MoveCorrelatedExpressions(Binder &other);
14368
14363
 
14364
+ BoundStatement Bind(SelectStatement &stmt);
14365
+ BoundStatement Bind(InsertStatement &stmt);
14366
+ BoundStatement Bind(CopyStatement &stmt);
14367
+ BoundStatement Bind(DeleteStatement &stmt);
14368
+ BoundStatement Bind(UpdateStatement &stmt);
14369
+ BoundStatement Bind(CreateStatement &stmt);
14370
+ BoundStatement Bind(DropStatement &stmt);
14371
+ BoundStatement Bind(AlterStatement &stmt);
14372
+ BoundStatement Bind(TransactionStatement &stmt);
14373
+ BoundStatement Bind(PragmaStatement &stmt);
14374
+ BoundStatement Bind(ExplainStatement &stmt);
14375
+ BoundStatement Bind(VacuumStatement &stmt);
14376
+ BoundStatement Bind(RelationStatement &stmt);
14377
+ BoundStatement Bind(ShowStatement &stmt);
14378
+ BoundStatement Bind(CallStatement &stmt);
14379
+ BoundStatement Bind(ExportStatement &stmt);
14380
+ BoundStatement Bind(ExtensionStatement &stmt);
14381
+ BoundStatement Bind(SetStatement &stmt);
14382
+ BoundStatement Bind(LoadStatement &stmt);
14383
+ BoundStatement BindReturning(vector<unique_ptr<ParsedExpression>> returning_list, TableCatalogEntry *table,
14384
+ idx_t update_table_index, unique_ptr<LogicalOperator> child_operator,
14385
+ BoundStatement result);
14369
14386
 
14370
- namespace duckdb {
14387
+ unique_ptr<QueryNode> BindTableMacro(FunctionExpression &function, TableMacroCatalogEntry *macro_func, idx_t depth);
14371
14388
 
14372
- class QueryNode;
14389
+ unique_ptr<BoundQueryNode> BindNode(SelectNode &node);
14390
+ unique_ptr<BoundQueryNode> BindNode(SetOperationNode &node);
14391
+ unique_ptr<BoundQueryNode> BindNode(RecursiveCTENode &node);
14392
+ unique_ptr<BoundQueryNode> BindNode(QueryNode &node);
14373
14393
 
14374
- //! SelectStatement is a typical SELECT clause
14375
- class SelectStatement : public SQLStatement {
14376
- public:
14377
- SelectStatement() : SQLStatement(StatementType::SELECT_STATEMENT) {
14378
- }
14394
+ unique_ptr<LogicalOperator> VisitQueryNode(BoundQueryNode &node, unique_ptr<LogicalOperator> root);
14395
+ unique_ptr<LogicalOperator> CreatePlan(BoundRecursiveCTENode &node);
14396
+ unique_ptr<LogicalOperator> CreatePlan(BoundSelectNode &statement);
14397
+ unique_ptr<LogicalOperator> CreatePlan(BoundSetOperationNode &node);
14398
+ unique_ptr<LogicalOperator> CreatePlan(BoundQueryNode &node);
14379
14399
 
14380
- //! The main query node
14381
- unique_ptr<QueryNode> node;
14400
+ unique_ptr<BoundTableRef> Bind(BaseTableRef &ref);
14401
+ unique_ptr<BoundTableRef> Bind(CrossProductRef &ref);
14402
+ unique_ptr<BoundTableRef> Bind(JoinRef &ref);
14403
+ unique_ptr<BoundTableRef> Bind(SubqueryRef &ref, CommonTableExpressionInfo *cte = nullptr);
14404
+ unique_ptr<BoundTableRef> Bind(TableFunctionRef &ref);
14405
+ unique_ptr<BoundTableRef> Bind(EmptyTableRef &ref);
14406
+ unique_ptr<BoundTableRef> Bind(ExpressionListRef &ref);
14382
14407
 
14383
- protected:
14384
- SelectStatement(const SelectStatement &other);
14408
+ bool BindTableFunctionParameters(TableFunctionCatalogEntry &table_function,
14409
+ vector<unique_ptr<ParsedExpression>> &expressions, vector<LogicalType> &arguments,
14410
+ vector<Value> &parameters, named_parameter_map_t &named_parameters,
14411
+ unique_ptr<BoundSubqueryRef> &subquery, string &error);
14412
+ bool BindTableInTableOutFunction(vector<unique_ptr<ParsedExpression>> &expressions,
14413
+ unique_ptr<BoundSubqueryRef> &subquery, string &error);
14414
+ unique_ptr<LogicalOperator> BindTableFunction(TableFunction &function, vector<Value> parameters);
14415
+ unique_ptr<LogicalOperator>
14416
+ BindTableFunctionInternal(TableFunction &table_function, const string &function_name, vector<Value> parameters,
14417
+ named_parameter_map_t named_parameters, vector<LogicalType> input_table_types,
14418
+ vector<string> input_table_names, const vector<string> &column_name_alias,
14419
+ unique_ptr<ExternalDependency> external_dependency);
14385
14420
 
14386
- public:
14387
- //! Convert the SELECT statement to a string
14388
- string ToString() const override;
14389
- //! Create a copy of this SelectStatement
14390
- unique_ptr<SQLStatement> Copy() const override;
14391
- //! Serializes a SelectStatement to a stand-alone binary blob
14392
- void Serialize(Serializer &serializer) const;
14393
- //! Deserializes a blob back into a SelectStatement, returns nullptr if
14394
- //! deserialization is not possible
14395
- static unique_ptr<SelectStatement> Deserialize(Deserializer &source);
14396
- //! Whether or not the statements are equivalent
14397
- bool Equals(const SQLStatement *other) const;
14398
- };
14399
- } // namespace duckdb
14421
+ unique_ptr<LogicalOperator> CreatePlan(BoundBaseTableRef &ref);
14422
+ unique_ptr<LogicalOperator> CreatePlan(BoundCrossProductRef &ref);
14423
+ unique_ptr<LogicalOperator> CreatePlan(BoundJoinRef &ref);
14424
+ unique_ptr<LogicalOperator> CreatePlan(BoundSubqueryRef &ref);
14425
+ unique_ptr<LogicalOperator> CreatePlan(BoundTableFunction &ref);
14426
+ unique_ptr<LogicalOperator> CreatePlan(BoundEmptyTableRef &ref);
14427
+ unique_ptr<LogicalOperator> CreatePlan(BoundExpressionListRef &ref);
14428
+ unique_ptr<LogicalOperator> CreatePlan(BoundCTERef &ref);
14400
14429
 
14430
+ BoundStatement BindCopyTo(CopyStatement &stmt);
14431
+ BoundStatement BindCopyFrom(CopyStatement &stmt);
14401
14432
 
14402
- namespace duckdb {
14433
+ void BindModifiers(OrderBinder &order_binder, QueryNode &statement, BoundQueryNode &result);
14434
+ void BindModifierTypes(BoundQueryNode &result, const vector<LogicalType> &sql_types, idx_t projection_index);
14403
14435
 
14404
- class SelectStatement;
14436
+ BoundStatement BindSummarize(ShowStatement &stmt);
14437
+ unique_ptr<BoundResultModifier> BindLimit(OrderBinder &order_binder, LimitModifier &limit_mod);
14438
+ unique_ptr<BoundResultModifier> BindLimitPercent(OrderBinder &order_binder, LimitPercentModifier &limit_mod);
14439
+ unique_ptr<Expression> BindOrderExpression(OrderBinder &order_binder, unique_ptr<ParsedExpression> expr);
14405
14440
 
14406
- struct CommonTableExpressionInfo {
14407
- vector<string> aliases;
14408
- unique_ptr<SelectStatement> query;
14409
- };
14441
+ unique_ptr<LogicalOperator> PlanFilter(unique_ptr<Expression> condition, unique_ptr<LogicalOperator> root);
14410
14442
 
14411
- } // namespace duckdb
14443
+ void PlanSubqueries(unique_ptr<Expression> *expr, unique_ptr<LogicalOperator> *root);
14444
+ unique_ptr<Expression> PlanSubquery(BoundSubqueryExpression &expr, unique_ptr<LogicalOperator> &root);
14412
14445
 
14446
+ unique_ptr<LogicalOperator> CastLogicalOperatorToTypes(vector<LogicalType> &source_types,
14447
+ vector<LogicalType> &target_types,
14448
+ unique_ptr<LogicalOperator> op);
14413
14449
 
14414
- namespace duckdb {
14450
+ string FindBinding(const string &using_column, const string &join_side);
14451
+ bool TryFindBinding(const string &using_column, const string &join_side, string &result);
14415
14452
 
14416
- enum QueryNodeType : uint8_t {
14417
- SELECT_NODE = 1,
14418
- SET_OPERATION_NODE = 2,
14419
- BOUND_SUBQUERY_NODE = 3,
14420
- RECURSIVE_CTE_NODE = 4
14421
- };
14453
+ void AddUsingBindingSet(unique_ptr<UsingColumnSet> set);
14454
+ string RetrieveUsingBinding(Binder &current_binder, UsingColumnSet *current_set, const string &column_name,
14455
+ const string &join_side, UsingColumnSet *new_set);
14422
14456
 
14423
- class QueryNode {
14424
- public:
14425
- explicit QueryNode(QueryNodeType type) : type(type) {
14426
- }
14427
- virtual ~QueryNode() {
14428
- }
14457
+ void AddCTEMap(CommonTableExpressionMap &cte_map);
14429
14458
 
14430
- //! The type of the query node, either SetOperation or Select
14431
- QueryNodeType type;
14432
- //! The set of result modifiers associated with this query node
14433
- vector<unique_ptr<ResultModifier>> modifiers;
14434
- //! CTEs (used by SelectNode and SetOperationNode)
14435
- unordered_map<string, unique_ptr<CommonTableExpressionInfo>> cte_map;
14459
+ public:
14460
+ // This should really be a private constructor, but make_shared does not allow it...
14461
+ // If you are thinking about calling this, you should probably call Binder::CreateBinder
14462
+ Binder(bool I_know_what_I_am_doing, ClientContext &context, shared_ptr<Binder> parent, bool inherit_ctes);
14463
+ };
14436
14464
 
14437
- virtual const vector<unique_ptr<ParsedExpression>> &GetSelectList() const = 0;
14465
+ } // namespace duckdb
14438
14466
 
14439
- public:
14440
- //! Convert the query node to a string
14441
- virtual string ToString() const = 0;
14467
+ //===----------------------------------------------------------------------===//
14468
+ // DuckDB
14469
+ //
14470
+ // duckdb/planner/bound_query_node.hpp
14471
+ //
14472
+ //
14473
+ //===----------------------------------------------------------------------===//
14442
14474
 
14443
- virtual bool Equals(const QueryNode *other) const;
14444
14475
 
14445
- //! Create a copy of this QueryNode
14446
- virtual unique_ptr<QueryNode> Copy() const = 0;
14447
- //! Serializes a QueryNode to a stand-alone binary blob
14448
- DUCKDB_API void Serialize(Serializer &serializer) const;
14449
- //! Serializes a QueryNode to a stand-alone binary blob
14450
- DUCKDB_API virtual void Serialize(FieldWriter &writer) const = 0;
14451
- //! Deserializes a blob back into a QueryNode
14452
- DUCKDB_API static unique_ptr<QueryNode> Deserialize(Deserializer &source);
14453
14476
 
14454
- string CTEToString() const;
14455
- string ResultModifiersToString() const;
14456
14477
 
14457
- protected:
14458
- //! Copy base QueryNode properties from another expression to this one,
14459
- //! used in Copy method
14460
- void CopyProperties(QueryNode &other) const;
14461
- };
14462
14478
 
14463
- } // namespace duckdb
14464
14479
 
14465
14480
 
14466
14481
  namespace duckdb {