Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
23 changes: 23 additions & 0 deletions datafusion/sql/src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,16 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
table_name,
..
} => self.describe_table_to_plan(table_name),
Statement::Explain {
describe_alias: DescribeAlias::Describe | DescribeAlias::Desc, // only parse 'DESCRIBE statement' or 'DESC statement' and not 'EXPLAIN statement'
statement,
..
} => match *statement {
Statement::Query(query) => self.describe_query_to_plan(*query),
_ => Err(DataFusionError::NotImplemented(format!(
"Unsupported statement type for DESCRIBE: {statement:?}",
))),
},
Statement::Explain {
verbose,
statement,
Expand Down Expand Up @@ -1396,6 +1406,19 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
}))
}

fn describe_query_to_plan(&self, query: Query) -> Result<LogicalPlan> {
let plan = self.query_to_plan(query, &mut PlannerContext::new())?;

let schema = Arc::new(plan.schema().as_arrow().clone());

let output_schema = DFSchema::try_from(LogicalPlan::describe_schema()).unwrap();

Ok(LogicalPlan::DescribeTable(DescribeTable {
schema,
output_schema: Arc::new(output_schema),
}))
}

fn copy_to_plan(&self, statement: CopyToStatement) -> Result<LogicalPlan> {
// Determine if source is table or query and handle accordingly
let copy_source = statement.source;
Expand Down
26 changes: 26 additions & 0 deletions datafusion/sqllogictest/test_files/describe.slt
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,29 @@ col1 Int32 YES
# Test error cases
statement error
DESC nonexistent_table;

##########
# Describe statement
##########

# Test describing the schema of a simple statement
query TTT
DESCRIBE SELECT 1;
----
Int64(1) Int64 NO

# Insert some data into the existing test table...
statement ok
INSERT INTO test_desc_table (id, name) VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Charlie'), (4, 'Alice');

# ... and describe the schema of a more complex query
query TTT
DESCRIBE SELECT name, COUNT(*) AS name_count FROM test_desc_table
GROUP BY name HAVING COUNT(*) > 1 ORDER BY name_count DESC;
----
name Utf8View YES
name_count Int64 NO

# Describing a statement that's not a query is not supported
statement error Unsupported statement type for DESCRIBE
DESCRIBE CREATE TABLE test_desc_table (id INT, name VARCHAR);
6 changes: 6 additions & 0 deletions docs/source/library-user-guide/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ Users may need to update their paths to account for these changes.

See [issue #17713] for more details.

### `DESCRIBE query` support

`DESCRIBE query` was previously an alias for `EXPLAIN query`, which outputs the
_execution plan_ of the query. With this release, `DESCRIBE query` now outputs
the computed _schema_ of the query, consistent with the behavior of `DESCRIBE table_name`.

## DataFusion `50.0.0`

### ListingTable automatically detects Hive Partitioned tables
Expand Down