Skip to content

Commit df562cc

Browse files
Fix schema for projection with no columns
1 parent 2d3b586 commit df562cc

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

  • core/src/sql/sql_provider_datafusion

core/src/sql/sql_provider_datafusion/mod.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ use datafusion::{
1717
};
1818
use futures::TryStreamExt;
1919
use snafu::prelude::*;
20-
use std::fmt::{Display, Formatter};
2120
use std::{any::Any, fmt, sync::Arc};
21+
use std::{
22+
fmt::{Display, Formatter},
23+
sync::LazyLock,
24+
};
2225

2326
use datafusion::{
24-
arrow::datatypes::SchemaRef,
27+
arrow::datatypes::{DataType, Field, Schema, SchemaRef},
2528
datasource::TableProvider,
2629
error::{DataFusionError, Result as DataFusionResult},
2730
execution::TaskContext,
@@ -238,14 +241,20 @@ impl<T, P> Display for SqlTable<T, P> {
238241
}
239242
}
240243

244+
static ONE_COLUMN_SCHEMA: LazyLock<SchemaRef> =
245+
LazyLock::new(|| Arc::new(Schema::new(vec![Field::new("1", DataType::Int64, false)])));
246+
241247
pub fn project_schema_safe(
242248
schema: &SchemaRef,
243249
projection: Option<&Vec<usize>>,
244250
) -> DataFusionResult<SchemaRef> {
245251
let schema = match projection {
246252
Some(columns) => {
247253
if columns.is_empty() {
248-
Arc::clone(schema)
254+
// If the projection is Some([]) then it gets unparsed as `SELECT 1`, so return a schema with a single Int64 column.
255+
//
256+
// See: <https://github.com/apache/datafusion/blob/83ce79c39412a4f150167d00e40ea05948c4870f/datafusion/sql/src/unparser/plan.rs#L998>
257+
Arc::clone(&ONE_COLUMN_SCHEMA)
249258
} else {
250259
Arc::new(schema.project(columns)?)
251260
}

0 commit comments

Comments
 (0)