When running the search_api_db unit tests against a MySQL 5.7 database, a fatal error is thrown:
Drupal test run
---------------
Tests to be run:
- Drupal\Tests\search_api_db\Kernel\BackendTest
Test run started:
Monday, June 6, 2016 - 15:31
Test summary
------------
Drupal\Tests\search_api_db\Kernel\BackendTest 0 passes 2 fails
FATAL Drupal\Tests\search_api_db\Kernel\BackendTest: test runner returned a non-zero error code (2).
Drupal\Tests\search_api_db\Kernel\BackendTest 0 passes 1 fails
Test run duration: 5 sec
Detailed test results
---------------------
---- Drupal\Tests\search_api_db\Kernel\BackendTest ----
Status Group Filename Line Function
--------------------------------------------------------------------------------
Fail Other BackendTest.php 86 Drupal\Tests\search_api_db\Kernel\B
Drupal\Tests\search_api_db\Kernel\BackendTest::testFramework
Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[HY000]: General
error: 3065 Expression #1 of ORDER BY clause is not in SELECT list,
references column 'd8.t.id' which is not in SELECT list; this is
incompatible with DISTINCT: SELECT DISTINCT t.item_id AS item_id, :score AS
score
FROM
{search_api_db_database_search_index} t
WHERE (( (t.item_id IN (SELECT t.item_id AS item_id
FROM
(SELECT t.item_id AS item_id, t.score AS score
FROM
{search_api_db_database_search_index_text} t
WHERE (word IN (:db_condition_placeholder_0)) AND (field_name IN
(:db_condition_placeholder_1)) ) t)) OR (t.item_id IN (SELECT t.item_id AS
item_id
FROM
(SELECT t.item_id AS item_id, t.score AS score
FROM
{search_api_db_database_search_index_text} t
WHERE (word IN (:db_condition_placeholder_2)) AND (field_name IN
(:db_condition_placeholder_3)) ) t)) ))
ORDER BY t.id ASC
LIMIT 10 OFFSET 0; Array
(
[:db_condition_placeholder_0] => bar
[:db_condition_placeholder_1] => name
[:db_condition_placeholder_2] => bar
[:db_condition_placeholder_3] => body
[:score] => 1000
)
/Users/vangeiv/Workspace/d8/core/lib/Drupal/Core/Database/Connection.php:671
/Users/vangeiv/Workspace/d8/core/lib/Drupal/Core/Database/Connection.php:635
/Users/vangeiv/Workspace/d8/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php:81
/Users/vangeiv/Workspace/d8/core/lib/Drupal/Core/Database/Query/Select.php:476
/Users/vangeiv/Workspace/search_api/search_api_db/src/Plugin/search_api/backend/Database.php:1537
/Users/vangeiv/Workspace/search_api/src/Entity/Server.php:409
/Users/vangeiv/Workspace/search_api/src/Query/Query.php:376
/Users/vangeiv/Workspace/search_api/tests/src/Kernel/BackendTestBase.php:301
/Users/vangeiv/Workspace/search_api/tests/src/Kernel/BackendTestBase.php:94
Fail run-tests. Unknown 0 Unknown
FATAL Drupal\Tests\search_api_db\Kernel\BackendTest: test runner returned a
non-zero error code (2).
From the MySQL 5.7 reference manual, 13.20.3 MySQL Handling of GROUP BY:
To prevent this problem, a query that has DISTINCT and ORDER BY is rejected as invalid if any ORDER BY expression does not satisfy at least one of these conditions:
- The expression is equal to one in the select list
- All columns referenced by the expression and belonging to the query's selected tables are elements of the select list
Patch attached checks if the column(s) used for sorting are part of the select list, and adds them if they are not.
Comments
Comment #3
drunken monkeyThanks for reporting this problem – and already providing a patch that resolves it!
I don't really understand the problem, I have to admit, but it seems this doesn't introduce any regressions in any of our environments, so if you say it helps for another one (MySQL 5.7), that's good enough. Code looks fine, too, so: committed.
Thanks again!