use std::collections::HashMap;
use std::sync::Mutex;
use async_trait::async_trait;
use futures::stream::{self, Stream};
use serde_json::json;
use vlorql::VlorQl;
use vlorql_core::errors::{LlmErrorKind, VlorQLError};
use vlorql_core::policy::{PolicyConfig, RowFilter, TablePolicy};
use vlorql_core::schema::{
ColumnSchema, ComparisonOperator, DataType, Expression, FromClause, Predicate, Projection,
QueryPlan, SchemaMetadata, SchemaSnapshot, TableSchema,
};
use vlorql_llm::{LlmClient, LlmConfig, LlmProvider};
pub fn column(name: &str, data_type: DataType) -> ColumnSchema {
ColumnSchema {
name: name.to_owned(),
data_type,
nullable: false,
description: None,
is_primary_key: name == "id",
foreign_key: None,
}
}
pub fn snapshot() -> std::sync::Arc<SchemaSnapshot> {
std::sync::Arc::new(SchemaSnapshot::new(
vec![
TableSchema {
name: "users".to_owned(),
columns: vec![
column("id", DataType::Int),
column("name", DataType::String),
column("email", DataType::String),
column("tenant_id", DataType::Int),
column("active", DataType::Boolean),
],
description: Some("Application users".to_owned()),
primary_key: Some(vec!["id".to_owned()]),
},
TableSchema {
name: "orders".to_owned(),
columns: vec![
column("id", DataType::Int),
column("owner_id", DataType::Int),
column("total", DataType::Float),
],
description: Some("Customer orders".to_owned()),
primary_key: Some(vec!["id".to_owned()]),
},
],
SchemaMetadata {
version: Some("1.0".to_owned()),
source: Some("integration-tests".to_owned()),
generated_at: Some("2026-01-01T00:00:00Z".to_owned()),
},
))
}
pub fn base_plan() -> QueryPlan {
QueryPlan {
select: vec![Projection::Column {
table: Some("users".to_owned()),
column: "id".to_owned(),
alias: None,
}],
from: FromClause {
table: "users".to_owned(),
alias: Some("t1".to_owned()),
},
r#where: None,
group_by: None,
having: None,
order_by: None,
limit: None,
offset: None,
joins: None,
ctes: None,
distinct: false,
distinct_on: None,
set_operation: None, }
}
pub fn open_policy() -> PolicyConfig {
PolicyConfig::default()
}
pub fn strict_policy() -> PolicyConfig {
PolicyConfig {
table_policies: HashMap::from([
(
"users".to_owned(),
TablePolicy {
allowed: true,
allowed_columns: Some(vec!["id".to_owned(), "name".to_owned()]),
denied_columns: Vec::new(),
row_filter: None,
},
),
(
"orders".to_owned(),
TablePolicy {
allowed: false,
allowed_columns: None,
denied_columns: Vec::new(),
row_filter: None,
},
),
]),
global_denied_columns: vec!["email".to_owned()],
row_filters: Vec::new(),
}
}
pub fn row_filter_policy() -> PolicyConfig {
PolicyConfig {
table_policies: HashMap::from([(
"users".to_owned(),
TablePolicy {
allowed: true,
allowed_columns: None,
denied_columns: Vec::new(),
row_filter: Some(RowFilter {
condition: Predicate::Comparison {
left: Expression::ColumnRef {
table: Some("users".to_owned()),
column: "id".to_owned(),
},
op: ComparisonOperator::Gt,
right: Expression::Literal {
value: json!(10),
data_type: DataType::Int,
},
},
description: "Only expose users with id greater than 10".to_owned(),
}),
},
)]),
global_denied_columns: Vec::new(),
row_filters: Vec::new(),
}
}
pub fn facade_with(plan: QueryPlan, policy: PolicyConfig, dialect: &str) -> VlorQl {
VlorQl::builder()
.with_schema(snapshot())
.with_dialect_name(dialect)
.with_policy(policy)
.with_llm_client(sequence_client(vec![plan]))
.build()
.expect("facade should build")
}
#[allow(dead_code)]
pub fn facade_with_sequence(plans: Vec<QueryPlan>, policy: PolicyConfig, dialect: &str) -> VlorQl {
VlorQl::builder()
.with_schema(snapshot())
.with_dialect_name(dialect)
.with_policy(policy)
.with_llm_client(sequence_client(plans))
.build()
.expect("facade should build")
}
pub fn facade_with_streaming_chunks(
chunks: Vec<String>,
policy: PolicyConfig,
dialect: &str,
) -> VlorQl {
VlorQl::builder()
.with_schema(snapshot())
.with_dialect_name(dialect)
.with_policy(policy)
.with_llm_client(streaming_client(chunks))
.build()
.expect("facade should build")
}
#[derive(Debug)]
pub struct SequenceMockClient {
plans: Mutex<Vec<QueryPlan>>,
config: LlmConfig,
}
impl SequenceMockClient {
pub fn new(plans: Vec<QueryPlan>) -> Self {
let config = LlmConfig {
provider: LlmProvider::OpenAi,
model: "mock-sequence".to_owned(),
..LlmConfig::default()
};
Self {
plans: Mutex::new(plans),
config,
}
}
}
#[async_trait]
impl LlmClient for SequenceMockClient {
async fn generate_plan(
&self,
_question: &str,
_system_prompt: &str,
) -> Result<QueryPlan, VlorQLError> {
self.plans
.lock()
.expect("sequence lock should not be poisoned")
.pop()
.ok_or_else(|| {
VlorQLError::llm(
LlmErrorKind::ParseError {
details: "sequence exhausted".to_owned(),
},
json!({"source": "sequence_mock"}),
)
})
}
async fn stream_plan(
&self,
_question: String,
_system_prompt: String,
) -> Result<Box<dyn Stream<Item = Result<String, VlorQLError>> + Send + Unpin>, VlorQLError>
{
let plan = self
.generate_plan("", "")
.await
.expect("sequence client should yield a plan");
let serialized = serde_json::to_string(&plan).unwrap_or_default();
Ok(Box::new(stream::iter(vec![Ok(serialized)])))
}
fn provider(&self) -> LlmProvider {
self.config.provider
}
fn config(&self) -> &LlmConfig {
&self.config
}
}
pub fn sequence_client(plans: Vec<QueryPlan>) -> SequenceMockClient {
SequenceMockClient::new(plans)
}
#[derive(Debug)]
pub struct StreamingMockClient {
chunks: Vec<String>,
config: LlmConfig,
}
impl StreamingMockClient {
pub fn new(chunks: Vec<String>) -> Self {
let config = LlmConfig {
provider: LlmProvider::OpenAi,
model: "mock-streaming".to_owned(),
..LlmConfig::default()
};
Self { chunks, config }
}
}
#[async_trait]
impl LlmClient for StreamingMockClient {
async fn generate_plan(
&self,
_question: &str,
_system_prompt: &str,
) -> Result<QueryPlan, VlorQLError> {
let combined = self.chunks.concat();
serde_json::from_str(&combined).map_err(|error| {
VlorQLError::llm(
LlmErrorKind::ParseError {
details: format!("streaming client cannot decode its own chunks: {error}"),
},
json!({"chunks": self.chunks.len()}),
)
})
}
async fn stream_plan(
&self,
_question: String,
_system_prompt: String,
) -> Result<Box<dyn Stream<Item = Result<String, VlorQLError>> + Send + Unpin>, VlorQLError>
{
let chunks = self.chunks.clone();
let stream = stream::iter(chunks.into_iter().map(Ok::<String, VlorQLError>));
Ok(Box::new(Box::pin(stream)))
}
fn provider(&self) -> LlmProvider {
self.config.provider
}
fn config(&self) -> &LlmConfig {
&self.config
}
}
pub fn streaming_client(chunks: Vec<String>) -> StreamingMockClient {
StreamingMockClient::new(chunks)
}
pub fn chunked_plan_payload(plan: &QueryPlan) -> Vec<String> {
let serialized = serde_json::to_string(plan).expect("plan should serialize");
let total = serialized.len();
let first_break = total / 3;
let second_break = total * 2 / 3;
vec![
serialized[..first_break].to_owned(),
serialized[first_break..second_break].to_owned(),
serialized[second_break..].to_owned(),
]
}