diff --git a/db-service/lib/cqn4sql.js b/db-service/lib/cqn4sql.js index 8cad82eb0..1e032622d 100644 --- a/db-service/lib/cqn4sql.js +++ b/db-service/lib/cqn4sql.js @@ -82,6 +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 (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 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)` }, // ==============================