vlorql 0.5.0

High-level SQL query optimization and LLM-powered natural language querying library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use crate::{DEFAULT_MAX_RETRIES, VlorQl};
use serde_json::json;
use std::path::PathBuf;
use std::sync::Arc;
#[cfg(feature = "vector-search")]
use std::sync::atomic::AtomicBool;
use vlorql_core::cache::{CompileCache, LlmResponseCache, PromptCache, SchemaCache};
use vlorql_core::compile::{RewriteEngine, SqlCompiler, get_compiler};
use vlorql_core::errors::{ConfigErrorKind, VlorQLError};
use vlorql_core::execute::DatabaseExecutor;
use vlorql_core::observability::{TelemetryGuard, VlorqMetrics, init_telemetry};
use vlorql_core::optimizer::QueryOptimizer;
use vlorql_core::policy::PolicyConfig;
use vlorql_core::schema::{
    ArcSchemaSnapshot, DialectProfile, IdentifierQuoting, SchemaSnapshot, SqlDialect,
};
use vlorql_core::statistics::StatisticsProvider;
use vlorql_llm::{LlmClient, LlmConfig, create_llm_client};

/// Builder for [`VlorQl`].
///
/// # Examples
///
/// ```
/// use vlorql::VlorQl;
/// use vlorql_core::schema::{SchemaSnapshot, TableSchema, ColumnSchema, DataType, SchemaMetadata};
/// use vlorql_core::policy::PolicyConfig;
/// use std::sync::Arc;
///
/// let schema = Arc::new(SchemaSnapshot::new(
///     vec![TableSchema {
///         name: "users".to_owned(),
///         columns: vec![ColumnSchema {
///             name: "id".to_owned(), data_type: DataType::Int,
///             nullable: false, description: None,
///             is_primary_key: true, foreign_key: None,
///         }],
///         description: None, primary_key: Some(vec!["id".to_owned()]),
///     }],
///     SchemaMetadata::default(),
/// ));
/// let builder = VlorQl::builder()
///     .with_schema(schema)
///     .with_dialect_name("sqlite")
///     .with_policy(PolicyConfig::default());
/// let vlorql = builder.build().expect("valid facade");
/// assert_eq!(vlorql.max_retries(), 3);
/// ```
pub struct VlorQlBuilder {
    schema: Option<ArcSchemaSnapshot>,
    dialect: Option<DialectProfile>,
    dialect_name: Option<String>,
    policy: PolicyConfig,
    compiler: Option<Box<dyn SqlCompiler>>,
    rewrite_engine: Option<RewriteEngine>,
    llm_client: Option<Box<dyn LlmClient>>,
    llm_config: Option<LlmConfig>,
    max_retries: usize,
    stats_provider: Option<Arc<dyn StatisticsProvider>>,
    schema_cache_config: Option<(u64, u64)>,
    compile_cache: Option<Arc<CompileCache>>,
    prompt_cache: Option<Arc<PromptCache>>,
    llm_cache: Option<LlmResponseCache>,
    telemetry_endpoint: Option<String>,
    telemetry_guard: Option<TelemetryGuard>,
    metrics: Option<Arc<VlorqMetrics>>,
    executor: Option<Arc<dyn DatabaseExecutor>>,
    /// Enables vector-based schema retrieval via Qdrant (default: false).
    vector_search: bool,
    /// Optional SchemaIndexer for semantic table/column search (requires `vector-search` feature).
    #[cfg(feature = "vector-search")]
    schema_indexer: Option<Arc<vlorql_core::prompt::schema_index::SchemaIndexer>>,
}

impl Default for VlorQlBuilder {
    fn default() -> Self {
        Self {
            schema: None,
            dialect: None,
            dialect_name: None,
            policy: PolicyConfig::default(),
            compiler: None,
            rewrite_engine: None,
            llm_client: None,
            llm_config: None,
            max_retries: DEFAULT_MAX_RETRIES,
            stats_provider: None,
            schema_cache_config: None,
            compile_cache: None,
            prompt_cache: None,
            llm_cache: None,
            telemetry_endpoint: None,
            telemetry_guard: None,
            metrics: None,
            executor: None,
            vector_search: false,
            #[cfg(feature = "vector-search")]
            schema_indexer: None,
        }
    }
}

impl VlorQlBuilder {
    /// Supplies the shared schema snapshot.
    #[must_use]
    pub fn with_schema(mut self, schema: Arc<SchemaSnapshot>) -> Self {
        self.schema = Some(schema);
        self
    }

    /// Supplies the complete dialect profile.
    #[must_use]
    pub fn with_dialect(mut self, dialect: DialectProfile) -> Self {
        self.dialect = Some(dialect);
        self.dialect_name = None;
        self
    }

    /// Supplies access-control policy configuration.
    #[must_use]
    pub fn with_policy(mut self, policy: PolicyConfig) -> Self {
        self.policy = policy;
        self
    }

    /// Supplies an LLM client. Any `LlmClient` implementation can be passed directly.
    #[must_use]
    pub fn with_llm_client<C>(mut self, client: C) -> Self
    where
        C: LlmClient + 'static,
    {
        self.llm_client = Some(Box::new(client));
        self
    }

    /// Builds an LLM client from an [`LlmConfig`] using the crate's factory.
    #[must_use]
    pub fn with_llm_config(mut self, config: LlmConfig) -> Self {
        self.llm_config = Some(config);
        self
    }

    /// Supplies a custom SQL compiler instead of the dialect default.
    #[must_use]
    pub fn with_compiler<C>(mut self, compiler: C) -> Self
    where
        C: SqlCompiler + 'static,
    {
        self.compiler = Some(Box::new(compiler));
        self
    }

    /// Supplies a [`RewriteEngine`] for post-compilation SQL rewrites.
    #[must_use]
    pub fn with_rewrite_engine(mut self, engine: RewriteEngine) -> Self {
        self.rewrite_engine = Some(engine);
        self
    }

    /// Selects a dialect by name and lets the builder create its compiler.
    ///
    /// Accepted names are `postgres`, `postgresql`, `sqlite`, and `mysql`
    /// (case-insensitive). Invalid names are reported by [`Self::build`].
    #[must_use]
    pub fn with_dialect_name(mut self, dialect: impl Into<String>) -> Self {
        self.dialect_name = Some(dialect.into());
        self.dialect = None;
        self
    }

    /// Sets the number of validation retries after the initial LLM attempt.
    #[must_use]
    pub fn with_max_retries(mut self, max_retries: usize) -> Self {
        self.max_retries = max_retries;
        self
    }

    /// Supplies a statistics provider used for cost-based query optimisation.
    ///
    /// When set, the built [`VlorQl`] facade will run the
    /// [`QueryOptimizer`] after
    /// validation succeeds, applying constant folding, predicate pushdown,
    /// column pruning, and (when statistics are available) cost-based join
    /// reordering.
    #[must_use]
    pub fn with_statistics_provider(mut self, provider: Arc<dyn StatisticsProvider>) -> Self {
        self.stats_provider = Some(provider);
        self
    }

    /// Configures a [`SchemaCache`] with the given capacity and TTL.
    ///
    /// Caches schema snapshots keyed by version + source to avoid
    /// re-parsing or re-fetching.
    #[must_use]
    pub fn with_schema_cache(mut self, capacity: u64, ttl_seconds: u64) -> Self {
        self.schema_cache_config = Some((capacity, ttl_seconds));
        self
    }

    /// Configures a [`CompileCache`] with the given weight limit and TTL.
    ///
    /// Caches compiled SQL results keyed by plan hash + dialect so that
    /// the same plan does not need to be re-compiled for the same dialect.
    #[must_use]
    pub fn with_compile_cache(mut self, max_size: u64, ttl_seconds: u64) -> Self {
        self.compile_cache = Some(Arc::new(CompileCache::new(max_size, ttl_seconds)));
        self
    }

    /// Configures a [`CompileCache`] with bincode disk persistence.
    ///
    /// The cache is loaded from `path` on startup (if the file exists) and
    /// persisted on shutdown (via [`VlorQl::drop`]). Uses bincode format.
    #[must_use]
    pub fn with_persistent_compile_cache(
        mut self,
        max_size: u64,
        ttl_seconds: u64,
        path: impl Into<PathBuf>,
    ) -> Self {
        let path = path.into();
        if let Ok(handle) = tokio::runtime::Handle::try_current() {
            let cache = handle.block_on(CompileCache::load(&path, max_size, ttl_seconds));
            self.compile_cache = Some(Arc::new(cache));
        } else {
            self.compile_cache = Some(Arc::new(CompileCache::with_persistence(
                max_size,
                ttl_seconds,
                path,
            )));
        }
        self
    }

    /// Configures a [`PromptCache`] with the given capacity and TTL.
    ///
    /// Caches system prompts keyed by schema version + dialect + policy
    /// hash, avoiding re-generation when the configuration has not
    /// changed.
    #[must_use]
    pub fn with_prompt_cache(mut self, capacity: u64, ttl_seconds: u64) -> Self {
        self.prompt_cache = Some(Arc::new(PromptCache::new(capacity, ttl_seconds)));
        self
    }

    /// Configures an [`LlmResponseCache`] with the given capacity and TTL.
    ///
    /// Caches LLM-generated [`QueryPlan`](vlorql_core::schema::QueryPlan) values keyed by question text,
    /// schema version, and model fingerprint, avoiding redundant LLM
    /// invocations for identical questions.
    #[must_use]
    pub fn with_llm_cache(mut self, max_entries: u64, ttl_seconds: u64) -> Self {
        self.llm_cache = Some(LlmResponseCache::new(max_entries, ttl_seconds));
        self
    }

    /// Configures OpenTelemetry tracing and metrics with the given OTLP
    /// endpoint (e.g. `http://localhost:4317`).
    ///
    /// The exporter is initialised immediately so that any subsequent
    /// operations (including build errors) can be traced. The
    /// [`TelemetryGuard`] is kept alive for the lifetime of the
    /// [`VlorQl`] facade and is shut down when the facade is dropped.
    #[must_use]
    pub fn with_telemetry(mut self, otlp_endpoint: String) -> Self {
        match init_telemetry("vlorql", &otlp_endpoint) {
            Ok(guard) => {
                self.telemetry_guard = Some(guard);
            }
            Err(e) => {
                tracing::warn!(error = %e, "failed to initialise OTLP telemetry; continuing without it");
            }
        }
        self.telemetry_endpoint = Some(otlp_endpoint);
        self
    }

    /// Supplies a [`VlorqMetrics`] handle for recording business metrics.
    ///
    /// The metrics are recorded at key points in the query pipeline
    /// (query count, duration, cache hits/misses, LLM latency, errors).
    #[must_use]
    pub fn with_metrics(mut self, metrics: Arc<VlorqMetrics>) -> Self {
        self.metrics = Some(metrics);
        self
    }

    /// Supplies a `DatabaseExecutor` that can run compiled SQL queries.
    ///
    /// When an executor is configured, you can use [`VlorQl::run`] to
    /// perform the full plan → validate → compile → execute pipeline
    /// in a single call.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use vlorql::execute::PgExecutor;
    /// use std::sync::Arc;
    ///
    /// let (client, connection) = tokio_postgres::connect(
    ///     "host=localhost user=postgres", tokio_postgres::NoTls,
    /// )
    /// .await
    /// .expect("connect");
    /// tokio::spawn(async move { connection.await.expect("connection") });
    ///
    /// let vlorql = VlorQl::builder()
    ///     .with_schema(schema)
    ///     .with_dialect_name("postgres")
    ///     .with_executor(Arc::new(PgExecutor::new(client)))
    ///     .build()
    ///     .expect("facade");
    /// ```
    #[must_use]
    pub fn with_executor(mut self, executor: Arc<dyn DatabaseExecutor>) -> Self {
        self.executor = Some(executor);
        self
    }

    /// Enables or disables vector-based schema retrieval via Qdrant.
    ///
    /// When enabled, the prompt builder will use semantic vector search
    /// to find relevant tables. Default is `false`.
    #[cfg(feature = "vector-search")]
    #[must_use]
    pub fn with_vector_search(mut self, enabled: bool) -> Self {
        self.vector_search = enabled;
        self
    }

    /// Supplies a SchemaIndexer for semantic schema retrieval via Qdrant.
    #[cfg(feature = "vector-search")]
    #[must_use]
    pub fn with_schema_indexer(
        mut self,
        indexer: Arc<vlorql_core::prompt::schema_index::SchemaIndexer>,
    ) -> Self {
        self.schema_indexer = Some(indexer);
        self
    }

    /// Builds the facade and verifies the required schema and dialect/compiler setup.
    pub fn build(self) -> Result<VlorQl, VlorQLError> {
        vlorql_core::observability::init_console_logging();
        let schema = self.schema.ok_or_else(|| {
            VlorQLError::config(
                ConfigErrorKind::MissingSchema,
                json!({"component": "schema"}),
            )
        })?;
        let dialect = match (self.dialect, self.dialect_name) {
            (Some(dialect), _) => dialect,
            (None, Some(name)) => parse_dialect_name(&name)?,
            (None, None) => {
                return Err(VlorQLError::config(
                    ConfigErrorKind::InvalidDialect {
                        dialect: "not configured".to_owned(),
                    },
                    json!({"component": "dialect"}),
                ));
            }
        };
        let compiler = self
            .compiler
            .unwrap_or_else(|| get_compiler(dialect.dialect));

        let llm_client = match (self.llm_client, self.llm_config) {
            (Some(client), _) => Some(Arc::from(client)),
            (None, Some(config)) => Some(Arc::from(create_llm_client(config)?)),
            (None, None) => None,
        };

        let optimizer = self.stats_provider.map(QueryOptimizer::new);

        // Build the schema cache (deferred so metrics — if any — are available).
        let schema_cache = self.schema_cache_config.map(|(capacity, ttl_seconds)| {
            Arc::new(SchemaCache::new(
                capacity,
                ttl_seconds,
                self.metrics.clone(),
            ))
        });

        let vector_search = self.vector_search;

        Ok(VlorQl {
            schema,
            dialect,
            policy: self.policy,
            compiler: Arc::from(compiler),
            rewrite_engine: self.rewrite_engine,
            llm_client,
            max_retries: self.max_retries,
            optimizer,
            schema_cache,
            compile_cache: self.compile_cache,
            prompt_cache: self.prompt_cache,
            llm_cache: self
                .llm_cache
                .unwrap_or_else(|| LlmResponseCache::new(1000, 3600)),
            telemetry_guard: self.telemetry_guard,
            metrics: self.metrics,
            executor: self.executor,
            vector_search,
            #[cfg(feature = "vector-search")]
            schema_indexer: self.schema_indexer,
            #[cfg(feature = "vector-search")]
            schema_indexed: AtomicBool::new(false),
        })
    }
}

fn parse_dialect_name(name: &str) -> Result<DialectProfile, VlorQLError> {
    let normalized = name.trim().to_ascii_lowercase();
    let dialect = match normalized.as_str() {
        "postgres" | "postgresql" => SqlDialect::Postgres,
        "sqlite" => SqlDialect::Sqlite,
        "mysql" | "my_sql" => SqlDialect::MySql,
        _ => {
            return Err(VlorQLError::config(
                ConfigErrorKind::InvalidDialect {
                    dialect: name.to_owned(),
                },
                json!({"accepted": ["postgres", "sqlite", "mysql"]}),
            ));
        }
    };

    let quote_style = if dialect == SqlDialect::MySql {
        IdentifierQuoting::Backtick
    } else {
        IdentifierQuoting::DoubleQuote
    };
    Ok(DialectProfile {
        dialect,
        quote_style,
        ..DialectProfile::default()
    })
}