hyparquet/test/schema.test.js

89 lines
3.2 KiB
JavaScript
Raw Normal View History

2024-01-14 23:36:58 +00:00
import { describe, expect, it } from 'vitest'
import {
getMaxDefinitionLevel,
getMaxRepetitionLevel,
2024-04-29 23:47:52 +00:00
isListLike,
isMapLike,
2024-01-14 23:36:58 +00:00
isRequired,
schemaElement,
skipDefinitionBytes,
} from '../src/schema.js'
describe('Parquet schema utils', () => {
2024-02-11 22:33:56 +00:00
/**
* @typedef {import('../src/types.js').SchemaElement} SchemaElement
* @type {SchemaElement[]}
*/
2024-01-14 23:36:58 +00:00
const schema = [
2024-04-29 23:47:52 +00:00
{ name: 'root', num_children: 3 },
2024-02-11 22:33:56 +00:00
{ name: 'child1', repetition_type: 'OPTIONAL' },
2024-04-29 23:47:52 +00:00
{ name: 'child2', repetition_type: 'OPTIONAL', num_children: 1, converted_type: 'LIST' },
{ name: 'list', repetition_type: 'REPEATED', num_children: 1 },
{ name: 'element', repetition_type: 'REQUIRED' },
{ name: 'child3', repetition_type: 'OPTIONAL', num_children: 1, converted_type: 'MAP' },
{ name: 'map', repetition_type: 'REPEATED', num_children: 2 },
{ name: 'key', repetition_type: 'REQUIRED' },
{ name: 'value', repetition_type: 'OPTIONAL' },
2024-01-14 23:36:58 +00:00
]
describe('schemaElement', () => {
2024-04-29 23:47:52 +00:00
it('should return the schema element', () => {
2024-03-13 02:58:54 +00:00
expect(schemaElement(schema, ['child1'])).toEqual({
children: [],
count: 1,
element: { name: 'child1', repetition_type: 'OPTIONAL' },
})
2024-01-14 23:36:58 +00:00
})
it('should throw an error if element not found', () => {
expect(() => schemaElement(schema, ['nonexistent']))
.toThrow('parquet schema element not found: nonexistent')
})
})
2024-04-29 23:47:52 +00:00
it('isRequired', () => {
expect(isRequired(schema, [])).toBe(true)
expect(isRequired(schema, ['child1'])).toBe(false)
expect(isRequired(schema, ['child2'])).toBe(false)
expect(isRequired(schema, ['child3'])).toBe(false)
2024-01-14 23:36:58 +00:00
})
2024-04-29 23:47:52 +00:00
it('getMaxRepetitionLevel', () => {
expect(getMaxRepetitionLevel(schema, ['child1'])).toBe(0)
expect(getMaxRepetitionLevel(schema, ['child2'])).toBe(0)
expect(getMaxRepetitionLevel(schema, ['child2', 'list', 'element'])).toBe(1)
expect(getMaxRepetitionLevel(schema, ['child3'])).toBe(0)
expect(getMaxRepetitionLevel(schema, ['child3', 'map', 'key'])).toBe(1)
2024-01-14 23:36:58 +00:00
})
2024-04-29 23:47:52 +00:00
it('getMaxDefinitionLevel', () => {
2024-01-14 23:36:58 +00:00
expect(getMaxDefinitionLevel(schema, ['child1'])).toBe(1)
2024-04-29 23:47:52 +00:00
expect(getMaxDefinitionLevel(schema, ['child2'])).toBe(1)
expect(getMaxDefinitionLevel(schema, ['child3'])).toBe(1)
2024-01-14 23:36:58 +00:00
})
2024-04-29 23:47:52 +00:00
it('skipDefinitionBytes', () => {
2024-01-14 23:36:58 +00:00
expect(skipDefinitionBytes(100)).toBe(6)
expect(skipDefinitionBytes(1000)).toBe(7)
})
2024-04-29 23:47:52 +00:00
it('isListLike', () => {
expect(isListLike(schema, [])).toBe(false)
expect(isListLike(schema, ['child1'])).toBe(false)
expect(isListLike(schema, ['child2'])).toBe(false)
expect(isListLike(schema, ['child2', 'list', 'element'])).toBe(true)
expect(isListLike(schema, ['child3'])).toBe(false)
expect(isListLike(schema, ['child3', 'map', 'key'])).toBe(false)
})
it('isMapLike', () => {
expect(isMapLike(schema, [])).toBe(false)
expect(isMapLike(schema, ['child1'])).toBe(false)
expect(isMapLike(schema, ['child2'])).toBe(false)
expect(isMapLike(schema, ['child2', 'list', 'element'])).toBe(false)
expect(isMapLike(schema, ['child3'])).toBe(false)
expect(isMapLike(schema, ['child3', 'map', 'key'])).toBe(true)
expect(isMapLike(schema, ['child3', 'map', 'value'])).toBe(true)
})
2024-01-14 23:36:58 +00:00
})