|
| 1 | +"""The bridge's bulk writers have one temporal encoding, and it is the engine's (#10522). |
| 2 | +
|
| 3 | +``to_json_list()`` (``RowBatcher``) and ``to_columns()`` (``ColumnBatcher``) used to carry an encoder of their |
| 4 | +own for a date, written when the engine had no type for a ``LocalDate``. The engine has since fixed what that |
| 5 | +encoder worked around (a ``LocalDate`` is UTC midnight whatever the JVM zone, a ``Calendar`` is its milliseconds, |
| 6 | +a date past the range of a long is text), so the private copy only differed from the engine's generic JSON writer |
| 7 | +in the one place it was worse: a date past year 292 million threw and failed the whole batch. |
| 8 | +
|
| 9 | +What these tests hold is the rule the issue asks for, in a form that survives the next engine-side type mapping: |
| 10 | +for every temporal type the JSON writer, the columnar writer and ``JSONObject.put`` write the same number, at the |
| 11 | +top level and inside a list or a map, in a record row and in a projection row. The one number that differs, a |
| 12 | +DATE column at the top level of ``Result.to_json()`` of a record row (days), is the engine's round-trip form of |
| 13 | +the record and is pinned separately, so its difference is a decision written down and not a drift. |
| 14 | +
|
| 15 | +Author: Luca Garulli ([email protected]) |
| 16 | +""" |
| 17 | + |
| 18 | +import datetime as dt |
| 19 | +import json |
| 20 | + |
| 21 | +import arcadedb_embedded as arcadedb # noqa: F401 (starts the JVM through the fixtures) |
| 22 | +import jpype |
| 23 | +import numpy as np |
| 24 | +import pytest |
| 25 | +from arcadedb_embedded import results |
| 26 | +from arcadedb_embedded.exceptions import ArcadeDBError |
| 27 | + |
| 28 | +MIDNIGHT_UTC_2024_01_02 = 1704153600000 |
| 29 | +DAY_MS = 86_400_000 |
| 30 | + |
| 31 | + |
| 32 | +def _in_memory(rows): |
| 33 | + """A result set over rows built in Java, for values SQL cannot produce.""" |
| 34 | + internal = jpype.JClass("com.arcadedb.query.sql.executor.InternalResultSet")() |
| 35 | + result_internal = jpype.JClass("com.arcadedb.query.sql.executor.ResultInternal") |
| 36 | + for row in rows: |
| 37 | + java_row = result_internal() |
| 38 | + for key, value in row.items(): |
| 39 | + java_row.setProperty(key, value) |
| 40 | + internal.add(java_row) |
| 41 | + return internal |
| 42 | + |
| 43 | + |
| 44 | +def _java_temporals(): |
| 45 | + """One value of every temporal class a row can hold, with sub-millisecond digits and a pre-epoch instant.""" |
| 46 | + java = jpype.JClass |
| 47 | + instant = java("java.time.Instant").parse("2024-01-02T03:04:05.123456789Z") |
| 48 | + calendar = java("java.util.Calendar").getInstance( |
| 49 | + java("java.util.TimeZone").getTimeZone("UTC") |
| 50 | + ) |
| 51 | + calendar.setTimeInMillis(jpype.JLong(1704164645123)) |
| 52 | + return { |
| 53 | + "local_date": java("java.time.LocalDate").of(2024, 1, 2), |
| 54 | + "pre_epoch_date": java("java.time.LocalDate").of(1969, 12, 31), |
| 55 | + "local_date_time": java("java.time.LocalDateTime").of( |
| 56 | + 2024, 1, 2, 3, 4, 5, 123_456_789 |
| 57 | + ), |
| 58 | + "pre_epoch_date_time": java("java.time.LocalDateTime").of( |
| 59 | + 1969, 12, 31, 23, 59, 59, 999_500_000 |
| 60 | + ), |
| 61 | + "instant": instant, |
| 62 | + "zoned": java("java.time.ZonedDateTime").ofInstant( |
| 63 | + instant, java("java.time.ZoneId").of("Asia/Seoul") |
| 64 | + ), |
| 65 | + "offset": java("java.time.OffsetDateTime").parse("2024-01-02T03:04:05.5+09:00"), |
| 66 | + "util_date": java("java.util.Date")(jpype.JLong(1704164645123)), |
| 67 | + "calendar": calendar, |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | +def _engine_json(name, value): |
| 72 | + """What the engine's generic JSON writer makes of ``value``: the parsed text, or the exception's class name.""" |
| 73 | + json_object = jpype.JClass("com.arcadedb.serializer.json.JSONObject")() |
| 74 | + try: |
| 75 | + json_object.put(name, value) |
| 76 | + except jpype.JException as exc: |
| 77 | + return {"raises": str(exc.getClass().getName())} |
| 78 | + return json.loads(str(json_object.toString())) |
| 79 | + |
| 80 | + |
| 81 | +def test_the_json_and_columnar_writers_write_the_same_number_for_every_temporal( |
| 82 | + temp_db, |
| 83 | +): |
| 84 | + values = _java_temporals() |
| 85 | + |
| 86 | + as_json = results.ResultSet(_in_memory([values]), temp_db).to_json_list()[0] |
| 87 | + columns = results.ResultSet(_in_memory([values]), temp_db).to_columns() |
| 88 | + |
| 89 | + assert set(columns) == set(values) |
| 90 | + for name in values: |
| 91 | + column = columns[name] |
| 92 | + assert column.dtype == np.dtype("datetime64[ms]"), name |
| 93 | + assert int(column[0].astype("int64")) == as_json[name], name |
| 94 | + |
| 95 | + |
| 96 | +def test_the_json_writer_is_the_engines_generic_writer_for_every_temporal(temp_db): |
| 97 | + """No encoder of its own: if the engine's JSONObject changes how it writes a type, this fails and a decision is made.""" |
| 98 | + values = _java_temporals() |
| 99 | + # a date past the range of a long of milliseconds: text for an engine that has #10483, a failure for one that has not, |
| 100 | + # and the bridge follows either way |
| 101 | + values["far_date"] = jpype.JClass("java.time.LocalDate").of(999_999_999, 12, 31) |
| 102 | + dates = jpype.JClass("java.util.ArrayList")() |
| 103 | + dates.add(values["local_date"]) |
| 104 | + dates.add(values["pre_epoch_date"]) |
| 105 | + values["dates"] = dates |
| 106 | + members = jpype.JClass("java.util.LinkedHashMap")() |
| 107 | + members.put("on", values["local_date"]) |
| 108 | + values["meta"] = members |
| 109 | + |
| 110 | + for name, value in values.items(): |
| 111 | + expected = _engine_json(name, value) |
| 112 | + if "raises" in expected: |
| 113 | + # the bridge follows the engine it bundles, and the bindings raise a Java failure of a read as ArcadeDBError |
| 114 | + with pytest.raises(ArcadeDBError): |
| 115 | + results.ResultSet(_in_memory([{name: value}]), temp_db).to_json_list() |
| 116 | + else: |
| 117 | + row = results.ResultSet(_in_memory([{name: value}]), temp_db).to_json_list() |
| 118 | + assert row == [expected], name |
| 119 | + |
| 120 | + |
| 121 | +def test_a_date_is_one_number_in_a_record_row_a_projection_row_a_list_and_a_map( |
| 122 | + temp_db, |
| 123 | +): |
| 124 | + temp_db.command("sql", "CREATE DOCUMENT TYPE Event") |
| 125 | + temp_db.command("sql", "CREATE PROPERTY Event.day DATE") |
| 126 | + with temp_db.transaction(): |
| 127 | + temp_db.command( |
| 128 | + "sql", |
| 129 | + "INSERT INTO Event SET day = :day, days = :days, meta = :meta", |
| 130 | + { |
| 131 | + "day": dt.date(2024, 1, 2), |
| 132 | + "days": [dt.date(2024, 1, 2)], |
| 133 | + "meta": {"on": dt.date(2024, 1, 2)}, |
| 134 | + }, |
| 135 | + ) |
| 136 | + |
| 137 | + record_row = temp_db.query("sql", "SELECT FROM Event").to_json_list()[0] |
| 138 | + projection_row = temp_db.query( |
| 139 | + "sql", "SELECT day, days, meta FROM Event" |
| 140 | + ).to_json_list()[0] |
| 141 | + |
| 142 | + expected = { |
| 143 | + "day": MIDNIGHT_UTC_2024_01_02, |
| 144 | + "days": [MIDNIGHT_UTC_2024_01_02], |
| 145 | + "meta": {"on": MIDNIGHT_UTC_2024_01_02}, |
| 146 | + } |
| 147 | + assert {key: record_row[key] for key in expected} == expected |
| 148 | + assert projection_row == expected |
| 149 | + |
| 150 | + |
| 151 | +def test_result_to_json_writes_the_top_level_date_of_a_record_row_as_days(temp_db): |
| 152 | + """The one place the engine writes days: the round-trip JSON of the record, pinned so the difference is a decision. |
| 153 | +
|
| 154 | + A DATE column read back by ``fromJSON`` is a count of days, which is why ``Result.to_json()`` of a record row |
| 155 | + writes it so. ``to_json_list()`` is a read-only bulk transport and writes milliseconds everywhere, so a caller |
| 156 | + decodes every temporal the same way; the two differ by exactly the milliseconds in a day, and by nothing else. |
| 157 | + """ |
| 158 | + temp_db.command("sql", "CREATE DOCUMENT TYPE Event") |
| 159 | + temp_db.command("sql", "CREATE PROPERTY Event.day DATE") |
| 160 | + with temp_db.transaction(): |
| 161 | + temp_db.command( |
| 162 | + "sql", |
| 163 | + "INSERT INTO Event SET day = :day", |
| 164 | + {"day": dt.date(2024, 1, 2)}, |
| 165 | + ) |
| 166 | + |
| 167 | + single = json.loads(temp_db.query("sql", "SELECT FROM Event").first().to_json()) |
| 168 | + batch = temp_db.query("sql", "SELECT FROM Event").to_json_list()[0] |
| 169 | + |
| 170 | + assert single["day"] == MIDNIGHT_UTC_2024_01_02 // DAY_MS |
| 171 | + assert batch["day"] == single["day"] * DAY_MS |
| 172 | + assert "@rid" in single and "@rid" not in batch |
0 commit comments