@@ -23,11 +23,12 @@ use std::time::Duration;
23
23
use color_eyre:: eyre:: Result ;
24
24
use datafusion:: execution:: runtime_env:: RuntimeEnv ;
25
25
use datafusion:: execution:: session_state:: SessionStateBuilder ;
26
- use datafusion:: execution:: TaskContext ;
27
- use datafusion:: physical_plan:: { execute_stream, visit_execution_plan, ExecutionPlanVisitor } ;
26
+ use datafusion:: execution:: { SendableRecordBatchStream , TaskContext } ;
27
+ use datafusion:: physical_plan:: {
28
+ execute_stream, visit_execution_plan, ExecutionPlan , ExecutionPlanVisitor ,
29
+ } ;
28
30
use datafusion:: prelude:: * ;
29
31
use datafusion:: sql:: parser:: Statement ;
30
- use datafusion:: { arrow:: util:: pretty:: pretty_format_batches, physical_plan:: ExecutionPlan } ;
31
32
#[ cfg( feature = "deltalake" ) ]
32
33
use deltalake:: delta_datafusion:: DeltaTableFactory ;
33
34
use log:: { error, info} ;
@@ -197,7 +198,7 @@ impl ExecutionContext {
197
198
}
198
199
}
199
200
} else {
200
- match self . execute_sql ( sql, false ) . await {
201
+ match self . execute_sql_and_discard_results ( sql) . await {
201
202
Ok ( _) => {
202
203
let elapsed = start. elapsed ( ) ;
203
204
query. set_execution_time ( elapsed) ;
@@ -215,53 +216,46 @@ impl ExecutionContext {
215
216
Ok ( ( ) )
216
217
}
217
218
218
- /// Executes the specified parsed DataFusion statement and discards the result
219
- pub async fn execute_sql ( & self , sql : & str , print : bool ) -> Result < ( ) > {
220
- let df = self . session_ctx . sql ( sql) . await ?;
221
- self . execute_stream_dataframe ( df, print) . await
219
+ /// Executes the specified sql string, driving it to completion but discarding the results
220
+ pub async fn execute_sql_and_discard_results (
221
+ & self ,
222
+ sql : & str ,
223
+ ) -> datafusion:: error:: Result < ( ) > {
224
+ let mut stream = self . execute_sql ( sql) . await ?;
225
+ while let Some ( maybe_batch) = stream. next ( ) . await {
226
+ match maybe_batch {
227
+ Ok ( _) => { }
228
+ Err ( e) => {
229
+ return Err ( e) ;
230
+ }
231
+ }
232
+ }
233
+ Ok ( ( ) )
222
234
}
223
235
224
- /// Executes the specified parsed DataFusion statement and prints the result to stdout
225
- pub async fn execute_and_print_statement ( & self , statement : Statement ) -> Result < ( ) > {
236
+ /// Executes the specified sql string, returning the resulting Stream
237
+ pub async fn execute_sql (
238
+ & self ,
239
+ sql : & str ,
240
+ ) -> datafusion:: error:: Result < SendableRecordBatchStream > {
241
+ self . session_ctx . sql ( sql) . await ?. execute_stream ( ) . await
242
+ }
243
+
244
+ /// Executes the specified parsed DataFusion statement, returning the resulting Stream
245
+ pub async fn execute_statement (
246
+ & self ,
247
+ statement : Statement ,
248
+ ) -> datafusion:: error:: Result < SendableRecordBatchStream > {
226
249
let plan = self
227
250
. session_ctx
228
251
. state ( )
229
252
. statement_to_plan ( statement)
230
253
. await ?;
231
- let df = self . session_ctx . execute_logical_plan ( plan) . await ?;
232
- self . execute_stream_dataframe ( df, true ) . await
233
- }
234
-
235
- /// Executes the specified query and prints the result to stdout
236
- pub async fn execute_and_print_stream_sql ( & self , query : & str ) -> Result < ( ) > {
237
- let df = self . session_ctx . sql ( query) . await ?;
238
- self . execute_stream_dataframe ( df, true ) . await
239
- }
240
-
241
- pub async fn execute_stream_dataframe ( & self , df : DataFrame , print : bool ) -> Result < ( ) > {
242
- let physical_plan = df. create_physical_plan ( ) . await ?;
243
- let stream_cfg = SessionConfig :: default ( ) ;
244
- let stream_task_ctx = TaskContext :: default ( ) . with_session_config ( stream_cfg) ;
245
- let mut stream = execute_stream ( physical_plan, stream_task_ctx. into ( ) ) . unwrap ( ) ;
246
-
247
- while let Some ( maybe_batch) = stream. next ( ) . await {
248
- if print {
249
- let batch = maybe_batch. unwrap ( ) ;
250
- let d = pretty_format_batches ( & [ batch] ) . unwrap ( ) ;
251
- println ! ( "{}" , d) ;
252
- } else {
253
- let _ = maybe_batch. unwrap ( ) ;
254
- info ! ( "Discarding batch" ) ;
255
- }
256
- }
257
- Ok ( ( ) )
258
- }
259
-
260
- pub async fn show_catalog ( & self ) -> Result < ( ) > {
261
- let tables = self . session_ctx . sql ( "SHOW tables" ) . await ?. collect ( ) . await ?;
262
- let formatted = pretty_format_batches ( & tables) . unwrap ( ) ;
263
- println ! ( "{}" , formatted) ;
264
- Ok ( ( ) )
254
+ self . session_ctx
255
+ . execute_logical_plan ( plan)
256
+ . await ?
257
+ . execute_stream ( )
258
+ . await
265
259
}
266
260
}
267
261
0 commit comments