     agg.columns = ->
       columnsData = query { dims: dims, vals: vals, where: where }
       data = {}
+      rcols = aggbycols.concat sumcols
       for d, i in columnsData
         col = rcols[i]
         col = rename[col] if rename[col]?


  columnIndex = (name) ->
    for col,index in table
      if col.name is name then return index
    console.warn "Column '#{name}' not found. Available columns: #{(c.name for c in table)}"
    return null


    query = table.query
    dims = []
    vals = []
    aggbycols = []
    sumcols = []
    agg = {}
    where = null
    rename = {}
    agg.sparse = -> query = table.sparse_query; agg
    agg.count = -> vals.push dv.count(); sumcols.push("count"); agg

    agg.by = (cols...) -> 
      for c in cols
        aggbycols.push c
        dims.push(columnIndex(c))
      agg

    pushVals = (columns, fn) ->
      for c in columns
        sumcols.push c
        vals.push fn(columnIndex(c))
      agg

