Open andrewbaxter opened 8 months ago
Curious. What do you mean by:
Record matching for non-relations, like := my_rule{a: a, b: b}
isn't this already supported? Also, could you use json objects in place of records? It won't be pretty but I guess it'll have similar semantics.
My brain cache has been purged, but I think it was something like this:
{
stuff[x,y] <- [[1, 2]]
?[y] := stuff{y: 2, x: y}
}
This gives a syntax error. It would be equivalent to stuff[y, 2]
but more explicit.
× The query parser has encountered unexpected input / end of input at 38..38
╭─[2:1]
2 │ stuff[x,y] <- [[1, 2]]
3 │ ?[y] := stuff{y: 2, x: y}
· ▲
4 │ }
╰────
You're right, JSON might work. This works for instance:
{
stuff[x] <- [[json({"x": 1, "y": 2})]]
?[y] := stuff[x], y = get(x, "x"), get(x, "y") == 2
}
And you can effectively name output parameters
{
stuff[x] <- [[json({"x": 1, "y": 2})]]
?[res] := stuff[x], y = get(x, "x"), get(x, "y") == 2, res = json({"y": get(x, "x")})
}
which I think checks all my boxes above.
If I were to push for having this natively instead of via json, I think the reasons are:
{}
is supported in some places but not others)There's definitely an overhead of json conversions and it can be error-prone for no real benefit other than syntax niceties. I do second this on second thought, it may make sense that records -> json mapping is easy and simply calling json({...})
on them works.
Using tuples everywhere relies on positioning to correlate symbols which is risky (mix up the order of two symbols and you can end up with strange results).
Relations kind of support a record syntax, but this isn't supported anywhere else.
I'd like:
?{something: a, something_else: b} := ...
. (Also this would solve the issue that right now there's no way to rename results other than writing a new rule that just forwards the elements with a new name, which is excessive and makes queries more fragile):= my_rule{a: a, b: b}
I assume the framework for this is already there, since results already have headers etc.
And maybe
y = {a: 4, b: something}
get(y, "a")