chai icon indicating copy to clipboard operation
chai copied to clipboard

Add `len` function for array type

Open icexin opened this issue 1 year ago • 2 comments

Proposal

Currently, it’s not possible to compute the length of array type in Genji

icexin avatar Jul 13 '22 10:07 icexin

It was planned for another release but I guess we can add it to the next one.

Specs

Function name: len Arity: 1 Params: an expression that evaluates to either:

  • a TEXT
  • an ARRAY
  • a DOCUMENT

Example:

SELECT len("hello");
{
  "len(\"hello"\)": 5
}

SELECT len([1, 2, 3, [4, 5]]) AS length;
{
  "length": 4
}

-- len with incompatible types evaluate to NULL
SELECT len(10) AS length;
{
  "length": NULL
}

-- example with a table
CREATE TABLE foo(
  a TEXT,
  b ARRAY,
  c (
      ...
  )
);

INSERT INTO foo VALUES (
  "hello",
  [1, 2, 3, [4, 5]],
  {
    a: 1,
    b: 2,
    c: {
      d: 3
    }
  }
);

-- length of a text field
SELECT len(a) FROM foo;
{
  "len(a)":10
}


SELECT len(b) FROM foo;
{
  "len(a)": 4
}

SELECT len(c) FROM foo;
{
  "len(a)": 3
}

SELECT len(b[3]) FROM foo;
{
  "len(b[3])": 2
}

SELECT len(c.c)  FROM foo;
{
  "len(c.c)": 1
}

asdine avatar Jul 15 '22 07:07 asdine

This looks great

gedw99 avatar Jul 22 '22 08:07 gedw99