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