From 6e3c6a72565844ebb8c47c446946c9640fbc155d Mon Sep 17 00:00:00 2001 From: kysshsy Date: Sun, 6 Oct 2024 22:21:25 +0800 Subject: [PATCH] temp --- src/hooks/utility/explain.rs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/hooks/utility/explain.rs b/src/hooks/utility/explain.rs index bdfca82b..d3709bda 100644 --- a/src/hooks/utility/explain.rs +++ b/src/hooks/utility/explain.rs @@ -16,12 +16,16 @@ // along with this program. If not, see . use std::ffi::CString; +use std::time::Instant; use anyhow::Result; use pgrx::{error, pg_sys}; use super::parse_query_from_utility_stmt; -use crate::hooks::query::{get_query_relations, is_duckdb_query}; +use crate::{ + duckdb::connection, + hooks::query::{get_query_relations, is_duckdb_query, set_search_path_by_pg}, +}; pub fn explain_query( query_string: &core::ffi::CStr, @@ -41,19 +45,27 @@ pub fn explain_query( error!("the EXPLAIN options provided are not supported for DuckDB pushdown queries."); } + let query = parse_query_from_utility_stmt(query_string)?; + let start_time = Instant::now(); + set_search_path_by_pg()?; + connection::execute(&query, [])?; + let duration = start_time.elapsed(); + + let output = format!( + "DuckDB Scan: {}\nExecution Time: {} ms", + query, + duration.as_millis() + ); unsafe { let tstate = pg_sys::begin_tup_output_tupdesc( dest, pg_sys::ExplainResultDesc(stmt), &pg_sys::TTSOpsVirtual, ); - let query = format!( - "DuckDB Scan: {}", - parse_query_from_utility_stmt(query_string)? - ); - let query_c_str = CString::new(query)?; - pg_sys::do_text_output_multiline(tstate, query_c_str.as_ptr()); + let output_cstr = CString::new(output)?; + + pg_sys::do_text_output_multiline(tstate, output_cstr.as_ptr()); pg_sys::end_tup_output(tstate); }