1#include "duckdb/planner/operator/logical_update.hpp"
2#include "duckdb/common/field_writer.hpp"
3#include "duckdb/catalog/catalog_entry/table_catalog_entry.hpp"
4#include "duckdb/parser/parsed_data/create_table_info.hpp"
5#include "duckdb/main/config.hpp"
6
7namespace duckdb {
8
9LogicalUpdate::LogicalUpdate(TableCatalogEntry &table)
10 : LogicalOperator(LogicalOperatorType::LOGICAL_UPDATE), table(table), table_index(0), return_chunk(false) {
11}
12
13void LogicalUpdate::Serialize(FieldWriter &writer) const {
14 table.Serialize(serializer&: writer.GetSerializer());
15 writer.WriteField(element: table_index);
16 writer.WriteField(element: return_chunk);
17 writer.WriteIndexList<PhysicalIndex>(elements: columns);
18 writer.WriteSerializableList(elements: bound_defaults);
19 writer.WriteField(element: update_is_del_and_insert);
20 writer.WriteSerializableList(elements: this->expressions);
21}
22
23unique_ptr<LogicalOperator> LogicalUpdate::Deserialize(LogicalDeserializationState &state, FieldReader &reader) {
24 auto &context = state.gstate.context;
25 auto info = TableCatalogEntry::Deserialize(source&: reader.GetSource(), context);
26 auto &catalog = Catalog::GetCatalog(context, catalog_name: info->catalog);
27
28 auto &table_catalog_entry = catalog.GetEntry<TableCatalogEntry>(context, schema_name: info->schema, name: info->table);
29 auto result = make_uniq<LogicalUpdate>(args&: table_catalog_entry);
30 result->table_index = reader.ReadRequired<idx_t>();
31 result->return_chunk = reader.ReadRequired<bool>();
32 result->columns = reader.ReadRequiredIndexList<PhysicalIndex>();
33 result->bound_defaults = reader.ReadRequiredSerializableList<Expression>(args&: state.gstate);
34 result->update_is_del_and_insert = reader.ReadRequired<bool>();
35 result->expressions = reader.ReadRequiredSerializableList<duckdb::Expression>(args&: state.gstate);
36 return std::move(result);
37}
38
39idx_t LogicalUpdate::EstimateCardinality(ClientContext &context) {
40 return return_chunk ? LogicalOperator::EstimateCardinality(context) : 1;
41}
42
43vector<ColumnBinding> LogicalUpdate::GetColumnBindings() {
44 if (return_chunk) {
45 return GenerateColumnBindings(table_idx: table_index, column_count: table.GetTypes().size());
46 }
47 return {ColumnBinding(0, 0)};
48}
49
50void LogicalUpdate::ResolveTypes() {
51 if (return_chunk) {
52 types = table.GetTypes();
53 } else {
54 types.emplace_back(args: LogicalType::BIGINT);
55 }
56}
57
58string LogicalUpdate::GetName() const {
59#ifdef DEBUG
60 if (DBConfigOptions::debug_print_bindings) {
61 return LogicalOperator::GetName() + StringUtil::Format(" #%llu", table_index);
62 }
63#endif
64 return LogicalOperator::GetName();
65}
66
67} // namespace duckdb
68