documentModel

docs/model.md

Model

Semantics::analyze() returns a SqlSemantics\Query implemented as an Operation in the selected database package. This page describes what an operation holds and how to read it. Every value described here is immutable: it has only readonly properties, and cloning, dynamic properties and serialization are refused by the SqlSemantics\Platform\Sqlite\Model\Statement\Snapshot trait, which the value audit requires of every published class.

Operations

An operation is one analyzed root:

MemberContent
$operation->contextThe AnalysisContext the facts were derived against: the language profile and the declarations. See Contexts.
$operation->statementThe statement structure: a concrete class of the database package, or a Script of several statements.
$operation->factsEvery fact derived for the structure against the context.
toString()SQL rendered from the structure and checked against it. See Rendering.
profile()The language profile: grammar release, lexical settings, parameter style.
declarations()The Table declarations the statement provides, for example those of a CREATE TABLE or CREATE VIEW.
shape(), fields(), field(), lookupField()The rows the statement returns, when it returns rows.
inputRelation(), singleNamedInput()The input relation of a statement that reads rows.

The constructor new Operation($context, $statement) is the only way to obtain an operation, and analyze() uses it too. All facts are established in the constructor; nothing is filled in later.

Statement structure and facts

An operation separates two kinds of information.

The structure describes what the SQL requests: the operation, its operands and its input relations, as concrete classes of the database package (for example SqlSemantics\Platform\Sqlite\Statement\Query\Select, ...\Mutation\InsertRows, ...\Schema\CreateTable). It holds decoded names (Name, QualifiedName), exact literal values (digits as strings, decoded text, hexadecimal digits; never a PHP float), enum cases for closed choices, and lists in written order. It holds no parser node, no token and no binding: which declaration a name denotes is not part of the structure. It holds no source text either, except the checked spelling of a result column that the database names after its text (a Layout, see Rendering). A structure value may occur at one position of a statement only.

The facts are everything that depends on the context: name resolutions, row shapes, output fields, types and NULL facts. They are kept in $operation->facts, keyed by the identity of the structure node they are about.

use SqlSemantics\Platform\Sqlite\Semantics;
use SqlSemantics\Platform\Sqlite\Statement\Expression\Operator\Binary;
use SqlSemantics\Platform\Sqlite\Statement\Expression\Operator\BinaryOperator;
use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ResolvedColumn;
use SqlSemantics\Platform\Sqlite\Model\Statement\Type\NullOnly;

$semantics = new Semantics();
$users = $semantics->analyze('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)');
$query = $semantics->analyze('SELECT name FROM users WHERE id > 10', [$users]);

$where = $query->statement->where;
$where instanceof Binary; // => true
$where->operator; // => BinaryOperator::Greater
$where->right->digits; // => '10'
$query->facts->scalar($where->left)->resolution instanceof ResolvedColumn; // => true

$sum = $semantics->analyze('SELECT 1 + NULL');
$sum->facts->scalar($sum->field(0)->expression->right)->type instanceof NullOnly; // => true

$facts->scalar($node) answers the ScalarFact of an expression (type, nullability, and resolution for a name use), $facts->relation($node) the RelationFact of a relation occurrence (shape, and table for a named relation), and $facts->query($node) the QueryFact of a query, including subqueries. $facts->declarations lists the provided declarations, and $facts->output the QueryFact of the rows the statement returns, or null. Asking for a node that is not part of the operation throws InvalidConstruction; a node of one operation has no facts in another.

The structure classes and their properties are documented in each database package. Each database package defines the roles its structure classes play:

InterfaceRole
StatementA statement root.
QuerySomething that produces rows: a SELECT, VALUES, a set operation, a WITH query.
RelationA relation in an input: a named table, a join, a derived table, a table function.
NamedRelationA relation that is one named table, view or common table; name() and alias().
SelectionA statement or query that reads from an input relation; input().
ScalarAn expression.
NodeAny structure value.

A Script holds the statements of an input with several statements, in order. It collects declarations from its members; it returns no rows itself.

Fields and lookups

When the statement returns rows and every output position is known, fields() answers a Fields collection; otherwise it answers null. The collection keeps order and duplicate names, and offers counting, iteration, at($position) and lookup($name). It has no method that adds, removes, reorders or replaces a field.

A Field has:

Property or methodContent
positionThe zero-based output position.
nameThe output Name, or null when the position has no name or its name depends on missing inputs (see slot->unnamed).
type, nullabilityThe type fact and the NULL fact of the position.
expressionThe expression that computes the field; null when no single expression does, as for a set operation.
resolutionThe resolution, when the expression is a direct name use.
slotThe OutputSlot of the position.
column()The declared Column the field directly returns, when it is one.

Operation::field($key) takes a position or a name. By name, it answers the field only when exactly one field has that name, and throws InvalidConstruction otherwise; it never picks the first of several. Operation::lookupField($name) answers one of:

ResultMeaning
UniqueFieldExactly one field has the name: ->field.
AbsentFieldThe shape is complete and no field has the name.
AmbiguousFieldsSeveral fields have the name: ->fields, in output order.
DependentFieldThe shape is open, or the name of a field depends on missing inputs, so further fields may have the name: ->candidates (the known fields with the name) and ->missing.

A field whose name is undecided could have any name, so while one field of the result is in that state, every lookup by name is a DependentField, and field($name) throws:

use SqlSemantics\Platform\Sqlite\Semantics;
use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\DependentField;

$semantics = new Semantics();
$query = $semantics->analyze('SELECT "a", 1 AS b FROM t');

$query->field(0)->name; // => null
$query->field(0)->slot->unnamed[0]->describe(); // => 'the declaration of relation t'
$query->lookupField('b') instanceof DependentField; // => true
$query->lookupField('b')->candidates[0] === $query->field(1); // => true
$query->field('b'); // throws InvalidConstruction

In SQLite, a double-quoted word is a column when a column has that name and a string otherwise, and the two are named differently; here t is not declared, so the name is undecided. MySQL leaves a name undecided where it depends on session state or a character set conversion, as described in its package.

Names are compared as the database compares output names: without regard to ASCII case for MySQL and SQLite, exactly for PostgreSQL (whose unquoted names are already folded to lower case). A field and its expression are reading results. They are not inputs for another query, and an expression of one operation can be resolved differently at another position.

Row shapes, output slots and open stars

A RowShape lists the OutputSlots of a query or a relation occurrence in order. shape() answers the shape of the returned rows; $facts->relation($occurrence)->shape answers the shape an input contributes.

An OutputSlot is an output position, not a declaration. A slot of a named table refers to the declared column (column); a slot that a join or a query re-exposes refers to the slot it comes from (origin) and carries its own type and NULL fact. declaration() follows origin back to the declared column. A slot without a name lists in unnamed the missing inputs its name depends on; the list is empty when the position has no name at all. When no slot of a relation has a column name that is looked up and one of its slots is unnamed in this way, the name resolves to a ConditionalColumn, not to a MissingColumn. An outer join therefore makes the re-exposed slot nullable without changing the declaration:

use SqlSemantics\Platform\Sqlite\Semantics;
use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;

$semantics = new Semantics();
$users = $semantics->analyze('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL)');
$orders = $semantics->analyze('CREATE TABLE orders (id INTEGER PRIMARY KEY, user_id INTEGER NOT NULL)');
$query = $semantics->analyze('SELECT u.name, o.user_id FROM users AS u LEFT JOIN orders AS o ON o.user_id = u.id', [$users, $orders]);
$userId = $orders->declarations()[0]->columns[1];

$query->field('name')->nullability; // => Nullability::NotNull
$query->field('user_id')->nullability; // => Nullability::Nullable
$query->field('user_id')->column() === $userId; // => true
$userId->nullability; // => Nullability::NotNull

A shape is open when missing declarations prevent listing every position. The known slots are still exact, and missing names the inputs that would complete the shape. A star that cannot be expanded stays an OpenStar in $facts->output->projection, carrying its missing inputs; no placeholder field is invented for it, and fields() answers null.

use SqlSemantics\Platform\Sqlite\Semantics;
use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\OpenStar;

$semantics = new Semantics();
$open = $semantics->analyze('SELECT * FROM users');

$open->fields(); // => null
$open->shape()->complete(); // => false
$open->shape()->missing[0]->describe(); // => 'the declaration of relation users'
$open->facts->output->projection[0] instanceof OpenStar; // => true

$users = $semantics->analyze('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)');
count($semantics->analyze('SELECT * FROM users', [$users])->fields()); // => 2

Resolutions

A name used as a value resolves to one of the following (namespace SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column). Finding a candidate and resolving uniquely are different outcomes.

ResolutionMeaning
ResolvedColumnExactly one slot: relation (the occurrence it was found in), slot (the slot visible at the use position), depth (how many enclosing queries lie between the use and the occurrence; 0 for the same query), declaration().
AliasTargetAn output field of the same query, named by its alias, for example in ORDER BY: field.
MissingColumnNo visible relation has the name, established by complete declarations. A diagnostic.
AmbiguousColumnSeveral slots have the name at the same precedence: candidates. A diagnostic.
ConditionalColumnThe outcome depends on missing declarations: candidates (known slots), relations (incompletely known occurrences that can own the name), missing.

A known candidate in a farther scope is not chosen while a nearer scope is incompletely known. A database package can add resolutions of its own, for example SqlSemantics\Platform\MySql\Statement\Name\AmbiguousAlias; test with instanceof and treat other classes as database-specific.

use SqlSemantics\Platform\Sqlite\Semantics;
use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\AliasTarget;

$semantics = new Semantics();
$users = $semantics->analyze('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)');
$orders = $semantics->analyze('CREATE TABLE orders (id INTEGER PRIMARY KEY, user_id INTEGER)');

$sorted = $semantics->analyze('SELECT name AS label FROM users ORDER BY label', [$users]);
$use = $sorted->facts->scalar($sorted->statement->orderBy[0]->expression)->resolution;
$use instanceof AliasTarget; // => true
$use->field === $sorted->field('label'); // => true

$correlated = $semantics->analyze('SELECT name, (SELECT count(*) FROM orders WHERE orders.user_id = users.id) AS n FROM users', [$users, $orders]);
$outer = $correlated->facts->scalar($correlated->statement->columns[1]->expression->query->where->right)->resolution;
$outer->depth; // => 1
$outer->relation === $correlated->inputRelation(); // => true

$semantics->analyze('SELECT id FROM users, orders', [$users, $orders]); // throws SemanticException

A relation name resolves to one of the following (namespace SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table), read from $facts->relation($occurrence)->table:

ResolutionMeaning
DeclaredTableExactly one declaration of the context: table, the very object that was passed.
CommonTableA common table expression of the same statement: definition.
MissingTableA complete context does not declare it. A diagnostic.
ConflictingTablesSeveral distinct declarations claim the name: candidates. A diagnostic.
UndeclaredTableAn open context has no declaration for it; it may exist: missing.
ConditionalTableA declaration was found in a later schema of the search path while an earlier schema is not completely known: candidates, missing.

Types and nullability

A type fact (SqlSemantics\Platform\Sqlite\Model\Statement\Type\TypeFact) is one of:

Type factMeaning
KnownThe rules of the database determine the type: descriptor.
ChoiceOne of several known types, decided by values at run time: alternatives.
NullOnlyThe type of a bare NULL, which has no other type.
DependentThe type cannot be decided because inputs are missing from the context: missing.
InvalidThe request itself is semantically wrong, so there is no type: cause, a diagnostic.

Dependent always names the missing inputs, and it is never used because a rule is not implemented: that is an ImplementationGap.

A TypeDescriptor is a data type of one database; name() names it as the database reports it. Each database package has its own closed set of descriptor classes, for example SQLite's Storage (the storage class of a computed value) and ColumnDomain (a declared column type with its affinity), PostgreSQL's Builtin and NamedOnPath, and MySQL's Integral, Character or Temporal.

use SqlSemantics\Platform\Sqlite\Semantics;
use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Choice;
use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;

$semantics = new Semantics();

$semantics->analyze('SELECT 1')->field(0)->type->descriptor; // => Storage::Integer
$choice = $semantics->analyze("SELECT CASE WHEN 1 THEN 1 ELSE 'a' END")->field(0)->type;
$choice instanceof Choice; // => true
array_map(static fn ($type) => $type->name(), $choice->alternatives); // => ['INTEGER', 'TEXT']
$semantics->analyze('SELECT NULL')->field(0)->nullability; // => Nullability::Nullable

Nullability is NotNull, Nullable or Dependent. Dependent means the answer needs information the context does not hold; treat such a value as possibly NULL.

Diagnostics

A semantic problem of grammatical SQL, such as a missing table, a missing or ambiguous column, a wrong number of values, a statement on a relation of the wrong kind, or a write into a generated column, is a value implementing SqlSemantics\Platform\Sqlite\Model\Statement\Fact\Diagnostic. analyze() throws SemanticException with these values in its diagnostics property, in derivation order. Warnings remain available through the query’s facts->diagnostics property. message() describes the problem for a person; the concrete class is the machine-readable kind. Some resolutions are diagnostics themselves (MissingColumn, AmbiguousColumn, MissingTable, ConflictingTables); the database packages define the others, mostly in Problem namespaces.

use SqlSemantics\Platform\Sqlite\Semantics;
use SqlSemantics\Platform\Sqlite\Model\Diagnostic\SemanticException;
use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\ArityMismatch;

$semantics = new Semantics();
$users = $semantics->analyze('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)');

try {
    $semantics->analyze('INSERT INTO users (id) VALUES (1, 2)', [$users]);
} catch (SemanticException $error) {
    $error->diagnostics[0] instanceof ArityMismatch; // => true
    $error->getMessage(); // => '2 values for 1 columns.'
}

Missing inputs

A MissingInput names information that a fact depends on and that the context does not hold. describe() explains it for a person.

Missing inputInformation
UndeclaredRelationThe declaration of a relation an open or partial context does not have.
IncompleteMembersThe complete column list of a declaration marked incomplete.
UndeclaredRoutineThe signature of a function or procedure that the language profile does not fix.
UndeclaredDomainThe definition of a named data type.
UnboundParameterThe value bound to a statement parameter, which also fixes its type.
SessionStateConnection state, such as a user variable or the current database.

They are in SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Missing. Database packages add their own, for example SQLite's RandomColumnName, the random suffix SQLite gives a column of a subquery whose name repeats five earlier names, and MySQL's NameConversion, the character set conversion the server applies to some strings when it names a column after them.

use SqlSemantics\Platform\Sqlite\Semantics;
use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Dependent;

$semantics = new Semantics();
$parameters = $semantics->analyze('SELECT ?, :name');

$parameters->field(0)->type instanceof Dependent; // => true
$parameters->field(0)->type->missing[0]->describe(); // => 'the value bound to parameter ?'
$parameters->field(1)->type->missing[0]->describe(); // => 'the value bound to parameter :name'

A version 1 context declares relations only. Routines, data types and session variables cannot be declared yet; a fact that needs them names them as missing inputs instead of guessing.