Uploaded image for project: 'Couchbase Documentation'
  1. Couchbase Documentation
  2. DOC-1887

Fix Object_pairs() documentation bugs, and other related functions

    XMLWordPrintable

Details

    • Bug
    • Resolution: Won't Fix
    • Major
    • 4.6.4
    • 4.0.x
    • query

    Description

      Hi Don,

      Thank you for the kind words.

      Keshav is very excited about the picture from Connect!

      On to your questions.

      (1) OBJECT_PAIRS(). It is possible that the documentation is wrong for this and some other features. We need to review it. OBJECT_PAIRS() accepts an object and returns an array of objects. The result array contains objects of the form

      { “name” : n, “value” : v }

      , with one object for each name-value pair in the original input object. In short, OBJECT_PAIRS() converts an object to an array of name-value objects.

      (2) should say, in the result of UNNEST OBJECT_PAIRS(), “value” is not an array. It is a scalar. Therefore, SUM() can be applied as normal. UNNEST is why it is a scalar.

      (3) This is the best N1QL specification document available. It is not user facing, but is an internal working spec. https://github.com/couchbase/query/blob/master/docs/n1ql-select.md.

      We have at least a couple of object constructors. This particular one takes a comprehension. It constructs an object from an array of name-value pairs.

      (4) Adding Prasad so he can look at the documentation. Prasad, please don’t reply to this thread. If you want to discuss, please open a new thread with me and Keshav.

      Thanks Don, I’m glad you found the query interesting. I feel that we don’t know how users will use schema as data, i.e. JSON. It is early days. So I find these user questions interesting and instructive.

      Keshav has a magazine article on schema as data. https://dzone.com/articles/json-files-whats-in-a-new-york-name-unlocking-data

      Gerald

      From: Don Chamberlin <chamberlin.don@gmail.com>
      Date: Thursday, November 10, 2016 at 10:02 PM
      To: Gerald Sangudi <gerald@couchbase.com>
      Cc: Keshav Murthy <keshav@couchbase.com>, Mike Carey <mike.carey@couchbase.com>, Till Westmann <Till@couchbase.com>, Yingyi Bu <yingyi@couchbase.com>, Yannis Papakonstantinou <yannis@cs.ucsd.edu>
      Subject: Re: Interesting data + meta question

      Hi Gerald,
      This is an interesting query and an ingenious solution. The OBJECT functions in N1QL are quite powerful! Reading the solution raises a few questions for me:

      1. The query includes a function call: OBJECT_PAIRS(data). But OBJECT_PAIRS takes an array of objects as its argument, and data is not an array, it is a single object. Does data somehow get coerced into an array?

      2. Similarly, the OBJECT_PAIRS() function returns an array of objects, each containing a "name" and a "value" field, where the "value" field is itself an array. In the query, each p.`value` is a singleton array, and the expression SUM(p.`value`) is trying to compute the sum of a collection of singleton arrays. But this is not how SUM works. This time we need to coerce singleton arrays back into scalars. How does this happen?

      3. The query contains this expression:
      OBJECT d.name : d.sum FOR d IN ARRAY_AGG(

      {"name":name, "sum":sum}

      ) END
      This expression seems to include both an object constructor and a list comprehension. Are these features always used together? I have not been able to find a syntax or description for this expression in the N1QL manual for Couchbase Server 4.5. Can you tell me where to look?

      4. In reading the documentation for the OBJECT_PAIRS() function, I see this expression in an example:
      SELECT object_pairs(schedule[0].special_flights[*])
      I don't understand the meaning of special_flights[*] here. I haven't been able to find any documentation in the N1QL manual on the use of * as an array index. Also, since object_pairs() takes an array as its argument, and special_flights is already an array, I don't see why it needs any index.

      Thanks for sharing this interesting query!
      -Don C.

      On Wed, Nov 9, 2016 at 10:55 AM, Gerald Sangudi <Gerald@couchbase.com> wrote:
      Don + All,

      I found this one interesting. Hard to anticipate what users will do with modeling flexibility.

      http://stackoverflow.com/questions/40489388/couchbase-aggregation-of-nested-field

      Gerald

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            simon.dew Simon Dew
            Prasad.Varakur Prasad Varakur
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes

                PagerDuty