1use serde_json::{json, Map, Value};
4
5use crate::bson::id_key;
6use crate::computes::{collect_field_deps, merge_depends_into_ast, InjectInfo};
7use crate::permission::{can_read_schema, is_relation_readable, merge_owner_condition, Context};
8use crate::pipeline::{
9 build_pipeline, build_projection, flatten_object_fields, is_nullish, param, parse_gql, Ast,
10 RelAst, REL_PRED_PREFIX,
11};
12use crate::schema::{Registry, Schema};
13use crate::types::is_truthy;
14
15use super::cmd::{cmd_aggregate, cmd_find, num_value, to_number};
16use super::{ensure_context, ERR_PERMISSION, MAX_PAGE_SIZE, PHASE1_IDS};
17
18#[derive(Debug, Clone, Copy, PartialEq)]
19pub struct Page {
20 pub page: f64,
21 pub page_size: f64,
22}
23
24impl Page {
25 pub fn to_value(&self) -> Value {
26 json!({
27 "page": num_value(self.page),
28 "pageSize": num_value(self.page_size),
29 })
30 }
31
32 pub fn has_more(&self, total: f64) -> bool {
34 (self.page + 1.0) * self.page_size < total
35 }
36}
37
38pub fn resolve_page(ast: &Ast, params: &Map<String, Value>) -> Page {
40 let (page, page_size) = if params.contains_key("page") || params.contains_key("pageSize") {
41 let page = match params.get("page").filter(|v| !v.is_null()) {
42 Some(v) => to_number(Some(v)).max(0.0).floor(),
43 None => 0.0,
44 };
45 let page_size = match params.get("pageSize").filter(|v| !v.is_null()) {
46 Some(v) => to_number(Some(v)),
47 None => 50.0,
48 };
49 (page, page_size)
50 } else {
51 let skip_val = param(params, ast.params.get("skip"));
52 let limit_val = param(params, ast.params.get("limit"));
53 let page = if !is_nullish(skip_val) && is_truthy(limit_val.unwrap_or(&Value::Null)) {
54 (to_number(skip_val) / to_number(limit_val)).floor()
55 } else {
56 0.0
57 };
58 let page_size = if is_nullish(limit_val) {
59 50.0
60 } else {
61 to_number(limit_val)
62 };
63 (page, page_size)
64 };
65
66 Page {
67 page,
68 page_size: if page_size < MAX_PAGE_SIZE {
69 page_size
70 } else {
71 MAX_PAGE_SIZE
72 },
73 }
74}
75
76#[derive(Debug, Clone, Copy, PartialEq)]
78pub enum Mode {
79 Find,
81 TwoPhase,
83 Aggregate,
85}
86
87impl Mode {
88 pub fn as_str(self) -> &'static str {
89 match self {
90 Mode::Find => "find",
91 Mode::TwoPhase => "two_phase",
92 Mode::Aggregate => "aggregate",
93 }
94 }
95}
96
97#[derive(Debug, Clone)]
98pub struct QueryPlan {
99 pub collection: String,
100 pub mode: Mode,
101 pub commands: Vec<Value>,
102 pub postprocess: Option<Value>,
104 pub sort: Option<Value>,
106}
107
108impl QueryPlan {
109 pub fn to_value(&self) -> Value {
110 json!({
111 "collection": self.collection,
112 "mode": self.mode.as_str(),
113 "commands": self.commands,
114 "postprocess": self.postprocess.clone().unwrap_or(Value::Null),
115 "sort": self.sort.clone().unwrap_or(Value::Null),
116 })
117 }
118}
119
120fn postprocess_value(ast: &Ast, inject: &InjectInfo) -> Value {
121 json!({
122 "ast": ast.to_value(),
123 "inject": if inject.is_empty() { Value::Null } else { inject.to_value() },
124 })
125}
126
127pub fn plan_query(
129 gql: &str,
130 params: &Map<String, Value>,
131 registry: &Registry,
132 ctx: Option<&Context>,
133) -> Result<QueryPlan, String> {
134 let mut params = params.clone();
135 plan_query_mut(gql, &mut params, registry, ctx)
136}
137
138pub fn plan_query_mut(
140 gql: &str,
141 params: &mut Map<String, Value>,
142 registry: &Registry,
143 ctx: Option<&Context>,
144) -> Result<QueryPlan, String> {
145 let ast = parse_gql(gql)?;
146 plan_query_ast_mut(ast, params, registry, ctx)
147}
148
149pub fn plan_query_one(
155 gql: &str,
156 params: &Map<String, Value>,
157 registry: &Registry,
158 ctx: Option<&Context>,
159) -> Result<QueryPlan, String> {
160 let mut params = params.clone();
161 let mut ast = parse_gql(gql)?;
162 if !ast.params.contains_key("limit") {
163 ast.params
164 .insert("limit".to_string(), "@__core_one_limit__".to_string());
165 params.insert("__core_one_limit__".to_string(), json!(1));
166 }
167 plan_query_ast_mut(ast, &mut params, registry, ctx)
168}
169
170pub fn plan_query_ast_mut(
176 mut ast: Ast,
177 params: &mut Map<String, Value>,
178 registry: &Registry,
179 ctx: Option<&Context>,
180) -> Result<QueryPlan, String> {
181 ensure_context(registry, ctx)?;
182 let schema = registry.get(&ast.model)?;
183
184 if ctx.is_some() && !can_read_schema(schema, ctx) {
185 return Err(ERR_PERMISSION.to_string());
186 }
187
188 if ctx.is_some() {
190 if let Some(r) = ast.params.get("condition").cloned() {
191 let key = r.get(1..).unwrap_or("").to_string();
192 match merge_owner_condition(schema, ctx, params.get(&key).cloned()) {
193 Some(v) => {
194 params.insert(key, v);
195 }
196 None => {
197 params.remove(&key);
198 }
199 }
200 } else if let Some(owner) = merge_owner_condition(schema, ctx, None) {
201 ast.params
203 .insert("condition".to_string(), "@__core_owner__".to_string());
204 params.insert("__core_owner__".to_string(), owner);
205 }
206 }
207
208 let mut inject = merge_depends_into_ast(&mut ast.relations, schema)?;
209 inject.fields = collect_field_deps(&mut ast.fields, schema);
212
213 let mut post_ast = ast.clone();
215 flatten_object_fields(&mut post_ast, schema);
216
217 build_plan(ast, &post_ast, &inject, params, registry, ctx)
218}
219
220pub fn check_readable_relations(
227 relations: &[(String, RelAst)],
228 schema: &Schema,
229 registry: &Registry,
230 ctx: Option<&Context>,
231) -> Result<(), String> {
232 if ctx.is_none() {
233 return Ok(());
234 }
235 for (rel_name, rel_ast) in relations {
236 let Some(rel_def) = schema.relations.get(rel_name) else {
237 continue;
238 };
239 if rel_def.model.is_empty() {
241 continue;
242 }
243 if !is_relation_readable(schema, ctx, rel_name) {
244 return Err(ERR_PERMISSION.to_string());
245 }
246 let rel_schema = registry.get(&rel_def.model)?;
247 if !can_read_schema(rel_schema, ctx) {
248 return Err(ERR_PERMISSION.to_string());
249 }
250 check_readable_relations(&rel_ast.relations, rel_schema, registry, ctx)?;
251 }
252 Ok(())
253}
254
255pub fn build_plan(
261 mut fetch_ast: Ast,
262 post_ast: &Ast,
263 inject: &InjectInfo,
264 params: &Map<String, Value>,
265 registry: &Registry,
266 ctx: Option<&Context>,
267) -> Result<QueryPlan, String> {
268 ensure_context(registry, ctx)?;
269 let schema = registry.get(&fetch_ast.model)?;
270
271 check_readable_relations(&fetch_ast.relations, schema, registry, ctx)?;
274
275 let pipeline = build_pipeline(&mut fetch_ast, params, registry, ctx)?;
276 let stages: Vec<Value> = pipeline.as_array().cloned().unwrap_or_default();
277
278 let grouped = stages.iter().any(|s| s.get("$group").is_some());
282
283 let projection = if grouped {
284 None
285 } else {
286 build_projection(&fetch_ast, schema, ctx)
287 };
288
289 let collection = schema.collection.clone();
290 let post = || {
291 if grouped {
292 None
293 } else {
294 Some(postprocess_value(post_ast, inject))
295 }
296 };
297
298 if stages.len() == 1 {
300 if let Some(filter) = stages[0].get("$match") {
301 return Ok(QueryPlan {
302 collection: collection.clone(),
303 mode: Mode::Find,
304 commands: vec![cmd_find(schema, filter, projection.as_ref())],
305 postprocess: post(),
306 sort: None,
307 });
308 }
309 }
310
311 let first_lookup = stages.iter().position(|s| {
315 s.get("$lookup")
316 .map(|lo| {
317 !lo.get("as")
318 .and_then(|v| v.as_str())
319 .map(|a| a.starts_with(REL_PRED_PREFIX))
320 .unwrap_or(false)
321 })
322 .unwrap_or(false)
323 });
324 let has_skip_limit = stages
325 .iter()
326 .any(|s| s.get("$skip").is_some() || s.get("$limit").is_some());
327 let sort_stage = stages.iter().find(|s| s.get("$sort").is_some()).cloned();
328
329 if let Some(idx) = first_lookup {
330 if !grouped && has_skip_limit && !sorts_by_relation(sort_stage.as_ref()) {
331 let mut id_pipeline: Vec<Value> = stages[..idx].to_vec();
332 for key in ["$sort", "$skip", "$limit"] {
333 if let Some(st) = stages.iter().find(|s| s.get(key).is_some()) {
334 id_pipeline.push(st.clone());
335 }
336 }
337 id_pipeline.push(json!({ "$project": { "_id": 1 } }));
338
339 let mut full: Vec<Value> = stages[idx..]
340 .iter()
341 .filter(|s| {
342 !(s.get("$sort").is_some()
343 || s.get("$skip").is_some()
344 || s.get("$limit").is_some())
345 })
346 .cloned()
347 .collect();
348 full.insert(0, json!({ "$match": { "_id": { "$in": PHASE1_IDS } } }));
349 if let Some(p) = projection.as_ref() {
350 full.push(json!({ "$project": p }));
351 }
352
353 return Ok(QueryPlan {
354 collection: collection.clone(),
355 mode: Mode::TwoPhase,
356 commands: vec![
357 cmd_aggregate(schema, &id_pipeline),
358 cmd_aggregate(schema, &full),
359 ],
360 postprocess: post(),
361 sort: sort_stage,
362 });
363 }
364 }
365
366 let mut final_stages = stages;
368 if let Some(p) = projection.as_ref() {
369 final_stages.push(json!({ "$project": p }));
370 }
371
372 Ok(QueryPlan {
373 collection,
374 mode: Mode::Aggregate,
375 commands: vec![cmd_aggregate(schema, &final_stages)],
376 postprocess: post(),
377 sort: None,
378 })
379}
380
381pub fn sorts_by_relation(sort_stage: Option<&Value>) -> bool {
383 sort_stage
384 .and_then(|s| s.get("$sort"))
385 .and_then(|s| s.as_object())
386 .map(|o| o.keys().any(|k| k.contains('.')))
387 .unwrap_or(false)
388}
389
390pub fn restore_sort_order(items: &mut [Value], ids: &[Value], sort_stage: Option<&Value>) {
392 if sort_stage.is_none() || ids.len() <= 1 {
393 return;
394 }
395 let mut index: std::collections::HashMap<_, usize> =
398 std::collections::HashMap::with_capacity(ids.len());
399 for (i, id) in ids.iter().enumerate() {
400 index.entry(id_key(id)).or_insert(i);
401 }
402 let keyed: Vec<(usize, Value)> = items
403 .iter()
404 .map(|it| {
405 let k = id_key(it.get("_id").unwrap_or(&Value::Null));
406 let pos = index.get(&k).copied().unwrap_or(ids.len());
407 (pos, it.clone())
408 })
409 .collect();
410 let mut sorted = keyed;
411 sorted.sort_by_key(|(pos, _)| *pos);
412 for (slot, (_, v)) in items.iter_mut().zip(sorted) {
413 *slot = v;
414 }
415}