diff --git a/src/build/infer-type.js b/src/build/infer-type.js index 2deaea5..d1681b0 100644 --- a/src/build/infer-type.js +++ b/src/build/infer-type.js @@ -27,8 +27,8 @@ function profiler() { let structCount = 0; let min = Infinity; let max = -Infinity; - let minLength = 0; - let maxLength = 0; + let minLength = Infinity; + let maxLength = -Infinity; let minBigInt; let maxBigInt; let arrayProfile; @@ -112,7 +112,7 @@ function profiler() { * @returns {import('../types.js').DataType} The data type. */ function arrayType(type, minLength, maxLength) { - return (maxLength - minLength) === 0 + return maxLength === minLength ? fixedSizeList(type, minLength) : list(type); } diff --git a/test/infer-type-test.js b/test/infer-type-test.js index 95e81ca..9b59385 100644 --- a/test/infer-type-test.js +++ b/test/infer-type-test.js @@ -1,5 +1,5 @@ import assert from 'node:assert'; -import { bool, dateDay, dictionary, float64, int16, int32, int64, int8, list, struct, timestamp, utf8 } from '../src/index.js'; +import { bool, dateDay, dictionary, fixedSizeList, float64, int16, int32, int64, int8, list, struct, timestamp, utf8 } from '../src/index.js'; import { inferType } from '../src/build/infer-type.js'; function matches(actual, expect) { @@ -71,11 +71,26 @@ describe('inferType', () => { }); it('infers list types', () => { - matches(infer([[1, 2], [3, 4]]), list(int8())); - matches(infer([[true, null, false], null, undefined, [false, undefined, true]]), list(bool())); + matches(infer([[1, 2], [3, 4], [5]]), list(int8())); + matches(infer([[true, null, false], null, undefined, [false, undefined]]), list(bool())); matches(infer([['foo', 'bar', null], null, ['bar', 'baz']]), list(dictionary(utf8(), int32()))); }); + it('infers fixed size list types', () => { + matches( + infer([[1, 2], [3, 4]]), + fixedSizeList(int8(), 2) + ); + matches( + infer([[true, null, false], null, undefined, [false, true, true]]), + fixedSizeList(bool(), 3) + ); + matches( + infer([['foo', 'bar'], null, ['bar', 'baz']]), + fixedSizeList(dictionary(utf8(), int32()), 2) + ); + }); + it('infers struct types', () => { matches( infer([