In-Database Text Classification with BornSQL

Emanuele Guidotti, Darya Shlyk, Stefano Montanelli, Alfio Ferrara

TL;DR

BornSQL is the first text classification algorithm implemented entirely in standard SQL, allowing machine learning to happen directly inside a database without external libraries or specialized functions. It supports the full ML lifecycle—including training, inference, incremental updates, and even "unlearning" for privacy compliance—all while maintaining high performance on millions of records.

What Problem Does This Paper Address?

Traditionally, In-Database Machine Learning (In-DB ML) has been a "hybrid" or "bolted-on" affair. Most solutions rely on User-Defined Functions (UDFs), which can be security risks and are rarely portable between different database systems (like moving from PostgreSQL to MySQL). Furthermore, text classification is particularly difficult for databases because it requires heavy preprocessing (tokenization, vectorization) that usually happens outside the database in Python or R.

This creates a "data gravity" problem: moving massive datasets out of a secure database into an ML environment is slow, expensive, and risky. The authors tackle this by asking: Can we build a powerful text classifier using nothing but standard SQL queries?

Methods

The authors introduce BornSQL, an implementation of the Born Classifier. The core innovation is treating database attributes as "words" in a document and using sparse tensor representation within SQL tables.

Key technical highlights include:

  • Pure SQL Implementation: It uses standard SQL features like Common Table Expressions (CTEs), JOINs, and GROUP BY statements. This makes it highly portable across PostgreSQL, MySQL, and SQLite.
  • Sparse Tensors: Instead of massive, empty matrices, BornSQL stores data in a "long" format (Item ID, Feature ID, Value), which is exactly how relational databases are optimized to work.
  • Exact Incremental Learning & Unlearning: Unlike many models that require a full retrain to update, BornSQL can update its weights for new data or "forget" specific data points (crucial for GDPR "right to be forgotten" requests) using simple INSERT or UPDATE queries.
  • Built-in Explainability: Because the model is based on probability distributions, it can natively output why it made a certain prediction (local explanation) or which features are most important overall (global explanation) using standard queries.

Datasets

The researchers evaluated BornSQL using several benchmarks:

  • Scopus Dataset: A massive collection of 2,359,828 scientific publications with nearly 4 million features. The task was to classify papers into three subject areas (AI, Statistics, or Decision Sciences).
  • Adult Dataset (UCI): A standard benchmark for predicting income levels based on census data.
  • RLCP Dataset: A large-scale dataset (over 5.7 million instances) used for record linkage.
  • 20 Newsgroup & Reuters (R8/R52): Used to verify classification accuracy against established baselines.

Key Insights and Ideas

  • Relational Data as Text: The "aha moment" is treating any categorical database attribute as a feature analogous to a word. This allows the Born Classifier to work on structured data just as easily as raw text.
  • Portability is Power: Because it’s standard SQL, the exact same model logic works on a high-end server running PostgreSQL and a tiny MacBook Air running SQLite.
  • Efficiency through Sparsity: While tools like Apache MADlib struggle with high-dimensional text data because they don't natively support sparse training, BornSQL thrives on it by leveraging the relational engine's strengths.
  • Privacy by Design: The "unlearning" feature is a standout. In most ML models, removing one person's data requires retraining from scratch. In BornSQL, it’s a single SQL command that subtracts that person's contribution from the model weights.

Key Results

  • Speed: On the Scopus dataset, the average inference time was just 1 millisecond per publication.
  • Scalability: Training time was found to be linear relative to the number of publications, meaning it can scale to even larger datasets without exponential slowdowns.
  • Accuracy: BornSQL performed comparably to popular algorithms like Logistic Regression and Support Vector Machines (SVM) on the Adult and RLCP datasets, while being significantly easier to deploy within a database environment.
  • Resource Friendly: The authors successfully ran the entire 2.3-million-record experiment on a standard laptop (MacBook Air M2 with 8GB RAM), proving that In-DB ML doesn't always require a massive cluster.

Limitations and Future Work

The primary limitation of BornSQL is its focus on categorical data. It cannot directly handle continuous numerical values (like "temperature" or "price") without first "discretizing" them (turning them into ranges or buckets). Additionally, it is currently designed for classification tasks and does not support regression (predicting a continuous number).

Future work may involve extending the framework to handle more complex data types or exploring how database query optimizers can be further tuned specifically for these types of mathematical SQL workloads.

Summary updated

Abstract

The integration of databases and machine learning promises to enhance various aspects of data management, analysis, and application. However, in-database machine learning (In-DB ML) is not easily portable to different database management systems and current approaches are typically limited to training and inference, while modern machine learning pipelines often involve aspects such as continuous learning, unlearning, and explainability. This paper presents BornSQL, a In-DB ML algorithm based on the Born Classifier [7], and exclusively implemented through standard SQL queries. BornSQL can handle categorical data, and it is particularly appropriate for classification of textual data. Further contributions of BornSQL are i) incremental learning to efficiently enforce model updates when new data become available in the db, ii) unlearning when selected data needs to be excluded due to privacy issues, and iii) global/local explainability to associate the importance of a feature/attribute in determining the classification result. We illustrate the usage and scalability of the algorithm using a benchmark database consisting of 2,359,828 scientific publications divided into three classes and composed of 3,942,559 features. The training time is linear in the number of publications and the average inference time for a publication is 1 millisecond on our experimental environment. We discuss potential applications such as cost-effective model serving, exploratory data analysis, and data privacy.
BibTeX
@inproceedings{guidotti2026database,
  author = {Emanuele Guidotti and Darya Shlyk and Stefano Montanelli and Alfio Ferrara},
  title = {{In-Database Text Classification with BornSQL}},
  booktitle = {EDBT},
  year = {2026},
  doi = {10.48786/edbt.2026.19},
}

Loading related content...

Figure