Skip to content

Apache DataFusion SQL Query Engine

fix: support Substrait VirtualTables with no columns (#12339)

Arttuf2bbf25cfbfc
diff --git a/datafusion/physical-plan/src/values.rs b/datafusion/physical-plan/src/values.rs
index 3ea27d62d8..e01aea1fdd 100644
--- a/datafusion/physical-plan/src/values.rs
+++ b/datafusion/physical-plan/src/values.rs
@@ -88,7 +88,11 @@ impl ValuesExec {
.and_then(ScalarValue::iter_to_array)
})
.collect::<Result<Vec<_>>>()?;
- let batch = RecordBatch::try_new(Arc::clone(&schema), arr)?;
+ let batch = RecordBatch::try_new_with_options(
+ Arc::clone(&schema),
+ arr,
+ &RecordBatchOptions::new().with_row_count(Some(n_row)),
+ )?;
let data: Vec<RecordBatch> = vec![batch];
Self::try_new_from_batches(schema, data)
}
diff --git a/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs b/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs
index 98daac65e1..cc353ab36d 100644
--- a/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs
+++ b/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs
@@ -22,9 +22,6 @@ use datafusion_substrait::logical_plan::{
consumer::from_substrait_plan, producer::to_substrait_plan,
};
-use std::hash::Hash;
-use std::sync::Arc;
-
use datafusion::arrow::datatypes::{DataType, Field, IntervalUnit, Schema, TimeUnit};
use datafusion::common::{not_impl_err, plan_err, DFSchema, DFSchemaRef};
use datafusion::error::Result;
@@ -32,10 +29,12 @@ use datafusion::execution::registry::SerializerRegistry;
use datafusion::execution::runtime_env::RuntimeEnv;
use datafusion::logical_expr::{
Extension, LogicalPlan, PartitionEvaluator, Repartition, UserDefinedLogicalNode,
- Volatility,
+ Values, Volatility,
};
use datafusion::optimizer::simplify_expressions::expr_simplifier::THRESHOLD_INLINE_INLIST;
use datafusion::prelude::*;
+use std::hash::Hash;
+use std::sync::Arc;
use datafusion::execution::session_state::SessionStateBuilder;
use substrait::proto::extensions::simple_extension_declaration::{
@@ -767,6 +766,18 @@ async fn roundtrip_values() -> Result<()> {
true).await
}
+#[tokio::test]
+async fn roundtrip_values_no_columns() -> Result<()> {
+ let ctx = create_context().await?;
+ // "VALUES ()" is not yet supported by the SQL parser, so we construct the plan manually
+ let plan = LogicalPlan::Values(Values {
+ values: vec![vec![], vec![]], // two rows, no columns
+ schema: DFSchemaRef::new(DFSchema::empty()),
+ });
+ roundtrip_logical_plan_with_ctx(plan, ctx).await?;
+ Ok(())
+}
+
#[tokio::test]
async fn roundtrip_values_empty_relation() -> Result<()> {
roundtrip("SELECT * FROM (VALUES ('a')) LIMIT 0").await
@@ -1121,9 +1132,10 @@ async fn test_alias(sql_with_alias: &str, sql_no_alias: &str) -> Result<()> {
Ok(())
}
-async fn roundtrip_with_ctx(sql: &str, ctx: SessionContext) -> Result<Box<Plan>> {
- let df = ctx.sql(sql).await?;
- let plan = df.into_optimized_plan()?;
+async fn roundtrip_logical_plan_with_ctx(
+ plan: LogicalPlan,
+ ctx: SessionContext,
+) -> Result<Box<Plan>> {
let proto = to_substrait_plan(&plan, &ctx)?;
let plan2 = from_substrait_plan(&ctx, &proto).await?;
let plan2 = ctx.state().optimize(&plan2)?;
@@ -1143,6 +1155,12 @@ async fn roundtrip_with_ctx(sql: &str, ctx: SessionContext) -> Result<Box<Plan>>
Ok(proto)
}
+async fn roundtrip_with_ctx(sql: &str, ctx: SessionContext) -> Result<Box<Plan>> {
+ let df = ctx.sql(sql).await?;
+ let plan = df.into_optimized_plan()?;
+ roundtrip_logical_plan_with_ctx(plan, ctx).await
+}
+
async fn roundtrip(sql: &str) -> Result<()> {
roundtrip_with_ctx(sql, create_context().await?).await?;
Ok(())