substance / data

A uniform interface for domain data (deprecated)
http://code.substance.io/docs/substance-manual/
MIT License
655 stars 44 forks source link

Aggregates are wrong for non unique columns #30

Open jamescasbon opened 13 years ago

jamescasbon commented 13 years ago

If you have a repeated value in a Data.Collection, the aggregation produces incorrect values:

Test data, note we have two german countries with the same population:

var countries_data = { "properties": { "name": {"name": "Country Name", "type": "string", "unique": true }, "official_language": {"name": "Official Language", "type": "string", "unique": false }, "population": { "name": "Population", "type": "number", "unique": false } }, "items": { 0: { "name": "Austria", "official_language": "German", "population": 8356700, },

  'usa': {
    "name": "United States of America",
    "official_language": "English",
    "population": 310955497,
  },
  'ger': {
    "name": "Germany",
    "official_language": "German",
    "population": 82062200,
  },
  'ger2': {
    "name": "Germany2",
    "official_language": "German",
    "population": 82062200,
  }
}

}

var countries = new Data.Collection(countries_data); console.log('oh sweet, just made a collection:'); console.log(countries_data);

var population = countries.properties().get('population'); console.log('get returns a Data.Property: '); console.log(population);

console.log('which supports aggregation: '); var population_sum = population.aggregate(Data.Aggregators.SUM) // => Returns total population of all countries in the collection. console.log(population_sum);

equals(population_sum, 310955497 + 82062200 + 8356700 + 82062200, 'We can aggregate a column');

This test fails, I think because Data.Property.aggregate is defined:

aggregate: function (fn) {
  return fn(this.values("values"));
},

this.values produces a hash where each key is the value considered, so in this case it has three rather than 4 values do to the repeated value.