# prestodb/presto-query-predictor architecture

> Architecture diagram of the GitHub repository prestodb/presto-query-predictor, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/prestodb/presto-query-predictor
- Repository: https://github.com/prestodb/presto-query-predictor

## Overview

##### Project type and purpose
- This is a Python-based machine-learning toolkit and micro-service for predicting CPU and memory usage of Presto SQL queries.
- It comprises:
- A core ML library (`query_predictor/`) that implements data ingestion, feature extraction (vectorization), model training, evaluation, and serialization.
- A set of example scripts (`examples/`) demonstrating data loading, transformation, training, hyperparameter tuning, and launching a Flask-based web UI.
- Documentation and tests to support development, CI, and deployment.

##### File structure
- Top-level directories:
- `query_predictor/` – the installable Python package
- `examples/` – usage examples (load_data, transform, train, tune, app)
- `docs/` – architecture, pipeline, serving, FAQs
- `tests/` – unit tests covering each core component
- Inside `query_predictor/`:
- `conf/` – YAML configurations for logging, training, serving, model tuning
- `datasets/` – embedded TPCH-based CSV data + loader
- `predictor/` – the heart of the library
– `assembler.py` – ties vectorizers, label creators, models into pipelines
– `classifier/` – factory and implementations (sklearn, XGBoost)
– `config/` – configuration manager and validator
– `data_loader.py`, `data_transformer.py`, `data_vectorizer.py`, `label_creator.py`
– `model/` – generic ML model interface, model manager, vectorizer model
– `pipeline.py` – end-to-end training/evaluation pipeline
– `predictor_app.py` + `webapp/templates/index.html` – Flask serving layer
– `logging_utils.py`, `metadata.py`, `exceptions.py`, `version.py`
- Build/deployment files:
- `setup.py`, `setup.cfg`, `requirements.txt`, `tox.ini`, `MANIFEST.in`

##### Readme insights
- Two main workflows:
A. Training pipeline: data → transform → train/tune → output serialized vectorizers & models
B. Serving pipeline: load serialized artifacts → Flask HTTP API + web UI → predict resources
- Default algorithms: TF-IDF vectorizer + XGBoost classifier

a. Main components
1. Data Source
- Embedded TPCH CSV in `datasets/data` or external log store
2. Data Ingestion & Preprocessing
- DataLoader → DataTransformer
3. Feature Extraction
- DataVectorizer (TF-IDF)
4. Label Creation
- LabelCreator for CPU & memory targets
5. Model Training & Management
- ClassifierFactory → MLModel (XGBoost/Sklearn)
- ModelManager handles persistence (.bin files)
6. Configuration Layer
- ConfigManager loads/validates `conf/*.yaml`
7. Serving Layer
- Flask App (predictor_app.py + templates)
- REST endpoint: /predict
- Web UI form
8. Artifacts Storage
- models/ directory with serialized vectorizers & models
b. Relationships & interactions
- The training pipeline streams data from the Data Source into the Data Ingestion module, then into Feature Extraction and Label Creation, before passing feature-label pairs into the Model Training component. Trained artifacts flow into Artifact Storage.
- The serving pipeline starts with ConfigManager loading the serving YAML, then ModelManager deserializes vectorizers & models, and the Flask app exposes an API and UI that accept raw SQL queries, run them through the DataVectorizer and MLModel, and return resource predictions.
c. Architectural patterns & principles
- Modular pipeline design: each step (loading, transforming, vectorizing, training) is a separate component.
- Factory pattern in `classifier_factory.py` for pluggable algorithms.
- Manager classes (`ModelManager`, `ConfigManager`) centralize persistence and configuration.
- Clear separation of concerns: training vs serving, core library vs examples.
d. Key technologies
- Python 3.6+
- Scikit-learn, XGBoost for ML
- Flask for serving
- YAML for configuration
- TF-IDF for vectorization

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Training Pipeline
    subgraph "Training Pipeline"
        direction TB
        DS["TPCH CSV Data"]:::storage
        DL["DataLoader"]:::dataflow
        DT["DataTransformer"]:::dataflow
        DV["DataVectorizer\n(TF-IDF)"]:::ml
        LC["LabelCreator"]:::ml
        CF["ClassifierFactory"]:::ml
        SM["MLModel\n(XGBoost/Sklearn)"]:::ml
        MM["ModelManager"]:::ml
        AS["Artifact Storage\n(models/*.bin)"]:::storage
        CFG_TRAIN["Trainer Configs\n(trainer-cpu.yaml,\ntrainer-memory.yaml,\ntransformer.yaml)"]:::config
        LC -->|"config"| CFG_TRAIN
        DS -->|"raw SQL CSV"| DL
        DL -->|"DataFrame"| DT
        DT -->|"Transformed Data"| DV
        DV -->|"Feature Vectors"| LC
        LC -->|"Labels"| CF
        CF -->|"algorithm selection"| SM
        SM -->|"trained model"| MM
        MM -->|"serialize"| AS
        CFG_TRAIN -.->|loaded by| MM
    end

    %% Serving Pipeline
    subgraph "Serving Pipeline"
        direction TB
        CFG_SERVE["Serving Config\n(serving.yaml)"]:::config
        MM2["ModelManager"]:::ml
        DV2["DataVectorizer"]:::ml
        SM2["MLModel"]:::ml
        APP["Flask App\n(predictor_app.py)"]:::service
        UI["Web UI\n(index.html)"]:::service
        CLIENT["REST Client"]:::service
        CFG_SERVE -->|"config"| MM2
        MM2 -->|"load vectorizer/model"| DV2
        MM2 -->|"load vectorizer/model"| SM2
        CLIENT -->|"SQL Query"| APP
        UI -->|"SQL Query"| APP
        APP -->|"vectorize"| DV2
        APP -->|"predict"| SM2
        APP -->|"JSON Response"| CLIENT
        APP -->|"HTML Response"| UI
    end

    %% Click Events Training
    click DS "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/datasets/data/tpch.csv"
    click DL "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/predictor/data_loader.py"
    click DT "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/predictor/data_transformer.py"
    click DV "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/predictor/data_vectorizer.py"
    click LC "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/predictor/label_creator.py"
    click CF "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/predictor/assembler.py"
    click CF "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/predictor/classifier/classifier_factory.py"
    click SM "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/predictor/classifier/sklearn_classifier.py"
    click SM "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/predictor/classifier/xgboost_classifier.py"
    click SM2 "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/predictor/model/ml_model.py"
    click MM "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/predictor/model/model_manager.py"
    click MM2 "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/predictor/model/model_manager.py"
    click AS "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/predictor/model/vectorizer_model.py"
    click PIPE "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/predictor/pipeline.py"
    click CFG_TRAIN "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/conf/trainer-cpu.yaml"
    click CFG_TRAIN "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/conf/trainer-memory.yaml"
    click CFG_TRAIN "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/conf/transformer.yaml"

    %% Click Events Serving
    click CFG_SERVE "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/conf/serving.yaml"
    click APP "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/predictor/predictor_app.py"
    click UI "https://github.com/prestodb/presto-query-predictor/blob/main/query_predictor/predictor/webapp/templates/index.html"

    %% Click Examples, Docs, Tests, Setup
    click EX1 "https://github.com/prestodb/presto-query-predictor/blob/main/examples/load_data.py"
    click EX2 "https://github.com/prestodb/presto-query-predictor/blob/main/examples/transform.py"
    click EX3 "https://github.com/prestodb/presto-query-predictor/blob/main/examples/train.py"
    click EX4 "https://github.com/prestodb/presto-query-predictor/blob/main/examples/tune.py"
    click EX5 "https://github.com/prestodb/presto-query-predictor/blob/main/examples/app.py"
    click DOC1 "https://github.com/prestodb/presto-query-predictor/blob/main/docs/architecture.md"
    click DOC2 "https://github.com/prestodb/presto-query-predictor/blob/main/docs/pipeline.md"
    click DOC3 "https://github.com/prestodb/presto-query-predictor/blob/main/docs/serving.md"
    click DOC4 "https://github.com/prestodb/presto-query-predictor/blob/main/docs/faq.md"
    click TEST1 "https://github.com/prestodb/presto-query-predictor/blob/main/tests/test_data_vectorizer.py"
    click TEST2 "https://github.com/prestodb/presto-query-predictor/blob/main/tests/test_assembler.py"
    click TEST3 "https://github.com/prestodb/presto-query-predictor/blob/main/tests/test_config_manager.py"
    click TEST4 "https://github.com/prestodb/presto-query-predictor/blob/main/tests/test_config_validator.py"
    click TEST5 "https://github.com/prestodb/presto-query-predictor/blob/main/tests/test_pipeline.py"
    click TEST6 "https://github.com/prestodb/presto-query-predictor/blob/main/tests/test_ml_model.py"
    click TEST7 "https://github.com/prestodb/presto-query-predictor/blob/main/tests/test_vectorizer_model.py"
    click TEST8 "https://github.com/prestodb/presto-query-predictor/blob/main/tests/test_version.py"
    click TEST9 "https://github.com/prestodb/presto-query-predictor/blob/main/tests/test_metadata.py"
    click SETUP1 "https://github.com/prestodb/presto-query-predictor/blob/main/setup.py"
    click SETUP2 "https://github.com/prestodb/presto-query-predictor/blob/main/setup.cfg"
    click REQ "https://github.com/prestodb/presto-query-predictor/blob/main/requirements.txt"
    click TOX "https://github.com/prestodb/presto-query-predictor/blob/main/tox.ini"
    click MAN "https://github.com/prestodb/presto-query-predictor/blob/main/MANIFEST.in"

    %% Styles
    classDef dataflow fill:#E3F2FD,stroke:#2196F3,color:#0D47A1
    classDef ml fill:#E8F5E9,stroke:#43A047,color:#1B5E20
    classDef service fill:#FFF3E0,stroke:#FB8C00,color:#E65100
    classDef storage fill:#E0F7FA,stroke:#00ACC1,color:#006064
    classDef config fill:#F3E5F5,stroke:#8E24AA,color:#4A148C
```
