Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
kysshsy committed Oct 6, 2024
1 parent df5d685 commit 6e3c6a7
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/hooks/utility/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

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,
Expand All @@ -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);
}

Expand Down

0 comments on commit 6e3c6a7

Please sign in to comment.