1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/planner/operator/logical_insert.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/planner/logical_operator.hpp"
12#include "duckdb/common/index_vector.hpp"
13#include "duckdb/parser/statement/insert_statement.hpp"
14
15namespace duckdb {
16class TableCatalogEntry;
17
18class Index;
19
20//! LogicalInsert represents an insertion of data into a base table
21class LogicalInsert : public LogicalOperator {
22public:
23 static constexpr const LogicalOperatorType TYPE = LogicalOperatorType::LOGICAL_INSERT;
24
25public:
26 LogicalInsert(TableCatalogEntry &table, idx_t table_index);
27
28 vector<vector<unique_ptr<Expression>>> insert_values;
29 //! The insertion map ([table_index -> index in result, or DConstants::INVALID_INDEX if not specified])
30 physical_index_vector_t<idx_t> column_index_map;
31 //! The expected types for the INSERT statement (obtained from the column types)
32 vector<LogicalType> expected_types;
33 //! The base table to insert into
34 TableCatalogEntry &table;
35 idx_t table_index;
36 //! if returning option is used, return actual chunk to projection
37 bool return_chunk;
38 //! The default statements used by the table
39 vector<unique_ptr<Expression>> bound_defaults;
40
41 //! Which action to take on conflict
42 OnConflictAction action_type;
43 // The types that the DO UPDATE .. SET (expressions) are cast to
44 vector<LogicalType> expected_set_types;
45 // The (distinct) column ids to apply the ON CONFLICT on
46 unordered_set<column_t> on_conflict_filter;
47 // The WHERE clause of the conflict_target (ON CONFLICT .. WHERE <condition>)
48 unique_ptr<Expression> on_conflict_condition;
49 // The WHERE clause of the DO UPDATE clause
50 unique_ptr<Expression> do_update_condition;
51 // The columns targeted by the DO UPDATE SET expressions
52 vector<PhysicalIndex> set_columns;
53 // The types of the columns targeted by the DO UPDATE SET expressions
54 vector<LogicalType> set_types;
55 // The table_index referring to the column references qualified with 'excluded'
56 idx_t excluded_table_index;
57 // The columns to fetch from the 'destination' table
58 vector<column_t> columns_to_fetch;
59 // The columns to fetch from the 'source' table
60 vector<column_t> source_columns;
61
62public:
63 void Serialize(FieldWriter &writer) const override;
64 static unique_ptr<LogicalOperator> Deserialize(LogicalDeserializationState &state, FieldReader &reader);
65
66protected:
67 vector<ColumnBinding> GetColumnBindings() override;
68 void ResolveTypes() override;
69
70 idx_t EstimateCardinality(ClientContext &context) override;
71 vector<idx_t> GetTableIndex() const override;
72 string GetName() const override;
73};
74} // namespace duckdb
75