pitrou commented on code in PR #379:
URL: https://github.com/apache/arrow-js/pull/379#discussion_r2783770345
##########
test/unit/vector/interval-month-day-nano-tests.ts:
##########
@@ -77,4 +78,39 @@ describe(`MonthDayNanoIntervalVector`, () => {
expect(vec.get(0)).toStrictEqual(array);
expect(toIntervalMonthDayNanoObjects(vec.get(0),
false)).toStrictEqual([{ ...EMPTY_INTERVAL_MONTH_DAY_NANO_OBJECT, ...obj }]);
});
+
+ test(`Unsafe integer nanoseconds parsed as numbers roundtrip correctly`,
() => {
+ const samples = [
+ '-390122861233460600',
+ '6684525287992311000'
+ ];
+ for (const sample of samples) {
+ const nanoseconds = Number.parseFloat(sample);
Review Comment:
This seems incorrect since that will introduce FP inaccuracies. You need
`BigInt(sample)`.
For example `console.log(BigInt(Number.parseFloat("-390122861233460600")));`
gives me `-390122861233460608n`.
##########
src/util/json.ts:
##########
@@ -0,0 +1,42 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// @ts-ignore
+import { parse as bignumJSONParse, BigNumber } from 'json-bignum';
+
+/** @ignore */
+export function parseArrowJSON(source: string): any {
+ return normalizeUnsafeIntegers(bignumJSONParse(source));
+}
+
+function normalizeUnsafeIntegers(value: any): any {
+ if (typeof value === 'number') {
+ if (Number.isInteger(value) && !Number.isSafeInteger(value)) {
Review Comment:
Oops, I see that library is unmaintained. If there is a better replacement,
perhaps we should just switch to that?
##########
src/util/json.ts:
##########
@@ -0,0 +1,42 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// @ts-ignore
+import { parse as bignumJSONParse, BigNumber } from 'json-bignum';
+
+/** @ignore */
+export function parseArrowJSON(source: string): any {
+ return normalizeUnsafeIntegers(bignumJSONParse(source));
+}
+
+function normalizeUnsafeIntegers(value: any): any {
+ if (typeof value === 'number') {
+ if (Number.isInteger(value) && !Number.isSafeInteger(value)) {
Review Comment:
So this would be for values such as `100000000000000000`? Should this be
reported as a bug in `json-bignum`?
##########
src/util/json.ts:
##########
@@ -0,0 +1,42 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// @ts-ignore
+import { parse as bignumJSONParse, BigNumber } from 'json-bignum';
+
+/** @ignore */
+export function parseArrowJSON(source: string): any {
+ return normalizeUnsafeIntegers(bignumJSONParse(source));
+}
+
+function normalizeUnsafeIntegers(value: any): any {
+ if (typeof value === 'number') {
+ if (Number.isInteger(value) && !Number.isSafeInteger(value)) {
Review Comment:
Ok, it seems https://www.npmjs.com/package/json-bigint calls
`Number.isSafeInteger` correctly (and also can produce native JS `BigInt`
instead of a home-grown type).
##########
src/util/json.ts:
##########
@@ -0,0 +1,42 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// @ts-ignore
+import { parse as bignumJSONParse, BigNumber } from 'json-bignum';
+
+/** @ignore */
+export function parseArrowJSON(source: string): any {
+ return normalizeUnsafeIntegers(bignumJSONParse(source));
+}
+
+function normalizeUnsafeIntegers(value: any): any {
Review Comment:
Can you add a comment explaining the rationale as you did in the PR
description?
##########
test/unit/vector/interval-month-day-nano-tests.ts:
##########
@@ -77,4 +78,39 @@ describe(`MonthDayNanoIntervalVector`, () => {
expect(vec.get(0)).toStrictEqual(array);
expect(toIntervalMonthDayNanoObjects(vec.get(0),
false)).toStrictEqual([{ ...EMPTY_INTERVAL_MONTH_DAY_NANO_OBJECT, ...obj }]);
});
+
+ test(`Unsafe integer nanoseconds parsed as numbers roundtrip correctly`,
() => {
+ const samples = [
+ '-390122861233460600',
+ '6684525287992311000'
+ ];
+ for (const sample of samples) {
+ const nanoseconds = Number.parseFloat(sample);
+ expect(`${nanoseconds}`).toBe(sample);
+ const obj: Partial<IntervalMonthDayNanoObject> = { nanoseconds };
+ const array = toIntervalMonthDayNanoInt32Array([obj]);
+ const vec = makeIntervalMonthDayNanoVector(array);
+ expect(vec.type).toBeInstanceOf(IntervalMonthDayNano);
Review Comment:
Should we also call `expect(vec.get(0)).toStrictEqual(array);` as above?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]