Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions quasardb/numpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,12 @@ def write_arrays(
assert len(dtype) is len(cinfos)

if index is None and isinstance(data_, dict) and "$timestamp" in data_:
# Create shallow copy of `data_` so that we don't modify the reference, i.e.
# delete keys.
#
# This ensures that the user can call the same function multiple times without
# side-effects.
data_ = data_.copy()
index_ = data_.pop("$timestamp")
assert "$timestamp" not in data_
elif index is not None:
Expand Down
2 changes: 1 addition & 1 deletion scripts/teamcity/20.test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

SCRIPT_DIR="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null && pwd)"

Expand Down
54 changes: 54 additions & 0 deletions tests/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,60 @@ def test_arrays_read_write_data_as_dict(array_with_index_and_table, qdbd_connect
assert_indexed_arrays_equal((index, data), res)


@conftest.override_cdtypes("native")
def test_provide_index_as_dict(array_with_index_and_table, qdbd_connection):
"""
For convenience, we allow the `$timestamp` index also to provided as a dict
key.
"""
(ctype, dtype, data, index, table) = array_with_index_and_table

col = table.column_id_by_index(0)
dict_ = {"$timestamp": index, col: data}

qdbnp.write_arrays(
dict_,
qdbd_connection,
table,
dtype=dtype,
infer_types=False,
truncate=True,
)

res = qdbnp.read_array(table, col)

assert_indexed_arrays_equal((index, data), res)


@conftest.override_cdtypes("native")
def test_provide_index_as_dict_has_no_side_effects_sc16279(
array_with_index_and_table, qdbd_connection
):
"""
In earlier versions of the API, we `pop`'ed the $timestamp from the provided dict without making a
shallow copy of the dict. This would cause re-invocations of the same function (e.g. in case of an
error) to not work, as the original dict had been modified.

The test below has been confirmed to trigger the original bug described in QDB-16279, and was fixed
afterwards.
"""
(ctype, dtype, data, index, table) = array_with_index_and_table

col = table.column_id_by_index(0)

dict_ = {"$timestamp": index, col: data}
qdbnp.write_arrays(
dict_,
qdbd_connection,
table,
dtype=dtype,
infer_types=False,
truncate=True,
)

assert "$timestamp" in dict_


######
#
# Arrays tests
Expand Down