# apache/incubator-gluten architecture

> Architecture diagram of the GitHub repository apache/incubator-gluten, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/apache/incubator-gluten
- Repository: https://github.com/apache/incubator-gluten

## Overview

##### Spark SQL Engine (JVM)
- This represents the Spark SQL frontend along with Catalyst optimizer.
- It produces the Spark physical plan as part of the usual control flow.
- Optionally, add elements like the DataFrame API, Spark’s UI, and the scheduler.

##### Gluten Plugin and Query Conversion Module
- Positioned as an interceptor inside SparkSQL, this component loads as a Spark plugin.
- This module also implements fallback logic that checks for operators not supported in the native execution engine. When necessary, it converts columnar data to row-based formats (using Columnar-to-Row (C2R) and Row-to-Columnar (R2C) transformations).

##### JNI Bridge / Shims
- A dedicated layer that bridges between the JVM (Gluten’s Java modules) and the native side.
- In addition, shims support compatibility with multiple Spark versions. (Mark these as “Shim Layer” components.)

##### Native Execution Engine(s)
- This large block represents one or more specialized native backends (e.g., ClickHouse backend, Velox backend, and potentially others).
- Data processing is done in native code with its own operator pipelines and optimizations.

##### Unified Memory Management and Data Exchange
- A memory management component that handles off-heap/native memory allocation and subsequently returns ArrowColumnarBatches.

##### Fallback / Error Handling Path
- Clearly label a fallback path that converts native columnar data back into Spark’s row format when an operator does not have a native implementation.
- This ensures that users see transparent, correct results even when full offloading is not available.

##### External Tools and Qualification

- For the Metrics module, show how native engine metrics get transmitted back to the driver for display in the Spark UI.
- Finally, include explicit labeling for key patterns: “Plan Conversion”, “JNI Bridge”, “Fallback Mechanism”, “Unified Memory Management”, and “Native Backends”.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% JVM Layer
    subgraph "JVM Layer"
        A["Spark SQL Engine (Catalyst & Query Planner)"]:::jvm
        B["Gluten Plugin & Query Conversion Module"]:::plugin
    end

    %% JNI Bridge Layer
    subgraph "JNI Bridge Layer"
        C["JNI Bridge / Shim Layer"]:::jni
    end

    %% Native Layer
    subgraph "Native Layer"
        D["Native Execution Engines (Backends)"]:::native
        E["Unified Memory Management & Data Exchange (Apache Arrow)"]:::memory
        F["Fallback / Error Handling Mechanism"]:::fallback
        G["Metrics Collection & Monitoring"]:::metrics
    end

    %% External Tools
    subgraph "External Tools"
        H["External Tools & Qualification Utilities"]:::external
    end

    %% Data Flow Connections
    A -->|"ProducesSparkPlan"| B
    B -->|"PlanConversion"| C
    C -->|"SubstraitPlan"| D
    D -->|"NativeExecution"| E
    D -->|"FallbackTrigger"| F
    F -->|"RowConversion"| A
    D -->|"Metrics"| G
    G -->|"Reports"| A
    G -->|"Analysis"| H

    %% Click Events
    click A "https://github.com/apache/incubator-gluten/tree/main/gluten-core/src/main/scala/org/apache/spark (and its subdirectories)"
    click B "https://github.com/apache/incubator-gluten/blob/main/gluten-core and gluten-arrow/src/main/scala/org/apache/gluten/execution (including plan conversion code such as ColumnarToColumnarExec.scala and GlutenPlan.scala)"
    click C "https://github.com/apache/incubator-gluten/tree/main/shims/ (e.g., shims/spark32, shims/spark33, shims/spark34, shims/spark35 directories)"
    click D "https://github.com/apache/incubator-gluten/tree/main/backends-clickhouse, backends-velox, gluten-delta, gluten-hudi, gluten-iceberg, gluten-kafka"
    click E "https://github.com/apache/incubator-gluten/tree/main/gluten-arrow/src/main/java/org/apache/gluten/memory/arrow (and related memory pool classes in gluten-arrow and gluten-core/src/main/java/org/apache/gluten/memory)"
    click F "https://github.com/apache/incubator-gluten/blob/main/gluten-substrait/src/main/scala/org/apache/gluten/execution (e.g., fallback conversion in ColumnarToRowExecBase.scala)"
    click G "https://github.com/apache/incubator-gluten/tree/main/gluten-core/src/main/java/org/apache/gluten/metrics (and similar sub-packages in gluten-substrait and others)"
    click H "https://github.com/apache/incubator-gluten/tree/main/tools/qualification-tool"

    %% Styles
    classDef jvm fill:#ffcccc,stroke:#990000,stroke-width:2px;
    classDef plugin fill:#fff2cc,stroke:#b38f00,stroke-width:2px;
    classDef jni fill:#d9ead3,stroke:#38761d,stroke-width:2px;
    classDef native fill:#c9daf8,stroke:#0b5394,stroke-width:2px;
    classDef memory fill:#d0e0e3,stroke:#115740,stroke-width:2px;
    classDef fallback fill:#f4cccc,stroke:#cc0000,stroke-width:2px;
    classDef metrics fill:#ead1dc,stroke:#741b47,stroke-width:2px;
    classDef external fill:#d9d2e9,stroke:#6d3b97,stroke-width:2px;
```
