Search code examples
vega-lite

Vega-lite chart fails when lookup, aggregate combined in transform


Why does this Vega-lite code, that uses both a Lookup transform and an Aggregate transform, fail to display a chart? This is a small example that models what I am trying to do in my project.

{
  "data": {    
          "values": [ 
            {"group": 1, "person": "Alan"},
            {"group": 1, "person": "George"},
            {"group": 1, "person": "Fred"},
            {"group": 2, "person": "Steve"},
            {"group": 2, "person": "Nick"},
            {"group": 2, "person": "Will"},
            {"group": 2, "person": "Cole"},
            {"group": 3, "person": "Rick"},
            {"group": 3, "person": "Tom"}
          ]},
  "transform": [
    {
      "lookup": "person",
      "from": {
        "data": {    
          "values": [
              {"name": "Alan", "_source": { "age": 10, "category": 5}},
              {"name": "Tom", "_source": { "age": 7, "category": 10}}
          ]},
        "key": "name",
        "fields": ["_source.age", "_source.category"]
      }
    },
    {"aggregate": 
      [{"op": "sum", "field": "_source.age", "as": "totalage"}], 
      "groupby": ["totalage", "_source.category"]}
  ],
  "mark": "bar",
  "encoding": {
    "x": {"field": "_source.category", "type": "ordinal"},
    "y": {"field": "totalage", "type": "quantitative"}
  }
}

But this Vega-lite code, that uses a Lookup transform but does not use an Aggregate transform, succeeds in displaying a chart?

{
  "data": {    
          "values": [ 
            {"group": 1, "person": "Alan"},
            {"group": 1, "person": "George"},
            {"group": 1, "person": "Fred"},
            {"group": 2, "person": "Steve"},
            {"group": 2, "person": "Nick"},
            {"group": 2, "person": "Will"},
            {"group": 2, "person": "Cole"},
            {"group": 3, "person": "Rick"},
            {"group": 3, "person": "Tom"}
          ]},
  "transform": [
    {
      "lookup": "person",
      "from": {
        "data": {    
          "values": [
              {"name": "Alan", "_source": { "age": 10, "category": 5}},
              {"name": "Tom", "_source": { "age": 7, "category": 10}}
          ]},
        "key": "name",
        "fields": ["_source.age", "_source.category"]
      }
    }
  ],
  "mark": "bar",
  "encoding": {
    "x": {"field": "_source.category", "type": "ordinal"},
    "y": {"field": "_source.age", "aggregate": "sum", "type": "quantitative"}
  }
}

Solution

  • Compound fields in the groupby are not being properly escaped (I believe this is a bug in vega-lite). As a workaround, you can escape them manually, i.e. replace

    "groupby": ["_source.category"]
    

    with

    "groupby": ["_source\\.category"]
    

    I've reported this bug at vega/vega-lite#6606