hyparquet/test/schema.test.js

85 lines
3.5 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-30 00:38:26 +00:00
getSchemaPath,
2024-04-29 23:47:52 +00:00
isListLike,
isMapLike,
2024-01-14 23:36:58 +00:00
isRequired,
} 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
]
2024-04-30 00:38:26 +00:00
describe('getSchemaPath', () => {
it('should return the schema path', () => {
const path = getSchemaPath(schema, ['child1'])
expect(path[path.length - 1]).toEqual({
2024-03-13 02:58:54 +00:00
children: [],
count: 1,
element: { name: 'child1', repetition_type: 'OPTIONAL' },
2024-05-06 20:18:27 +00:00
path: ['child1'],
2024-03-13 02:58:54 +00:00
})
2024-01-14 23:36:58 +00:00
})
it('should throw an error if element not found', () => {
2024-04-30 00:38:26 +00:00
expect(() => getSchemaPath(schema, ['nonexistent']))
2024-01-14 23:36:58 +00:00
.toThrow('parquet schema element not found: nonexistent')
})
})
2024-04-29 23:47:52 +00:00
it('isRequired', () => {
2024-04-30 00:38:26 +00:00
expect(isRequired(getSchemaPath(schema, []))).toBe(true)
expect(isRequired(getSchemaPath(schema, ['child1']))).toBe(false)
expect(isRequired(getSchemaPath(schema, ['child2']))).toBe(false)
expect(isRequired(getSchemaPath(schema, ['child3']))).toBe(false)
2024-01-14 23:36:58 +00:00
})
2024-04-29 23:47:52 +00:00
it('getMaxRepetitionLevel', () => {
2024-04-30 00:38:26 +00:00
expect(getMaxRepetitionLevel(getSchemaPath(schema, ['child1']))).toBe(0)
expect(getMaxRepetitionLevel(getSchemaPath(schema, ['child2']))).toBe(0)
expect(getMaxRepetitionLevel(getSchemaPath(schema, ['child2', 'list', 'element']))).toBe(1)
expect(getMaxRepetitionLevel(getSchemaPath(schema, ['child3']))).toBe(0)
expect(getMaxRepetitionLevel(getSchemaPath(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-04-30 00:38:26 +00:00
expect(getMaxDefinitionLevel(getSchemaPath(schema, ['child1']))).toBe(1)
expect(getMaxDefinitionLevel(getSchemaPath(schema, ['child2']))).toBe(1)
expect(getMaxDefinitionLevel(getSchemaPath(schema, ['child3']))).toBe(1)
2024-01-14 23:36:58 +00:00
})
2024-04-29 23:47:52 +00:00
it('isListLike', () => {
expect(isListLike(getSchemaPath(schema, []))).toBe(false)
expect(isListLike(getSchemaPath(schema, ['child1']))).toBe(false)
expect(isListLike(getSchemaPath(schema, ['child2']))).toBe(false)
expect(isListLike(getSchemaPath(schema, ['child2', 'list', 'element']))).toBe(true)
expect(isListLike(getSchemaPath(schema, ['child3']))).toBe(false)
expect(isListLike(getSchemaPath(schema, ['child3', 'map', 'key']))).toBe(false)
2024-04-29 23:47:52 +00:00
})
it('isMapLike', () => {
expect(isMapLike(getSchemaPath(schema, []))).toBe(false)
expect(isMapLike(getSchemaPath(schema, ['child1']))).toBe(false)
expect(isMapLike(getSchemaPath(schema, ['child2']))).toBe(false)
expect(isMapLike(getSchemaPath(schema, ['child2', 'list', 'element']))).toBe(false)
expect(isMapLike(getSchemaPath(schema, ['child3']))).toBe(false)
expect(isMapLike(getSchemaPath(schema, ['child3', 'map', 'key']))).toBe(true)
expect(isMapLike(getSchemaPath(schema, ['child3', 'map', 'value']))).toBe(true)
2024-04-29 23:47:52 +00:00
})
2024-01-14 23:36:58 +00:00
})