diff -u b/core/modules/comment/comment.module b/core/modules/comment/comment.module --- b/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1046,7 +1046,7 @@ function comment_cron() { // Store the maximum possible comments per thread (used for node search // ranking by reply count). - \Drupal::state()->set('comment.node_comment_statistics_scale', 1.0 / max(1.0, (float) db_query('SELECT MAX(comment_count) FROM {comment_entity_statistics}')->fetchField())); + \Drupal::state()->set('comment.node_comment_statistics_scale', 1.0 / max(1, db_query('SELECT MAX(comment_count) FROM {comment_entity_statistics}')->fetchField())); } /** @@ -1056,8 +1056,8 @@ */ function comment_cron() { // Store the maximum possible comments per thread (used for node search - // ranking by reply count). - \Drupal::state()->set('comment.node_comment_statistics_scale', 1.0 / max(1, db_query('SELECT MAX(comment_count) FROM {comment_entity_statistics}')->fetchField())); + // ranking by reply count). Make sure it is a float value. + \Drupal::state()->set('comment.node_comment_statistics_scale', 1.0 / max(1.0, (float) db_query('SELECT MAX(comment_count) FROM {comment_entity_statistics}')->fetchField())); } /** @@ -1558,8 +1558,8 @@ 'on' => "ces.entity_id = i.sid AND ces.entity_type = 'node' AND ces.field_id = 'node__comment'", ), // Inverse law that maps the highest reply count on the site to 1 and 0 to 0. - 'score' => '2.0 - 2.0 / (1.0 + ces.comment_count * (CAST (:comment_scale AS DECIMAL(10, 4))))', - 'arguments' => array(':comment_scale' => \Drupal::state()->get('comment.node_comment_statistics_scale') ?: 0), + 'score' => '2.0 - 2.0 / (1.0 + ces.comment_count * :scale)', + 'arguments' => array(':scale' => (float) \Drupal::state()->get('comment.node_comment_statistics_scale') ?: 0), ), ); } @@ -1569,9 +1569,12 @@ // nodes. 'on' => "ces.entity_id = i.sid AND ces.entity_type = 'node' AND ces.field_id = 'node__comment'", ), - // Inverse law that maps the highest reply count on the site to 1 and 0 to 0. - 'score' => '2.0 - 2.0 / (1.0 + ces.comment_count * :scale)', - 'arguments' => array(':scale' => (float) \Drupal::state()->get('comment.node_comment_statistics_scale') ?: 0), + // Inverse law that maps the highest reply count on the site to 1 and 0 + // to 0. Note that the CAST here is necessary for PostgreSQL, because the + // PostgreSQL PDO driver sometimes puts values in as strings instead of + // numbers in complex expressions like this. + 'score' => '2.0 - 2.0 / (1.0 + ces.comment_count * (CAST (:comment_scale AS DECIMAL(10, 4))))', + 'arguments' => array(':comment_scale' => \Drupal::state()->get('comment.node_comment_statistics_scale') ?: 0), ), ); } diff -u b/core/modules/statistics/statistics.module b/core/modules/statistics/statistics.module --- b/core/modules/statistics/statistics.module +++ b/core/modules/statistics/statistics.module @@ -119,7 +119,8 @@ \Drupal::state()->set('statistics.day_timestamp', REQUEST_TIME); } - // Calculate the maximum of node views, for node search ranking. + // Calculate the maximum of node views, for node search ranking. Make sure + // it is a float value. \Drupal::state()->set('statistics.node_counter_scale', 1.0 / max(1.0, (float) db_query('SELECT MAX(totalcount) FROM {node_counter}')->fetchField())); } @@ -243,7 +244,10 @@ 'alias' => 'node_counter', 'on' => 'node_counter.nid = i.sid', ), - // Inverse law that maps the highest view count on the site to 1 and 0 to 0. + // Inverse law that maps the highest view count on the site to 1 and 0 + // to 0. Note that the CAST here is necessary for PostgreSQL, because + // the PostgreSQL PDO driver sometimes puts values in as strings + // instead of numbers in complex expressions like this. 'score' => '2.0 - 2.0 / (1.0 + node_counter.totalcount * (CAST (:statistics_scale AS DECIMAL(10,4))))', 'arguments' => array(':statistics_scale' => \Drupal::state()->get('statistics.node_counter_scale') ?: 0), ),