Thanks for our quick reply. I tried to extend the query with a document as input and somehow I never get a result. Below you will find one of my tests. Do you know how to get your example working with a select in the with / what am I doing wrong?
WITH
s AS (
SELECT raw name
FROM travel-sample.inventory.hotel limit 1
),
step as (2)
SELECT ARRAY {“sequence”:pos, “name”:“xyz”, “body”: SUBSTR(s,v,step)}
FOR pos:v IN ARRAY_RANGE(0,LENGTH(s),step)
END AS obj;
Of course you’d need to quote travel-sample else you’d be getting a syntax error (but I assume this isn’t the case) and the results are an array so you’d have to dereference it:
cbq> WITH s AS ((SELECT raw name FROM `travel-sample`.inventory.hotel limit 1)[0]), step as (2) SELECT ARRAY {"sequence":pos, "name":"xyz", "body": SUBSTR(s,v,step)} FOR pos:v IN ARRAY_RANGE(0,LENGTH(s),step) END AS obj;
{
"requestID": "f74f3f14-5f71-47b5-a4a1-871d0bfe000c",
"signature": {
"obj": "array"
},
"results": [
{
"obj": [
{
"body": "Me",
"name": "xyz",
"sequence": 0
},
{
...
Or you could fetch it element by element rather than using it as the array (from clause):
cbq> WITH s AS (SELECT raw name FROM `travel-sample`.inventory.hotel limit 1), step as (2) SELECT ARRAY {"sequence":pos, "name":"xyz", "body": SUBSTR(s,v,step)} FOR pos:v IN ARRAY_RANGE(0,LENGTH(s),step) END AS obj FROM s;
{
"requestID": "fc11b227-2ad5-4eb7-aaca-8df0c3da35c5",
"signature": {
"obj": "array"
},
"results": [
{
"obj": [
{
"body": "Me",
"name": "xyz",
"sequence": 0
},
{
"body": "dw",
"name": "xyz",
"sequence": 1
},
...
WITH step AS (2)
SELECT ARRAY {"sequence":pos, "name":"xyz", "body": SUBSTR(h.name , v, step)}
FOR pos:v IN ARRAY_RANGE(0,LENGTH(h.name),step)
END AS obj
FROM `travel-sample`.inventory.hotel AS h
LIMIT 1;