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

Allow order fields to be subqueries with order #18909

Merged
merged 3 commits into from
Jul 11, 2019

Conversation

kbrock
Copy link
Member

@kbrock kbrock commented Jun 24, 2019

Fixes: ManageIQ/manageiq-ui-classic#5374

Overview

Virtual fields that reference a has_many relationship, often need to have a LIMIT and ORDER in the field definition.

When rails works with these fields in an ORDER BY clause, they sometimes add that field to the SELECT clause for Sql reasons. In that process, they munge the field and break things.

Before

We see this issue on the Vm explorer page. If we sort by compliance (i.e. last_compliance_status) it blows up:

VmOrTemplate.includes(:hardware => :disks).references('x').order(:last_compliance_status).first

ActiveRecord::StatementInvalid (PG::InvalidColumnReference: ERROR:  for SELECT DISTINCT, ORDER BY expressions must appear in select list)
LINE 1: ..."disks"."hardware_id" = "hardwares"."id" ORDER BY (SELECT  "...

After

I put in a Rails PR that avoids string munging and leverages Arel. In the mean time, this PR is a monkey patch to our codebase with the same code that fixes the problem in the short term.

@kbrock
Copy link
Member Author

kbrock commented Jun 24, 2019

RE Rubocops: - I copy and pasted these bits from the rails code.
I am aiming to keep it as close to rails style and concepts as possible.

@kbrock kbrock force-pushed the fix_order branch 3 times, most recently from fa0c7e0 to 146c09a Compare June 24, 2019 20:17
@Fryguy
Copy link
Member

Fryguy commented Jun 24, 2019

cc @jrafanie

@jrafanie
Copy link
Member

@kbrock Can you make the first commit the current code in rails (include which rails version you're pulling from... I have no idea if it's changed in 5.1 -> 5.2 -> 6), and then apply your patch in the second commit... It should make this easier to dissect. I'm pretty lost.

@kbrock kbrock force-pushed the fix_order branch 3 times, most recently from db39bc9 to bf2d349 Compare July 2, 2019 15:23
@kbrock
Copy link
Member Author

kbrock commented Jul 8, 2019

@jrafanie ok, updated. is this easier to figure out?

lib/extensions/ar_order.rb Outdated Show resolved Hide resolved
s = collector.value
end
end
# Remove any ASC/DESC modifiers
Copy link
Member

Choose a reason for hiding this comment

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

This comment should be updated to reflect the conditional you're doing here. We should explain that there's an existing ORDER clause so we want to retain any ASC/DESC.

kbrock added 3 commits July 8, 2019 15:55
This lets us see just what was done in the money patch
Fields like last_compliance_status (on VmOrTemplate.yaml) contain
a limit and order.

This is because the column joins to a has_many and references
the most recent record.

ManageIQ/manageiq-ui-classic#5374
@miq-bot
Copy link
Member

miq-bot commented Jul 8, 2019

Checked commits kbrock/manageiq@18f71ad~...e237d0a with ruby 2.4.6, rubocop 0.69.0, haml-lint 0.20.0, and yamllint 1.10.0
2 files checked, 5 offenses detected

lib/extensions/ar_order.rb

@jrafanie
Copy link
Member

Note, ignoring cops because it's rails code... we're trying to avoid making unrelated changes to the core rails code.

@jrafanie
Copy link
Member

@kbrock did you test the UI suite with this PR?

Copy link
Member

@jrafanie jrafanie left a comment

Choose a reason for hiding this comment

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

LGTM, I'll merge when we get confirmation that UI classic test suite works with this change.

@kbrock
Copy link
Member Author

kbrock commented Jul 11, 2019

@jrafanie ui classic passed

@jrafanie jrafanie merged commit 5d6eb83 into ManageIQ:master Jul 11, 2019
@jrafanie jrafanie self-assigned this Jul 11, 2019
@jrafanie jrafanie added this to the Sprint 116 Ending Jul 22, 2019 milestone Jul 11, 2019
@kbrock kbrock deleted the fix_order branch July 11, 2019 21:30
simaishi pushed a commit that referenced this pull request Aug 27, 2019
Allow order fields to be subqueries with order

(cherry picked from commit 5d6eb83)

https://bugzilla.redhat.com/show_bug.cgi?id=1738266
@simaishi
Copy link
Contributor

Hammer backport details:

$ git log -1
commit 004033cb16b3dbbe0ed8fd7ad45936ae88d35ff2
Author: Joe Rafaniello <[email protected]>
Date:   Thu Jul 11 17:14:32 2019 -0400

    Merge pull request #18909 from kbrock/fix_order
    
    Allow order fields to be subqueries with order
    
    (cherry picked from commit 5d6eb830cf0faded6d54c5661cdee6834f16259f)
    
    https://bugzilla.redhat.com/show_bug.cgi?id=1738266

@kbrock
Copy link
Member Author

kbrock commented Aug 27, 2019

Other note - it also addresses https://bugzilla.redhat.com/show_bug.cgi?id=1745660

@jrafanie
Copy link
Member

jrafanie commented Apr 2, 2020

@miq-bot add_label rails5.2

@chessbyte chessbyte mentioned this pull request Apr 2, 2020
38 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Report data request fails on invalid SQL query when sorting
5 participants