Skip to content

Commit f3af87d

Browse files
Copilotmrjf
andauthored
fix(eval_query): stabilize eval zero behavior and property test
Agent-Logs-Url: https://github.com/githubnext/tsessebe/sessions/86da2f84-1920-4e7c-b7bd-edc31603939f Co-authored-by: mrjf <180956+mrjf@users.noreply.github.com>
1 parent d5a1d27 commit f3af87d

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

src/stats/eval_query.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ function numericCmp(l: Scalar, r: Scalar): number {
446446

447447
function numericOp(l: Scalar, r: Scalar, fn: (a: number, b: number) => number): Scalar {
448448
if (l == null || r == null) return null;
449-
if (typeof l === "number" && typeof r === "number") return fn(l, r);
449+
if (typeof l === "number" && typeof r === "number") return canonicalizeZero(fn(l, r));
450450
return null;
451451
}
452452

@@ -471,10 +471,14 @@ function applyBinOp(op: string, l: Scalar, r: Scalar): Scalar {
471471
function addScalar(l: Scalar, r: Scalar): Scalar {
472472
if (l == null || r == null) return null;
473473
if (typeof l === "string" || typeof r === "string") return String(l) + String(r);
474-
if (typeof l === "number" && typeof r === "number") return l + r;
474+
if (typeof l === "number" && typeof r === "number") return canonicalizeZero(l + r);
475475
return null;
476476
}
477477

478+
function canonicalizeZero(value: number): number {
479+
return Object.is(value, -0) ? 0 : value;
480+
}
481+
478482
function evalInOp(node: Extract<AstNode, { type: "InOp" }>, row: ReadonlyMap<string, Scalar>): boolean {
479483
const val = evalNode(node.value, row);
480484
const found = node.list.some((item) => scalarEq(val, evalNode(item, row)));

tests/stats/eval_query.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ describe("evalDataFrame", () => {
364364
it("eval('a * 0') is all zeros", () => {
365365
fc.assert(
366366
fc.property(
367-
fc.array(fc.float({ noNaN: true }), { minLength: 1, maxLength: 10 }),
367+
fc.array(fc.float({ noNaN: true, min: -1e6, max: 1e6 }), { minLength: 1, maxLength: 10 }),
368368
(values) => {
369369
const df = DataFrame.fromArrays({ a: values });
370370
const result = evalDataFrame(df, "a * 0");

0 commit comments

Comments
 (0)