summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDamien McKenna2018-04-11 14:56:58 (GMT)
committerDamien McKenna2018-04-11 14:56:58 (GMT)
commit82987986d4ca7b4efc91ecfcbedede60b4256d9f (patch)
treea9456e57163d432f4a8e0e9652cc22c320c7a3e2
parent4c60cf4a26203c7ffb9b21441291d1039bb6ce09 (diff)
Issue #1331056 by thekevinday, robertwb, steinmb, DamienMcKenna, joseph.olstad, chamilsanjeewa, Proteo, dawehner, steverweber, hubScrappy, tim.plunkett, colan, acbramley, stevector, merlinofchaos, sylus, seanB, scott.whittaker, matheushamonangan, drmabuse_, bellerophons_pegasus: Regression: Improper use of GROUP BY statement produces ambiguous column error.7.x-3.19
-rw-r--r--plugins/views_plugin_query_default.inc11
-rw-r--r--tests/views_groupby.test80
2 files changed, 89 insertions, 2 deletions
diff --git a/plugins/views_plugin_query_default.inc b/plugins/views_plugin_query_default.inc
index ad05eba..2911a56 100644
--- a/plugins/views_plugin_query_default.inc
+++ b/plugins/views_plugin_query_default.inc
@@ -1214,7 +1214,9 @@ class views_plugin_query_default extends views_plugin_query {
}
// This is a formula, using no tables.
elseif (empty($field['table'])) {
- $non_aggregates[] = $fieldname;
+ if (!in_array($fieldname, $non_aggregates)) {
+ $non_aggregates[] = $fieldname;
+ }
$placeholders = !empty($field['placeholders']) ? $field['placeholders'] : array();
$query->addExpression($string, $fieldname, $placeholders);
}
@@ -1222,10 +1224,15 @@ class views_plugin_query_default extends views_plugin_query {
elseif ($this->distinct && !in_array($fieldname, $this->groupby)) {
// d7cx: This code was there, apparently needed for PostgreSQL
// $string = db_driver() == 'pgsql' ? "FIRST($string)" : $string;
+ if (!in_array($string, $non_aggregates)) {
+ $non_aggregates[] = $string;
+ }
$query->addField(!empty($field['table']) ? $field['table'] : $this->base_table, $field['field'], $fieldname);
}
elseif (empty($field['aggregate'])) {
- $non_aggregates[] = $fieldname;
+ if (!in_array($string, $non_aggregates)) {
+ $non_aggregates[] = $string;
+ }
$query->addField(!empty($field['table']) ? $field['table'] : $this->base_table, $field['field'], $fieldname);
}
diff --git a/tests/views_groupby.test b/tests/views_groupby.test
index 93a96eb..364e61b 100644
--- a/tests/views_groupby.test
+++ b/tests/views_groupby.test
@@ -21,6 +21,86 @@ class ViewsQueryGroupByTest extends ViewsSqlTest {
);
}
+ // tests ambiguous group by column error (postgresql)
+ public function testAggregateAmbiguity() {
+ // Create 4 nodes of type1
+ $type1 = $this->drupalCreateContentType();
+
+ $node_1 = array(
+ 'type' => $type1->type,
+ );
+ $this->drupalCreateNode($node_1);
+ $this->drupalCreateNode($node_1);
+ $this->drupalCreateNode($node_1);
+ $this->drupalCreateNode($node_1);
+
+ $view = $this->viewsAggregateAmbiguityView();
+ $output = $view->execute_display();
+
+ $this->assertEqual(count($view->result), 1, 'Make sure there are no ambiguity problems with the group by operation.');
+ }
+
+ public function viewsAggregateAmbiguityView() {
+ $view = new view();
+ $view->name = 'aggregate_ambiguity';
+ $view->description = '';
+ $view->tag = 'default';
+ $view->base_table = 'node';
+ $view->human_name = '';
+ $view->core = 7;
+ $view->api_version = '3.0';
+ $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
+
+ /* Display: Master */
+ $handler = $view->new_display('default', 'Master', 'default');
+ $handler->display->display_options['use_more_always'] = FALSE;
+ $handler->display->display_options['group_by'] = TRUE;
+ $handler->display->display_options['access']['type'] = 'none';
+ $handler->display->display_options['cache']['type'] = 'none';
+ $handler->display->display_options['query']['type'] = 'views_query';
+ $handler->display->display_options['exposed_form']['type'] = 'basic';
+ $handler->display->display_options['pager']['type'] = 'full';
+ $handler->display->display_options['style_plugin'] = 'default';
+ $handler->display->display_options['row_plugin'] = 'fields';
+ /* Field: COUNT(Content revision: Nid) */
+ $handler->display->display_options['fields']['nid']['id'] = 'nid';
+ $handler->display->display_options['fields']['nid']['table'] = 'node_revision';
+ $handler->display->display_options['fields']['nid']['field'] = 'nid';
+ $handler->display->display_options['fields']['nid']['group_type'] = 'count';
+ $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0;
+ $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0;
+ $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1;
+ $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1;
+ $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0;
+ $handler->display->display_options['fields']['nid']['alter']['trim'] = 0;
+ $handler->display->display_options['fields']['nid']['alter']['html'] = 0;
+ $handler->display->display_options['fields']['nid']['hide_empty'] = 0;
+ $handler->display->display_options['fields']['nid']['empty_zero'] = 0;
+ /* Field: Content: Nid */
+ $handler->display->display_options['fields']['nid_1']['id'] = 'nid_1';
+ $handler->display->display_options['fields']['nid_1']['table'] = 'node';
+ $handler->display->display_options['fields']['nid_1']['field'] = 'nid';
+ $handler->display->display_options['fields']['nid_1']['alter']['alter_text'] = 0;
+ $handler->display->display_options['fields']['nid_1']['alter']['make_link'] = 0;
+ $handler->display->display_options['fields']['nid_1']['alter']['word_boundary'] = 1;
+ $handler->display->display_options['fields']['nid_1']['alter']['ellipsis'] = 1;
+ $handler->display->display_options['fields']['nid_1']['alter']['strip_tags'] = 0;
+ $handler->display->display_options['fields']['nid_1']['alter']['trim'] = 0;
+ $handler->display->display_options['fields']['nid_1']['alter']['html'] = 0;
+ $handler->display->display_options['fields']['nid_1']['hide_empty'] = 0;
+ $handler->display->display_options['fields']['nid_1']['empty_zero'] = 0;
+ /* Contextual filter: Content: Type */
+ $handler->display->display_options['arguments']['type']['id'] = 'type';
+ $handler->display->display_options['arguments']['type']['table'] = 'node';
+ $handler->display->display_options['arguments']['type']['field'] = 'type';
+ $handler->display->display_options['arguments']['type']['default_action'] = 'summary';
+ $handler->display->display_options['arguments']['type']['default_argument_type'] = 'fixed';
+ $handler->display->display_options['arguments']['type']['summary']['format'] = 'default_summary';
+
+
+ return $view;
+ }
+
/**
* Tests aggregate count feature.
*/