1pub mod functions;
78pub mod migration;
79pub mod operator_tree_bridge;
80#[cfg(feature = "test-support")]
81#[doc(hidden)]
82pub use uqa_storage::test_support as storage_test_support;
83mod queries;
84pub use uqa_execution::result as sql;
86
87mod analyzers;
88mod async_sql_engine;
89mod cancellation;
90mod capabilities;
91mod catalog_indexes;
92mod domains;
93mod fdw;
94mod fts;
95mod generated;
96mod graphs;
97mod hierarchy;
98mod hook;
99mod models;
100mod notifications;
101mod open;
102mod parameters;
103mod prepared;
104pub use prepared::{PreparedStatement, PreparedStatementDescription};
105mod relations;
106mod roles;
107mod schema_security;
108mod search;
109mod sequence_catalog;
110mod sequence_session;
111mod sequence_snapshot;
112mod sequences;
113mod session;
114mod sql_registry;
115mod state;
116mod statistics;
117pub use statistics::AutomaticStatisticsStatus;
118pub use uqa_execution::maintenance::diskann::{
119 DiskANNMaintenanceCensus, DiskANNMaintenancePhase, DiskANNMaintenanceStatus,
120 DiskANNRebuildPolicy,
121};
122mod row_locks;
123mod statement_cache;
124mod table_storage;
125mod tables;
126mod transactions;
127pub use transactions::{ExecutionCompletion, ExecutionOutcome};
128mod user_functions;
129mod value_index;
130
131pub(crate) use uqa_execution::mutation::{
132 deferred::DeferredForeignKeyCheck, overlay::CommandMutationOverlay,
133};
134pub(crate) use uqa_execution::row_locks::publication::TransactionRowChange;
135
136use std::collections::{BTreeMap, BTreeSet};
137use std::path::Path;
138use std::sync::atomic::{AtomicBool, Ordering};
139use std::sync::Arc;
140
141use parking_lot::RwLock;
142use uqa_analysis::{analyzer::standard_analyzer, registry as analyzer_registry, Analyzer};
143use uqa_core::{DocId, FieldName, Value};
144use uqa_ml::{DeepLearnOutput, DeepModel, LearnOptions, TrainingSet};
145use uqa_operators::ExecutionContext;
146use uqa_scoring::{
147 BM25Params, BayesianBM25Params, BayesianBM25Scorer, CalibrationMetrics, CalibrationReport,
148 ParameterLearner, RawBm25Score, UnsupervisedBm25ScoreEstimator,
149};
150use uqa_sql::SQLError;
151use uqa_storage::{
152 document_store::Document, AnalyzerPhase, CatalogFacade, CatalogIndexRow, ColumnStatsInput,
153 ColumnStatsRow, DocumentStore, HNSWIndex, HNSWIndexParams, IVFIndex, IVFIndexParams,
154 InvertedIndex, MemoryDocumentStore, MemoryInvertedIndex, MemoryVectorIndex,
155 PersistentStorageBackend, PersistentStorageProvider, PersistentStorageSession,
156 RelationIdentity, SequenceRow, StorageBackendError, StorageBackendResult, StorageSavepointId,
157 TableSchema, VectorFieldSchema, VectorIndex, VectorIndexOpenMode, VectorIndexSpec,
158};
159use uqa_storage_sqlite::{
160 ManagedConnection, SQLiteCompressedContainerAnchor, SQLiteStorageProvider,
161};
162
163pub use notifications::{
164 NotificationReceive, NotificationSubscription, NotificationSubscriptionError,
165 NotificationSubscriptionOptions, NotificationSubscriptionPermit, NotificationWait,
166 SQLNotification, SQLNotificationOptions,
167};
168pub use sql::{SQLCursor, SQLCursorSummary};
169pub use uqa_execution::{ColumnVector, ColumnarBatch};
170pub use uqa_sql::{
171 ast::{SequenceBound, SequenceDataType, SequenceRestart},
172 AsyncSQLEngine, NoticeLevel, SQLBatchError, SQLNotice, SQLParam, SQLResult,
173};
174pub use uqa_storage_sqlite::{DatabaseFileFormat, SQLiteCompressionOptions, SQLiteError};
175
176use functions::RegisteredSQLFunction;
177pub use functions::{
178 SQLAggregateFactory, SQLAggregateFunction, SQLAggregateState, SQLFunctionContext,
179 SQLFunctionOptions, SQLFunctionVolatility, SQLRowSource, SQLRowStream, SQLScalarFunction,
180 SQLTableFunction, SQLTableFunctionResult, SQLTableFunctionStream,
181};
182use notifications::{
183 NotificationHubOwner as NotificationHub, PendingListenAction, PendingNotification,
184};
185pub use sql_registry::{SQLFunctionRegistration, SQLFunctionRegistrationError};
186use state::{
187 DurableCatalogSnapshot, DurableCatalogState, EpochCoordinator, QueryRuntime, RuntimeExtensions,
188 SessionContext, StorageContext, StoredView, StoredViewKind,
189};
190use statement_cache::{PreparedStatementPlan, SQLStatementCache};
191
192#[cfg(test)]
193use uqa_execution::catalog::sequence::restoration::SEQUENCES_METADATA_KEY;
194const SQL_FUNCTION_DEPTH_LIMIT: usize = 128;
198
199#[derive(Debug, thiserror::Error)]
200pub enum EngineError {
201 #[error("SQL error: {0}")]
202 SQL(#[from] SQLError),
203 #[error("storage error: {0}")]
204 Storage(#[from] SQLiteError),
205}
206
207pub type EngineResult<T> = std::result::Result<T, EngineError>;
208
209pub use uqa_core::ScoredEntry;
210
211pub use uqa_scoring::{TextSearchAlgorithm, TextSearchProfile};
212
213pub use uqa_storage::FtsIndexStat;
214
215pub use uqa_scoring::ScoringMode;
216
217type TableFieldAnalyzerRegistry = BTreeMap<(String, String), uqa_storage::FieldAnalyzerBinding>;
218type SessionPortalTableSnapshots = Arc<BTreeMap<RelationIdentity, Arc<TableState>>>;
219type SessionPortalViewSnapshots = Arc<BTreeMap<RelationIdentity, StoredView>>;
220type SessionPortalSQLFunctionSnapshots =
221 Arc<BTreeMap<String, Vec<Arc<user_functions::SQLUserFunction>>>>;
222type SessionPortalCatalogSnapshot = Arc<session::RetainedCatalogSnapshot>;
223type SessionPortalTransactionOverlay =
224 Arc<BTreeMap<String, uqa_execution::query::document_changes::DocumentChanges>>;
225type ColumnStatsMap = BTreeMap<String, uqa_planner::ColumnStats>;
226type FixedTransactionCatalogBaseline = BTreeMap<[u8; 16], (RelationIdentity, Vec<u8>)>;
227type NontransactionalSequenceValues = BTreeMap<[u8; 16], NontransactionalSequenceHistory>;
228
229#[derive(Clone, Default)]
230struct NontransactionalSequenceHistory {
231 values_by_definition: BTreeMap<[u8; 16], NontransactionalSequenceValue>,
232 object_id: [u8; 16],
233 session_currval: Option<SessionSequenceValue>,
234 defines_lastval: bool,
235}
236
237pub struct Engine {
240 storage: StorageContext,
241 durable: Arc<DurableCatalogState>,
242 session: Arc<SessionContext>,
243 extensions: RuntimeExtensions,
244 epochs: Arc<EpochCoordinator>,
245 runtime: QueryRuntime,
246 row_locks: Arc<row_locks::RowLockManager>,
247 statistics: Arc<statistics::StatisticsCoordinator>,
248 notification_hub: Arc<NotificationHub>,
249 session_id: u64,
250 owns_session_registration: bool,
251 query_table_snapshots: Option<SessionPortalTableSnapshots>,
252 query_view_snapshots: Option<SessionPortalViewSnapshots>,
253 query_sql_function_snapshots: Option<SessionPortalSQLFunctionSnapshots>,
254 query_catalog_snapshot: Option<SessionPortalCatalogSnapshot>,
255 query_transaction_overlay: Option<SessionPortalTransactionOverlay>,
256 query_transaction_origin: Option<u64>,
257}
258
259pub use uqa_execution::catalog::sequence::SequenceState;
260
261#[derive(Clone, Copy, Default)]
262struct TransactionDirtyState {
263 table_data: bool,
264 table_catalog: bool,
265 catalog_registry: bool,
266}
267
268#[derive(Clone, Copy, PartialEq, Eq)]
270enum TransactionIntent {
271 ReadOnly,
272 ReadWrite,
273}
274
275#[derive(Clone, Copy, PartialEq, Eq)]
276enum BackendTransactionMode {
277 Deferred,
278 Writer,
279 Versioned,
280}
281
282#[derive(Clone, Copy, PartialEq, Eq)]
283enum TransactionStatus {
284 Active,
285 Failed,
286 FailedBackendAborted,
287 CommitPending(uqa_storage::mvcc::TransactionOutcomeId),
288 RollbackPending(uqa_storage::mvcc::TransactionOutcomeId),
289}
290
291#[derive(Clone, Copy, PartialEq, Eq)]
292enum TransactionFrameKind {
293 ExplicitBlock,
294 ImplicitStatement,
295 SimpleQuery,
296}
297
298#[derive(Debug, Clone, Copy, PartialEq, Eq)]
299struct TransactionCharacteristicsState {
300 isolation: uqa_sql::ast::TransactionIsolationLevel,
301 read_only: bool,
302 deferrable: bool,
303 assigned: u8,
305}
306
307impl TransactionCharacteristicsState {
308 const ISOLATION_ASSIGNED: u8 = 1;
309 const READ_ONLY_ASSIGNED: u8 = 2;
310 const DEFERRABLE_ASSIGNED: u8 = 4;
311}
312
313impl Default for TransactionCharacteristicsState {
314 fn default() -> Self {
315 Self {
316 isolation: uqa_sql::ast::TransactionIsolationLevel::ReadCommitted,
317 read_only: false,
318 deferrable: false,
319 assigned: 0,
320 }
321 }
322}
323
324pub(crate) use uqa_sql::catalog::constraints::ConstraintIdentity;
325
326#[derive(Debug, Clone, Default)]
327pub(crate) struct ConstraintModeState {
328 all: Option<bool>,
329 named: BTreeMap<ConstraintIdentity, bool>,
330}
331
332struct TransactionFrame {
333 started_at_micros: i64,
334 implicit_statement: bool,
336 explicit_transaction_block: bool,
338 storage_savepoint: Option<StorageSavepointId>,
339 intent: TransactionIntent,
340 backend_mode: BackendTransactionMode,
341 status: TransactionStatus,
342 characteristics: TransactionCharacteristicsState,
343 first_snapshot_set: bool,
344 fixed_snapshot: Option<FixedTransactionSnapshot>,
345 fixed_catalog_records: Option<Arc<uqa_storage::mvcc::CommittedReadBoundary>>,
346 fixed_catalog_baseline: Option<FixedTransactionCatalogBaseline>,
348 xid_levels: Vec<Option<u32>>,
350 savepoints: Vec<TransactionSavepoint>,
351 session_snapshot: SessionStateSnapshot,
352 data_snapshot: Option<EngineDataSnapshot>,
353 dirty_at_begin: TransactionDirtyState,
354 prepared_changes: uqa_execution::statement::prepared::invalidation::PreparedInvalidationLog,
355 begin_lock_mark: u32,
357 lock_mark: u32,
358 next_lock_mark: u32,
359 snapshot_change_baseline: row_locks::RowChangeBaseline,
360 row_changes: Vec<TransactionRowChange>,
361 fixed_identities: Option<uqa_execution::query::document_changes::ChangedIdentities>,
363 statistics_changes: statistics::StatisticsChanges,
364 statistics_settlement: statistics::StatisticsSettlement,
366 deferred_foreign_key_checks: Vec<DeferredForeignKeyCheck>,
367 deferred_constraint_trigger_events:
368 Vec<uqa_execution::mutation::triggers::DeferredConstraintTriggerEvent>,
369 pending_listen_actions: Vec<PendingListenAction>,
370 pending_notifications: Vec<PendingNotification>,
371 pending_notification_commit:
372 parking_lot::Mutex<Option<Box<notifications::CrossNotificationCommit>>>,
373 constraint_modes: ConstraintModeState,
374 nontransactional_sequence_values: NontransactionalSequenceValues,
376}
377
378enum FixedTransactionSnapshot {
379 Pinned(Arc<Engine>),
380 Detached(SessionPortalTableSnapshots),
381}
382
383impl FixedTransactionSnapshot {
384 fn table(&self, relation: &RelationIdentity) -> Option<Arc<TableState>> {
385 match self {
386 Self::Pinned(snapshot) => snapshot.storage.tables.read().get(relation).cloned(),
387 Self::Detached(tables) => tables.get(relation).cloned(),
388 }
389 }
390
391 fn table_for_live_relation(
392 &self,
393 relation: &RelationIdentity,
394 live: &TableState,
395 ) -> Option<Arc<TableState>> {
396 let storage_generation = live.storage_generation();
397 let exact = self.table(relation);
398 if exact
399 .as_ref()
400 .is_some_and(|table| table.storage_generation() == storage_generation)
401 {
402 return exact;
403 }
404 match self {
405 Self::Pinned(snapshot) => snapshot
406 .storage
407 .tables
408 .read()
409 .values()
410 .find(|table| table.storage_generation() == storage_generation)
411 .cloned(),
412 Self::Detached(tables) => tables
413 .values()
414 .find(|table| table.storage_generation() == storage_generation)
415 .cloned(),
416 }
417 }
418}
419
420struct TransactionSavepoint {
421 name: String,
422 storage_savepoint: StorageSavepointId,
423 characteristics: TransactionCharacteristicsState,
424 session_snapshot: SessionStateSnapshot,
425 data_snapshot: Option<EngineDataSnapshot>,
426 dirty: TransactionDirtyState,
427 prepared_change_mark:
428 uqa_execution::statement::prepared::invalidation::PreparedInvalidationMark,
429 lock_mark: u32,
430 row_changes: usize,
432 statistics_changes: statistics::StatisticsChanges,
433 deferred_foreign_key_checks: Vec<DeferredForeignKeyCheck>,
434 deferred_constraint_trigger_events:
435 Vec<uqa_execution::mutation::triggers::DeferredConstraintTriggerEvent>,
436 pending_listen_actions: Vec<PendingListenAction>,
437 pending_notifications: Vec<PendingNotification>,
438 constraint_modes: ConstraintModeState,
439}
440
441#[derive(Clone, Default)]
443struct SessionStateSnapshot {
444 command_overlays: Vec<uqa_execution::mutation::overlay::CommandOverlayCheckpoint>,
445 graph_overlay: Option<GraphTransactionOverlay>,
447 search_path: Vec<String>,
448 temporary_namespace: Option<uqa_sql::catalog::temporary_namespace::TemporaryNamespaceOids>,
450 session_vars: BTreeMap<String, String>,
451 parameter_scopes: uqa_sql::semantics::parameters::ParameterScopes<state::RuntimeParameterValue>,
452 sequence_currvals: BTreeMap<RelationIdentity, SessionSequenceValue>,
453 last_sequence: Option<SessionLastSequenceReference>,
454 sequence_discard_generation: u64,
455 sql_statement_cache: SQLStatementCache,
456 portal_names: BTreeSet<String>,
458 listened_channels: Vec<String>,
459 authorization: uqa_sql::catalog::roles::session::SessionAuthorization,
460}
461
462#[derive(Clone)]
463struct GraphTransactionOverlay {
464 store: Arc<uqa_graph::PersistentGraphStore>,
465 names: Arc<BTreeSet<String>>,
466}
467
468#[derive(Clone, Copy, PartialEq, Eq)]
469enum PinnedPortalTransactionControl {
470 MakeHoldable,
471 Reject,
472}
473
474struct SessionPortalState {
475 data: SessionPortalData,
476 schema: uqa_sql::RowSchema,
477 transaction_origin: u64,
478 position: SessionPortalPosition,
479 scrollable: bool,
480 holdable: bool,
481 pinned_transaction_control: PinnedPortalTransactionControl,
483 pin_count: usize,
485 _registration: uqa_execution::statement::portal::PortalRegistration,
487}
488
489use uqa_execution::statement::portal::{SessionPortalCommandDeclaration, SessionPortalDeclaration};
490
491enum SessionPortalData {
492 Pending {
493 query: uqa_planner::QueryPlan,
494 params: Vec<SQLParam>,
495 table_snapshots: SessionPortalTableSnapshots,
496 view_snapshots: SessionPortalViewSnapshots,
497 sql_function_snapshots: SessionPortalSQLFunctionSnapshots,
498 catalog_snapshot: SessionPortalCatalogSnapshot,
499 restart: Option<SessionPortalRestart>,
500 },
501 PendingCommand {
502 command: Box<uqa_planner::CommandPlan>,
503 params: Vec<SQLParam>,
504 null_returning_values: bool,
506 },
507 Result(SQLResult),
508 Indexed(SessionPortalMaterialization),
509 Streaming {
510 worker: SessionPortalWorker,
511 materialized: Option<SessionPortalMaterialization>,
512 eof: bool,
513 restart: Option<SessionPortalRestart>,
514 },
515}
516
517struct SessionPortalRestart {
518 query: uqa_planner::QueryPlan,
519 params: Vec<SQLParam>,
520 table_snapshots: SessionPortalTableSnapshots,
521 view_snapshots: SessionPortalViewSnapshots,
522 sql_function_snapshots: SessionPortalSQLFunctionSnapshots,
523 catalog_snapshot: SessionPortalCatalogSnapshot,
524}
525
526struct SessionPortalMaterialization {
527 rows: uqa_execution::IndexedSpill,
528}
529
530use uqa_execution::statement::portal::worker::{
531 SessionPortalWorker, SessionPortalWorkerRequest, SessionPortalWorkerResponse,
532};
533
534#[derive(Debug, Clone, Copy, PartialEq, Eq)]
536enum SessionPortalPosition {
537 BeforeFirst,
538 OnRow(usize),
540 AfterLast(usize),
542}
543
544#[derive(Clone, Copy)]
545struct SessionRandomState {
546 s0: u64,
547 s1: u64,
548}
549
550impl Default for SessionRandomState {
551 fn default() -> Self {
552 Self {
553 s0: 0x5851_f42d_4c95_7f2d,
554 s1: 0x1405_7b7e_f767_814f,
555 }
556 }
557}
558
559#[derive(Clone)]
560struct EngineDataSnapshot {
561 tables: BTreeMap<RelationIdentity, TableDataSnapshot>,
562 durable: DurableCatalogSnapshot,
563 foreign_memory_tables: BTreeMap<RelationIdentity, Vec<uqa_sql::ResultRow>>,
564}
565
566#[derive(Clone)]
567#[expect(clippy::struct_excessive_bools, reason = "independent snapshot flags")]
568struct TableDataSnapshot {
569 state: Arc<TableState>,
570 security: state::BoundTableSecurity,
571 storage_generation: [u8; 16],
572 document_store: Arc<dyn DocumentStore>,
573 inverted_index: Arc<dyn InvertedIndex>,
574 vector_indexes: BTreeMap<FieldName, Arc<dyn VectorIndex>>,
575 value_indexes: BTreeMap<uqa_storage::ValueIndexKey, value_index::ValueIndexCache>,
576 fts_fields: Vec<FieldName>,
577 columns: Vec<uqa_sql::ast::ColumnDef>,
578 columns_declared: bool,
579 next_id: u128,
583 analyzer: Analyzer,
584 column_stats: uqa_planner::ColumnStatistics,
585 column_stats_loaded: bool,
586 column_stats_dirty: bool,
587 statistics_maintenance: statistics::MaintenanceState,
588 table_checks: Vec<uqa_sql::ast::TableCheck>,
589 foreign_keys: Vec<uqa_sql::ast::ForeignKey>,
590 key_constraints: Vec<uqa_sql::ast::TableKeyConstraint>,
591 hierarchy: uqa_sql::ast::TableHierarchy,
592 row_type_array_name: Option<String>,
593 dropped_attributes: Vec<uqa_sql::catalog::relation_attributes::DroppedAttribute>,
594 doc_count_cache: u64,
595 doc_count_dirty: bool,
596}
597
598pub(crate) struct TableState {
599 lifecycle_id: std::sync::atomic::AtomicU64,
601 object_id: [u8; 16],
603 security: state::CatalogCell<state::BoundTableSecurity>,
605 storage_generation: RwLock<[u8; 16]>,
607 pub(crate) document_store: RwLock<Box<dyn DocumentStore>>,
608 inverted_index: RwLock<Box<dyn InvertedIndex>>,
609 vector_indexes: RwLock<uqa_storage::vector_index::VectorIndexes>,
610 fts_fields: state::CatalogCell<Vec<FieldName>>,
611 columns: state::CatalogCell<Vec<uqa_sql::ast::ColumnDef>>,
613 columns_declared: state::CatalogCell<bool>,
614 next_id: parking_lot::Mutex<u128>,
616 maps_integer_keys: AtomicBool,
618 analyzer: state::CatalogCell<Analyzer>,
619 column_stats: state::CatalogCell<uqa_planner::ColumnStatistics>,
623 column_stats_loaded: AtomicBool,
624 column_stats_dirty: AtomicBool,
625 statistics_maintenance: parking_lot::Mutex<statistics::MaintenanceState>,
627 table_checks: state::CatalogCell<Vec<uqa_sql::ast::TableCheck>>,
630 foreign_keys: state::CatalogCell<Vec<uqa_sql::ast::ForeignKey>>,
633 key_constraints: state::CatalogCell<Vec<uqa_sql::ast::TableKeyConstraint>>,
636 hierarchy: state::CatalogCell<uqa_sql::ast::TableHierarchy>,
639 value_indexes: RwLock<BTreeMap<uqa_storage::ValueIndexKey, value_index::ValueIndexCache>>,
643 doc_count_cache: std::sync::atomic::AtomicU64,
648 doc_count_dirty: AtomicBool,
649 persistence: uqa_sql::ast::RelationPersistence,
651 on_commit: uqa_sql::ast::OnCommitAction,
652 catalog_oids: Option<uqa_sql::catalog::relation_oids::RelationCatalogOids>,
654 row_type_array_name: state::CatalogCell<Option<String>>,
655 dropped_attributes:
656 state::CatalogCell<Vec<uqa_sql::catalog::relation_attributes::DroppedAttribute>>,
657}
658
659impl TableState {
660 fn lifecycle_id(&self) -> u64 {
661 self.lifecycle_id.load(Ordering::Acquire)
662 }
663
664 fn storage_generation(&self) -> [u8; 16] {
665 *self.storage_generation.read()
666 }
667
668 fn object_id(&self) -> [u8; 16] {
669 self.object_id
670 }
671
672 fn recorded_catalog_oids(
673 &self,
674 ) -> Option<uqa_sql::catalog::relation_oids::RelationCatalogOids> {
675 self.catalog_oids
676 }
677
678 fn relation_oids(&self) -> uqa_sql::catalog::relation_oids::RelationCatalogOids {
680 self.catalog_oids.unwrap_or_else(|| {
681 uqa_sql::catalog::relation_oids::RelationCatalogOids::legacy(
682 uqa_sql::catalog::relation_oids::RelationOidKind::Table,
683 &self.object_id,
684 )
685 })
686 }
687
688 fn document_id_namespace(
690 &self,
691 ) -> uqa_storage::document_store::identifiers::DocumentIdNamespace {
692 uqa_storage::document_store::identifiers::DocumentIdNamespace {
693 object: self.object_id(),
694 generation: self.storage_generation(),
695 }
696 }
697
698 fn role_owner(&self) -> uqa_sql::catalog::roles::RoleIdentity {
699 self.security.read().role_owner
700 }
701
702 fn security(&self) -> state::BoundTableSecurity {
703 self.security.read().clone()
704 }
705
706 fn fts_fields(&self) -> Vec<FieldName> {
707 self.fts_fields.read().clone()
708 }
709}
710
711fn next_table_lifecycle_id() -> u64 {
712 static NEXT_TABLE_LIFECYCLE_ID: std::sync::atomic::AtomicU64 =
713 std::sync::atomic::AtomicU64::new(1);
714 uqa_core::atomic::try_update_u64(
715 &NEXT_TABLE_LIFECYCLE_ID,
716 Ordering::Relaxed,
717 Ordering::Relaxed,
718 |current| current.checked_add(1),
719 )
720 .expect("table lifecycle id space exhausted")
721}
722
723pub(crate) use uqa_execution::catalog::identity::new_nonzero_catalog_identity;
724
725fn new_table_object_id() -> StorageBackendResult<[u8; 16]> {
726 new_nonzero_catalog_identity("table", "object identity")
727}
728
729fn new_view_object_id() -> StorageBackendResult<[u8; 16]> {
730 new_nonzero_catalog_identity("view", "object identity")
731}
732
733fn new_table_storage_generation() -> StorageBackendResult<[u8; 16]> {
734 new_nonzero_catalog_identity("table", "storage generation")
735}
736
737fn new_sequence_object_id() -> StorageBackendResult<[u8; 16]> {
738 new_nonzero_catalog_identity("sequence", "object identity")
739}
740
741fn new_sequence_definition_generation() -> StorageBackendResult<[u8; 16]> {
742 new_nonzero_catalog_identity("sequence", "definition generation")
743}
744
745fn normalize_analyzer_config_value(value: &mut serde_json::Value) {
746 if let Some(tokenizer) = value.get_mut("tokenizer") {
747 if let Some(name) = tokenizer.as_str() {
748 *tokenizer = serde_json::json!({
749 "type": name.to_ascii_lowercase().replace('-', "_")
750 });
751 }
752 }
753 if let Some(filters) = value
754 .get_mut("token_filters")
755 .and_then(|v| v.as_array_mut())
756 {
757 for filter in filters {
758 if let Some(name) = filter.as_str() {
759 *filter = serde_json::json!({
760 "type": name.to_ascii_lowercase().replace('-', "_")
761 });
762 }
763 }
764 }
765}
766
767fn parse_analyzer_config(name: &str, config_json: &str) -> std::result::Result<Analyzer, String> {
768 let mut value: serde_json::Value = serde_json::from_str(config_json)
769 .map_err(|e| format!("analyzer `{name}` config is not valid JSON: {e}"))?;
770 normalize_analyzer_config_value(&mut value);
771 let analyzer: Analyzer = serde_json::from_value(value)
772 .map_err(|e| format!("analyzer `{name}` config is not a valid analyzer: {e}"))?;
773 analyzer
774 .validate()
775 .map_err(|e| format!("analyzer `{name}` config is invalid: {e}"))?;
776 Ok(analyzer)
777}
778
779fn normalize_analyzer_phase(phase: &str) -> std::result::Result<(String, AnalyzerPhase), String> {
780 let phase = AnalyzerPhase::parse(&phase.to_ascii_lowercase())?;
781 let normalized = match phase {
782 AnalyzerPhase::Index => "index",
783 AnalyzerPhase::Search => "search",
784 AnalyzerPhase::Both => "both",
785 };
786 Ok((normalized.to_string(), phase))
787}
788
789impl Drop for Engine {
790 fn drop(&mut self) {
791 if self.owns_session_registration {
792 let _ = self.close();
793 self.session.portals.lock().clear();
794 if let Some(backend) = self.storage.backend.as_ref() {
795 if backend.in_transaction() {
796 let _ = backend.rollback_transaction();
797 }
798 }
799 for frame in self.session.transactions.lock().iter() {
800 drop(frame.pending_notification_commit.lock().take());
801 }
802 self.row_locks.close_temporary_roles(self.session_id);
803 self.row_locks.release_session(self.session_id);
804 self.notification_hub.unregister(self.session_id);
805 self.release_automatic_statistics_client();
806 }
807 }
808}
809
810fn initial_random_state() -> SessionRandomState {
811 use std::sync::atomic::{AtomicU64, Ordering};
812
813 static NEXT_STATE: AtomicU64 = AtomicU64::new(0x4d59_5df4_d0f3_3173);
814 random_state_from_seed(NEXT_STATE.fetch_add(0x9e37_79b9_7f4a_7c15, Ordering::Relaxed))
815}
816
817fn random_state_from_seed(mut seed: u64) -> SessionRandomState {
818 let mut splitmix64 = || {
819 seed = seed.wrapping_add(0x9e37_79b9_7f4a_7c15);
820 let mut value = seed;
821 value = (value ^ (value >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
822 value = (value ^ (value >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
823 value ^ (value >> 31)
824 };
825 let state = SessionRandomState {
826 s0: splitmix64(),
827 s1: splitmix64(),
828 };
829 if state.s0 == 0 && state.s1 == 0 {
830 SessionRandomState::default()
831 } else {
832 state
833 }
834}
835
836#[derive(Debug, Clone)]
840pub struct HybridSearchParams<'a> {
841 pub table: &'a str,
842 pub text_field: &'a str,
843 pub text_query: &'a str,
844 pub vector_field: &'a str,
845 pub query_vector: Vec<f32>,
846 pub knn_pool: usize,
849 pub top_k: usize,
850}
851
852#[derive(Debug, Clone)]
856pub struct RobustHybridSearchParams<'a> {
857 pub table: &'a str,
858 pub text_field: &'a str,
859 pub text_query: &'a str,
860 pub vector_field: &'a str,
861 pub query_vector: Vec<f32>,
862 pub knn_pool: usize,
865 pub alpha: f64,
868 pub top_k: usize,
869}
870
871#[cfg(test)]
872mod tests;
873
874mod copy;
875
876use uqa_execution::catalog::sequence::session::{
877 NontransactionalSequenceValue, SessionLastSequenceReference, SessionSequenceCache,
878 SessionSequenceValue,
879};