#![allow(unsafe_code)]
use crate::hton::SecondaryEngineOptimizerRequest;
use crate::hton::result::result_to_error;
use crate::panic_guard::FfiBoundary;
use crate::runtime;
use crate::sys;
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust__hton__prepare_secondary_engine(
thd: *const sys::THD,
lex: *const sys::Lex,
) -> bool {
FfiBoundary::run_default(true, || {
let thd_ref = unsafe { thd.as_ref() };
let lex_ref = unsafe { lex.as_ref() };
match runtime::handlerton() {
Some(h) => result_to_error(h.prepare_secondary_engine(thd_ref, lex_ref)),
None => false,
}
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust__hton__optimize_secondary_engine(
thd: *const sys::THD,
lex: *const sys::Lex,
) -> bool {
FfiBoundary::run_default(true, || {
let thd_ref = unsafe { thd.as_ref() };
let lex_ref = unsafe { lex.as_ref() };
match runtime::handlerton() {
Some(h) => result_to_error(h.optimize_secondary_engine(thd_ref, lex_ref)),
None => false,
}
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust__hton__compare_secondary_engine_cost(
thd: *const sys::THD,
join: *const sys::Join,
optimizer_cost: f64,
use_best_so_far: *mut bool,
cheaper: *mut bool,
secondary_engine_cost: *mut f64,
) -> bool {
FfiBoundary::run_default(true, || {
let thd_ref = unsafe { thd.as_ref() };
let join_ref = unsafe { join.as_ref() };
let triple = match runtime::handlerton() {
Some(h) => h.compare_secondary_engine_cost(thd_ref, join_ref, optimizer_cost),
None => Ok(None),
};
let (best, ch, cost) = match triple {
Ok(Some(t)) => t,
Ok(None) => (false, false, optimizer_cost),
Err(_) => return true,
};
if !use_best_so_far.is_null() {
unsafe { use_best_so_far.write(best) };
}
if !cheaper.is_null() {
unsafe { cheaper.write(ch) };
}
if !secondary_engine_cost.is_null() {
unsafe { secondary_engine_cost.write(cost) };
}
false
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust__hton__secondary_engine_modify_access_path_cost(
thd: *const sys::THD,
hypergraph: *const sys::JoinHypergraph,
access_path: *const sys::AccessPath,
) -> bool {
FfiBoundary::run_default(true, || {
let thd_ref = unsafe { thd.as_ref() };
let hg_ref = unsafe { hypergraph.as_ref() };
let ap_ref = unsafe { access_path.as_ref() };
match runtime::handlerton() {
Some(h) => {
result_to_error(h.secondary_engine_modify_access_path_cost(thd_ref, hg_ref, ap_ref))
}
None => false,
}
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust__hton__external_engine_explain_check(thd: *const sys::THD) -> bool {
FfiBoundary::run_default(true, || {
let thd_ref = unsafe { thd.as_ref() };
match runtime::handlerton() {
Some(h) => h.external_engine_explain_check(thd_ref),
None => false,
}
})
}
#[unsafe(no_mangle)]
#[allow(clippy::too_many_arguments)]
pub unsafe extern "C" fn rust__hton__secondary_engine_check_optimizer_request(
thd: *const sys::THD,
hypergraph: *const sys::JoinHypergraph,
access_path: *const sys::AccessPath,
current_subgraph_pairs: i32,
current_subgraph_pairs_limit: i32,
is_root_access_path: bool,
out_request: *mut i32,
out_subgraph_pair_limit: *mut i32,
) {
FfiBoundary::run_void(|| {
let thd_ref = unsafe { thd.as_ref() };
let hg_ref = unsafe { hypergraph.as_ref() };
let ap_ref = unsafe { access_path.as_ref() };
let req = match runtime::handlerton() {
Some(h) => h.secondary_engine_check_optimizer_request(
thd_ref,
hg_ref,
ap_ref,
current_subgraph_pairs,
current_subgraph_pairs_limit,
is_root_access_path,
),
None => SecondaryEngineOptimizerRequest::keep_going(),
};
if !out_request.is_null() {
unsafe { out_request.write(req.request.to_raw()) };
}
if !out_subgraph_pair_limit.is_null() {
unsafe { out_subgraph_pair_limit.write(req.subgraph_pair_limit) };
}
});
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust__hton__secondary_engine_pre_prepare_hook(
thd: *const sys::THD,
) -> bool {
FfiBoundary::run_default(false, || {
let thd_ref = unsafe { thd.as_ref() };
match runtime::handlerton() {
Some(h) => h.secondary_engine_pre_prepare_hook(thd_ref),
None => false,
}
})
}