Skip to content

Commit

Permalink
fix: Fix fixedSizeList type inference.
Browse files Browse the repository at this point in the history
  • Loading branch information
jheer committed Sep 12, 2024
1 parent 8bca868 commit d870178
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/build/infer-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
21 changes: 18 additions & 3 deletions test/infer-type-test.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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([
Expand Down

0 comments on commit d870178

Please sign in to comment.