Skip to content

Commit

Permalink
feat: change the way to get cached plan
Browse files Browse the repository at this point in the history
  • Loading branch information
kysshsy committed Oct 2, 2024
1 parent a8a9715 commit 9cc49c9
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/hooks/utility/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,29 @@ pub fn execute_query<T: pgbox::WhoAllocated>(
) -> Result<bool> {
unsafe {
let prepared_stmt = pg_sys::FetchPreparedStatement((*stmt).name, true);
let plan_source = *(*prepared_stmt).plansource;
let plan_source = (*prepared_stmt).plansource;

if plan_source.query_list.is_null() {
if (*plan_source).query_list.is_null() || !(*plan_source).fixed_result {
return Ok(true);
}
let query =
(*((*plan_source.query_list).elements).offset(0)).ptr_value as *mut pg_sys::Query;
let query_relations = get_query_relations((*query).rtable);
if !is_duckdb_query(&query_relations) {

let cached_plan = pg_sys::GetCachedPlan(plan_source, null_mut(), null_mut(), null_mut());
if (*cached_plan).stmt_list.is_null() {
return Ok(true);
}

let planned_stmt = (*(*(*cached_plan).stmt_list).elements.offset(0)).ptr_value as *mut pg_sys::PlannedStmt;
let query_relations = get_query_relations((*planned_stmt).rtable);
if (*planned_stmt).commandType != pg_sys::CmdType::CMD_SELECT || !is_duckdb_query(&query_relations) {
return Ok(true);
}

(*query_desc.as_ptr()).tupDesc = (plan_source).resultDesc
(*query_desc.as_ptr()).tupDesc = (*plan_source).resultDesc
}

let query = unsafe { CStr::from_ptr((*query_desc.as_ptr()).sourceText) };

set_search_path_by_pg()?;
match connection::create_arrow(query.to_str()?) {
Err(err) => {
connection::clear_arrow();
Expand Down

0 comments on commit 9cc49c9

Please sign in to comment.