zed icon indicating copy to clipboard operation
zed copied to clipboard

join-key expressions are too fussy

Open mccanne opened this issue 5 years ago • 2 comments

The join-key expressions currently work on each side's respective table. You should be able to refer to either side on either side of the join expression. The cut-expressions will continue to come from the dimension-side of the join. This is easy to fix by first trying the preferred side for the key's expression and if it is MISSING, try the other side. This would be preferred over doing the work to creating a new joined record only to throw it out in a common case.

mccanne avatar Feb 25 '21 15:02 mccanne

The Zed docs published in #2836 cited this issue as a current limitation. When this issue is addressed, the docs should be updated.

philrz avatar Jul 01 '21 22:07 philrz

It looks like the join-key expressions in the new join implementation added in #5962 are no longer "fussy".

Details

Here's a walk through of how things stand as of super commit f86de86.

Let's start with the top example from the Join Tutorial.

$ super -version
Version: f86de86d5

$ super -c "
from fruit.json
| inner join (
  from people.json
) as {f,p} on f.flavor=p.likes
| values {...f, eater:p.name}"

{name:"figs",color:"brown",flavor:"plain",eater:"jessie"}
{name:"banana",color:"yellow",flavor:"sweet",eater:"quinn"}
{name:"strawberry",color:"red",flavor:"sweet",eater:"quinn"}
{name:"dates",color:"brown",flavor:"sweet",note:"in season",eater:"quinn"}
{name:"apple",color:"red",flavor:"tart",eater:"morgan"}
{name:"apple",color:"red",flavor:"tart",eater:"chris"}

While the join key expression f.flavor=p.likes used here happens to match the relative left/right order of the inputs, it can now be reversed and produces the same result.

$ super -c "
from fruit.json
| inner join (
  from people.json
) as {f,p} on p.likes=f.flavor        
| values {...f, eater:p.name}"

{name:"figs",color:"brown",flavor:"plain",eater:"jessie"}
{name:"banana",color:"yellow",flavor:"sweet",eater:"quinn"}
{name:"strawberry",color:"red",flavor:"sweet",eater:"quinn"}
{name:"dates",color:"brown",flavor:"sweet",note:"in season",eater:"quinn"}
{name:"apple",color:"red",flavor:"tart",eater:"morgan"}
{name:"apple",color:"red",flavor:"tart",eater:"chris"}

The concept of "cut-expressions" in the join as described in this issue's opening text no longer exist, as the full left/right records that were successfully joined are now output as embedded records, e.g.,

$ super -c "
from fruit.json
| inner join (
  from people.json
) as {f,p} on p.likes=f.flavor"

{f:{name:"figs",color:"brown",flavor:"plain"},p:{name:"jessie",age:30,likes:"plain"}}
{f:{name:"banana",color:"yellow",flavor:"sweet"},p:{name:"quinn",age:14,likes:"sweet",note:"many kids enjoy sweets"}}
{f:{name:"strawberry",color:"red",flavor:"sweet"},p:{name:"quinn",age:14,likes:"sweet",note:"many kids enjoy sweets"}}
{f:{name:"dates",color:"brown",flavor:"sweet",note:"in season"},p:{name:"quinn",age:14,likes:"sweet",note:"many kids enjoy sweets"}}
{f:{name:"apple",color:"red",flavor:"tart"},p:{name:"morgan",age:61,likes:"tart"}}
{f:{name:"apple",color:"red",flavor:"tart"},p:{name:"chris",age:47,likes:"tart"}}

Hence the values {...f, eater:p.name} that assembled the desired fields into a top-level record to suit our preferences.

I'll hold this one open for some peer review to make sure I'm not missing any other subtleties, but if it passes muster I'll close it out.

philrz avatar Jul 03 '25 16:07 philrz