How prestodb/presto-query-predictor works
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