Skip to content

Commit 741c01c

Browse files
committed
feat!: Use useDecimalInt instead of useDecimalBigInt (#34)
* feat: Add 32- and 64-bit decimal support. * fix: Fix docs link typo. * feat!: Change to useDecimalInt flag.
1 parent 80a128f commit 741c01c

File tree

8 files changed

+23
-21
lines changed

8 files changed

+23
-21
lines changed

docs/api/data-types.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The table below provides an overview of all data types supported by the Apache A
1818
| 4 | [Binary](#binary) |||| `Uint8Array` |
1919
| 5 | [Utf8](#utf8) |||| `string` |
2020
| 6 | [Bool](#bool) |||| `boolean` |
21-
| 7 | [Decimal](#decimal) |||| `number`, or `bigint` via the `useDecimalBigInt` flag |
21+
| 7 | [Decimal](#decimal) |||| `number`, or scaled integers via the `useDecimalInt` flag |
2222
| 8 | [Date](#date) |||| `number`, or `Date` via the `useDate` flag. |
2323
| 9 | [Time](#time) |||| `number`, or `bigint` for 64-bit values via the `useBigInt` flag |
2424
| 10 | [Timestamp](#timestamp) |||| `number`, or `Date` via the `useDate` flag. |
@@ -241,7 +241,7 @@ bool()
241241

242242
Create an Decimal data type instance for exact decimal values, represented as a 32, 64, 128, or 256-bit integer value in two's complement. Decimals are fixed point numbers with a set *precision* (total number of decimal digits) and *scale* (number of fractional digits). For example, the number `35.42` can be represented as `3542` with *precision* ≥ 4 and *scale* = 2.
243243

244-
By default, Flechette converts decimals to 64-bit floating point numbers upon extraction (e.g., mapping `3542` back to `35.42`). While useful for many downstream applications, this conversion may be lossy and introduce inaccuracies. Pass the `useDecimalBigInt` extraction option (e.g., to [`tableFromIPC`](/flechette/api/#tableFromIPC) or [`tableFromArrays`](/flechette/api/#tableFromArrays)) to instead extract decimal data as `BigInt` values (64-bit or larger decimals) or integer `number` values (32-bit decimals).
244+
By default, Flechette converts decimals to 64-bit floating point numbers upon extraction (e.g., mapping `3542` back to `35.42`). While useful for many downstream applications, this conversion may be lossy and introduce inaccuracies. Pass the `useDecimalInt` extraction option (e.g., to [`tableFromIPC`](/flechette/api/#tableFromIPC) or [`tableFromArrays`](/flechette/api/#tableFromArrays)) to instead extract decimal data as `BigInt` values (64-bit or larger decimals) or integer `number` values (32-bit decimals).
245245

246246
* *precision* (`number`): The total number of decimal digits that can be represented.
247247
* *scale* (`number`): The number of fractional digits, beyond the decimal point.

docs/api/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Decode [Apache Arrow IPC data](https://arrow.apache.org/docs/format/Columnar.htm
2323
* *options* (`ExtractionOptions`): Options for controlling how values are transformed when extracted from an Arrow binary representation.
2424
* *useBigInt* (`boolean`): If true, extract 64-bit integers as JavaScript `BigInt` values. Otherwise, coerce long integers to JavaScript number values (default `false`).
2525
* *useDate* (`boolean`): If true, extract dates and timestamps as JavaScript `Date` objects. Otherwise, return numerical timestamp values (default `false`).
26-
* *useDecimalBigInt* (`boolean`): If true, extract decimal-type data as BigInt values, where fractional digits are scaled to integers. Otherwise, return converted floating-point numbers (default `false`).
26+
* *useDecimalInt* (`boolean`): If true, extract decimal-type data as scaled integer values, where fractional digits are scaled to integer positions. Returned integers are `BigInt` values for decimal bit widths of 64 bits or higher and 32-bit integers (as JavaScript `number`) otherwise. If false, decimals are converted to floating-point numbers (default).
2727
* *useMap* (`boolean`): If true, extract Arrow 'Map' values as JavaScript `Map` instances. Otherwise, return an array of [key, value] pairs compatible with both `Map` and `Object.fromEntries` (default `false`).
2828
* *useProxy* (`boolean`): If true, extract Arrow 'Struct' values and table row objects using zero-copy proxy objects that extract data from underlying Arrow batches. The proxy objects can improve performance and reduce memory usage, but do not support property enumeration (`Object.keys`, `Object.values`, `Object.entries`) or spreading (`{ ...object }`). Otherwise, use standard JS objects for structs and table rows (default `false`).
2929

docs/index.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ Data extraction can be customized using options provided to table generation met
114114

115115
```js
116116
const table = tableFromIPC(ipc, {
117-
useDate: true, // map dates and timestamps to Date objects
118-
useDecimalBigInt: true, // use BigInt for decimals, do not coerce to number
119-
useBigInt: true, // use BigInt for 64-bit ints, do not coerce to number
120-
useMap: true, // create Map objects for [key, value] pair lists
121-
useProxy: true // use zero-copy proxies for struct and table row objects
117+
useDate: true, // map dates and timestamps to Date objects
118+
useDecimalInt: true, // use scaled ints for decimals, not floating point
119+
useBigInt: true, // use BigInt for 64-bit ints, do not coerce to number
120+
useMap: true, // create Map objects for [key, value] pair lists
121+
useProxy: true // use zero-copy proxies for struct and table row objects
122122
});
123123
```
124124

src/batch-type.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { invalidDataType } from './data-types.js';
44

55
export function batchType(type, options = {}) {
66
const { typeId, bitWidth, precision, unit } = type;
7-
const { useBigInt, useDate, useDecimalBigInt, useMap, useProxy } = options;
7+
const { useBigInt, useDate, useDecimalInt, useMap, useProxy } = options;
88

99
switch (typeId) {
1010
case Type.Null: return NullBatch;
@@ -30,8 +30,8 @@ export function batchType(type, options = {}) {
3030
);
3131
case Type.Decimal:
3232
return bitWidth === 32
33-
? (useDecimalBigInt ? DirectBatch : Decimal32NumberBatch)
34-
: (useDecimalBigInt ? DecimalBigIntBatch : DecimalNumberBatch);
33+
? (useDecimalInt ? DirectBatch : Decimal32NumberBatch)
34+
: (useDecimalInt ? DecimalBigIntBatch : DecimalNumberBatch);
3535
case Type.Interval:
3636
return unit === IntervalUnit.DAY_TIME ? IntervalDayTimeBatch
3737
: unit === IntervalUnit.YEAR_MONTH ? DirectBatch

src/build/builder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function builder(type, ctx = builderContext()) {
6666
return new BoolBuilder(type, ctx);
6767
case Type.Decimal:
6868
return type.bitWidth === 32
69-
? new TransformBuilder(type, ctx, toDecimal32(10 ** type.scale))
69+
? new TransformBuilder(type, ctx, toDecimal32(type.scale))
7070
: new DecimalBuilder(type, ctx);
7171
case Type.Date:
7272
return new TransformBuilder(type, ctx, type.unit ? toBigInt : toDateDay);

src/types.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,13 @@ export interface ExtractionOptions {
312312
*/
313313
useDate?: boolean;
314314
/**
315-
* If true, extract decimal-type data as BigInt values, where fractional
316-
* digits are scaled to integers. Otherwise, return converted floating-point
317-
* numbers (default).
315+
* If true, extract decimal-type data as scaled integer values, where
316+
* fractional digits are scaled to integer positions. Returned integers
317+
* are `BigInt` values for decimal bit widths of 64 bits or higher and
318+
* 32-bit integers (as JavaScript `number`) otherwise. If false, decimals
319+
* are converted to floating-point numbers (default).
318320
*/
319-
useDecimalBigInt?: boolean;
321+
useDecimalInt?: boolean;
320322
/**
321323
* If true, extract 64-bit integers as JavaScript `BigInt` values.
322324
* Otherwise, coerce long integers to JavaScript number values (default).

test/column-from-array-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe('columnFromArray', () => {
106106
test([0.1000012345678987, -0.1000012345678987], decimal(40, 16, 256));
107107
test([0.12345678987654321, -0.12345678987654321], decimal(40, 18, 256));
108108

109-
const opt = { useDecimalBigInt: true };
109+
const opt = { useDecimalInt: true };
110110

111111
test([11n, 23n, 34n], decimal(18, 1, 128), opt);
112112
test([-11n, -23n, -34n], decimal(18, 1, 128), opt);

test/table-from-ipc-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ describe('tableFromIPC', () => {
7979
it('decodes decimal64 data', () => test(decimal64, Float64Array));
8080
it('decodes decimal128 data', () => test(decimal128, Float64Array));
8181
it('decodes decimal256 data', () => test(decimal256, Float64Array));
82-
it('decodes decimal32 data to int', () => test(decimal32, Int32Array, { useDecimalBigInt: true }, toDecimalInt));
83-
it('decodes decimal64 data to bigint', () => test(decimal64, Array, { useDecimalBigInt: true }, toDecimalBigInt));
84-
it('decodes decimal128 data to bigint', () => test(decimal128, Array, { useDecimalBigInt: true }, toDecimalBigInt));
85-
it('decodes decimal256 data to bigint', () => test(decimal256, Array, { useDecimalBigInt: true }, toDecimalBigInt));
82+
it('decodes decimal32 data to int', () => test(decimal32, Int32Array, { useDecimalInt: true }, toDecimalInt));
83+
it('decodes decimal64 data to bigint', () => test(decimal64, Array, { useDecimalInt: true }, toDecimalBigInt));
84+
it('decodes decimal128 data to bigint', () => test(decimal128, Array, { useDecimalInt: true }, toDecimalBigInt));
85+
it('decodes decimal256 data to bigint', () => test(decimal256, Array, { useDecimalInt: true }, toDecimalBigInt));
8686

8787
it('decodes date day data', () => test(dateDay, Float64Array));
8888
it('decodes date day data to dates', () => test(dateDay, Array, { useDate: true }, toDate));

0 commit comments

Comments
 (0)