From 33295a9420a0cccbfdb6942fb2d201c2d630710e Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Thu, 5 Dec 2024 12:11:13 +0500 Subject: [PATCH] Fix pg numeric sort (#7261) Signed-off-by: Denis Bykhov --- server/postgres/src/storage.ts | 8 +++++++- server/postgres/src/utils.ts | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/server/postgres/src/storage.ts b/server/postgres/src/storage.ts index 5d56814511..87f5d87d5d 100644 --- a/server/postgres/src/storage.ts +++ b/server/postgres/src/storage.ts @@ -85,6 +85,7 @@ import { isDataField, isOwner, type JoinProps, + NumericTypes, parseDoc, parseDocWithProjection, parseUpdate, @@ -920,7 +921,12 @@ abstract class PostgresAdapterBase implements DbAdapter { continue } if (typeof val === 'number') { - res.push(`${this.getKey(_class, baseDomain, key, joins)} ${val === 1 ? 'ASC' : 'DESC'}`) + const attr = this.hierarchy.findAttribute(_class, key) + if (attr !== undefined && NumericTypes.includes(attr.type._class)) { + res.push(`(${this.getKey(_class, baseDomain, key, joins)})::numeric ${val === 1 ? 'ASC' : 'DESC'}`) + } else { + res.push(`${this.getKey(_class, baseDomain, key, joins)} ${val === 1 ? 'ASC' : 'DESC'}`) + } } else { // todo handle custom sorting } diff --git a/server/postgres/src/utils.ts b/server/postgres/src/utils.ts index 1d1e9cc70f..67fef5ba32 100644 --- a/server/postgres/src/utils.ts +++ b/server/postgres/src/utils.ts @@ -64,6 +64,13 @@ export async function retryTxn ( }) } +export const NumericTypes = [ + core.class.TypeNumber, + core.class.TypeTimestamp, + core.class.TypeDate, + core.class.Collection +] + export async function createTables (client: postgres.Sql, domains: string[]): Promise { const filtered = domains.filter((d) => !loadedDomains.has(d)) if (filtered.length === 0) {