17
17
18
18
//! Execution functions
19
19
20
+ use std:: io:: prelude:: * ;
21
+ use std:: io:: BufReader ;
22
+ use std:: time:: Instant ;
23
+ use std:: { fs:: File , sync:: Arc } ;
24
+
25
+ use crate :: print_format:: PrintFormat ;
20
26
use crate :: {
21
27
command:: { Command , OutputFormat } ,
22
28
helper:: { unescape_input, CliHelper } ,
@@ -26,21 +32,19 @@ use crate::{
26
32
} ,
27
33
print_options:: { MaxRows , PrintOptions } ,
28
34
} ;
29
- use datafusion:: common:: plan_datafusion_err;
35
+
36
+ use datafusion:: common:: { exec_datafusion_err, plan_datafusion_err} ;
37
+ use datafusion:: datasource:: listing:: ListingTableUrl ;
38
+ use datafusion:: datasource:: physical_plan:: is_plan_streaming;
39
+ use datafusion:: error:: { DataFusionError , Result } ;
40
+ use datafusion:: logical_expr:: { CreateExternalTable , DdlStatement , LogicalPlan } ;
41
+ use datafusion:: physical_plan:: { collect, execute_stream} ;
42
+ use datafusion:: prelude:: SessionContext ;
30
43
use datafusion:: sql:: { parser:: DFParser , sqlparser:: dialect:: dialect_from_str} ;
31
- use datafusion:: {
32
- datasource:: listing:: ListingTableUrl ,
33
- error:: { DataFusionError , Result } ,
34
- logical_expr:: { CreateExternalTable , DdlStatement } ,
35
- } ;
36
- use datafusion:: { logical_expr:: LogicalPlan , prelude:: SessionContext } ;
44
+
37
45
use object_store:: ObjectStore ;
38
46
use rustyline:: error:: ReadlineError ;
39
47
use rustyline:: Editor ;
40
- use std:: io:: prelude:: * ;
41
- use std:: io:: BufReader ;
42
- use std:: time:: Instant ;
43
- use std:: { fs:: File , sync:: Arc } ;
44
48
use url:: Url ;
45
49
46
50
/// run and execute SQL statements and commands, against a context with the given print options
@@ -125,8 +129,6 @@ pub async fn exec_from_repl(
125
129
) ) ) ;
126
130
rl. load_history ( ".history" ) . ok ( ) ;
127
131
128
- let mut print_options = print_options. clone ( ) ;
129
-
130
132
loop {
131
133
match rl. readline ( "❯ " ) {
132
134
Ok ( line) if line. starts_with ( '\\' ) => {
@@ -138,9 +140,7 @@ pub async fn exec_from_repl(
138
140
Command :: OutputFormat ( subcommand) => {
139
141
if let Some ( subcommand) = subcommand {
140
142
if let Ok ( command) = subcommand. parse :: < OutputFormat > ( ) {
141
- if let Err ( e) =
142
- command. execute ( & mut print_options) . await
143
- {
143
+ if let Err ( e) = command. execute ( print_options) . await {
144
144
eprintln ! ( "{e}" )
145
145
}
146
146
} else {
@@ -154,7 +154,7 @@ pub async fn exec_from_repl(
154
154
}
155
155
}
156
156
_ => {
157
- if let Err ( e) = cmd. execute ( ctx, & mut print_options) . await {
157
+ if let Err ( e) = cmd. execute ( ctx, print_options) . await {
158
158
eprintln ! ( "{e}" )
159
159
}
160
160
}
@@ -165,7 +165,7 @@ pub async fn exec_from_repl(
165
165
}
166
166
Ok ( line) => {
167
167
rl. add_history_entry ( line. trim_end ( ) ) ?;
168
- match exec_and_print ( ctx, & print_options, line) . await {
168
+ match exec_and_print ( ctx, print_options, line) . await {
169
169
Ok ( _) => { }
170
170
Err ( err) => eprintln ! ( "{err}" ) ,
171
171
}
@@ -198,7 +198,6 @@ async fn exec_and_print(
198
198
sql : String ,
199
199
) -> Result < ( ) > {
200
200
let now = Instant :: now ( ) ;
201
-
202
201
let sql = unescape_input ( & sql) ?;
203
202
let task_ctx = ctx. task_ctx ( ) ;
204
203
let dialect = & task_ctx. session_config ( ) . options ( ) . sql_parser . dialect ;
@@ -227,18 +226,24 @@ async fn exec_and_print(
227
226
if let LogicalPlan :: Ddl ( DdlStatement :: CreateExternalTable ( cmd) ) = & mut plan {
228
227
create_external_table ( ctx, cmd) . await ?;
229
228
}
229
+
230
230
let df = ctx. execute_logical_plan ( plan) . await ?;
231
- let results = df. collect ( ) . await ?;
231
+ let physical_plan = df. create_physical_plan ( ) . await ?;
232
232
233
- let print_options = if should_ignore_maxrows {
234
- PrintOptions {
235
- maxrows : MaxRows :: Unlimited ,
236
- ..print_options. clone ( )
237
- }
233
+ if is_plan_streaming ( & physical_plan) ? {
234
+ let stream = execute_stream ( physical_plan, task_ctx. clone ( ) ) ?;
235
+ print_options. print_stream ( stream, now) . await ?;
238
236
} else {
239
- print_options. clone ( )
240
- } ;
241
- print_options. print_batches ( & results, now) ?;
237
+ let mut print_options = print_options. clone ( ) ;
238
+ if should_ignore_maxrows {
239
+ print_options. maxrows = MaxRows :: Unlimited ;
240
+ }
241
+ if print_options. format == PrintFormat :: Automatic {
242
+ print_options. format = PrintFormat :: Table ;
243
+ }
244
+ let results = collect ( physical_plan, task_ctx. clone ( ) ) . await ?;
245
+ print_options. print_batches ( & results, now) ?;
246
+ }
242
247
}
243
248
244
249
Ok ( ( ) )
@@ -272,10 +277,7 @@ async fn create_external_table(
272
277
. object_store_registry
273
278
. get_store ( url)
274
279
. map_err ( |_| {
275
- DataFusionError :: Execution ( format ! (
276
- "Unsupported object store scheme: {}" ,
277
- scheme
278
- ) )
280
+ exec_datafusion_err ! ( "Unsupported object store scheme: {}" , scheme)
279
281
} ) ?
280
282
}
281
283
} ;
0 commit comments