Skip to content

Commit

Permalink
feat (DAL): client-side support for float4 (#671)
Browse files Browse the repository at this point in the history
This PR adds client-side support for float4.
Numbers provided by the user are rounded to the nearest float4
representation using `Math.fround()`.
Similarly, when numbers are read from a float4 column in the DB they are
rounded to the nearest float4 representation.
This would not strictly be necessary if all writes happen through the
DAL but because of SQLite's dynamic typing, users can write
larger/smaller numbers to float4 columns in which case we would read
numbers that fall out of range.
  • Loading branch information
kevin-dp authored Nov 28, 2023
1 parent f32abc4 commit 27ff924
Show file tree
Hide file tree
Showing 22 changed files with 9,856 additions and 6,188 deletions.
11 changes: 6 additions & 5 deletions clients/typescript/src/cli/migrations/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,15 @@ function addValidator(ln: string): string {

if (field) {
const intValidator = '@zod.number.int().gte(-2147483648).lte(2147483647)'
const float8Validator = '@zod.custom.use(z.number().or(z.nan()))'
const floatValidator = '@zod.custom.use(z.number().or(z.nan()))'

// Map attributes to validators
const attributeValidatorMapping = new Map([
['@db.Uuid', '@zod.string.uuid()'],
['@db.SmallInt', '@zod.number.int().gte(-32768).lte(32767)'],
['@db.Int', intValidator],
['@db.DoublePrecision', float8Validator],
['@db.DoublePrecision', floatValidator],
['@db.Real', floatValidator],
])
const attribute = field.attributes
.map((a) => a.type)
Expand All @@ -371,9 +372,9 @@ function addValidator(ln: string): string {
['Int', intValidator],
['Int?', intValidator],
['Int[]', intValidator],
['Float', float8Validator],
['Float?', float8Validator],
['Float[]', float8Validator],
['Float', floatValidator],
['Float?', floatValidator],
['Float[]', floatValidator],
])
const typeValidator = typeValidatorMapping.get(field.type)

Expand Down
15 changes: 14 additions & 1 deletion clients/typescript/src/client/conversions/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export function toSqlite(v: any, pgType: PgType): any {
// and deserialise it back to `NaN` when reading from the DB.
// cf. https://github.com/WiseLibs/better-sqlite3/issues/1088
return 'NaN'
} else if (
pgType === PgBasicType.PG_FLOAT4 ||
pgType === PgBasicType.PG_REAL
) {
return Math.fround(v)
} else {
return v
}
Expand All @@ -46,10 +51,18 @@ export function fromSqlite(v: any, pgType: PgType): any {
return deserialiseBoolean(v)
} else if (
v === 'NaN' &&
(pgType === PgBasicType.PG_FLOAT8 || pgType === PgBasicType.PG_FLOAT4)
(pgType === PgBasicType.PG_FLOAT8 ||
pgType === PgBasicType.PG_FLOAT4 ||
pgType === PgBasicType.PG_REAL)
) {
// it's a serialised NaN
return NaN
} else if (
pgType === PgBasicType.PG_FLOAT4 ||
pgType === PgBasicType.PG_REAL
) {
// convert to float4 in case someone would have written a bigger value to SQLite directly
return Math.fround(v)
} else {
return v
}
Expand Down
3 changes: 2 additions & 1 deletion clients/typescript/test/client/conversions/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ await tbl.sync()
function setupDB() {
db.exec('DROP TABLE IF EXISTS DataTypes')
db.exec(
"CREATE TABLE DataTypes('id' int PRIMARY KEY, 'date' varchar, 'time' varchar, 'timetz' varchar, 'timestamp' varchar, 'timestamptz' varchar, 'bool' int, 'uuid' varchar, 'int2' int2, 'int4' int4, 'float8' real, 'relatedId' int);"
"CREATE TABLE DataTypes('id' int PRIMARY KEY, 'date' varchar, 'time' varchar, 'timetz' varchar, 'timestamp' varchar, 'timestamptz' varchar, 'bool' int, 'uuid' varchar, 'int2' int2, 'int4' int4, 'float4' real, 'float8' real, 'relatedId' int);"
)

db.exec('DROP TABLE IF EXISTS Dummy')
Expand Down Expand Up @@ -232,6 +232,7 @@ const dateNulls = {
bool: null,
int2: null,
int4: null,
float4: null,
float8: null,
uuid: null,
}
Expand Down
23 changes: 17 additions & 6 deletions clients/typescript/test/client/conversions/sqlite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ await tbl.sync()
function setupDB() {
db.exec('DROP TABLE IF EXISTS DataTypes')
db.exec(
"CREATE TABLE DataTypes('id' int PRIMARY KEY, 'date' varchar, 'time' varchar, 'timetz' varchar, 'timestamp' varchar, 'timestamptz' varchar, 'bool' int, 'uuid' varchar, 'int2' int2, 'int4' int4, 'float8' real, 'relatedId' int);"
"CREATE TABLE DataTypes('id' int PRIMARY KEY, 'date' varchar, 'time' varchar, 'timetz' varchar, 'timestamp' varchar, 'timestamptz' varchar, 'bool' int, 'uuid' varchar, 'int2' int2, 'int4' int4, 'float4' real, 'float8' real, 'relatedId' int);"
)
}

Expand Down Expand Up @@ -183,31 +183,42 @@ test.serial('floats are converted correctly to SQLite', async (t) => {
data: [
{
id: 1,
float4: 1.234,
float8: 1.234,
},
{
id: 2,
float4: NaN,
float8: NaN,
},
{
id: 3,
float4: Infinity,
float8: +Infinity,
},
{
id: 4,
float4: -Infinity,
float8: -Infinity,
},
],
})

const rawRes = await electric.db.raw({
sql: 'SELECT id, float8 FROM DataTypes ORDER BY id ASC',
sql: 'SELECT id, float4, float8 FROM DataTypes ORDER BY id ASC',
args: [],
})
t.deepEqual(rawRes, [
{ id: 1, float8: 1.234 },
{ id: 2, float8: 'NaN' },
{ id: 3, float8: Infinity },
{ id: 4, float8: -Infinity },
// 1.234 cannot be stored exactly in a float4
// hence, there is a rounding error, which is observed when we
// read the float4 value back into a 64-bit JS number
// The value 1.2339999675750732 that we read back
// is also what Math.fround(1.234) returns
// as being the nearest 32-bit single precision
// floating point representation of 1.234
{ id: 1, float4: 1.2339999675750732, float8: 1.234 },
{ id: 2, float4: 'NaN', float8: 'NaN' },
{ id: 3, float4: Infinity, float8: Infinity },
{ id: 4, float4: -Infinity, float8: -Infinity },
])
})
173 changes: 0 additions & 173 deletions clients/typescript/test/client/generated/client/index-browser.js

This file was deleted.

Loading

0 comments on commit 27ff924

Please sign in to comment.