Skip to content

fix(condition): a time key fetched on MySQL restricts its own table - #1765

Open
dimitri-yatsenko wants to merge 1 commit into
masterfrom
fix/1762-time-key-restriction
Open

dimitri-yatsenko wants to merge 1 commit into
masterfrom
fix/1762-time-key-restriction

Conversation

@dimitri-yatsenko

Copy link
Copy Markdown
Member

Fixes #1762.

Summary

On MySQL, a time attribute is fetched as a datetime.timedelta. A restriction by that value raised "Unsupported value of type timedelta", so a key fetched from a table with a time attribute could not restrict its own table, delete by it, or populate below it.

  • condition.py: make_condition writes a timedelta as a TIME literal, [-]HH:MM:SS[.ffffff], computed from its total microseconds. Hours may exceed 24 and the value may be negative, as MySQL's TIME allows. str(timedelta) is not used, because '1 day, 2:00:00' is not a TIME literal.
  • A numpy.timedelta64 (the values of a timedelta64 DataFrame column, as from to_pandas()) is converted to a timedelta first; NaT restricts as IS NULL. A timedelta64 without a fixed unit (generic, years, months) is refused.

PostgreSQL fetches a time attribute as datetime.time, which was already supported. A timedelta within one day also restricts a PostgreSQL time attribute.

Tests

New integration tests in test_restriction_values.py, on MySQL and PostgreSQL:

  • a fetched key restricts its own table, with time and time(6), as a dict, a list of keys and a DataFrame;
  • delete by a fetched time key;
  • populate below a time key, with and without reserve_jobs;
  • negative and over-24-hour values on MySQL;
  • timedelta values against a PostgreSQL time attribute;
  • timedelta64 values without a fixed unit are refused.

populate(reserve_jobs=True) below a native time key is skipped on PostgreSQL: the jobs table cannot yet be declared there (#1763).

The full suite passes: 2068 passed, 20 skipped.

This conflicts with #1735 in condition.py and test_restriction_values.py (neighboring lines). Whichever merges second needs master merged in.

MySQL fetches a time attribute as a datetime.timedelta, which a
restriction refused, so a key fetched from a table with a time attribute
could not restrict that table, delete by it, or populate below it.
A timedelta is now written as [-]HH:MM:SS[.ffffff], with hours past 24
and negative values as MySQL's TIME allows. numpy.timedelta64 values,
as in a DataFrame from to_pandas(), are converted first; those without a
fixed unit are refused.

Fixes #1762.
@dimitri-yatsenko dimitri-yatsenko added this to the v2.3.7 milestone Nov 28, 2026
@dimitri-yatsenko dimitri-yatsenko added the bug Indicates an unexpected problem or unintended behavior label Nov 28, 2026

@MilagrosMarin MilagrosMarin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dimitri-yatsenko, this closes a real gap: a key fetched from a MySQL table with a time attribute now restricts its own table.

What I checked, with master merged in locally:

  • Full suite: 2068 passed, 20 skipped, on MySQL and PostgreSQL.
  • With master's condition.py restored, 12 of the new tests fail, so they guard the fix.
  • On MySQL, fetched keys for 8 values (0, 1 µs, 12:34:56.789012, 23:59:59.999999, -1 µs, -25h, 100:00:00.5, 838:59:59) each restrict to exactly 1 row and - leaves 7; table & table.to_pandas() matches all 8. On PostgreSQL the same holds for the 4 in-day values.
  • date and datetime(6) keys still restrict as before on both backends.

Minor points, none blocking:

  • A timedelta64 with sub-microsecond precision is silently truncated: np.timedelta64(1500, "ns") matches the row at 1 µs. It may be worth raising or noting this.
  • On PostgreSQL, a timedelta outside 0..24h reaches the server and comes back as a driver error (DatetimeFieldOverflow for 2 days, InvalidDatetimeFormat for -1 s) rather than a DataJointError.
  • Not from this PR: table & table.to_pandas() still fails on a datetime column ("Unsupported value of type datetime64"). Since timedelta64 is now converted, the datetime64 counterpart could be a small follow-up.

The conflict with #1735 is two neighboring additions in condition.py and the test file; keeping both sides resolves it.

Negative and over-24-hour MySQL values round-tripping through a fetched key was the case I most wanted to see covered, and it is.

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

bug Indicates an unexpected problem or unintended behavior

2 participants