From 9c7fc79af198ebfc272aea738c6e9b24f58e6ac7 Mon Sep 17 00:00:00 2001 From: Bob den Os Date: Thu, 9 Apr 2026 11:44:37 +0200 Subject: [PATCH 1/3] Consider search result for result order --- db-service/lib/cqn4sql.js | 1 + 1 file changed, 1 insertion(+) diff --git a/db-service/lib/cqn4sql.js b/db-service/lib/cqn4sql.js index 8cad82eb0..57d4555ea 100644 --- a/db-service/lib/cqn4sql.js +++ b/db-service/lib/cqn4sql.js @@ -82,6 +82,7 @@ function _cqn4sql(originalQuery, model, useTechnicalAlias = true) { const { where, having } = transformSearch(searchTerm) if (where) inferred.SELECT.where = where else if (having) inferred.SELECT.having = having + if (where || having) (inferred.SELECT.orderBy ??= []).unshift(...(where || having).map(fn => ({ __proto__: fn, sort: 'desc' }))) } } // query modifiers can also be defined in from ref leaf infix filter From 909605018cb1915f28e1f93c24ccbb3663e3a26a Mon Sep 17 00:00:00 2001 From: Bob den Os Date: Thu, 9 Apr 2026 12:08:21 +0200 Subject: [PATCH 2/3] Introduce numeric return type for search on HANA --- db-service/lib/cqn4sql.js | 3 ++- hana/lib/cql-functions.js | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/db-service/lib/cqn4sql.js b/db-service/lib/cqn4sql.js index 57d4555ea..d64189804 100644 --- a/db-service/lib/cqn4sql.js +++ b/db-service/lib/cqn4sql.js @@ -82,7 +82,8 @@ function _cqn4sql(originalQuery, model, useTechnicalAlias = true) { const { where, having } = transformSearch(searchTerm) if (where) inferred.SELECT.where = where else if (having) inferred.SELECT.having = having - if (where || having) (inferred.SELECT.orderBy ??= []).unshift(...(where || having).map(fn => ({ __proto__: fn, sort: 'desc' }))) + if (where || having) (inferred.SELECT.orderBy ??= []).unshift(...(where || having).map(fn => ({ func: fn.func, args:[...fn.args,true], sort: 'desc' }))) + // if (where || having) (inferred.SELECT.columns ??= []).unshift(...(where || having).map(fn => ({ func: fn.func, args:[...fn.args,true], as: 'score' }))) } } // query modifiers can also be defined in from ref leaf infix filter diff --git a/hana/lib/cql-functions.js b/hana/lib/cql-functions.js index 87324ed7e..3e3022e0b 100644 --- a/hana/lib/cql-functions.js +++ b/hana/lib/cql-functions.js @@ -114,7 +114,7 @@ const StandardFunctions = { * @param {string} arg - Argument object containing search values * @returns {string} - SQL statement */ - search: function (ref, arg) { + search: function (ref, arg, numeric) { if (cds.env.hana.fuzzy === false) { // Handle non-fuzzy search arg = arg.xpr ? arg.xpr : arg @@ -207,7 +207,7 @@ const StandardFunctions = { } } - return `(CASE WHEN SCORE(${arg} IN ${ref}) > 0 THEN TRUE ELSE FALSE END)` + return numeric ? `SCORE(${arg} IN ${ref})` : `(CASE WHEN SCORE(${arg} IN ${ref}) > 0 THEN TRUE ELSE FALSE END)` }, // ============================== From 0c4fe6e97dc72424f915c26209b6759809dacbcb Mon Sep 17 00:00:00 2001 From: Bob den Os Date: Fri, 10 Apr 2026 10:19:28 +0200 Subject: [PATCH 3/3] Add support for deep search terms (almost) --- db-service/lib/cqn4sql.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/db-service/lib/cqn4sql.js b/db-service/lib/cqn4sql.js index d64189804..1e032622d 100644 --- a/db-service/lib/cqn4sql.js +++ b/db-service/lib/cqn4sql.js @@ -82,8 +82,17 @@ function _cqn4sql(originalQuery, model, useTechnicalAlias = true) { const { where, having } = transformSearch(searchTerm) if (where) inferred.SELECT.where = where else if (having) inferred.SELECT.having = having - if (where || having) (inferred.SELECT.orderBy ??= []).unshift(...(where || having).map(fn => ({ func: fn.func, args:[...fn.args,true], sort: 'desc' }))) - // if (where || having) (inferred.SELECT.columns ??= []).unshift(...(where || having).map(fn => ({ func: fn.func, args:[...fn.args,true], as: 'score' }))) + if (searchTerm.func) (inferred.SELECT.orderBy ??= []).unshift({ func: searchTerm.func, args: [...searchTerm.args, true], sort: 'desc' }) + else if (searchTerm.xpr) { + const searchSelect = searchTerm.xpr[2] + const searchFunc = searchSelect.SELECT.where[0] + ; (inferred.SELECT.orderBy ??= []).unshift({ + __proto__: SELECT.from(searchSelect.SELECT.from) + .columns({ func: searchFunc.func, args: [...searchFunc.args, true] }) + .where([searchTerm.xpr[0], 'in', { list: searchSelect.SELECT.columns }]), // TODO: <-- ensure that the sub select in the order by is bound to the original query result row + sort: 'desc' + }) + } } } // query modifiers can also be defined in from ref leaf infix filter