Yeah so I have tried to solve this with macros but as @OvermindDL1 correctly notes it would impose a max size.
Regarding more concrete examples, the column in question is just a blob of JSON that can be anything, so concrete examples are not that helpful I donât think. I can explain what I am trying to do a little more, and they may help move the discussion.
The api in question lets you provide a couple of date ranges, a segment value, along with a query on a topic value, and get back a list of segmented results for the date range with a count and a total sum. eg:
-topic=account_created
-before=2019-02-22T00:00:00Z
-after=2019-02-19T00:00:00Z
-segment[unit]=day
-segment[value]=1
will return:
{
"total": 5,
"range": [
{
"value": 0,
"total": 0,
"before": "2019-02-20T00:00:00+00:00",
"after": "2019-02-19T00:00:00+00:00"
},
{
"value": 2,
"total": 2,
"before": "2019-02-21T00:00:00+00:00",
"after": "2019-02-20T00:00:00+00:00"
},
{
"value": 3,
"total": 5,
"before": "2019-02-22T00:00:00+00:00",
"after": "2019-02-21T00:00:00+00:00"
}
]
}
That works fine, and is not that hard to do. No though, we want to break up the value field of each segment by some arbitrary group_by. It is arbitrary because different topics have different chunks of JSON associated.
The example I provided in my opening question âdata->âchangesâ->âitemsâ->0->âplanââ should result in something like:
{
"value": {
plan_1: 2,
plan_2: 1
},
"total": 5,
"before": "2019-02-22T00:00:00+00:00",
"after": "2019-02-21T00:00:00+00:00"
}
Hopefully that is clear. Here is the query I would want to make in the above example:
query
|> group_by([e], fragment("data->'changes'->'items'->0->'plan'"))
|> select(
[e],
{fragment("data->'changes'->'items'->0->'plan'"),
fragment("count(data->'changes'->'items'->0->'plan')")}
)
pretty simple, we are just adding in the group_by, and instead of selecting the count, we are selecting on the fragments. I have written this in SQL, and got the results I want.
unsafe_fragment is what I want. I like that Ecto is protecting young players, but in this case it is really limiting me.
Do you know where I might be able to find that source, maybe I need to write my own unsafe_fragment?
Thanks for the help here