improve grouping_distribution() to consider all grouping keys
authorTomas Vondra <tomas@2ndquadrant.com>
Sun, 8 Jan 2017 22:24:17 +0000 (23:24 +0100)
committerPavan Deolasee <pavan.deolasee@gmail.com>
Tue, 31 Jan 2017 17:56:42 +0000 (23:26 +0530)
commiteed18da412d79c2e093c03ad2bc0c996953ead9e
tree53a8d7c137f4dc58b103ae983c8ab9ad68abc9fe
parent9540e4adebb1468b6b3862507feac47a2c6f5cb5
improve grouping_distribution() to consider all grouping keys

The original implementation required the distribution expression
to match the first grouping key, which resulted in suprising
inconsistencies when pushing down aggregation.

For example consider table 't' distributed by column 'a'

  CREATE TABLE t (a int, b int, c int) DISTRIBUTED BY (a);

and queries

  SELECT a, b, COUNT(*) FROM t GROUP BY a, b;
  SELECT b, a, COUNT(*) FROM t GROUP BY b, a;

Those queries are by definition equivalent, but the planner
used the full push-down only for the first one, as it has 'a'
as the first grouping key. The second query was using 2-phase
aggregation unnecessarily.
src/backend/optimizer/plan/planner.c