Skip to content

chunkById misbuilds query between where/orWhere  #29655

Closed
@iBotPeaches

Description

@iBotPeaches
  • Laravel Version: 5.8.32
  • PHP Version: 7.3.8
  • Database Driver & Version: MariaDB 10.2.23

Description / Repro Steps:

  1. Build a query that leverages where/orWhere
        User::query()
            ->where(function (Builder $query) {
                // basic where
            })
            ->orWhere(function (Builder $query) {
                // basic where
            })
            ->chunkById(50, function (Collection $users) {
                // logic
            });

This generates

SELECT * FROM … WHERE a OR b AND id > X

Which when grouped becomes

SELECT * FROM … WHERE a OR (b AND id > X)

While I think it should be

SELECT * FROM … WHERE (a OR b) AND id > X

This led to a situation where our queue system ran out of bounds and just queued millions of millions of jobs until redis died out.

Workaround

        User::query()
            ->where(function (Builder $query) {
                $query->where(function (Builder $query) {
                    // basic where
                })
                ->orWhere(function (Builder $query) {
                    // basic where
                });
            })
            ->chunkById(50, function (Collection $users) {
                    // logic
            });

We basically just shove the logic into a where closure to force the grouping. I think this is a bug or at least unexpected behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions