Details
Description
The following N1QL query incorrectly throws an exception in QueryParser, "property 'distance' does not begin with a declared 'AS' alias":
SELECT VECTOR_DISTANCE(a.vecIndex) AS distance FROM _default AS a JOIN other ON META(a).id = other.refID WHERE VECTOR_MATCH(a.vecIndex, $target) ORDER BY distance
The JSON translation is:
*
{"FROM":[\{"AS":"a","COLLECTION":"_default"},{"COLLECTION":"other","JOIN":"INNER","ON":["=",[".",["meta()","a"],".id"],[".other.refID"]]}],"ORDER_BY":[[".distance"]],"WHAT":[[".",["meta()","a"],".id"],["_.",["meta()","other"],".id"],["AS",["VECTOR_DISTANCE()","a.vecIndex"],"distance"]],"WHERE":["AND",["VECTOR_MATCH()","a.vecIndex",["$target"]],["MATCH()","other.sentence","search"]]}*
(I'm sure this can be reduced a lot; it obviously doesn't require vector search. I think it does require using multiple collections though.)
The exception comes from "ORDER BY distance" clause. writePropertyGetter calls verifyDbAlias, which finds "distance" but explicitly rejects it for some reason; the comment above says "we only look for alias in the db context.". This code was added in fixing CBL-2176, which has to do with fixing META subproperties.
I tried removing the line that disqualifies the alias (by setting iType = _aliases.end()), but that caused QueryParser to generate invalid SQL: "...ORDER BY fl_root(distance.body)".
NOTE: I'm on the master branch of LiteCore; not sure if this affects released versions, but CBL-2176 was fixed in 2021 so this may go way back.