sdxjs icon indicating copy to clipboard operation
sdxjs copied to clipboard

Section 6.4, asBinary

Open adecora opened this issue 6 months ago • 1 comments

Hi on the asBinary when saving the rows for each column

labels.forEach(label => {
    const temp = new Float64Array(table[label])
    result.set(temp, current)
    current += temp.byteLength
  })

We are storing each value with 8 bytes (Float64Array) as we get the result from sizeof

> sizeof(0)
8

But if we have same data like this, and if we try to save it the way we are doing on asBinary function the is the following problem

{ 'field_1': [1, 2, 3] }

const result = new ArrayBuffer(3 * 8)  // We store 3 values, 8 bytes / value

const temp = new Float64Array([1, 2, 3])
result.set(temp, 0)
// [1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 

So actually we are not storing the real bytes inside Float64Array is just creating an array of the same length and storing the values. An even its not possible to store values bigger than 8bytes (256)

const result = new ArrayBuffer(2 * 8)  // We store 2 values, 8 bytes / value

const temp = new Float64Array([256, 2])
result.set(temp, 0)
// [0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 

So to store the real bytes of the Float64Array created on our result array, we must first store the Float64Array buffer as an Uint8Array and then set this on result

const result = new ArrayBuffer(3 * 8)  // We store 3 values, 8 bytes / value

const temp = new Float64Array([256, 2, 5])
const temp8 = new Uint8Array(temp.buffer)

result.set(temp8, 0)
// [0,0,0,0,0,0,112,64,0,0,0,0,0,0,0,64,0,0,0,0,0,0,20,64]

// To recover the original values
new Float64Array(result.buffer)
// [256, 2, 5]

d = new DataView(result.buffer)
d.getFloat64(0, true) // offset 0, true to little-endian
// 256
d.getFloat64(8, true)  // 2
d.getFloat64(16, true)  // 5  

adecora avatar Aug 23 '24 13:08 adecora