alamb opened a new issue, #27950:
URL: https://github.com/apache/datafusion/issues/27950

   ### Is your feature request related to a problem or challenge?
   
   I am building a custom REPL (see [aaldb]) on top of the [`datafusion-cli`] 
crate. I use [`exec_from_repl`] with my own [`SessionContext`] and want to 
record one trace per SQL statement with [datafusion-tracing].
   
   To get one trace per statement, I need a single root `tracing` span to cover 
everything DataFusion does for that statement, including logical planning and 
execution.
   
   The only per-statement hook on [`CliSessionContext`] is 
`execute_logical_plan`, which covers just the step from `LogicalPlan` to 
`DataFrame`. After that method returns, private code in [`exec.rs`] creates and 
executes the physical plan.
   
   ```rust
   // StatementExecutor::execute (private), called from exec_and_print 
(pub(super))
   let (df, adjusted) = self
       .create_and_execute_logical_plan(ctx, print_options)  // calls 
ctx.execute_logical_plan
       .await?;
   let physical_plan = df.create_physical_plan().await?;      // not covered by 
any hook
   ...
   let stream = execute_stream(physical_plan, task_ctx.clone())?;  // not 
covered by any hook
   ```
   
   I want to use my `CliSessionContext` implementation to observe or wrap a 
statement from start to finish. The current hook covers only part of that work.
   
   I can work around this by copying code from `exec_from_repl`, but then I 
have to keep my copy in sync with upstream.
   
   ### Describe the solution you'd like
   
   I'd like to capture the execution of an entire statement by overriding a 
method on `CliSessionContext`.
   
   For example, we could add a method that receives a future that plans the 
statement, executes it, and prints the results.
   
   ```rust
   #[async_trait]
   pub trait CliSessionContext {
       // ... existing methods ...
   
       /// Called once for each statement, around planning, execution and
       /// printing of the results. Implementations must await `fut`.
       async fn execute_statement<'a>(
           &'a self,
           statement: &'a Statement,
           fut: BoxFuture<'a, Result<()>>,
       ) -> Result<()> {
           fut.await
       }
   }
   ```
   
   `exec_and_print` would then call the hook for each statement.
   
   ```rust
   ctx.execute_statement(&statement, Box::pin(executor.execute(ctx, 
print_options))).await?;
   ```
   
   For my tracing use case, the implementation could look like this (have some 
way to wrap the whole statement).
   
   ```rust
   async fn execute_statement<'a>(
       &'a self,
       statement: &'a Statement,
       fut: BoxFuture<'a, Result<()>>,
   ) -> Result<()> {
       fut.instrument(tracing::info_span!("query", sql = %statement)).await
   }
   ```
   
   (The exact details might differ -- for for example, the hook could receive 
the SQL text instead of the parsed `Statement`)
   
   Beyond tracing, the same hook would support per-statement timing, audit 
logging, timeouts, custom cancellation, etc.
   
   ### Describe alternatives you've considered
   
   1. **Make `exec_and_print` (and `split_from_semicolon`) public.** This is a 
smaller change and makes the copied loop easier to write correctly, but users 
still have to copy and maintain the loop.
   
   2. **Create a `tracing` span inside `datafusion-cli` itself.** This would 
work without additional user code, but it would add a dependency and bake one 
observability choice into the crate. The hook would let users choose how to 
instrument each statement.
   
   3. **Widen `execute_logical_plan`** to also cover physical planning and 
execution. This would change the meaning of an existing method and its return 
type (`DataFrame`), so it seems worse than adding a new method.
   
   ### Additional context
   
   I observed this behavior with `datafusion-cli` 56.0.0.
   
   [aaldb]: https://github.com/alamb/aaldb
   [`datafusion-cli`]: https://crates.io/crates/datafusion-cli
   [`SessionContext`]: 
https://docs.rs/datafusion/56.0.0/datafusion/execution/context/struct.SessionContext.html
   [datafusion-tracing]: 
https://github.com/datafusion-contrib/datafusion-tracing
   [`exec_from_repl`]: 
https://docs.rs/datafusion-cli/56.0.0/datafusion_cli/exec/fn.exec_from_repl.html
   [`CliSessionContext`]: 
https://docs.rs/datafusion-cli/56.0.0/datafusion_cli/cli_context/trait.CliSessionContext.html
   [`exec.rs`]: 
https://github.com/apache/datafusion/blob/56.0.0/datafusion-cli/src/exec.rs
   


-- 
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