Skip to content

Commit

Permalink
Actually fix conversion of decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
platypii committed May 13, 2024
1 parent fe7e19b commit bd4e7c1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
1 change: 1 addition & 0 deletions demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ async function render(asyncBuffer, metadata, name) {
function renderSidebar(asyncBuffer, metadata, name) {
label.innerText = name
// render file layout
layout.innerHTML = ''
layout.appendChild(fileLayout(metadata, asyncBuffer.byteLength))
// display metadata
metadataDiv.innerHTML = ''
Expand Down
20 changes: 11 additions & 9 deletions src/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,34 @@ const dayMillis = 86400000 // 1 day in milliseconds
* @returns {DecodedArray} series of rich types
*/
export function convert(data, schemaElement) {
if (!Array.isArray(data)) return data
const ctype = schemaElement.converted_type
if (ctype === 'UTF8') {
const decoder = new TextDecoder()
return data.map(v => v && decoder.decode(v))
}
if (ctype === 'DECIMAL') {
const scale = schemaElement.scale || 0
const precision = schemaElement.precision || 0
const factor = Math.pow(10, scale - precision)
const factor = Math.pow(10, -scale)
if (typeof data[0] === 'number') {
return factor === 1 ? data : data.map(v => v * factor)
if (factor === 1) return data
return Array.from(data).map(v => v * factor)
} else if (typeof data[0] === 'bigint') {
return factor === 1 ? data : data.map(v => v * BigInt(factor))
if (factor === 1) return data
// @ts-expect-error data is either BigInt64Array or bigint[]
if (factor > 1) return data.map(v => v * BigInt(factor))
return Array.from(data).map(v => Number(v) * factor)
} else {
return data.map(v => parseDecimal(v) * factor)
return Array.from(data).map(v => parseDecimal(v) * factor)
}
}
if (ctype === 'DATE') {
return data.map(v => new Date(v * dayMillis))
return Array.from(data).map(v => new Date(v * dayMillis))
}
if (ctype === undefined && schemaElement.type === 'INT96') {
return data.map(parseInt96Date)
return Array.from(data).map(parseInt96Date)
}
if (ctype === 'TIME_MILLIS') {
return data.map(v => new Date(v))
return Array.from(data).map(v => new Date(v))
}
if (ctype === 'JSON') {
return data.map(v => JSON.parse(v))
Expand Down
8 changes: 4 additions & 4 deletions test/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('convert function', () => {
const data = [100, 200]
/** @type {SchemaElement} */
const schemaElement = { name, converted_type: 'DECIMAL', scale: 2 }
expect(convert(data, schemaElement)).toEqual([10000, 20000])
expect(convert(data, schemaElement)).toEqual([1, 2])
})

it('converts bigint to DECIMAL', () => {
Expand All @@ -42,10 +42,10 @@ describe('convert function', () => {
})

it('converts bigint to DECIMAL with scale', () => {
const data = [BigInt(1000), BigInt(2000)]
const data = [BigInt(10), BigInt(20)]
/** @type {SchemaElement} */
const schemaElement = { name, converted_type: 'DECIMAL', scale: 3 }
expect(convert(data, schemaElement)).toEqual([1000000n, 2000000n])
const schemaElement = { name, converted_type: 'DECIMAL', scale: 2 }
expect(convert(data, schemaElement)).toEqual([0.1, 0.2])
})

it('converts byte arrays to DECIMAL', () => {
Expand Down

0 comments on commit bd4e7c1

Please sign in to comment.