# datamole-ai/pysparkdt architecture

> Architecture diagram of the GitHub repository datamole-ai/pysparkdt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/datamole-ai/pysparkdt
- Repository: https://github.com/datamole-ai/pysparkdt

## Overview

##### Project Type and Purpose
This is an open-source testing library specifically designed to facilitate local testing of Databricks workflows using PySpark and Delta tables. The key purpose is to emulate Unity Catalog behavior outside of Databricks environment.

##### Main Components

A. Core Library Components:
- Metastore Emulation Layer
- Spark Session Management
- Delta Table Handler

B. Testing Infrastructure:
- Local File System Interface
- Table Schema Management
- Test Data Management

C. Integration Points:
- PySpark Integration
- Delta Lake Integration
- Unity Catalog Emulation

**┌─ Application Layer ─────────────────────────────────────┐**

- User's Test Code                                    │
- (pytest fixtures & test cases)                      │

- PySparkDT Core ──────────────────────────────────────┤

- ┌─ Test Setup ─┐   ┌─ Metastore ─┐   ┌─ Spark ─┐   │

- Data Loading │◄──┤Local Storage│◄──┤Session  │   │
- Schema Init  │   │  Management │   │Manager  │   │
- ┘   └─────────────┘   └─────────┘   │

- Infrastructure Layer ─────────────────────────────────┤

- ┌─ File System ─┐   ┌─ Delta Lake ┐   ┌─ PySpark ┐ │

- NDJSON Data  │   │ Table       │   │ Execution│ │
- JSON Schema  │   │ Operations  │   │ Engine   │ │
- ┘   └─────────────┘   └──────────┘ │

- ┘

##### Additional Elements

- Component type indicators

B. Notes Section:
- Key limitations (e.g., Map Key Type restriction)
- Important configuration points
- Test parallelization considerations

##### Implementation Details

- The `spark_base` function's role in session management
- The `reinit_local_metastore` function's operation
- Test data directory structure
- Delta table interaction patterns

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles and Classes
    classDef core fill:#2374ab,color:white
    classDef test fill:#48a9a6,color:white
    classDef infra fill:#758e4f,color:white
    classDef storage fill:#c6c5b9,color:black
    classDef processing fill:#9b6b9d,color:white
    classDef example fill:#d4b483,color:black

    %% Application Layer
    subgraph ApplicationLayer["Application Layer"]
        TestCode["User's Test Code"]:::test
        ExampleTest["Example Tests"]:::example
        ExampleImpl["Example Implementation"]:::example
    end

    %% PySparkDT Core Layer
    subgraph CoreLayer["PySparkDT Core"]
        MetastoreEmulation["Metastore Emulation"]:::core
        SparkSession["Spark Session Management"]:::core
        TestSetup["Test Setup & Data Loading"]:::core
    end

    %% Infrastructure Layer
    subgraph InfraLayer["Infrastructure Layer"]
        FileSystem["File System Interface"]:::infra
        DeltaLake["Delta Lake Operations"]:::infra
        PySparkEngine["PySpark Execution Engine"]:::infra
    end

    %% Storage Components
    subgraph StorageLayer["Storage Layer"]
        TestData["Test Data"]:::storage
        SchemaManagement["Schema Management"]:::storage
        LocalStorage["Local Storage"]:::storage
    end

    %% Example Components
    subgraph ExampleLayer["Example Components"]
        ExampleProcessing["Processing Logic"]:::example
        ExampleEntryPoint["Entry Point"]:::example
    end

    %% Relationships
    TestCode --> TestSetup
    ExampleTest --> TestSetup
    TestSetup --> MetastoreEmulation
    MetastoreEmulation --> SparkSession
    SparkSession --> PySparkEngine
    TestSetup --> TestData
    TestData --> SchemaManagement
    SchemaManagement --> LocalStorage
    LocalStorage --> DeltaLake
    DeltaLake --> FileSystem
    ExampleImpl --> ExampleProcessing
    ExampleProcessing --> ExampleEntryPoint
    ExampleEntryPoint --> PySparkEngine

    %% Click Events
    click MetastoreEmulation "https://github.com/datamole-ai/pysparkdt/blob/main/pysparkdt/metastore.py"
    click SparkSession "https://github.com/datamole-ai/pysparkdt/blob/main/pysparkdt/spark_base.py"
    click TestData "https://github.com/datamole-ai/pysparkdt/tree/main/tests/data/tables/"
    click SchemaManagement "https://github.com/datamole-ai/pysparkdt/tree/main/tests/data/tables/schema/"
    click ExampleImpl "https://github.com/datamole-ai/pysparkdt/tree/main/example/myjobpackage/"
    click TestCode "https://github.com/datamole-ai/pysparkdt/blob/main/tests/test_example.py"
    click ExampleTest "https://github.com/datamole-ai/pysparkdt/blob/main/example/tests/test_processing.py"
    click ExampleProcessing "https://github.com/datamole-ai/pysparkdt/blob/main/example/myjobpackage/processing.py"
    click ExampleEntryPoint "https://github.com/datamole-ai/pysparkdt/blob/main/example/myjobpackage/entrypoint.py"

    %% Legend
    subgraph Legend["Legend"]
        CoreComp["Core Component"]:::core
        TestComp["Test Component"]:::test
        InfraComp["Infrastructure Component"]:::infra
        StorageComp["Storage Component"]:::storage
        ProcessingComp["Processing Component"]:::processing
        ExampleComp["Example Component"]:::example
    end
```
