JingsongLi commented on code in PR #1448: URL: https://github.com/apache/paimon-rust/pull/1448#discussion_r4656451917
########## crates/integrations/datafusion/src/sql_context/ctas.rs: ########## @@ -0,0 +1,286 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Persistent CREATE TABLE AS SELECT. Population failures retain the table +//! because concurrent writers can use it as soon as creation succeeds. + +use std::ops::ControlFlow; +use std::sync::Arc; + +use datafusion::arrow::datatypes::SchemaRef; +use datafusion::error::{DataFusionError, Result as DFResult}; +use datafusion::execution::context::SQLOptions; +use datafusion::execution::session_state::SessionState; +use datafusion::execution::TaskContext; +use datafusion::physical_plan::{execute_stream, ExecutionPlan}; +use datafusion::prelude::DataFrame; +use datafusion::sql::planner::object_name_to_table_reference; +use datafusion::sql::sqlparser::ast::helpers::stmt_create_table::CreateTableBuilder; +use datafusion::sql::sqlparser::ast::{ + CreateTable, CreateTableOptions, Query, Select, SqlOption, Statement, Visit, Visitor, +}; +use futures::StreamExt; +use paimon::spec::{CoreOptions, DataType as PaimonDataType, TableType}; +use paimon::table::Table; + +use super::{create_table_schema, extract_options, ok_result, SQLContext, TimeTravelSource}; +use crate::error::to_datafusion_error; +use crate::physical_plan::cast_batch_to_schema; + +impl SQLContext { + pub(super) async fn handle_create_table_as( + &self, + ct: &CreateTable, + query: Box<Query>, + partition_keys: Vec<String>, + state: SessionState, + options: SQLOptions, + time_travel_sources: &[TimeTravelSource], + ) -> DFResult<DataFrame> { + validate_create_table_as(ct, &query)?; + Self::ensure_main_branch_base_table(&ct.name, "CREATE TABLE AS SELECT")?; + let enable_ident_normalization = + state.config_options().sql_parser.enable_ident_normalization; + let target = object_name_to_table_reference(ct.name.clone(), enable_ident_normalization)?; + let (catalog, catalog_name, identifier) = + self.resolve_table_reference(&target, &state.config_options().catalog)?; + + // An external target can exist even when its table engine is unavailable. + match catalog.load_table(&identifier).await { Review Comment: **[P2] Check target metadata without opening its data backend** An existing Object Table may have readable catalog metadata while its object store is unavailable to this client. `load_table` initializes FileIO for the object path, so it can fail before CTAS honors `IF NOT EXISTS`. I registered an `oss://` Object Table in a local filesystem catalog, then used a client without `fs.oss.endpoint`. Ordinary `CREATE TABLE IF NOT EXISTS objects_probe (id INT)` succeeds. `CREATE TABLE IF NOT EXISTS objects_probe AS SELECT * FROM missing_source` returns `Missing required OSS config` instead of skipping the existing target. Please use a metadata-only existence lookup here and keep propagating actual catalog lookup errors. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
