Rust

classifier

Module

Statement classifiers: whether a wire protocol message reads, writes or controls a transaction, so a proxy can route it to a replica or the primary.

A classifier does not parse: the SQL classifiers read the leading keyword (skipping whitespace, comments and quoted literals), and the MongoDB classifier reads the first key of an OP_MSG command document. For a structural parse of SQL, see crate::ast.

Every classifier implements Classifier; the SQL ones also implement Sql, which classifies bare SQL text and holds the shared keyword logic. Messages are bytes (&[u8]), as they arrive on the wire.

Rust
use utopia_query::Type;
use utopia_query::classifier::{Classifier, PostgreSql, Sql};

let classifier = PostgreSql;
assert_eq!(classifier.classify(b"Q\x00\x00\x00\x0dSELECT 1\x00"), Type::Read);
assert_eq!(classifier.classify_sql(b"/* hint */ insert into t values (1)"), Type::Write);
assert_eq!(classifier.extract_keyword(b"  begin;"), b"BEGIN");

Contents

4 submodules, and what the module defines itself.

use utopia_query::classifier::Classifier;
Rust
pub trait Classifier

Classifies a raw wire protocol message as a read, a write, a transaction statement or unknown. Implement it to route another protocol.

classifier::mongodb

Own page

The MongoDB OP_MSG classifier.

use utopia_query::classifier::mongodb::MongoDb;
Rust
pub struct MongoDb

Classifies MongoDB OP_MSG (opcode 2013) messages by their command name, the first key of the body section's BSON document.

Reads (find, aggregate, count, ping, hello, ...) and writes (insert, update, delete, create, drop, ...) are looked up; commitTransaction / abortTransaction end a transaction, and a CRUD command carrying a top-level startTransaction key begins one. Anything else, or a malformed message, is unknown.

classifier::mysql

Own page

The MySQL client protocol classifier.

use utopia_query::classifier::mysql::MySql;
Rust
pub struct MySql

Classifies MySQL client protocol packets: 3-byte payload length, sequence id, command byte, payload.

COM_QUERY (0x03) is classified by its SQL text; the prepared statement commands (COM_STMT_PREPARE, _EXECUTE, _SEND_LONG_DATA, _CLOSE, _RESET) are writes, so they reach the primary; anything else is unknown.

classifier::postgresql

Own page

The PostgreSQL frontend protocol classifier.

use utopia_query::classifier::postgresql::PostgreSql;
Rust
pub struct PostgreSql

Classifies PostgreSQL frontend messages: type byte, big-endian length, body.

A simple query (Q) is classified by its SQL text (up to the first NUL); the extended query messages Parse, Bind and Execute are writes, so they reach the primary; anything else is unknown.

classifier::sql

Own page

The SQL classification shared by the SQL wire protocols.

use utopia_query::classifier::sql::Sql;
Rust
pub trait Sql: Classifier

Classifies SQL text by its leading keyword: the logic shared by the SQL wire protocol classifiers (super::MySql, super::PostgreSql), which implement Classifier::classify for their packet format and hand the SQL text to Sql::classify_sql.

Handles leading whitespace, -- and /* */ comments, quoted literals and identifiers ('..', "..", `..`, $tag$..$tag$), mixed case, COPY ... TO (read) against COPY ... FROM (write), and CTEs (WITH ... SELECT reads, WITH ... INSERT writes).