martin-g commented on code in PR #18842:
URL: https://github.com/apache/datafusion/pull/18842#discussion_r2548754381


##########
datafusion/substrait/src/logical_plan/producer/rel/read_rel.rs:
##########
@@ -83,26 +84,61 @@ pub fn from_table_scan(
     }))
 }
 
+/// Encodes an EmptyRelation as a Substrait VirtualTable.
+///
+/// EmptyRelation represents a relation with no input data. When 
`produce_one_row` is true,
+/// it generates a single row with all fields set to their default values 
(typically NULL).
+/// This is used for queries without a FROM clause, such as "SELECT 1 AS one" 
or
+/// "SELECT current_timestamp()".
+///
+/// When `produce_one_row` is false, it represents a truly empty relation with 
no rows,
+/// used in optimizations or as a placeholder.
 pub fn from_empty_relation(
     producer: &mut impl SubstraitProducer,
     e: &EmptyRelation,
 ) -> datafusion::common::Result<Box<Rel>> {
-    if e.produce_one_row {
-        return not_impl_err!("Producing a row from empty relation is 
unsupported");
-    }
-    #[allow(deprecated)]
+    let base_schema = to_substrait_named_struct(producer, &e.schema)?;
+
+    let read_type = if e.produce_one_row {
+        // Create one row with default scalar values for each field in the 
schema.
+        // For example, an Int32 field gets Int32(NULL), a Utf8 field gets 
Utf8(NULL), etc.
+        // This represents the "phantom row" that provides a context for 
evaluating
+        // scalar expressions in queries without a FROM clause.
+        let fields = e
+            .schema
+            .fields()
+            .iter()
+            .map(|f| {
+                let scalar = ScalarValue::try_from(f.data_type())?;
+                to_substrait_literal(producer, &scalar)
+            })
+            .collect::<datafusion::common::Result<_>>()?;
+
+        ReadType::VirtualTable(VirtualTable {
+            // Use deprecated 'values' field instead of 'expressions' because 
the consumer's
+            // nested expression support (RexType::Nested) is not yet 
implemented.

Review Comment:
   ```suggestion
               // nested expression support (RelType::Nested) is not yet 
implemented.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to