Skip to content

Commit

Permalink
Merge pull request #2883 from finos/fix-filter-float32
Browse files Browse the repository at this point in the history
Fix `ViewConfig` serialization for internal arrow types
  • Loading branch information
texodus authored Dec 21, 2024
2 parents 74df6a2 + 035ad88 commit 5c864a0
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
41 changes: 40 additions & 1 deletion cpp/perspective/src/cpp/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1017,17 +1017,32 @@ coerce_to(const t_dtype dtype, const A& val) {
case DTYPE_BOOL:
scalar.set(val == "true");
return scalar;
case DTYPE_FLOAT32:
scalar.set(std::stof(val));
return scalar;
case DTYPE_FLOAT64:
scalar.set(std::stod(val));
return scalar;
case DTYPE_UINT8:
scalar.set((std::uint8_t)std::stoul(val));
return scalar;
case DTYPE_UINT16:
scalar.set((std::uint16_t)std::stoul(val));
return scalar;
case DTYPE_UINT32:
scalar.set((std::uint32_t)std::stoul(val));
return scalar;
case DTYPE_UINT64:
scalar.set((std::uint64_t)std::stoull(val));
return scalar;
case DTYPE_INT8:
scalar.set((std::int8_t)std::stoi(val));
return scalar;
case DTYPE_INT16:
scalar.set((std::int16_t)std::stoi(val));
return scalar;
case DTYPE_INT32:
scalar.set(std::stoi(val));
scalar.set((std::int32_t)std::stoi(val));
return scalar;
case DTYPE_INT64:
scalar.set((std::int64_t)std::stoll(val));
Expand Down Expand Up @@ -1964,12 +1979,36 @@ ProtoServer::_handle_request(std::uint32_t client_id, const Request& req) {
case DTYPE_BOOL:
s->set_bool_(scalar.get<bool>());
break;
case DTYPE_FLOAT32:
s->set_float_(scalar.get<float>());
break;
case DTYPE_FLOAT64:
s->set_float_(scalar.get<double>());
break;
case DTYPE_INT8:
s->set_float_((double)scalar.get<std::int8_t>());
break;
case DTYPE_INT16:
s->set_float_((double)scalar.get<std::int16_t>());
break;
case DTYPE_INT32:
s->set_float_((double)scalar.get<std::int32_t>());
break;
case DTYPE_INT64:
s->set_float_((double)scalar.get<std::int64_t>());
break;
case DTYPE_UINT8:
s->set_float_((double)scalar.get<std::uint8_t>());
break;
case DTYPE_UINT16:
s->set_float_((double)scalar.get<std::uint16_t>());
break;
case DTYPE_UINT32:
s->set_float_((double)scalar.get<std::uint32_t>());
break;
case DTYPE_UINT64:
s->set_float_((double)scalar.get<std::uint64_t>());
break;
case DTYPE_STR:
s->set_string(scalar.get<const char*>());
break;
Expand Down
Binary file added rust/perspective-js/test/arrow/float32.arrow
Binary file not shown.
13 changes: 13 additions & 0 deletions rust/perspective-js/test/js/constructors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,19 @@ function validate_typed_array(typed_array, column_data) {
table.delete();
});

test("Arrow float32 constructor", async function () {
const table = await perspective.table(arrows.float32_arrow.slice());
const view = await table.view();
const result = await view.to_columns();
expect(result).toEqual({
id: [1, 2, 3],
name: ["Alice", "Bob", "Charlie"],
score: [92.5, 87.30000305175781, 95.80000305175781],
});
view.delete();
table.delete();
});

test("Arrow date64 constructor", async function () {
const table = await perspective.table(arrows.date64_arrow.slice());
const view = await table.view();
Expand Down
32 changes: 32 additions & 0 deletions rust/perspective-js/test/js/filters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import { test, expect } from "@finos/perspective-test";
import perspective from "./perspective_client";
import * as arrows from "./test_arrows.js";

var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
Expand Down Expand Up @@ -471,6 +472,37 @@ const datetime_data_local = [
});
});

test.describe("Arrow types", function () {
// https://github.com/finos/perspective/issues/2881
test("Arrow float32 filters", async function () {
const table = await perspective.table(
arrows.float32_arrow.slice()
);

const view = await table.view({ filter: [["score", "<", 93]] });
const result = await view.to_columns();
expect(result).toEqual({
id: [1, 2],
name: ["Alice", "Bob"],
score: [92.5, 87.30000305175781],
});

const cfg = await view.get_config();
expect(cfg).toEqual({
aggregates: {},
columns: ["id", "name", "score"],
expressions: {},
filter: [["score", "<", 93]],
group_by: [],
sort: [],
split_by: [],
});

view.delete();
table.delete();
});
});

test.describe("multiple", function () {
test("x > 1 & x < 4", async function () {
var table = await perspective.table(data);
Expand Down
4 changes: 4 additions & 0 deletions rust/perspective-js/test/js/test_arrows.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export const int_float_str_file_arrow = load_arrow(
path.join(__dirname, "..", "arrow", "int_float_str_file.arrow")
);

export const float32_arrow = load_arrow(
path.join(__dirname, "..", "arrow", "float32.arrow")
);

export const date32_arrow = load_arrow(
path.join(__dirname, "..", "arrow", "date32.arrow")
);
Expand Down

0 comments on commit 5c864a0

Please sign in to comment.