Skip to content

Commit dc85be7

Browse files
authored
Merge pull request #3084 from sinistersnare/bugfix/more-arrow-integers
Support a wider array of integers
2 parents e699207 + 7051bbb commit dc85be7

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

rust/perspective-python/perspective/tests/table/test_table_arrow.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
import pandas as pd
1616
from perspective.tests.conftest import Util
1717
import pyarrow as pa
18+
import pyarrow.ipc as ipc
1819
from datetime import date, datetime
1920
import perspective as psp
21+
import io
22+
2023

2124
client = psp.Server().new_local_client()
2225
Table = client.table
@@ -25,11 +28,38 @@
2528
DATE64_ARROW = os.path.join(os.path.dirname(__file__), "arrow", "date64.arrow")
2629
DICT_ARROW = os.path.join(os.path.dirname(__file__), "arrow", "dict.arrow")
2730

31+
2832
names = ["a", "b", "c", "d"]
2933

34+
# Create sample data for every integer type
35+
ALL_INTEGERS_DATA = {
36+
"int8": pa.array([1, 2, 3], type=pa.int8()),
37+
"int16": pa.array([1000, 2000, 3000], type=pa.int16()),
38+
"int32": pa.array([100000, 200000, 300000], type=pa.int32()),
39+
"int64": pa.array([10000000000, 20000000000, 30000000000], type=pa.int64()),
40+
"uint8": pa.array([1, 2, 3], type=pa.uint8()),
41+
"uint16": pa.array([1000, 2000, 3000], type=pa.uint16()),
42+
"uint32": pa.array([100000, 200000, 300000], type=pa.uint32()),
43+
"uint64": pa.array([10000000000, 20000000000, 30000000000], type=pa.uint64()),
44+
"float32": pa.array([100000.0, 200000.0, 300000.0], type=pa.float32()),
45+
"float64": pa.array(
46+
[10000000000.0, 20000000000.0, 30000000000.0], type=pa.float64()
47+
),
48+
}
49+
50+
ALL_INTEGERS_TABLE = pa.Table.from_pydict(ALL_INTEGERS_DATA)
51+
52+
bytes_io = io.BytesIO()
53+
with ipc.new_stream(bytes_io, ALL_INTEGERS_TABLE.schema) as stream:
54+
stream.write_table(ALL_INTEGERS_TABLE)
55+
ALL_INTEGERS_ARROW = bytes_io.getvalue()
3056

3157
class TestTableArrow(object):
32-
# files
58+
def test_table_with_integer_types(self):
59+
tbl = Table(ALL_INTEGERS_ARROW)
60+
for k, values in ALL_INTEGERS_DATA.items():
61+
v = tbl.view(filter=[[k, "==", values[0].as_py()]])
62+
assert len(v.to_json()) == 1
3363

3464
def test_table_arrow_loads_date32_file(self, util: Util):
3565
with open(DATE32_ARROW, mode="rb") as file: # b is important -> binary

rust/perspective-python/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pytest>=7.4.3
2+
maturin==1.6.0
23

34
Faker==26.0.0
45
ipywidgets==8.1.3

rust/perspective-server/cpp/perspective/src/cpp/server.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,23 +1266,19 @@ coerce_to(const t_dtype dtype, const A& val) {
12661266
case DTYPE_BOOL:
12671267
scalar.set(val == 1);
12681268
return scalar;
1269-
case DTYPE_UINT32:
1270-
scalar.set((std::uint32_t)val);
1271-
return scalar;
1272-
case DTYPE_UINT64:
1273-
scalar.set((std::uint64_t)val);
1274-
return scalar;
1269+
case DTYPE_UINT8:
1270+
case DTYPE_UINT16:
1271+
case DTYPE_INT8:
1272+
case DTYPE_INT16:
12751273
case DTYPE_INT32:
1276-
scalar.set(val);
1274+
scalar.set(static_cast<std::int32_t>(val));
12771275
return scalar;
12781276
case DTYPE_INT64:
1279-
scalar.set((std::int64_t)val);
1280-
return scalar;
1277+
case DTYPE_UINT32:
1278+
case DTYPE_UINT64:
12811279
case DTYPE_FLOAT32:
1282-
scalar.set(static_cast<float>(val));
1283-
return scalar;
12841280
case DTYPE_FLOAT64:
1285-
scalar.set(val);
1281+
scalar.set(static_cast<double>(val));
12861282
return scalar;
12871283
case DTYPE_DATE: {
12881284
const auto time = static_cast<time_t>(val / 1000);
@@ -1302,7 +1298,9 @@ coerce_to(const t_dtype dtype, const A& val) {
13021298
return scalar;
13031299
}
13041300
default:
1305-
PSP_COMPLAIN_AND_ABORT("Unsupported double type");
1301+
std::stringstream ss;
1302+
ss << "Unsupported double type: " << dtype;
1303+
PSP_COMPLAIN_AND_ABORT(ss.str());
13061304
}
13071305
} else if constexpr (std::is_same_v<A, std::int32_t>) {
13081306
return t_tscalar(val);

0 commit comments

Comments
 (0)