Skip to content

Commit afc09f5

Browse files
authored
Check create table input logical plan effectively empty (#489)
1 parent ca292fa commit afc09f5

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

crates/runtime/src/execution/query.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use super::catalogs::{catalog::DFCatalog, metastore::DFMetastore};
6060
use super::datafusion::context_provider::ExtendedSqlToRel;
6161
use super::error::{self as ex_error, ExecutionError, ExecutionResult};
6262
use super::session::UserSession;
63-
use super::utils::NormalizedIdent;
63+
use super::utils::{is_logical_plan_effectively_empty, NormalizedIdent};
6464
use crate::execution::datafusion::visitors::{functions_rewriter, json_element};
6565
use tracing_attributes::instrument;
6666

@@ -463,7 +463,7 @@ impl UserQuery {
463463
..
464464
})) = plan
465465
{
466-
if matches!(*input, LogicalPlan::EmptyRelation(_)) {
466+
if is_logical_plan_effectively_empty(&input) {
467467
return created_entity_response();
468468
}
469469
let insert_plan = LogicalPlan::Dml(DmlStatement::new(
@@ -814,7 +814,7 @@ impl UserQuery {
814814
}
815815
}
816816
let select_query =
817-
format!("SELECT {values} FROM {source_query} JOIN {target_table} {target_alias} ON {on}{where_clause_str}");
817+
format!("SELECT {values} FROM {source_query} LEFT JOIN {target_table} {target_alias} ON {on}{where_clause_str}");
818818

819819
// Construct the INSERT statement
820820
let insert_query = format!("INSERT INTO {target_table} ({columns}) {select_query}");

crates/runtime/src/execution/utils.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ use chrono::DateTime;
2828
use datafusion::arrow::array::ArrayRef;
2929
use datafusion::arrow::datatypes::DataType;
3030
use datafusion::common::Result as DataFusionResult;
31+
use datafusion_common::ScalarValue;
32+
use datafusion_expr::{Expr, LogicalPlan};
3133
use embucket_metastore::SchemaIdent as MetastoreSchemaIdent;
3234
use embucket_metastore::TableIdent as MetastoreTableIdent;
3335
use sqlparser::ast::{Ident, ObjectName};
@@ -55,6 +57,23 @@ pub enum DataSerializationFormat {
5557
Json,
5658
}
5759

60+
#[must_use]
61+
pub fn is_logical_plan_effectively_empty(plan: &LogicalPlan) -> bool {
62+
match plan {
63+
LogicalPlan::EmptyRelation(e) => !e.produce_one_row,
64+
LogicalPlan::Projection(proj) => is_logical_plan_effectively_empty(&proj.input),
65+
LogicalPlan::SubqueryAlias(alias) => is_logical_plan_effectively_empty(&alias.input),
66+
LogicalPlan::Filter(filter) => {
67+
let is_false_predicate = matches!(
68+
filter.predicate,
69+
Expr::Literal(ScalarValue::Boolean(Some(false)))
70+
);
71+
is_false_predicate || is_logical_plan_effectively_empty(&filter.input)
72+
}
73+
_ => false,
74+
}
75+
}
76+
5877
/*#[async_trait::async_trait]
5978
pub trait S3ClientValidation: Send + Sync {
6079
async fn get_aws_bucket_acl(

0 commit comments

Comments
 (0)