Skip to content

Commit 7ef27cd

Browse files
genezhangCopilot
andauthored
docs: Add module documentation to query_planner submodules (#54)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 83dd4db commit 7ef27cd

25 files changed

Lines changed: 416 additions & 7 deletions

src/query_planner/logical_expr/errors.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
//! Error types for logical expression conversion.
2+
//!
3+
//! These errors occur when Cypher expressions cannot be translated
4+
//! to the internal LogicalExpr representation.
5+
16
use thiserror::Error;
27

38
#[derive(Debug, Error)]

src/query_planner/logical_plan/errors.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
//! Error types for logical plan construction.
2+
//!
3+
//! These errors arise during the translation of Cypher AST to LogicalPlan,
4+
//! covering schema validation, pattern matching, and type inference issues.
5+
16
use thiserror::Error;
27

8+
/// Errors that can occur during logical plan construction.
9+
///
10+
/// These errors are typically unrecoverable and indicate problems with
11+
/// the query structure or schema configuration that prevent plan building.
312
#[derive(Debug, Clone, Error, PartialEq)]
413
pub enum LogicalPlanError {
514
#[error(

src/query_planner/logical_plan/filter_view.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
//! Filter-ViewScan integration utilities.
2+
//!
3+
//! Provides helper methods for [`Filter`] nodes that operate on [`ViewScan`] inputs,
4+
//! enabling predicate pushdown and filter composition optimizations.
5+
16
use std::sync::Arc;
27

38
use super::{Filter, LogicalPlan, ViewScan};
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Error types for MATCH clause processing.
22
//!
3-
//! This module re-exports the main LogicalPlanError since match clause
4-
//! errors are part of the broader logical plan error hierarchy.
5-
6-
pub use crate::query_planner::logical_plan::errors::LogicalPlanError;
7-
pub use crate::query_planner::logical_plan::plan_builder::LogicalPlanResult;
3+
//! This module serves as a placeholder for MATCH clause specific error types.
4+
//! Currently, match clause errors use the broader LogicalPlanError hierarchy
5+
//! directly via `crate::query_planner::logical_plan::errors::LogicalPlanError`.
6+
//!
7+
//! This module exists for future extensibility if match-clause-specific
8+
//! error variants are needed.

src/query_planner/logical_plan/mod.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,67 @@
1+
//! Logical Plan representation for Cypher queries.
2+
//!
3+
//! This module defines the core data structures representing a Cypher query
4+
//! as an intermediate representation (IR) between parsed AST and generated SQL.
5+
//!
6+
//! # Architecture Overview
7+
//!
8+
//! ```text
9+
//! Cypher Query → AST → LogicalPlan → SQL Query
10+
//! ^^^^^^^^^^^
11+
//! This module
12+
//! ```
13+
//!
14+
//! # Key Components
15+
//!
16+
//! ## Core Types
17+
//! - [`LogicalPlan`] - Main enum representing all query plan nodes
18+
//! - [`ViewScan`] - Table scan with optional predicate pushdown
19+
//! - [`GraphNode`] / [`GraphRel`] - Graph pattern nodes and relationships
20+
//! - [`Projection`] - SELECT clause items
21+
//! - [`Filter`] - WHERE clause conditions
22+
//! - [`GroupBy`] - Aggregation and grouping
23+
//! - [`Cte`] - Common Table Expression (WITH clause in SQL sense)
24+
//! - [`CartesianProduct`] - CROSS JOIN for disconnected patterns
25+
//! - [`Union`] - UNION/UNION ALL for combined result sets
26+
//!
27+
//! ## Clause Processing Submodules
28+
//! - `match_clause` - MATCH pattern to scan/join translation
29+
//! - `optional_match_clause` - OPTIONAL MATCH (LEFT JOIN) handling
30+
//! - `with_clause` - WITH clause scope/projection boundaries
31+
//! - `return_clause` - RETURN projection handling
32+
//! - `where_clause` - WHERE condition processing
33+
//! - `order_by_clause` - ORDER BY generation
34+
//! - `skip_n_limit_clause` - SKIP/LIMIT pagination
35+
//! - `unwind_clause` - UNWIND array expansion
36+
//!
37+
//! # Plan Building
38+
//!
39+
//! Plans are built via [`plan_builder::build_logical_plan`] which:
40+
//! 1. Processes Cypher clauses in order
41+
//! 2. Builds nodes from inner to outer (scans → joins → filters → projections)
42+
//! 3. Tracks planning context via [`PlanCtx`]
43+
//!
44+
//! # Example Plan Structure
45+
//!
46+
//! ```text
47+
//! MATCH (u:User)-[f:FOLLOWS]->(friend)
48+
//! WHERE u.active = true
49+
//! RETURN friend.name
50+
//!
51+
//! → Projection(friend.name)
52+
//! └─ Filter(u.active = true)
53+
//! └─ GraphRel(f:FOLLOWS)
54+
//! ├─ left: ViewScan(users AS u)
55+
//! ├─ center: ViewScan(follows AS f)
56+
//! └─ right: ViewScan(users AS friend)
57+
//! ```
58+
//!
59+
//! # ID Generation
60+
//!
61+
//! For anonymous patterns, unique IDs are generated via:
62+
//! - [`generate_id`] - Simple incrementing aliases (t1, t2, t3...)
63+
//! - [`generate_cte_id`] - CTE names (cte1, cte2, cte3...)
64+
165
use serde::{Deserialize, Serialize};
266
use std::sync::atomic::{AtomicU32, Ordering};
367
use std::{collections::HashMap, fmt, sync::Arc};

src/query_planner/logical_plan/optional_match_clause.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
//! OPTIONAL MATCH clause processing.
2+
//!
3+
//! Handles Cypher's OPTIONAL MATCH which provides LEFT JOIN semantics -
4+
//! all rows from the base pattern are preserved, with NULL values
5+
//! where optional patterns don't match.
6+
//!
7+
//! # SQL Translation
8+
//!
9+
//! ```text
10+
//! MATCH (a) OPTIONAL MATCH (a)-[:FOLLOWS]->(b)
11+
//! → SELECT ... FROM a LEFT JOIN follows ON ... LEFT JOIN b ON ...
12+
//! ```
13+
//!
14+
//! # Implementation
15+
//!
16+
//! 1. Sets optional mode flag in [`PlanCtx`]
17+
//! 2. Processes patterns via standard MATCH logic
18+
//! 3. Aliases are auto-marked as optional for JOIN generation
19+
//! 4. Restores normal mode after processing
20+
121
use std::sync::Arc;
222

323
use crate::{

src/query_planner/logical_plan/order_by_clause.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
//! ORDER BY clause processing.
2+
//!
3+
//! Converts Cypher ORDER BY clauses into [`OrderBy`] logical plan nodes.
4+
//! Supports ascending/descending sort direction and multiple sort keys.
5+
//!
6+
//! # SQL Translation
7+
//!
8+
//! ```text
9+
//! ORDER BY u.name DESC, u.age ASC
10+
//! → ORDER BY users.full_name DESC, users.age ASC
11+
//! ```
12+
113
use std::sync::Arc;
214

315
use crate::{

src/query_planner/logical_plan/plan_builder.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
//! Logical plan builder for Cypher queries.
2+
//!
3+
//! This module orchestrates the construction of [`LogicalPlan`] trees from
4+
//! parsed Cypher AST. It processes clauses in sequential order, building
5+
//! up the plan from innermost scans to outermost projections.
6+
//!
7+
//! # Clause Processing Order
8+
//!
9+
//! ```text
10+
//! 1. MATCH clauses → ViewScan, GraphNode, GraphRel nodes
11+
//! 2. OPTIONAL MATCH → CartesianProduct with is_optional=true
12+
//! 3. UNWIND clauses → Unwind nodes (ARRAY JOIN)
13+
//! 4. WHERE clause → Filter nodes
14+
//! 5. WITH clause → WithClause (scope boundary + projection)
15+
//! 6. ORDER BY → OrderBy nodes
16+
//! 7. SKIP/LIMIT → Skip/Limit nodes
17+
//! 8. RETURN clause → Projection nodes
18+
//! ```
19+
//!
20+
//! # Key Functions
21+
//!
22+
//! - [`build_logical_plan`] - Main entry point for plan construction
23+
//!
24+
//! # Error Handling
25+
//!
26+
//! Returns [`LogicalPlanResult`] wrapping [`LogicalPlanError`] for:
27+
//! - Schema validation failures
28+
//! - Unsupported syntax
29+
//! - Type inference errors
30+
131
use std::collections::HashMap;
232
use std::sync::Arc;
333

src/query_planner/logical_plan/projection_view.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
//! Projection-ViewScan integration utilities.
2+
//!
3+
//! Provides helper methods for [`Projection`] nodes that operate on [`ViewScan`] inputs,
4+
//! enabling column mapping and projection pushdown optimizations.
5+
16
use std::sync::Arc;
27

38
use super::{LogicalPlan, Projection, ProjectionItem, ViewScan};

src/query_planner/logical_plan/return_clause.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
//! RETURN clause processing.
2+
//!
3+
//! Handles Cypher's RETURN clause which projects and optionally aggregates results.
4+
//! Creates [`Projection`] and [`GroupBy`] logical plan nodes.
5+
//!
6+
//! # Features
7+
//!
8+
//! - Simple projections: `RETURN u.name, u.email`
9+
//! - Aggregations: `RETURN count(*), avg(score)`
10+
//! - Aliasing: `RETURN u.name AS userName`
11+
//! - DISTINCT: `RETURN DISTINCT u.country`
12+
//! - Whole-entity: `RETURN u` (returns all properties as JSON)
13+
//!
14+
//! # Aggregation Handling
15+
//!
16+
//! When aggregates are detected, non-aggregate columns are automatically
17+
//! added to GROUP BY (ClickHouse requires explicit grouping).
18+
119
use crate::{
220
open_cypher_parser::ast::{Expression, ReturnClause, ReturnItem},
321
query_planner::logical_expr::{

0 commit comments

Comments
 (0)