Skip to content

docs: JSON path annotations and value restrictions work the same on MySQL and PostgreSQL - #318

Open
dimitri-yatsenko wants to merge 5 commits into
mainfrom
docs/json-path-portability
Open

dimitri-yatsenko wants to merge 5 commits into
mainfrom
docs/json-path-portability

Conversation

@dimitri-yatsenko

@dimitri-yatsenko dimitri-yatsenko commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Summary

Documents datajoint-python PR 1607, which fixes datajoint-python issue 1563 and datajoint-python issue 1564. The JSON path access section of the type-system spec described both issues as open defects. This PR describes the behavior after the fix.

Merge this PR after datajoint-python PR 1607. The "Changed in 2.3.7" note assumes PR 1607 ships in 2.3.7.

Changes

In src/reference/specs/type-system.md, section "JSON path access":

  • The warning "Two defects to know about when filtering" is removed.
  • A new "Type annotations" subsection lists how each common annotation maps onto MySQL and PostgreSQL. varchar without a length is cast as written on PostgreSQL, where it has no length limit.
  • It states that each integer and floating-point annotation except unsigned has the same range on both backends, and that each has the same precision (MySQL returns single-precision values with 6 significant digits, and MySQL 8.0.21 compares them in double precision; both are listed with the results that still differ). Floating-point precision follows PostgreSQL: float, float8, double and float(p) with p from 25 to 53 are double precision, and real, float4 and float(p) with p up to 24 are single precision (MySQL's own REAL is double precision and its RETURNING FLOAT single precision for every p). Every integer annotation except unsigned is a 64-bit signed integer, so int and smallint do not narrow the range.
  • It states that only decimal and numeric take two arguments (precision and scale), that char(n), varchar(n), float(p) and the time types take one, and that a type given more arguments than it takes, or float(p) with p outside 1 to 53, raises a DataJointError.
  • It lists the results that still differ between the backends:
    • a decimal without precision is exact on PostgreSQL; on MySQL it is decimal(65,30), which returns 30 decimal places, rounds beyond them (1e-40 becomes 0) and returns NULL from 1e35 in magnitude, and MySQL stores a JSON number that has a fraction as a 64-bit float;
    • a single-precision annotation returns 6 significant digits on MySQL (1234567.89 becomes 1234570.0) and the shortest decimal that reads back as the same single-precision value on PostgreSQL (1234567.9), and MySQL 8.0.21 compares it in double precision, so {"specs.gain:real": 0.1} matches a stored 0.1 there and not on PostgreSQL, MySQL 8.0.46 or MySQL 8.4;
    • unsigned is a 64-bit unsigned integer on MySQL and bigint on PostgreSQL: a negative value is NULL on MySQL and returned on PostgreSQL, and a value from 2**63 to 2**64 - 1 is returned on MySQL and fails the query on PostgreSQL;
    • a boolean comes back as 1/0 on MySQL and True/False on PostgreSQL;
    • a json or jsonb annotation returns the JSON text as a string on MySQL and the decoded value on PostgreSQL;
    • a value that the type cannot hold ("abc" under :int or :date, or an integer outside -2**63 to 2**63 - 1 under an integer annotation other than unsigned) is NULL on MySQL and fails the query on PostgreSQL, and an integer annotation on a number with a fraction rounds it on MySQL and fails the query on PostgreSQL;
    • a string longer than char(n) is NULL on MySQL and cut on PostgreSQL;
    • time, datetime and timestamp without (n) drop the fraction of a second on MySQL and keep microseconds on PostgreSQL (write (6) for the same value), and a time outside 00:00:00 to 24:00:00 comes back on MySQL and fails the query on PostgreSQL;
    • uuid, interval, bit, timetz and timestamptz raise a DataJointError on MySQL, and bit alone becomes bit varying on PostgreSQL;
    • MySQL holds a JSON integer outside -2**63 to 2**64 - 1, and every JSON number with a fraction or an exponent, as a 64-bit float, so a restriction by such a number compares rounded values on MySQL and exact values on PostgreSQL: {"specs.count": 2**70} also matches a stored 2**70 + 1 on MySQL. Integers within that range and numbers with up to 15 significant digits match the same rows on both.
  • A new "Restricting by value" subsection describes the comparison: a string compares with the extracted text; a bool or number compares with the JSON value, with the same result on both backends for integers within the 64-bit range and numbers with up to 15 significant digits. It covers numbers by value (64 matches 64.0), booleans matching only JSON booleans, numbers never matching JSON strings, missing keys and JSON null under -, and NaN and infinity raising.
  • A "Changed in 2.3.7" note says what earlier releases did and how to restrict on them, including that float was single precision on MySQL and real single precision on PostgreSQL, and that signed, unsigned, int and smallint were 32- or 16-bit on PostgreSQL.
  • Examples use :int instead of :unsigned.

Tests

  • mkdocs build --strict runs in CI.
  • Each statement was checked against PR 1607 on MySQL 8.0 and PostgreSQL 15, and the float and integer statements also on MySQL 8.0.46 and 8.4.

…ortable

Describe the JSON path behavior of datajoint-python PR 1607: every accepted
type annotation maps onto both backends or raises a DataJointError, a decimal
without precision keeps its value, and a bool or number restriction compares
JSON values with the same result on MySQL and PostgreSQL. Remove the warning
that described the two fixed defects as open.
…d differences

A decimal without precision is exact on PostgreSQL only: MySQL's
decimal(65,30) rounds beyond 30 decimal places and returns NULL from 1e35.
Add the json/jsonb result types, values the type cannot hold, and integer
annotations on fractional numbers to the differences. varchar without a
length is cast as written on PostgreSQL. Only decimal and numeric take two
arguments; bit alone becomes bit varying on PostgreSQL.
…ion and range on both backends

- float, float8, double and float(p) with p from 25 to 53 are double
  precision on both backends; real, float4 and float(p) with p up to 24 are
  single precision. float(p) outside 1 to 53 raises a DataJointError.
- Every integer annotation except unsigned is 64-bit on both backends.
- Results that still differ: the digits a single-precision value comes back
  with, MySQL 8.0.21 comparing it in double precision, unsigned outside 0 to
  2**63 - 1, integers outside the 64-bit range, and MySQL comparing JSON
  integers beyond 64 bits and numbers with more than 15 significant digits as
  64-bit floats.
…s to the same range and precision

The lead before the list of annotation mappings said every annotation has the
same precision and range on both backends. unsigned has a different range
(0 to 2**64 - 1 on MySQL, bigint on PostgreSQL), and MySQL returns
single-precision values with 6 significant digits and MySQL 8.0.21 compares
them in double precision. Name both exceptions in the lead.
… floating-point annotations; time types differ

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON path type annotation is not portable: data.n:int works on PostgreSQL, raises on MySQL

1 participant