Skip to content

Bug: [QueryBuilder] if set limit to 0, it will return all data. Perhaps it should return 0 data. #8278

Closed

Description

@RikhzanAmir

PHP Version

8.2

CodeIgniter4 Version

4

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

fpm-fcgi

Database

MariaDB 10.2.44

What happened?

$builder->limit(0, 0) , set the limit => 0 and the query will shows all the table rows. It should return no rows because limit is 0.

Steps to Reproduce

$builder->limit(0,0)

Expected Output

it should return no rows if it set limit to 0.

Anything else?

No response

Activity

  1. added
    bugVerified issues on the current code behavior or pull requests that will fix them
    on Nov 30, 2023
  2. kenjis commented on Nov 30, 2023

    @kenjis
    Member

    Thank you for reporting. This is very interesting.

    $builder->limit(10, 20);
    // Produces: LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)
    https://codeigniter4.github.io/CodeIgniter4/database/query_builder.html#limit

    As I read it, I expect limit(0, 0) produces LIMIT 0, 0.
    In reality, however, no LIMIT clause is generated.

  3. lonnieezell commented on Nov 30, 2023

    @lonnieezell
    Member

    What is the practical purpose of doing a query where you don't want any results? I'm not sure that I can think of a reason to do that.

    Think of it as "there's 0 limit on the number of results I'd like returned."

  4. kenjis commented on Nov 30, 2023

    @kenjis
    Member

    This behavior has not changed since the beginning of 4.0.

    // LIMIT
    if ($this->QBLimit)
    {
    return $this->_limit($sql."\n");
    }

  5. kenjis commented on Nov 30, 2023

    @kenjis
    Member

    In CI3, the following code generates SELECT * FROM "job" LIMIT 0 (sqlite3):

    		$jobs = $this->db->limit(0, 0)
    		                      ->get('job')
    		                      ->result_array();
    		// LIMIT
    		if ($this->qb_limit !== FALSE OR $this->qb_offset)
    		{
    			return $this->_limit($sql."\n");
    		}

    https://github.com/bcit-ci/CodeIgniter/blob/63d037565dd782795021dcbd3b1ca6f8c8a509e4/system/database/DB_query_builder.php#L2440-L2444

  6. changed the title [-]Bug: Query limit, if set limit => 0 , it will return all data. Perhaps it should return 0 data.[/-] [+]Bug: [QueryBuilder] if set limit to 0, it will return all data. Perhaps it should return 0 data.[/+] on Nov 30, 2023
  7. kenjis commented on Nov 30, 2023

    @kenjis
    Member

    I don't know if this is a bug or not.

    Since no one has reported it before, there are probably few use cases where 0 is specified.

  8. RikhzanAmir commented on Nov 30, 2023

    @RikhzanAmir
    Author

    I am writing this query in MariaDB: "SELECT * FROM payment limit 0 offset 0" , and it shows no result.

    So on Ci3, and Ci4 I am expecting $builder->limit(0,0) or $builder->limit($set, $offset) do the same.

    Maybe, it is not a bug, some query do different depending on database we use.

    https://dev.mysql.com/doc/refman/8.0/en/limit-optimization.html

    "LIMIT 0 quickly returns an empty set. This can be useful for checking the validity of a query. It can also be employed to obtain the types of the result columns within applications that use a MySQL API that makes result set metadata available."

  9. kenjis commented on Nov 30, 2023

    @kenjis
    Member

    Do you have a use case for limit 0 with Query Builder?

    LIMIT is not in SQL standards. So we don't need to implement like MySQL's.
    But since "limit 1" returns 1 row, it is logical that "limit 0" would return 0 rows.
    However, the implementation will be more complicated than it is now.

  10. RikhzanAmir commented on Nov 30, 2023

    @RikhzanAmir
    Author

    If that so, maybe do not change and stick to it. Because on my side it can be fix by adding 1 line of code.

    $query = $this->db->table('order')->limit($quantity)->offset($offset)->get()->getResultArray();
    return empty($quantity) ? [] : $query;
    
  11. kenjis commented on Nov 30, 2023

    @kenjis
    Member
    if ($quantity === 0) {
        return [];
    }
    $query = $this->db->table('order')->limit($quantity)->offset($offset)->get()->getResultArray();
  12. kenjis commented on Dec 1, 2023

    @kenjis
    Member

    I sent PR #8280 to fix this.

  13. reopened this on Dec 1, 2023
  14. kenjis commented on Dec 20, 2023

    @kenjis
    Member

    You can fix this behavior in v4.5.0. See #8280

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions