# tuannh982/query-planner-guide architecture

> Architecture diagram of the GitHub repository tuannh982/query-planner-guide, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tuannh982/query-planner-guide
- Repository: https://github.com/tuannh982/query-planner-guide

## Overview

##### Important Elements

- Blue: Core components (Parser, Planner, Executor)
- Green: Data structures (AST, Plans)
- Orange: Rules and transformations
- Yellow: Cost and statistics components

b. Connection Types:
- Double lines: Bidirectional communication

c. Component Labels:
- Version/implementation details
- Optional components
- Interface points

##### Implementation Details to Highlight

- Transformation from SQL to AST to Logical Plan
- Memo structure with groups and expressions
- Demonstrate the cost model's interaction with statistics
- Operator pipeline structure

##### The cost model and statistics integration

Remember to include notes about:
- Scale considerations
- Performance optimization points
- Extension mechanisms
- Configuration options
- Error handling strategies

This design captures the essence of a cost-based query planner while showing the specific implementation details of this project's Volcano-style optimization approach.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Styles
    classDef core fill:#2374f7,color:white
    classDef data fill:#4CAF50,color:white
    classDef rules fill:#FF9800,color:white
    classDef stats fill:#FFD700,color:black
    classDef operator fill:#E91E63,color:white

    %% Main Flow
    User((User))
    Parser["Query Parser"]:::core
    Planner["Volcano Planner"]:::core
    Executor["Query Executor"]:::core
    Catalog[(Table Catalog)]:::data
    Stats[(Statistics Provider)]:::stats

    %% Core Planning Components
    Memo["Memo Engine"]:::core
    LogicalPlan["Logical Plan"]:::data
    PhysicalPlan["Physical Plan"]:::data
    TransformRules["Transformation Rules"]:::rules
    ImplRules["Implementation Rules"]:::rules
    CostModel["Cost Model"]:::stats

    %% Physical Operators
    subgraph Operators["Physical Operators"]
        Scan["Scan Operator"]:::operator
        Project["Project Operator"]:::operator
        HashJoin["Hash Join"]:::operator
        MergeJoin["Merge Join"]:::operator
    end

    %% Relationships
    User -- "SQL Query" --> Parser
    Parser -- "AST" --> LogicalPlan
    LogicalPlan --> Memo
    Memo --> TransformRules
    TransformRules --> Memo
    Memo --> ImplRules
    ImplRules --> PhysicalPlan
    PhysicalPlan --> Executor
    
    Planner --> Memo
    Planner <--> Catalog
    Planner <--> Stats
    CostModel --> ImplRules
    Stats --> CostModel

    PhysicalPlan --> Operators

    %% Click Events
    click Parser "https://github.com/tuannh982/query-planner-guide/blob/master/core/src/main/scala/core/ql/QueryParser.scala"
    click Planner "https://github.com/tuannh982/query-planner-guide/blob/master/core/src/main/scala/core/planner/volcano/VolcanoPlanner.scala"
    click Memo "https://github.com/tuannh982/query-planner-guide/blob/master/core/src/main/scala/core/planner/volcano/memo/Memo.scala"
    click TransformRules "https://github.com/tuannh982/query-planner-guide/blob/master/core/src/main/scala/core/planner/volcano/rules/TransformationRule.scala"
    click ImplRules "https://github.com/tuannh982/query-planner-guide/blob/master/core/src/main/scala/core/planner/volcano/rules/ImplementationRule.scala"
    click CostModel "https://github.com/tuannh982/query-planner-guide/blob/master/core/src/main/scala/core/planner/volcano/cost/CostModel.scala"
    click LogicalPlan "https://github.com/tuannh982/query-planner-guide/blob/master/core/src/main/scala/core/planner/volcano/logicalplan/LogicalPlan.scala"
    click PhysicalPlan "https://github.com/tuannh982/query-planner-guide/blob/master/core/src/main/scala/core/planner/volcano/physicalplan/PhysicalPlan.scala"
    click Catalog "https://github.com/tuannh982/query-planner-guide/blob/master/core/src/main/scala/core/catalog/TableCatalog.scala"
    click Stats "https://github.com/tuannh982/query-planner-guide/blob/master/core/src/main/scala/core/stats/StatsProvider.scala"
    click HashJoin "https://github.com/tuannh982/query-planner-guide/blob/master/core/src/main/scala/core/planner/volcano/physicalplan/builder/HashJoinImpl.scala"
    click MergeJoin "https://github.com/tuannh982/query-planner-guide/blob/master/core/src/main/scala/core/planner/volcano/physicalplan/builder/MergeJoinImpl.scala"
    click Scan "https://github.com/tuannh982/query-planner-guide/blob/master/core/src/main/scala/core/planner/volcano/physicalplan/builder/NormalScanImpl.scala"
    click Project "https://github.com/tuannh982/query-planner-guide/blob/master/core/src/main/scala/core/planner/volcano/physicalplan/builder/ProjectionImpl.scala"
    click Executor "https://github.com/tuannh982/query-planner-guide/blob/master/core/src/main/scala/core/execution/Operator.scala"

    %% Legend
    subgraph Legend
        CoreComp["Core Component"]:::core
        DataStr["Data Structure"]:::data
        RuleComp["Rules"]:::rules
        StatsComp["Statistics Component"]:::stats
        OpComp["Operator"]:::operator
    end
```
