Skip to content

Commit ef93bce

Browse files
committed
Avoid serializing query to SQL string unless it is necessary
1 parent 0b50d4b commit ef93bce

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

datafusion/sql/src/statement.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use arrow_schema::{DataType, Fields};
3333
use datafusion_common::error::_plan_err;
3434
use datafusion_common::parsers::CompressionTypeVariant;
3535
use datafusion_common::{
36-
exec_err, not_impl_err, plan_datafusion_err, plan_err, schema_err,
36+
exec_err, internal_err, not_impl_err, plan_datafusion_err, plan_err, schema_err,
3737
unqualified_field_not_found, Column, Constraint, Constraints, DFSchema, DFSchemaRef,
3838
DataFusionError, Result, ScalarValue, SchemaError, SchemaReference, TableReference,
3939
ToDFSchema,
@@ -204,7 +204,6 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
204204
statement: Statement,
205205
planner_context: &mut PlannerContext,
206206
) -> Result<LogicalPlan> {
207-
let sql = Some(statement.to_string());
208207
match statement {
209208
Statement::ExplainTable {
210209
describe_alias: DescribeAlias::Describe, // only parse 'DESCRIBE table_name' and not 'EXPLAIN table_name'
@@ -518,6 +517,35 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
518517
return not_impl_err!("To not supported")?;
519518
}
520519

520+
// put the statement back together temporarily to get the SQL
521+
// string representation
522+
let stmt = Statement::CreateView {
523+
or_replace,
524+
materialized,
525+
name,
526+
columns,
527+
query,
528+
options: CreateTableOptions::None,
529+
cluster_by,
530+
comment,
531+
with_no_schema_binding,
532+
if_not_exists,
533+
temporary,
534+
to,
535+
};
536+
let sql = stmt.to_string();
537+
let Statement::CreateView {
538+
name,
539+
columns,
540+
query,
541+
or_replace,
542+
temporary,
543+
..
544+
} = stmt
545+
else {
546+
return internal_err!("Unreachable code in create view");
547+
};
548+
521549
let columns = columns
522550
.into_iter()
523551
.map(|view_column_def| {
@@ -538,7 +566,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
538566
name: self.object_name_to_table_reference(name)?,
539567
input: Arc::new(plan),
540568
or_replace,
541-
definition: sql,
569+
definition: Some(sql),
542570
temporary,
543571
})))
544572
}
@@ -1123,8 +1151,8 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
11231151
},
11241152
)))
11251153
}
1126-
_ => {
1127-
not_impl_err!("Unsupported SQL statement: {sql:?}")
1154+
stmt => {
1155+
not_impl_err!("Unsupported SQL statement: {stmt}")
11281156
}
11291157
}
11301158
}

0 commit comments

Comments
 (0)