DapperLib / DapperLib/Dapper

Querying for empty list fails with MySQL Server 5.6

Open
#565 18 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

db:mysql
Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

MySQL Server 5.6.29
Dapper 1.50.1
MySql.Data 6.9.9

Execute the following code (query adapted from https://github.com/StackExchange/dapper-dot-net#list-support):

var csb = new MySqlConnectionStringBuilder { Server = "localhost", UserID = "root", Password = "..." };
using (var connection = new MySqlConnection(csb.ConnectionString))
{
    connection.Open();
    connection.Query<int>("select * from (select 1 as Id) as X where Id in @Ids", new { Ids = new int[0] }).ToList();
}

It throws a MySqlException: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE 1 = 0)' at line 1".

For this statement, Dapper sets the CommandText to select * from (select 1 as Id) as X where Id in (SELECT @Ids WHERE 1 = 0)

The MySql.Data connector interpolates the command parameters and rewrites this as select * from (select 1 as Id) as X where Id in (SELECT NULL WHERE 1 = 0) before sending it to MySQL Server.

MySQL Server 5.6 rejects this as invalid SQL syntax.

Upgrading to MySQL Server 5.7.11 fixes the exception, but may not always be practical. For MySQL Server 5.6, the following query would be valid (and selects 0 rows): select * from (select 1 as Id) as X where Id in (SELECT NULL). EDIT: deleted this suggested workaround because—although it selects zero rows—the inverse query WHERE Id NOT IN (SELECT NULL) also selects zero rows when the intent is to select all rows.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided C# reproduction against MySQL Server 5.6 and inspect Dapper's list-parameter expansion together with MySql.Data's parameter interpolation. No file or test is named in the issue, so locating the relevant implementation and regression-test area will require repository research. Done means empty-list queries no longer produce invalid MySQL 5.6 SQL while preserving correct NOT IN behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, mysql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.