Uploaded image for project: 'Couchbase Lite'
  1. Couchbase Lite
  2. CBL-5515

Result alias can't be used elsewhere in query

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • Major
    • Beryllium
    • None
    • LiteCore
    • Security Level: Public
    • LiteCore 130
    • 4

    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.

      Attachments

        Activity

          People

            jianmin.zhao Jianmin Zhao
            jens Jens Alfke
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              PagerDuty