Skip to content

Commit

Permalink
feat(electric): Add support for the float4 column type (#657)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin <[email protected]>
  • Loading branch information
alco and kevin-dp authored Nov 29, 2023
1 parent 3ed5469 commit b6e589d
Show file tree
Hide file tree
Showing 22 changed files with 489 additions and 148 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-steaks-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@core/electric": patch
---

[VAX-846, VAX-849] Add support for the REAL / FLOAT4 column type in electrified tables.
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 @@ -372,14 +372,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 @@ -394,9 +395,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 if (pgType === PgBasicType.PG_INT8) {
// always return BigInts for PG_INT8 values
// because some drivers (e.g. wa-sqlite) return a regular JS number if the value fits into a JS number
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, 'int8' int8, '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, 'int8' int8, 'float4' real, 'float8' real, 'relatedId' int);"
)

db.exec('DROP TABLE IF EXISTS Dummy')
Expand Down Expand Up @@ -233,6 +233,7 @@ const dateNulls = {
int2: null,
int4: null,
int8: 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, 'int8' int8, '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, 'int8' int8, 'float4' real, 'float8' real, 'relatedId' int);"
)
}

Expand Down Expand Up @@ -183,32 +183,43 @@ 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 },
])
})

Expand Down
Loading

0 comments on commit b6e589d

Please sign in to comment.