Skip to content

Add prepared_schema() to get Arrow schema without executing statement#668

Open
fvaleye wants to merge 1 commit into
duckdb:mainfrom
altertable-ai:feat/prepared-schema
Open

Add prepared_schema() to get Arrow schema without executing statement#668
fvaleye wants to merge 1 commit into
duckdb:mainfrom
altertable-ai:feat/prepared-schema

Conversation

@fvaleye

@fvaleye fvaleye commented Jan 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a new prepared_schema() method that extracts the Arrow Schema from a prepared statement without executing it.

When using stream_arrow() for streaming query results, the caller must provide the schema upfront.
Currently, the only way to obtain the schema is to execute the query first, which forces a pattern like:

// Current workaround: prepare and execute twice 
let mut stmt1 = conn.prepare(sql)?;
stmt1.execute([])?; 
let schema = stmt1.schema();
drop(stmt1); 

let mut stmt2 = conn.prepare(sql)?;  // Second preparation        
let stream = stmt2.stream_arrow([], schema)?;

This doubles the preparation cost (parsing, planning, optimization) for every streaming query.
With prepared_schema(), the schema can be obtained directly from the prepared statement:

// New: single preparation                                                                                                                                                            
let mut stmt = conn.prepare(sql)?;                                                                                                                                                    
let schema = stmt.prepared_schema()?.unwrap();                                                                                                                                        
let stream = stmt.stream_arrow([], schema)?;           

@fvaleye fvaleye force-pushed the feat/prepared-schema branch from 3365960 to af1f068 Compare January 23, 2026 10:11
@mlafeldt mlafeldt added feature Used for auto generate changelog Arrow labels Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arrow feature Used for auto generate changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants