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
441
442
//! Aggregate and window query execution runner.
use std::sync::Arc;
use super::super::context::ExecutorContext;
use crate::{
db::{WhereClause, traits::DatabaseAdapter},
error::{FraiseQLError, Result},
runtime::suggest_similar,
security::{RlsWhereClause, SecurityContext},
};
/// Runner for aggregate and window analytics queries.
pub(in super::super) struct AggregateRunner<A: DatabaseAdapter> {
ctx: Arc<ExecutorContext<A>>,
}
impl<A: DatabaseAdapter> AggregateRunner<A> {
pub(in super::super) const fn new(ctx: Arc<ExecutorContext<A>>) -> Self {
Self { ctx }
}
/// Resolve configured session variables for `security_context` into owned
/// `(name, value)` pairs, for passing to the connection-affine
/// `*_with_session` adapter methods so `current_setting()`-backed RLS on
/// aggregate views is effective (#329).
fn resolve_session_vars(
&self,
security_context: Option<&SecurityContext>,
) -> Result<Vec<(String, String)>> {
let sv = &self.ctx.schema.session_variables;
match security_context {
Some(sec) if !sv.variables.is_empty() || sv.inject_started_at => {
crate::runtime::executor::security::resolve_session_variables(sv, sec)
},
_ => Ok(Vec::new()),
}
}
/// Execute an aggregate query dispatch.
///
/// # Errors
///
/// * [`FraiseQLError::Validation`] — the query name does not end with `_aggregate`, or the
/// derived fact table is not found in the compiled schema.
/// * Propagates errors from [`execute_aggregate_query`](Self::execute_aggregate_query).
pub(in super::super) async fn execute_aggregate_dispatch(
&self,
query_name: &str,
variables: Option<&serde_json::Value>,
security_context: Option<&SecurityContext>,
) -> Result<serde_json::Value> {
// Extract table name from query name (e.g., "sales_aggregate" -> "tf_sales")
let table_name =
query_name.strip_suffix("_aggregate").ok_or_else(|| FraiseQLError::Validation {
message: format!("Invalid aggregate query name: {}", query_name),
path: None,
})?;
let fact_table_name = format!("tf_{}", table_name);
// Get fact table metadata from schema
let metadata = self.ctx.schema.get_fact_table(&fact_table_name).ok_or_else(|| {
let known: Vec<&str> = self.ctx.schema.list_fact_tables();
let suggestion = suggest_similar(&fact_table_name, &known);
let base = format!("Fact table '{}' not found in schema", fact_table_name);
let message = match suggestion.as_slice() {
[s] => format!("{base}. Did you mean '{s}'?"),
_ => base,
};
FraiseQLError::Validation {
message,
path: Some(format!("fact_tables.{}", fact_table_name)),
}
})?;
// Parse query variables into aggregate query JSON
let empty_json = serde_json::json!({});
let query_json = variables.unwrap_or(&empty_json);
// Execute aggregate query
self.execute_aggregate_query(query_json, query_name, metadata, security_context)
.await
}
/// Execute a window query dispatch.
///
/// # Errors
///
/// * [`FraiseQLError::Validation`] — the query name does not end with `_window`, or the derived
/// fact table is not found in the compiled schema.
/// * Propagates errors from [`execute_window_query`](Self::execute_window_query).
pub(in super::super) async fn execute_window_dispatch(
&self,
query_name: &str,
variables: Option<&serde_json::Value>,
security_context: Option<&SecurityContext>,
) -> Result<serde_json::Value> {
// Extract table name from query name (e.g., "sales_window" -> "tf_sales")
let table_name =
query_name.strip_suffix("_window").ok_or_else(|| FraiseQLError::Validation {
message: format!("Invalid window query name: {}", query_name),
path: None,
})?;
let fact_table_name = format!("tf_{}", table_name);
// Get fact table metadata from schema
let metadata = self.ctx.schema.get_fact_table(&fact_table_name).ok_or_else(|| {
let known: Vec<&str> = self.ctx.schema.list_fact_tables();
let suggestion = suggest_similar(&fact_table_name, &known);
let base = format!("Fact table '{}' not found in schema", fact_table_name);
let message = match suggestion.as_slice() {
[s] => format!("{base}. Did you mean '{s}'?"),
_ => base,
};
FraiseQLError::Validation {
message,
path: Some(format!("fact_tables.{}", fact_table_name)),
}
})?;
// Parse query variables into window query JSON
let empty_json = serde_json::json!({});
let query_json = variables.unwrap_or(&empty_json);
// Execute window query
self.execute_window_query(query_json, query_name, metadata, security_context)
.await
}
/// Execute an aggregate query.
///
/// # Arguments
///
/// * `query_json` - JSON representation of the aggregate query
/// * `query_name` - GraphQL field name (e.g., "`sales_aggregate`")
/// * `metadata` - Fact table metadata
///
/// # Returns
///
/// GraphQL response as JSON string
///
/// When `security_context` is `Some`, evaluates the configured RLS policy and
/// AND-composes the resulting WHERE clause with the user-supplied WHERE before
/// planning. RLS conditions are always placed first so they cannot be bypassed.
///
/// # Errors
///
/// Returns error if:
/// - RLS policy evaluation fails
/// - Query parsing fails
/// - Execution plan generation fails
/// - SQL generation fails
/// - Database execution fails
/// - Result projection fails
///
/// # Example
///
/// ```no_run
/// // Requires: a live database adapter and compiled fact table metadata.
/// // See: tests/integration/ for runnable examples.
/// # use serde_json::json;
/// let query_json = json!({
/// "table": "tf_sales",
/// "groupBy": { "category": true },
/// "aggregates": [{"count": {}}]
/// });
/// // let result = executor.execute_aggregate_query(&query_json, "sales_aggregate", &metadata).await?;
/// ```
pub(in super::super) async fn execute_aggregate_query(
&self,
query_json: &serde_json::Value,
query_name: &str,
metadata: &crate::compiler::fact_table::FactTableMetadata,
security_context: Option<&SecurityContext>,
) -> Result<serde_json::Value> {
// 1. Parse JSON query into AggregationRequest. Build native_columns from
// denormalized_filters so the parser can emit direct column references instead of JSONB
// extraction for native columns.
let native_columns = crate::runtime::native_columns::filter_columns_to_native_map(
&metadata.denormalized_filters,
);
let mut request =
crate::runtime::AggregateQueryParser::parse(query_json, metadata, &native_columns)?;
// 1b. Evaluate RLS policy and compose with user-supplied WHERE.
// RLS WHERE is always AND-composed first so it cannot be bypassed.
if let Some(ctx) = security_context {
let rls_where: Option<RlsWhereClause> =
if let Some(ref policy) = self.ctx.config.rls_policy {
policy.evaluate(ctx, &request.table_name)?
} else {
None
};
request.where_clause = match (
rls_where.map(RlsWhereClause::into_where_clause),
request.where_clause.take(),
) {
(Some(rls), Some(user)) => Some(WhereClause::And(vec![rls, user])),
(Some(rls), None) => Some(rls),
(None, user) => user,
};
}
// 2. Check partial-period dispatch — if conditions are met, generate UNION ALL SQL instead
// of a single SELECT.
let today = chrono::Utc::now().date_naive();
if let Some((lower_bound, pp_config)) =
crate::runtime::partial_period::should_use_partial_period(
metadata,
request.where_clause.as_ref(),
today,
)
{
return self
.execute_partial_period_aggregate(
&request,
metadata,
pp_config,
lower_bound,
today,
query_name,
)
.await;
}
// 3. Standard path: generate execution plan
let plan =
crate::compiler::aggregation::AggregationPlanner::plan(request, metadata.clone())?;
// 4. Generate parameterized SQL
let sql_generator =
crate::runtime::AggregationSqlGenerator::new(self.ctx.adapter.database_type());
let parameterized = sql_generator.generate_parameterized(&plan)?;
// 5. Execute with bind parameters (eliminates escape-based injection risk), pinning session
// variables to the connection for current_setting() RLS (#329).
let resolved_session_vars = self.resolve_session_vars(security_context)?;
let session_pairs: Vec<(&str, &str)> =
resolved_session_vars.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect();
let rows = self
.ctx
.adapter
.execute_parameterized_aggregate_with_session(
¶meterized.sql,
¶meterized.params,
&session_pairs,
)
.await?;
// 6. Project results
let projected = crate::runtime::AggregationProjector::project(rows, &plan)?;
// 7. Wrap in GraphQL data envelope
let response =
crate::runtime::AggregationProjector::wrap_in_data_envelope(projected, query_name);
// 8. Serialize to JSON string
Ok(response)
}
/// Execute an aggregate query via partial-period UNION ALL.
///
/// Generates a UNION ALL query combining fine-grain and coarse-grain branches,
/// then executes and projects the result identically to the standard path.
///
/// # Errors
///
/// Returns error if plan generation, SQL generation, or database execution fails.
#[allow(clippy::too_many_arguments)] // Reason: all arguments are semantically required
async fn execute_partial_period_aggregate(
&self,
request: &crate::compiler::aggregation::AggregationRequest,
metadata: &crate::compiler::fact_table::FactTableMetadata,
config: &crate::compiler::fact_table::PartialPeriodConfig,
lower_bound: chrono::NaiveDate,
today: chrono::NaiveDate,
query_name: &str,
) -> Result<serde_json::Value> {
let branch_plan = crate::runtime::partial_period::determine_branches(
lower_bound,
config.time_grain_trunc,
today,
);
// Split the WHERE clause to separate the date condition from the rest
let extra_where = request
.where_clause
.as_ref()
.and_then(|wc| {
crate::runtime::partial_period::split_where_clause(wc, &config.time_grain_column)
})
.and_then(|split| split.remaining);
// Generate execution plan (for GROUP BY / aggregate expression resolution)
let plan = crate::compiler::aggregation::AggregationPlanner::plan(
request.clone(),
metadata.clone(),
)?;
// Generate UNION ALL SQL
let sql_generator =
crate::runtime::AggregationSqlGenerator::new(self.ctx.adapter.database_type());
let union_sql = sql_generator.generate_partial_period(
&plan,
config,
&branch_plan,
extra_where.as_ref(),
)?;
// Execute.
// No session vars: the partial-period UNION branch does not thread a
// SecurityContext (callers above do), so there is nothing to resolve
// session variables from here. RLS for partial-period aggregates over
// current_setting()-backed views is a follow-up (#329).
let rows = self
.ctx
.adapter
.execute_parameterized_aggregate(&union_sql.sql, &union_sql.params)
.await?;
// Project and wrap (same as standard path)
let projected = crate::runtime::AggregationProjector::project(rows, &plan)?;
let response =
crate::runtime::AggregationProjector::wrap_in_data_envelope(projected, query_name);
Ok(response)
}
/// Execute a window query.
///
/// # Arguments
///
/// * `query_json` - JSON representation of the window query
/// * `query_name` - GraphQL field name (e.g., "`sales_window`")
/// * `metadata` - Fact table metadata
///
/// # Returns
///
/// GraphQL response as JSON string
///
/// When `security_context` is `Some`, evaluates the configured RLS policy and
/// AND-composes the resulting WHERE clause with the user-supplied WHERE before
/// planning. RLS conditions are always placed first so they cannot be bypassed.
///
/// # Errors
///
/// Returns error if:
/// - RLS policy evaluation fails
/// - Query parsing fails
/// - Execution plan generation fails
/// - SQL generation fails
/// - Database execution fails
/// - Result projection fails
///
/// # Example
///
/// ```no_run
/// // Requires: a live database adapter and compiled fact table metadata.
/// // See: tests/integration/ for runnable examples.
/// # use serde_json::json;
/// let query_json = json!({
/// "table": "tf_sales",
/// "select": [{"type": "measure", "name": "revenue", "alias": "revenue"}],
/// "windows": [{
/// "function": {"type": "row_number"},
/// "alias": "rank",
/// "partitionBy": [{"type": "dimension", "path": "category"}],
/// "orderBy": [{"field": "revenue", "direction": "DESC"}]
/// }]
/// });
/// // let result = executor.execute_window_query(&query_json, "sales_window", &metadata).await?;
/// ```
pub(in super::super) async fn execute_window_query(
&self,
query_json: &serde_json::Value,
query_name: &str,
metadata: &crate::compiler::fact_table::FactTableMetadata,
security_context: Option<&SecurityContext>,
) -> Result<serde_json::Value> {
// 1. Parse JSON query into WindowRequest
let mut request = crate::runtime::WindowQueryParser::parse(query_json, metadata)?;
// 1b. Evaluate RLS policy and compose with user-supplied WHERE.
// RLS WHERE is always AND-composed first so it cannot be bypassed.
if let Some(ctx) = security_context {
let rls_where: Option<RlsWhereClause> =
if let Some(ref policy) = self.ctx.config.rls_policy {
policy.evaluate(ctx, &request.table_name)?
} else {
None
};
request.where_clause = match (
rls_where.map(RlsWhereClause::into_where_clause),
request.where_clause.take(),
) {
(Some(rls), Some(user)) => Some(WhereClause::And(vec![rls, user])),
(Some(rls), None) => Some(rls),
(None, user) => user,
};
}
// 2. Generate execution plan (validates semantic names against metadata)
let plan = crate::compiler::window_functions::WindowPlanner::plan(request, metadata)?;
// 3. Generate SQL
let sql_generator =
crate::runtime::WindowSqlGenerator::new(self.ctx.adapter.database_type());
let sql = sql_generator.generate(&plan)?;
// 4. Execute SQL — bind parameters via execute_parameterized_aggregate so WHERE clause
// values are passed as prepared-statement parameters, not inlined. Session variables are
// pinned to the connection for current_setting() RLS (#329).
let resolved_session_vars = self.resolve_session_vars(security_context)?;
let session_pairs: Vec<(&str, &str)> =
resolved_session_vars.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect();
let rows = self
.ctx
.adapter
.execute_parameterized_aggregate_with_session(
&sql.raw_sql,
&sql.parameters,
&session_pairs,
)
.await?;
// 5. Project results
let projected = crate::runtime::WindowProjector::project(rows, &plan)?;
// 6. Wrap in GraphQL data envelope
let response =
crate::runtime::WindowProjector::wrap_in_data_envelope(projected, query_name);
// 7. Serialize to JSON string
Ok(response)
}
}
#[cfg(test)]
#[path = "aggregate_tests.rs"]
mod aggregate_rls_tests;