Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Keyword fieldtype] Added ORDER BY to keyword query #346

Merged
merged 1 commit into from
Nov 26, 2022

Conversation

mnocon
Copy link
Member

@mnocon mnocon commented Oct 28, 2022

Question Answer
JIRA issue n/a
Type improvement
Target Ibexa version v3.3 and higher
BC breaks no

The keyword fieldtype tests sometimes fail:
https://github.com/ibexa/core/actions/runs/3195771808/jobs/5217051683

1) Ibexa\Tests\Integration\Core\Repository\FieldType\KeywordIntegrationTest::testDeleteTranslation
Failed asserting that two objects are equal.
--- Expected
+++ Actual
@@ @@
     'fieldDefIdentifier' => 'data'
     'value' => Ibexa\Core\FieldType\Keyword\Value Object (
         'values' => Array (
-            0 => 'eng-US sindelfingen'
+            0 => 'eng-US bar'
             1 => 'eng-US foo'
-            2 => 'eng-US bar'
+            2 => 'eng-US sindelfingen'
         )
     )
     'languageCode' => 'eng-US'
     'fieldTypeIdentifier' => 'ezkeyword'
 )

The returned order of keywords is different from the one we except. It's because the query does not specify the expected order, meaning they can be returned in a random one.

Things done:

  1. Added ORDER BY to the keyword query to make the returned value consistent
  2. Applied suggestions from Andrew made during private conversation when I first mentioned this failure:
  • extracted $query->expr() to a separate variable
  • changed parameter naming to follow our convention
  • changed deprecated fetchAll(FetchMode::COLUMN) usage

Testing

Basic sanities using the keyword fieldtype are needed (creating, updating)

Checklist:

  • Provided PR description.
  • Tested the solution manually.
  • Provided automated test coverage.
  • Checked that target branch is set correctly (master for features, the oldest supported for bugs).
  • Ran PHP CS Fixer for new PHP code (use $ composer fix-cs).
  • Asked for a review (ping @ezsystems/engineering-team).

@sonarcloud
Copy link

sonarcloud bot commented Oct 28, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@mnocon mnocon marked this pull request as ready for review October 28, 2022 14:48
@mnocon mnocon requested a review from a team October 28, 2022 15:39
@konradoboza konradoboza requested a review from a team October 31, 2022 06:20
@adamwojs adamwojs requested a review from a team October 31, 2022 06:45
@Steveb-p
Copy link
Contributor

Do note however that Doctrine documentation does not endorse storing ExpressionBuilder in a variable: https://www.doctrine-project.org/projects/doctrine-orm/en/2.13/reference/query-builder.html#working-with-querybuilder

You can see there that they are re-requesting the Expression Builder from the Query Builder every time:

<?php
// $qb instanceof QueryBuilder

$qb->select(array('u')) // string 'u' is converted to array internally
   ->from('User', 'u')
   ->where($qb->expr()->orX(
       $qb->expr()->eq('u.id', '?1'),
       $qb->expr()->like('u.nickname', '?2')
   ))
   ->orderBy('u.surname', 'ASC');

@alongosz
Copy link
Member

Do note however that Doctrine documentation does not endorse storing ExpressionBuilder in a variable: https://www.doctrine-project.org/projects/doctrine-orm/en/2.13/reference/query-builder.html#working-with-querybuilder

You can see there that they are re-requesting the Expression Builder from the Query Builder every time:

<?php
// $qb instanceof QueryBuilder

$qb->select(array('u')) // string 'u' is converted to array internally
   ->from('User', 'u')
   ->where($qb->expr()->orX(
       $qb->expr()->eq('u.id', '?1'),
       $qb->expr()->like('u.nickname', '?2')
   ))
   ->orderBy('u.surname', 'ASC');

@Steveb-p not everything that is there follows best practices. This at some point popped up on some profiling tools not as performance issue (atm), but as something which might impact performance in the future. If you have deterministic function result called multiple times, you should expand it to variable, unless 3rd party doc explicitly says why you shouldn't.

@alongosz alongosz added Improvement Changes not fixing or changing behavior Ready for QA labels Nov 21, 2022
@Steveb-p
Copy link
Contributor

@Steveb-p not everything that is there follows best practices.

I have no doubt about that, but you can criticize any code library in a similar fashion, because there is always something that can be done differently.

If you have deterministic function result called multiple times, you should expand it to variable, unless 3rd party doc explicitly says why you shouldn't.

It may stop being a deterministic function, because it is never stated that it is. It may start returning new ExpressionBuilder objects due to the need to maintain some sort of state (just like QueryBuilder does). Assuming it will not is, well, assuming something. And that works most of the time. Most.

@alongosz
Copy link
Member

alongosz commented Nov 21, 2022

We shouldn't add time and cognitive complexity to our code just because in a distant unlikely future something might change. The quoted examples are not convincing enough argument for me to change the approach. They're meant to visualize something else. If Doctrine states "always call $queryBuilder->expr() because it might become dynamic in the future", then we will. Until then we should follow best practices and don't call multiple times within a given scope a method which returns the same result.

Copy link

@tomaszszopinski tomaszszopinski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QA approved on IbexaDXP 3.3 commerce.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Improvement Changes not fixing or changing behavior QA approved Ready for review
8 participants