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

Issue when using union(), orderBy() and paginate() #71

Closed
jtomlinson opened this issue Dec 11, 2023 · 1 comment
Closed

Issue when using union(), orderBy() and paginate() #71

jtomlinson opened this issue Dec 11, 2023 · 1 comment

Comments

@jtomlinson
Copy link
Contributor

I've run into an issue with the way the SingleStore driver handles the following code block.

$orders = Order::select(['orders.make', 'orders.part_no'])
  ->where('orders.dealer_id', 1407)
  ->whereDate('orders.order_date', '>', now()->subWeek());

$shipments = Shipment::select(['shipments.make', 'shipments.part_no'])
  ->where('shipments.dealer_id', 1407)
  ->whereDate('shipments.shipped_date', '>', now()->subWeek());

$all = $orders->union($shipments);

$all->orderBy('part_no');

$all->paginate();

SingleStore driver generates the query as follows:

SELECT
FROM (select count() as aggregate
      from (SELECT
            FROM (SELECT *
                  FROM (select orders.make, orders.part_no
                        from scanitparts.orders
                        where orders.dealer_id = 1407
                          and date(orders.order_date) > 2023 - 12 - 04)
                  union
                  (select shipments.make, shipments.part_no
                   from scanitparts.shipments
                   where shipments.dealer_id = 1407
                     and date(shipments.shipped_date) > 2023 - 12 - 04))
            order by part_no asc) as temp_table)
order by part_no asc

When using the mysql driver the same code block generates the following query:

select count(*) as aggregate
from ((select `orders`.`make`, `orders`.`part_no`
       from `scanitparts`.`orders`
       where `orders`.`dealer_id` = 1407
         and date(`orders`.`order_date`) > '2023-12-04')
      union
      (select `shipments`.`make`, `shipments`.`part_no`
       from `scanitparts`.`shipments`
       where `shipments`.`dealer_id` = 1407
         and date(`shipments`.`shipped_date`) > '2023-12-04')
      order by `part_no` asc) as `temp_table`

The SingleStore driver is adding an extra Select From ... order by on the count() result, which then creates the error Unknown column 'part_no' in 'order clause'. I assume the SingleStore driver should function the same way the mysql driver does in this example?

@rzv-me
Copy link
Contributor

rzv-me commented Dec 11, 2023

I created a PR with a fix for this issue. The problem is that when having an aggregate query with unions or having the query can just be returned, no need to wrap it and add orders, limit or offset

AdalbertMemSQL added a commit that referenced this issue Dec 12, 2023
Fix issue #71 with combining union(), orderBy() and paginate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants