Skip to content

feat: opt-in blob size limit and static-first foreign-key lookup - #1738

Open
dimitri-yatsenko wants to merge 6 commits into
masterfrom
fix/definition-blob-follow-ups
Open

dimitri-yatsenko wants to merge 6 commits into
masterfrom
fix/definition-blob-follow-ups

Conversation

@dimitri-yatsenko

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

Copy link
Copy Markdown
Member

Summary

Follow-ups to the 2.3.6 changes for table definitions read back from the database and for compressed blobs (GHSA-gmj8-m8g8-9wg9).

  • An opt-in limit on a blob's size. Since 2.3.6, a compressed blob is expanded only up to the size it records. The recorded size itself had no limit, so a blob that truly holds a very large array is still expanded in full. The new setting blob.max_decompressed_size (environment variable DJ_BLOB_MAX_DECOMPRESSED_SIZE) refuses a compressed blob whose recorded uncompressed size exceeds it. The check runs before anything is decompressed. It is unset by default, so nothing changes unless a deployment sets it.
  • Foreign-key references are looked up statically first. resolve_foreign_key_reference followed each .name step with getattr, which runs a property, or a custom __getattr__, of the object it walks. Each step now first uses inspect.getattr_static, which only looks the name up: module attributes, table classes and part tables resolve this way without running code. The dynamic getattr is the fallback, and it runs only when the static lookup finds no value: when the object has no such attribute (a module-level or class __getattr__ may supply it, as in a module that loads its tables lazily), or when the attribute is a descriptor such as a property. References through these resolve as before.
  • Every Python identifier is a name in a foreign-key reference. The reference parser read a name with the regular expression \w, which misses characters Python allows in identifiers: combining marks (the vowel sign in the Hindi किताब), · (U+00B7) and a few others. DataJoint 2.3.5 and earlier declared such names, so a table with a projected foreign key named this way could not be altered on 2.3.6. The parser now reads every Python identifier as a name, and still refuses anything else.
  • Projected names that no definition can hold. describe() writes -> Parent.proj(new="old") from the foreign-key attribute map read from the database. The referenced name is quoted, but the new name is written as is, since a keyword argument name cannot be quoted, and alter() parses that text back. A column created or renamed by SQL can have a name that is not an identifier (for example x=1, a-b, 1st_ref, a$b, or a name with a quote or a backslash); no form of the reference holds such a name, and the text describe() writes for it may parse as a different foreign key. For such a name, describe() writes it as before and emits a warning, and alter() raises a DataJointError instead of parsing that text.
  • dj.Diagram renders a schema that holds such a table. A diagram writes each table's describe() into its tooltip, so one table could stop the diagram of the whole schema. A table whose describe() raises a DataJointError now gets a tooltip with only its name. The warning describe() emits for a name that alter() cannot read back is ignored while the diagram writes a tooltip, so it does not affect diagrams, also under -W error. Two characters made the DOT source invalid, and are now escaped:
    • Double quotes in the foreign-key attribute map passed to Graphviz: a column named a"b or a'b.
    • Backslashes in the node and edge tooltips, which pydot quotes without escaping them: a referenced column named p\ escaped the closing quote of the edge tooltip, and for a referenced column named p"id, the \" that describe() writes closed the node tooltip early. Backslashes are now doubled, and Graphviz shows each pair as one.

Refs #1717.

Behavior change

  • With blob.max_decompressed_size set, fetching a compressed blob whose recorded uncompressed size exceeds it raises DataJointError. Unset (the default), nothing changes.
  • A foreign-key reference may use any Python identifier as a name, as before 2.3.6.
  • For a projected foreign-key attribute whose new name is not a Python identifier, describe() emits a UserWarning and writes the name as before, and alter() raises DataJointError. Names that are identifiers, Python keywords included, are written and read back as before.
  • dj.Diagram renders a schema in which a table's describe() raises DataJointError; that table's tooltip shows only its name.
  • dj.Diagram does not emit the warning of describe(), and renders a schema whose column names hold double quotes or backslashes. Its tooltips show those characters as written.
  • Foreign-key references resolve as before, including through a module or class __getattr__ and through properties. Code of the object walked runs only for those cases, no longer for names the static lookup finds.

Changes

  • src/datajoint/settings.py: a blob settings group with max_decompressed_size (None or a non-negative integer).
  • src/datajoint/blob.py: unpack() and Blob take max_decompressed_size and check the recorded size before decompressing. Uncompressed blobs are not affected: they are already in memory.
  • src/datajoint/builtin_codecs/blob.py: BlobCodec.decode takes context and passes the limit from the connection's config.
  • src/datajoint/declare.py:
    • _lookup_attribute() for .name steps: inspect.getattr_static first, getattr only when that finds no attribute or finds a descriptor.
    • The reference tokenizer takes a run of word characters and non-ASCII characters as a name token, and str.isidentifier() decides whether it is a name.
  • src/datajoint/table.py:
    • _is_projection_name() writes the new name into a .proj() call and parses it back with parse_foreign_key_reference, the parser alter() uses; the name must come back unchanged.
    • The body of describe() moves to _describe(context, strict), which collects the new names that are not read back. describe() warns for them; alter() calls _describe(..., strict=True), which raises.
  • src/datajoint/diagram.py:
    • The tooltip falls back to the table name when describe() raises DataJointError, and describe() runs with warnings ignored.
    • The node and edge tooltips double their backslashes, and _encapsulate_edge_attributes escapes double quotes.

Tests

  • Unit (tests/unit/test_blob_decompression.py):
    • A blob that records its true size and has a high compression ratio (50 MB of zeros in about 50 kB) decodes without a limit and at a limit equal to its size. Below that, it is refused, with peak memory under 5 MB.
    • The limit does not apply to an uncompressed blob.
    • The setting is unset by default, can be read from the environment, and rejects negative values.
    • BlobCodec applies the limit from the config it is given.
    • MATLAB mYm blobs, with 64-bit and with 32-bit dimensions (use_32bit_dims), go through the limit and through the existing size-mismatch checks.
  • Unit (tests/unit/test_foreign_key_reference.py):
    • A name the static lookup finds is resolved without running __getattr__, on an object and on a module that both define one.
    • Static lookup comes first: for an object whose class attribute is a table and whose __getattribute__ returns another table, or raises, the reference resolves to the class attribute and __getattribute__ does not run. getattr alone gives the other table, or the error, so this test fails with getattr alone.
    • A lazy module (module-level __getattr__), a class __getattr__ and a property still resolve, and each runs exactly once. These three fail with static lookup alone.
    • A name found by neither lookup raises could not be resolved: no attribute ....
    • Class and module attributes, and part tables, resolve.
    • Projected names that are read back (parent_ref, the keywords class, from and lambda, _private, __x, Größe, and किताब_id, x·y, ℘x, x‿y, whose characters \w does not match) are accepted, with a referenced name holding quotes and a backslash. Names that are not identifiers (x=1, names with ", ', \, a space, a no-break space, #, ), ., -, $, a full-width = or a line break, a leading ASCII or Arabic-Indic digit, a="x",b, the empty name) are refused.
    • किताब.Part and x·y.Part resolve.
  • Integration (tests/integration/test_definition_blob_follow_ups.py), on MySQL and PostgreSQL:
    • Fetching a <blob> with the limit set below, and above, its size.
    • A column comment containing # and ', and a projected foreign key, through describe() and alter(). The test adds an attribute with such a comment, and a second alter() finds nothing to change.
    • A projected foreign key named class, from, किताब_id or x·y is declared, describe() writes -> Parent.proj(<name>="parent_id"), alter() reads it back with nothing to change, and dj.Diagram(schema).make_svg() renders.
    • After a foreign-key column is renamed in the database to x=1, a"b, a'b, a\b, a\"b, a-b, a b, 1st_ref or a$b:
      • describe() warns, naming it, and writes it as is with the rest of the definition; alter() raises and names it.
      • The diagram of the schema, read through dj.VirtualModule, renders with make_svg() with the warning of describe() raised as an error.
    • After the referenced column parent_id is renamed in the database to p"id or p\ (ending in a backslash), the diagram renders with that warning raised as an error, and its edge tooltip shows parent_ref ← p"id or parent_ref ← p\.
    • A table whose describe() raises DataJointError gets its name as its tooltip; the other table keeps its definition, and make_svg() renders.
  • The new tests fail before the change, with these exceptions. The foreign-key lookup tests other than test_static_lookup_comes_before_getattr guard behavior getattr alone also had. The keyword tests pass on master, which writes every name as is; they guard against refusing names that work. test_diagram_of_schema_with_projected_name_not_read_back passes on master for names without a double quote (x=1, a\b, a-b, a b, 1st_ref, a$b), because master emits no warning; those cases check that the new warning does not reach the diagram. The a"b, a'b and a\"b cases and test_diagram_with_referenced_column_renamed fail on master. The full suite passes locally on MySQL and PostgreSQL.

Documentation

datajoint/datajoint-docs#317 documents blob.max_decompressed_size (DJ_BLOB_MAX_DECOMPRESSED_SIZE) and merges after this PR.

Not in this PR

The issue asks to resolve foreign-key references without running properties. This PR looks names up statically first, but keeps getattr as the fallback, so a property, or a module or class __getattr__, on the path of a reference still runs: dropping the fallback would break references through modules that load their tables lazily. Whether to drop it stays open in #1717, so this PR refers to the issue instead of closing it.

Notes for reviewers

@dimitri-yatsenko dimitri-yatsenko added this to the v2.3.7 milestone Nov 21, 2026
@dimitri-yatsenko dimitri-yatsenko changed the title feat: opt-in blob size limit (blob.max_decompressed_size); foreign-key references resolve without running properties feat: opt-in blob size limit (blob.max_decompressed_size); foreign-key references are looked up statically first, with getattr as the fallback Nov 21, 2026
…me check in describe()

- blob.max_decompressed_size (DJ_BLOB_MAX_DECOMPRESSED_SIZE): refuse a
  compressed blob whose recorded uncompressed size exceeds it, before
  decompressing. Unset by default, so nothing changes unless it is set.
- resolve_foreign_key_reference looks each .name step up with
  inspect.getattr_static.
- describe() checks each projected foreign-key attribute name it reads
  from the database.

Refs #1717.
resolve_foreign_key_reference used inspect.getattr_static alone, so a
reference through a module __getattr__ (a module that loads its tables
lazily), a class __getattr__ or a property no longer resolved. The static
lookup still runs first and resolves module attributes, table classes and
part tables without running code. getattr runs only when the static lookup
finds no attribute, or finds a descriptor such as a property.
…ot read back

Python keywords such as `class` are valid attribute names, and
parse_foreign_key_reference reads them back as .proj() keyword arguments.
The identifier check refused them, so describe() failed for tables it
described before. The check now writes the new name into a .proj() call and
parses it back: a name read back as itself is written as before; one that is
not (`x=1`, quotes, backslashes) raises.

Add a test that tells the static-first lookup apart from getattr alone: an
object whose __getattribute__ returns another table, or raises, for a name
that is a table class attribute.
…rning; diagrams render such tables

The foreign-key reference parser took a name only from \w characters, which
miss characters that Python identifiers allow, such as combining marks and
U+00B7. DataJoint 2.3.5 and earlier declared such names. The parser now takes
every Python identifier as a name, so describe() writes them in a form that
alter() reads back.

For a projected foreign-key attribute whose new name is not an identifier (a
column named by SQL, such as x=1 or a-b), no form of the reference holds the
name. describe() no longer raises for it: it writes the name as is, as master
does, and emits a warning. alter() raises for such a table instead of parsing
that text.

dj.Diagram gives a table whose describe() raises DataJointError a tooltip with
only its name, and escapes double quotes in the attr_map edge attribute, which
made the DOT source invalid for a column named a"b or a'b. One table no longer
stops the diagram of its schema.
pydot quotes a tooltip and escapes its double quotes, but leaves its
backslashes as they are. A referenced column named p\ by SQL escaped the
closing quote of the renamed foreign key's edge tooltip, and the \" that
describe() writes for a referenced column named p"id closed the child's
node tooltip early: Graphviz rejected the DOT source. Both tooltips now
double their backslashes, which Graphviz shows as one.

The warning describe() emits for a projected name that alter() cannot read
back is ignored while the diagram writes a tooltip, so it does not affect
diagrams, also under -W error.
@dimitri-yatsenko
dimitri-yatsenko force-pushed the fix/definition-blob-follow-ups branch from 62968b5 to 259b1b8 Compare November 21, 2026 08:07
@dimitri-yatsenko dimitri-yatsenko changed the title feat: opt-in blob size limit (blob.max_decompressed_size); foreign-key references are looked up statically first, with getattr as the fallback feat: opt-in blob size limit; static-first foreign-key lookup; describe() and dj.Diagram handle unreadable projected names Nov 21, 2026
The diagram tests raised every warning as an error. With the minimum
networkx version, to_pydot emits a DeprecationWarning of its own, which
failed them. They now raise only the warning describe() emits for a name
that alter() cannot read back, which the diagram must not let through.
@dimitri-yatsenko dimitri-yatsenko changed the title feat: opt-in blob size limit; static-first foreign-key lookup; describe() and dj.Diagram handle unreadable projected names feat: opt-in blob size limit and static-first foreign-key lookup Nov 21, 2026

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant