@@ -153,41 +153,41 @@ InsertionOrderPreservingMap<string> PostgresInsert::ParamsToString() const {
153153// ===--------------------------------------------------------------------===//
154154// Plan
155155// ===--------------------------------------------------------------------===//
156- unique_ptr< PhysicalOperator> AddCastToPostgresTypes (ClientContext &context, unique_ptr< PhysicalOperator> plan) {
156+ PhysicalOperator & AddCastToPostgresTypes (ClientContext &context, PhysicalPlanGenerator &planner, PhysicalOperator & plan) {
157157 // check if we need to cast anything
158158 bool require_cast = false ;
159- auto &child_types = plan-> GetTypes ();
159+ auto &child_types = plan. GetTypes ();
160160 for (auto &type : child_types) {
161161 auto postgres_type = PostgresUtils::ToPostgresType (type);
162162 if (postgres_type != type) {
163163 require_cast = true ;
164164 break ;
165165 }
166166 }
167- if (require_cast) {
168- vector<LogicalType> postgres_types ;
169- vector<unique_ptr<Expression>> select_list;
170- for ( idx_t i = 0 ; i < child_types. size (); i++) {
171- auto &type = child_types[i] ;
172- unique_ptr<Expression> expr ;
173- expr = make_uniq<BoundReferenceExpression>(type, i);
174-
175- auto postgres_type = PostgresUtils::ToPostgresType (type) ;
176- if (postgres_type != type) {
177- // add a cast
178- expr = BoundCastExpression::AddCastToType (context, std::move (expr), postgres_type );
179- }
180- postgres_types. push_back ( std::move (postgres_type));
181- select_list. push_back ( std::move (expr));
182- }
183- // we need to cast: add casts
184- auto proj = make_uniq<PhysicalProjection> (std::move (postgres_types), std::move (select_list),
185- plan-> estimated_cardinality );
186- proj-> children . push_back ( std::move (plan));
187- plan = std::move (proj);
188- }
189-
190- return plan ;
167+ if (! require_cast) {
168+ return plan ;
169+ }
170+
171+ vector<LogicalType> postgres_types ;
172+ vector< unique_ptr<Expression>> select_list ;
173+ for ( idx_t i = 0 ; i < child_types. size (); i++) {
174+ auto &type = child_types[i];
175+ unique_ptr<Expression> expr ;
176+ expr = make_uniq<BoundReferenceExpression>( type, i);
177+
178+ auto postgres_type = PostgresUtils::ToPostgresType (type );
179+ if (postgres_type != type) {
180+ // add a cast
181+ expr = BoundCastExpression::AddCastToType (context, std::move (expr), postgres_type );
182+ }
183+ postgres_types. push_back ( std::move (postgres_type));
184+ select_list. push_back (std::move (expr));
185+ }
186+
187+ // we need to cast: add casts
188+ auto &proj = planner. Make <PhysicalProjection>( std::move (postgres_types), std::move (select_list), plan. estimated_cardinality );
189+ proj. children . push_back (plan);
190+ return proj ;
191191}
192192
193193bool PostgresCatalog::IsPostgresScan (const string &name) {
@@ -206,36 +206,34 @@ void PostgresCatalog::MaterializePostgresScans(PhysicalOperator &op) {
206206 }
207207 }
208208 for (auto &child : op.children ) {
209- MaterializePostgresScans (* child);
209+ MaterializePostgresScans (child);
210210 }
211211}
212212
213- unique_ptr<PhysicalOperator> PostgresCatalog::PlanInsert (ClientContext &context, LogicalInsert &op,
214- unique_ptr<PhysicalOperator> plan) {
213+ PhysicalOperator &PostgresCatalog::PlanInsert (ClientContext &context, PhysicalPlanGenerator &planner, LogicalInsert &op, optional_ptr<PhysicalOperator> plan) {
215214 if (op.return_chunk ) {
216215 throw BinderException (" RETURNING clause not yet supported for insertion into Postgres table" );
217216 }
218217 if (op.action_type != OnConflictAction::THROW) {
219218 throw BinderException (" ON CONFLICT clause not yet supported for insertion into Postgres table" );
220219 }
221- MaterializePostgresScans (*plan);
222220
223- plan = AddCastToPostgresTypes (context, std::move (plan));
221+ D_ASSERT (plan);
222+ MaterializePostgresScans (*plan);
223+ auto &inner_plan = AddCastToPostgresTypes (context, planner, *plan);
224224
225- auto insert = make_uniq <PostgresInsert>(op, op.table , op.column_index_map );
226- insert-> children .push_back (std::move (plan) );
227- return std::move ( insert) ;
225+ auto & insert = planner. Make <PostgresInsert>(op, op.table , op.column_index_map );
226+ insert. children .push_back (inner_plan );
227+ return insert;
228228}
229229
230- unique_ptr<PhysicalOperator> PostgresCatalog::PlanCreateTableAs (ClientContext &context, LogicalCreateTable &op,
231- unique_ptr<PhysicalOperator> plan) {
232- plan = AddCastToPostgresTypes (context, std::move (plan));
233-
234- MaterializePostgresScans (*plan);
230+ PhysicalOperator &PostgresCatalog::PlanCreateTableAs (ClientContext &context, PhysicalPlanGenerator &planner, LogicalCreateTable &op, PhysicalOperator &plan) {
231+ auto &inner_plan = AddCastToPostgresTypes (context, planner, plan);
232+ MaterializePostgresScans (inner_plan);
235233
236- auto insert = make_uniq <PostgresInsert>(op, op.schema , std::move (op.info ));
237- insert-> children .push_back (std::move (plan) );
238- return std::move ( insert) ;
234+ auto & insert = planner. Make <PostgresInsert>(op, op.schema , std::move (op.info ));
235+ insert. children .push_back (inner_plan );
236+ return insert;
239237}
240238
241239} // namespace duckdb
0 commit comments