Skip to content
Open
169 changes: 167 additions & 2 deletions src/datajoint/adapters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,129 @@
import hashlib
import re
from abc import ABC, abstractmethod
from typing import Any
from typing import Any, NamedTuple

from ..errors import DataJointError

# Parts of a JSON path type annotation (``attr.path:type``). The annotation has
# already passed ``condition._JSON_TYPE_PATTERN``; this only splits it.
_JSON_TYPE_PARTS = re.compile(
r"\s*(?P<name>(?:un)?signed(?:\s+int(?:eger)?)?|double(?:\s+precision)?|char(?:acter)?(?:\s+varying)?|[a-z]+\d?)"
r"(?:\s*\((?P<args>\s*\d+\s*(?:,\s*\d+\s*)?)\))?"
r"(?:\s+(?P<zone>with|without)\s+time\s+zone)?"
r"(?:\s+(?:character\s+set|charset)\s+(?P<charset>\w+))?"
r"(?:\s+collate\s+(?P<collate>\w+))?\s*",
re.IGNORECASE,
)

# Type names accepted in a JSON path annotation, grouped by the kind of value they name.
# Each adapter maps every name onto its own types, or rejects it with a DataJointError.
JSON_INTEGER_TYPES = frozenset(
"signed, signed int, signed integer, unsigned, unsigned int, unsigned integer, "
"int, int2, int4, int8, integer, tinyint, smallint, mediumint, bigint, year".split(", ")
)
JSON_FLOAT_TYPES = frozenset("real, float, float4, float8, double, double precision".split(", "))
JSON_DECIMAL_TYPES = frozenset(("decimal", "numeric", "dec"))
JSON_BOOLEAN_TYPES = frozenset(("bool", "boolean"))
JSON_STRING_TYPES = frozenset("char, character, nchar, varchar, char varying, character varying, text".split(", "))
JSON_BINARY_TYPES = frozenset(("binary", "varbinary", "bytea"))
JSON_JSON_TYPES = frozenset(("json", "jsonb"))
JSON_TIME_TYPES = frozenset(("date", "time", "timetz", "datetime", "timestamp", "timestamptz", "interval"))
JSON_OTHER_TYPES = frozenset(("bit", "uuid"))
_JSON_TYPE_NAMES = (
JSON_INTEGER_TYPES
| JSON_FLOAT_TYPES
| JSON_DECIMAL_TYPES
| JSON_BOOLEAN_TYPES
| JSON_STRING_TYPES
| JSON_BINARY_TYPES
| JSON_JSON_TYPES
| JSON_TIME_TYPES
| JSON_OTHER_TYPES
)

# How many arguments a type takes: precision and scale for a decimal; a length, a float
# precision, or fractional seconds; or an integer display width, which is dropped.
# Every other type takes none.
_JSON_TYPE_MAX_ARGS = {
**dict.fromkeys(JSON_DECIMAL_TYPES, 2),
**dict.fromkeys(
JSON_INTEGER_TYPES
| JSON_STRING_TYPES
| {"float", "binary", "varbinary", "bit", "time", "timetz", "datetime", "timestamp", "timestamptz", "interval"},
1,
),
}

# Annotations that work on every backend, for error messages
JSON_PORTABLE_TYPES = (
"signed, unsigned, int, integer, smallint, bigint, decimal(p,s), numeric(p,s), real, float, float(p), "
"double, bool, boolean, char(n), varchar(n), text, date, time, datetime, timestamp, json"
)


class JsonType(NamedTuple):
"""A JSON path type annotation split into its parts (see ``parse_json_type``)."""

name: str # lower case with single spaces, e.g. 'int', 'double precision'
args: str # '' or the argument list as written, e.g. '(10, 2)'
zone: str | None # 'with' or 'without' (time zone), or None
charset: str | None
collate: str | None

@property
def single_precision(self) -> bool:
"""
Whether a floating-point type is single precision.

As in PostgreSQL, ``real``, ``float4`` and ``float(p)`` with ``p`` up to 24 are
single precision; ``float``, ``float8``, ``double``, ``double precision`` and
``float(p)`` with ``p`` from 25 to 53 are double precision.
"""
return self.name in ("real", "float4") or (self.name == "float" and bool(self.args) and int(self.args[1:-1]) <= 24)


def parse_json_type(annotation: str) -> JsonType:
"""
Split a JSON path type annotation into its parts.

Parameters
----------
annotation : str
Type annotation from an ``attr.path:type`` key, e.g. ``'decimal(10, 2)'``.

Returns
-------
JsonType
Name, arguments, time zone qualifier, character set, and collation.

Raises
------
DataJointError
If the annotation is not a known type name, gives a type more arguments than
it takes (e.g. ``float(10,2)``), gives ``float`` a precision outside 1 to 53,
or combines a qualifier with a type that does not take it (e.g. ``int with
time zone``).
"""
match = _JSON_TYPE_PARTS.fullmatch(annotation)
name = " ".join(match["name"].lower().split()) if match else None
if match is None or name not in _JSON_TYPE_NAMES:
raise DataJointError(f"Unsupported JSON path type `{annotation}`. Portable types: {JSON_PORTABLE_TYPES}.")
max_args = _JSON_TYPE_MAX_ARGS.get(name, 0)
if match["args"] and match["args"].count(",") + 1 > max_args:
raise DataJointError(
f"JSON path type `{annotation}`: {name} takes "
+ ("no arguments." if max_args == 0 else "one argument. Only decimal and numeric take precision and scale.")
)
if name == "float" and match["args"] and not 1 <= int(match["args"]) <= 53:
# MySQL would take float(0) and PostgreSQL would not; neither takes more than 53
raise DataJointError(f"JSON path type `{annotation}`: float takes a precision from 1 to 53.")
if match["zone"] and name not in ("time", "datetime", "timestamp"):
raise DataJointError(f"JSON path type `{annotation}`: only time and timestamp types take a time zone qualifier.")
if (match["charset"] or match["collate"]) and name not in JSON_STRING_TYPES:
raise DataJointError(f"JSON path type `{annotation}`: only string types take a character set or collation.")
args = f"({match['args']})" if match["args"] else ""
return JsonType(name, args, match["zone"] and match["zone"].lower(), match["charset"], match["collate"])


class DatabaseAdapter(ABC):
Expand Down Expand Up @@ -1171,20 +1293,63 @@ def json_path_expr(self, column: str, path: str, return_type: str | None = None)
path : str
JSON path (e.g., 'field' or 'nested.field').
return_type : str, optional
Return type specification (MySQL-specific).
Type to cast the extracted value to: a MySQL ``JSON_VALUE ... RETURNING``
spelling (``signed``, ``unsigned``, ``decimal(p,s)``, ``char(n)``,
``double``, ``datetime``, ...) or a PostgreSQL spelling (``int``,
``integer``, ``bigint``, ``numeric(p,s)``, ``varchar(n)``, ``text``,
``boolean``, ``timestamp``, ...). Each backend maps the name onto its
own type (see ``parse_json_type`` for the accepted names).

Returns
-------
str
Database-specific JSON extraction SQL expression.

Raises
------
DataJointError
If the backend has no equivalent of the type (e.g. ``uuid`` on MySQL).

Examples
--------
MySQL: json_value(`column`, _utf8mb4'$.path' returning type)
PostgreSQL: jsonb_extract_path_text("column", 'path_part1', 'path_part2')
"""
...

@abstractmethod
def json_path_equals(self, column: str, path: str, value: str) -> str:
"""
Generate a condition comparing the JSON value at a path with a JSON literal.

The comparison is between JSON values, not text: numbers compare
numerically (``64`` equals ``64.0``), booleans match only JSON booleans,
and a value of another JSON type (e.g. a string) is simply unequal.
PostgreSQL compares numbers exactly. MySQL holds a JSON integer outside
the range -2**63 to 2**64 - 1, and any number with a fraction or an
exponent, as a 64-bit float, and compares the rounded values.

Parameters
----------
column : str
Column name containing JSON data.
path : str
JSON path (e.g., 'field' or 'nested.field').
value : str
JSON literal (e.g., '64', '2.5', 'true').

Returns
-------
str
Database-specific SQL condition.

Examples
--------
MySQL: json_extract(`column`, _utf8mb4'$.path') = cast('64' as json)
PostgreSQL: jsonb_extract_path("column", 'path') = '64'::jsonb
"""
...

def translate_expression(self, expr: str) -> str:
"""
Translate SQL expression for backend compatibility.
Expand Down
97 changes: 90 additions & 7 deletions src/datajoint/adapters/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@

from __future__ import annotations

import re
import ssl
from typing import Any

import pymysql as client
from pymysql.constants import CLIENT, CR

from .. import errors
from .base import DatabaseAdapter
from .base import (
JSON_BINARY_TYPES,
JSON_BOOLEAN_TYPES,
JSON_DECIMAL_TYPES,
JSON_FLOAT_TYPES,
JSON_INTEGER_TYPES,
JSON_JSON_TYPES,
JSON_PORTABLE_TYPES,
JSON_STRING_TYPES,
DatabaseAdapter,
JsonType,
parse_json_type,
)

# Core type mapping: DataJoint core types → MySQL types
CORE_TYPE_MAP = {
Expand Down Expand Up @@ -291,7 +304,6 @@ def quote_string(self, value: str) -> str:

def get_master_table_name(self, part_table: str) -> str | None:
"""Extract master table name from part table (MySQL backtick format)."""
import re

# MySQL format: `schema`.`master__part`
match = re.match(r"(?P<master>`\w+`.`#?\w+)__\w+`", part_table)
Expand Down Expand Up @@ -785,7 +797,6 @@ def get_constraint_info_sql(self, constraint_name: str, schema_name: str, table_

def parse_foreign_key_error(self, error_message: str) -> dict[str, str | list[str] | None] | None:
"""Parse MySQL foreign key violation error message."""
import re

# MySQL FK error pattern with backticks
pattern = re.compile(
Expand Down Expand Up @@ -942,23 +953,96 @@ def json_path_expr(self, column: str, path: str, return_type: str | None = None)
path : str
JSON path (e.g., 'field' or 'nested.field').
return_type : str, optional
Return type specification (e.g., 'decimal(10,2)').
Type annotation (e.g., 'decimal(10,2)'). PostgreSQL spellings such as
'int', 'numeric(10,2)' or 'text' are mapped onto the RETURNING types.

Returns
-------
str
MySQL json_value() expression.

Raises
------
DataJointError
If the annotation names a type that MySQL cannot return (e.g. 'uuid').

Examples
--------
>>> adapter.json_path_expr('data', 'field')
"json_value(`data`, _utf8mb4'$.field')"
>>> adapter.json_path_expr('data', 'value', 'decimal(10,2)')
"json_value(`data`, _utf8mb4'$.value' returning decimal(10,2))"
>>> adapter.json_path_expr('data', 'n', 'int')
"json_value(`data`, _utf8mb4'$.n' returning signed)"
"""
quoted_col = self.quote_identifier(column)
expr = f"json_value({quoted_col}, _utf8mb4{self.quote_string('$.' + path)}"
if not return_type:
return expr + ")"
json_type = parse_json_type(return_type)
expr += f" returning {self._json_returning_type(json_type, return_type)})"
# RETURNING takes no COLLATE clause; collate the returned string instead
return expr + (f" collate {json_type.collate}" if json_type.collate else "")

@staticmethod
def _json_returning_type(json_type: JsonType, annotation: str) -> str:
"""Map a parsed JSON path type annotation onto a ``JSON_VALUE ... RETURNING`` type."""
name, args = json_type.name, json_type.args
if name in JSON_INTEGER_TYPES:
# Integer display widths such as int(11) have no RETURNING equivalent
return "unsigned" if name.startswith("unsigned") else "signed"
if name in JSON_BOOLEAN_TYPES:
return "signed"
if name in JSON_FLOAT_TYPES:
# Precision as on PostgreSQL. MySQL's REAL is DOUBLE, and RETURNING FLOAT
# is single precision with or without a precision, so neither is passed on.
return "float" if json_type.single_precision else "double"
if name in JSON_DECIMAL_TYPES:
# A bare DECIMAL is DECIMAL(10,0) on MySQL and would round the value.
# DECIMAL(65,30) is the widest MySQL decimal, closest to PostgreSQL's
# unconstrained numeric: exact, and returned as a Python Decimal.
return "decimal" + (args or "(65,30)")
if name in JSON_STRING_TYPES:
# CHAR without a length is unlimited in RETURNING, like PostgreSQL text
charset = f" character set {json_type.charset}" if json_type.charset else ""
return "char" + args + charset
if name in JSON_BINARY_TYPES:
return "binary" + (args if name == "binary" else "")
if name in JSON_JSON_TYPES:
return "json"
if name in ("date", "time", "datetime", "timestamp") and json_type.zone != "with":
return {"timestamp": "datetime"}.get(name, name) + (args if name != "date" else "")
raise errors.DataJointError(
f"JSON path type `{annotation}` has no MySQL equivalent. Types that work on both "
f"MySQL and PostgreSQL: {JSON_PORTABLE_TYPES}."
)

def json_path_equals(self, column: str, path: str, value: str) -> str:
"""
Generate a condition comparing the JSON value at a path with a JSON literal.

Parameters
----------
column : str
Column name containing JSON data.
path : str
JSON path (e.g., 'field' or 'nested.field').
value : str
JSON literal (e.g., '64', '2.5', 'true').

Returns
-------
str
MySQL condition comparing two JSON values.

Examples
--------
>>> adapter.json_path_equals('data', 'n', '64')
"json_extract(`data`, _utf8mb4'$.n') = cast('64' as json)"
"""
quoted_col = self.quote_identifier(column)
return_clause = f" returning {return_type}" if return_type else ""
return f"json_value({quoted_col}, _utf8mb4{self.quote_string('$.' + path)}{return_clause})"
quoted_path = self.quote_string("$." + path)
return f"json_extract({quoted_col}, _utf8mb4{quoted_path}) = cast({self.quote_string(value)} as json)"

def translate_expression(self, expr: str) -> str:
"""
Expand All @@ -978,7 +1062,6 @@ def translate_expression(self, expr: str) -> str:
str
Translated expression for MySQL.
"""
import re

# STRING_AGG(col, 'sep') → GROUP_CONCAT(col SEPARATOR 'sep')
def replace_string_agg(match):
Expand Down
Loading
Loading