use std::sync::Arc;
use crate::control::security::credential::CredentialStore;
use super::catalog_inputs::CatalogInputs;
mod planning;
pub use planning::PlanSqlWithRlsParams;
pub struct QueryContext {
catalog_inputs: Option<CatalogInputs>,
retention_registry:
Option<Arc<crate::engine::timeseries::retention_policy::RetentionPolicyRegistry>>,
array_catalog: Option<crate::control::array_catalog::ArrayCatalogHandle>,
wal: Option<Arc<crate::wal::WalManager>>,
surrogate_assigner: Option<Arc<crate::control::surrogate::SurrogateAssigner>>,
cluster_enabled: bool,
bitemporal_retention_registry:
Option<Arc<crate::engine::bitemporal::BitemporalRetentionRegistry>>,
max_vector_dim: std::sync::atomic::AtomicU32,
force_shuffle_join: std::sync::atomic::AtomicBool,
shuffle_num_parts: std::sync::atomic::AtomicU32,
force_shuffle_agg: std::sync::atomic::AtomicBool,
shuffle_agg_num_parts: std::sync::atomic::AtomicU32,
broadcast_threshold_bytes: std::sync::atomic::AtomicUsize,
shuffle_agg_threshold: std::sync::atomic::AtomicUsize,
}
pub const DEFAULT_SHUFFLE_AGG_THRESHOLD: usize = 10_000;
impl QueryContext {
pub fn new() -> Self {
Self {
catalog_inputs: None,
retention_registry: None,
array_catalog: None,
wal: None,
surrogate_assigner: None,
cluster_enabled: false,
bitemporal_retention_registry: None,
max_vector_dim: std::sync::atomic::AtomicU32::new(0),
force_shuffle_join: std::sync::atomic::AtomicBool::new(false),
shuffle_num_parts: std::sync::atomic::AtomicU32::new(0),
force_shuffle_agg: std::sync::atomic::AtomicBool::new(false),
shuffle_agg_num_parts: std::sync::atomic::AtomicU32::new(0),
broadcast_threshold_bytes: std::sync::atomic::AtomicUsize::new(
default_broadcast_threshold_bytes(),
),
shuffle_agg_threshold: std::sync::atomic::AtomicUsize::new(
DEFAULT_SHUFFLE_AGG_THRESHOLD,
),
}
}
pub fn for_state(state: &crate::control::state::SharedState) -> Self {
let mut ctx = Self::with_catalog(
Arc::clone(&state.credentials),
Some(Arc::clone(&state.retention_policy_registry)),
);
ctx.surrogate_assigner = Some(Arc::clone(&state.surrogate_assigner));
ctx.cluster_enabled = state.cluster_topology.is_some();
ctx.bitemporal_retention_registry = Some(Arc::clone(&state.bitemporal_retention_registry));
ctx.max_vector_dim
.store(0, std::sync::atomic::Ordering::Relaxed);
ctx.broadcast_threshold_bytes.store(
state.tuning.cluster_transport.broadcast_threshold_bytes,
std::sync::atomic::Ordering::Relaxed,
);
ctx
}
pub fn for_state_with_lease(state: &Arc<crate::control::state::SharedState>) -> Self {
let retention = Some(Arc::clone(&state.retention_policy_registry));
Self {
catalog_inputs: Some(CatalogInputs {
credentials: Arc::clone(&state.credentials),
shared: Some(Arc::downgrade(state)),
retention_policy_registry: retention.clone(),
}),
retention_registry: retention,
array_catalog: Some(state.array_catalog.clone()),
wal: Some(Arc::clone(&state.wal)),
surrogate_assigner: Some(Arc::clone(&state.surrogate_assigner)),
cluster_enabled: state.cluster_topology.is_some(),
bitemporal_retention_registry: Some(Arc::clone(&state.bitemporal_retention_registry)),
max_vector_dim: std::sync::atomic::AtomicU32::new(0),
force_shuffle_join: std::sync::atomic::AtomicBool::new(false),
shuffle_num_parts: std::sync::atomic::AtomicU32::new(0),
force_shuffle_agg: std::sync::atomic::AtomicBool::new(false),
shuffle_agg_num_parts: std::sync::atomic::AtomicU32::new(0),
broadcast_threshold_bytes: std::sync::atomic::AtomicUsize::new(
state.tuning.cluster_transport.broadcast_threshold_bytes,
),
shuffle_agg_threshold: std::sync::atomic::AtomicUsize::new(
DEFAULT_SHUFFLE_AGG_THRESHOLD,
),
}
}
pub fn set_max_vector_dim(&self, dim: u32) {
self.max_vector_dim
.store(dim, std::sync::atomic::Ordering::Relaxed);
}
pub fn set_force_shuffle_join(&self, force: bool, num_parts: u32) {
self.force_shuffle_join
.store(force, std::sync::atomic::Ordering::Relaxed);
self.shuffle_num_parts
.store(num_parts, std::sync::atomic::Ordering::Relaxed);
}
pub fn set_force_shuffle_agg(&self, force: bool, num_parts: u32) {
self.force_shuffle_agg
.store(force, std::sync::atomic::Ordering::Relaxed);
self.shuffle_agg_num_parts
.store(num_parts, std::sync::atomic::Ordering::Relaxed);
}
pub fn set_broadcast_threshold_bytes(&self, bytes: usize) {
self.broadcast_threshold_bytes
.store(bytes, std::sync::atomic::Ordering::Relaxed);
}
pub fn set_shuffle_agg_threshold(&self, groups: usize) {
self.shuffle_agg_threshold
.store(groups, std::sync::atomic::Ordering::Relaxed);
}
pub fn default_broadcast_threshold(&self) -> usize {
self.broadcast_threshold_bytes
.load(std::sync::atomic::Ordering::Relaxed)
}
pub fn set_rounding_mode(&self, _mode: &str) {}
pub fn with_catalog(
credentials: Arc<CredentialStore>,
retention_policy_registry: Option<
Arc<crate::engine::timeseries::retention_policy::RetentionPolicyRegistry>,
>,
) -> Self {
let catalog_inputs = Some(CatalogInputs {
credentials,
shared: None,
retention_policy_registry: retention_policy_registry.clone(),
});
Self {
catalog_inputs,
retention_registry: retention_policy_registry,
array_catalog: None,
wal: None,
surrogate_assigner: None,
cluster_enabled: false,
bitemporal_retention_registry: None,
max_vector_dim: std::sync::atomic::AtomicU32::new(0),
force_shuffle_join: std::sync::atomic::AtomicBool::new(false),
shuffle_num_parts: std::sync::atomic::AtomicU32::new(0),
force_shuffle_agg: std::sync::atomic::AtomicBool::new(false),
shuffle_agg_num_parts: std::sync::atomic::AtomicU32::new(0),
broadcast_threshold_bytes: std::sync::atomic::AtomicUsize::new(
default_broadcast_threshold_bytes(),
),
shuffle_agg_threshold: std::sync::atomic::AtomicUsize::new(
DEFAULT_SHUFFLE_AGG_THRESHOLD,
),
}
}
}
fn default_broadcast_threshold_bytes() -> usize {
nodedb_types::config::tuning::ClusterTransportTuning::default().broadcast_threshold_bytes
}
impl Default for QueryContext {
fn default() -> Self {
Self::new()
}
}
pub const SYSTEM_FUNCTION_NAMES: &[&str] = &[
"doc_get",
"doc_exists",
"doc_array_contains",
"vector_distance",
"multi_vector_search",
"rrf_score",
"bm25_score",
"text_match",
"st_dwithin",
"st_contains",
"st_intersects",
"st_within",
"st_distance",
"geo_distance",
"time_bucket",
"ts_rate",
"ts_derivative",
"ts_moving_avg",
"ts_ema",
"ts_delta",
"ts_interpolate",
"ts_lag",
"ts_lead",
"ts_rank",
"ts_percentile",
"ts_stddev",
"ts_correlate",
"ts_zscore",
"ts_bollinger_upper",
"ts_bollinger_lower",
"ts_bollinger_mid",
"ts_bollinger_width",
"ts_moving_percentile",
"approx_count_distinct",
"approx_percentile",
"approx_topk",
"approx_count",
"round",
"nextval",
"currval",
"setval",
"next_preview",
];