Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions datafusion/core/src/physical_plan/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ use super::{
RecordBatchStream, SendableRecordBatchStream, Statistics,
};
use crate::error::Result;
use arrow::datatypes::SchemaRef;
use arrow::datatypes::{Schema, SchemaRef};
use arrow::error::Result as ArrowResult;
use arrow::record_batch::RecordBatch;
use arrow::record_batch::{RecordBatch, RecordBatchOptions};

use crate::execution::context::TaskContext;
use datafusion_common::DataFusionError;
Expand Down Expand Up @@ -195,6 +195,15 @@ impl Stream for MemoryStream {

// return just the columns requested
let batch = match self.projection.as_ref() {
Some(columns) if columns.is_empty() => {
let mut options = RecordBatchOptions::default();
options.row_count = Some(batch.num_rows());
RecordBatch::try_new_with_options(
Arc::new(Schema::empty()),
vec![],
&options,
)?
}
Some(columns) => batch.project(columns)?,
None => batch.clone(),
};
Expand Down
2 changes: 1 addition & 1 deletion datafusion/core/tests/sql/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async fn json_explain() {
"logical_plan",
"Projection: #COUNT(UInt8(1))\
\n Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1))]]\
\n TableScan: t1 projection=[a]",
\n TableScan: t1 projection=[]",
],
vec![
"physical_plan",
Expand Down
23 changes: 8 additions & 15 deletions datafusion/optimizer/src/projection_push_down.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,14 @@ fn get_projected_schema(
.filter_map(ArrowResult::ok)
.collect();

if projection.is_empty() {
if has_projection && !schema.fields().is_empty() {
// Ensure that we are reading at least one column from the table in case the query
// does not reference any columns directly such as "SELECT COUNT(1) FROM table",
// except when the table is empty (no column)
projection.insert(0);
} else {
// for table scan without projection, we default to return all columns
projection = schema
.fields()
.iter()
.enumerate()
.map(|(i, _)| i)
.collect::<BTreeSet<usize>>();
}
if projection.is_empty() && !has_projection {
// for table scan without projection, we default to return all columns
projection = schema
.fields()
.iter()
.enumerate()
.map(|(i, _)| i)
.collect::<BTreeSet<usize>>();
}

// create the projected schema
Expand Down