Skip to content

Array slice expression upper bound misinterpreted as parameter #946

Closed

Description

@shanlun

Since version 3 the following query has started failing for us:

context.Connection.Execute(@"
    drop table if exists stuff;
    create table stuff (a int primary key, b int[])");

context.Connection.Execute(@"insert into stuff values (1, array[1,2,3,4])");

var asdf = context.Connection
    .Query(@"select b[2:3] as foo from stuff")
    .Single();

Log.Debug("asdf = " + asdf.foo);

(using dapper)

...with the exception:

Exception: System.Exception: Parameter ':3' referenced in SQL but not found in parameter list
   at Npgsql.SqlQueryParser.ParseRawQuery(String sql, Boolean standardConformantStrings, NpgsqlParameterCollection parameters, List`1 queries)
   at Npgsql.NpgsqlCommand.ProcessRawQuery()
   at Npgsql.NpgsqlCommand.CreateMessagesNonPrepared(CommandBehavior behavior)
   at Npgsql.NpgsqlCommand.ExecuteDbDataReaderInternal(CommandBehavior behavior)
   at Dapper.SqlMapper.<QueryImpl>d__11`1.MoveNext() in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1562
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1452
   at Dapper.SqlMapper.Query(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1391

It looks like npgsql is parsing the query and misinterpreting the :3 part of the array slice expression as a parameter name.

Workaround:

Pass the upper bound in as a parameter using @, e.g.

var asdf = context.Connection
    .Query(@"select b[2:@Three] as foo from stuff", new { Three = 3 })
    .Single();

This seems to avoid the issue.

Activity

  1. Emill commented on Jan 14, 2016

    @Emill
    Contributor

    Previously parameter placeholders not found in the parameter collection were left as-is in the query, and therefore this issue did not happen. Now an exception is thrown instead. Let's make it illegal for a parameter name to start with a digit. That should probably be good for most people.

  2. roji commented on Jan 14, 2016

    @roji
    Member

    @Emill sounds like a good workaround for this particular case, but in general I really hate the fact that Npgsql parses the command text. I would be very happy if we could have a connection string parameter that turns off the parsing altogether. This would mean users have to use PostgreSQL placeholders ($1, $2), and they need some sort of alternative in order to do command batching (see https://github.com/dotnet/corefx/issues/3688 for a long-term view). This is the only good way forward IMHO, everything else is going to be a hack.

    But in the short term yes, making it illegal to start parameter names with digits should be OK.

  3. shanlun commented on Feb 2, 2016

    @shanlun
    Author

    BTW I think I've just found another similar issue:

    ALTER TABLE foo ADD COLUMN created_on timestamp(6) with time zone NOT NULL DEFAULT @Ts;
    

    (assuming have passed it a valid Ts param)

    Produces:

    Unhandled Exception: Npgsql.NpgsqlException: 42P02: there is no parameter $1
       at Npgsql.NpgsqlConnector.DoReadSingleMessage(DataRowLoadingMode dataRowLoadingMode, Boolean returnNullForAsyncMessage, Boolean isPrependedMessage)
       at Npgsql.NpgsqlConnector.ReadSingleMessage(DataRowLoadingMode dataRowLoadingMode, Boolean returnNullForAsyncMessage)
    
       at Npgsql.NpgsqlConnector.SkipUntil(BackendMessageCode stopAt1, BackendMessageCode stopAt2)
       at Npgsql.NpgsqlDataReader.SkipUntil(BackendMessageCode stopAt1, BackendMessageCode stopAt2)
       at Npgsql.NpgsqlDataReader.NextResultInternal()
       at Npgsql.NpgsqlDataReader.NextResult()
       at Npgsql.NpgsqlDataReader.Init()
       at Npgsql.NpgsqlCommand.Execute(CommandBehavior behavior)
       at Npgsql.NpgsqlCommand.ExecuteNonQueryInternal()
       at Npgsql.NpgsqlCommand.ExecuteNonQuery()
       at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action`2 paramReader) in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 3321
       at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1319
       at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable`1 commandTimeout, Nullable`1 commandType) in d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1194
    

    I've had to resort to string replacement, which works, but is not ideal. This is a slightly less obscure scenario than the original, and may pose more of a problem.

  4. added this to the 3.0.6 milestone on Apr 2, 2016
  5. added a commit that references this issue on Apr 10, 2016
    e0925cd
  6. self-assigned this
    on Apr 10, 2016
  7. roji commented on Apr 10, 2016

    @roji
    Member
  8. shanlun commented on Apr 10, 2016

    @shanlun
    Author

    Makes sense, thanks for the info.

  9. added a commit that references this issue on Apr 14, 2016
    044da7c
  10. dstarosta commented on May 2, 2016

    @dstarosta

    This change is going to break some ORM frameworks that automatically generate numbered parameters. Peta POCO and NPOCO are some of them.

  11. roji commented on May 2, 2016

    @roji
    Member

    @dstarosta, you know for a fact that these two ORMs generate parameter names that start with digits?

  12. lerrand commented on May 2, 2016

    @lerrand

    @roji NPoco does currently.

  13. dstarosta commented on May 3, 2016

    @dstarosta

    @roji Yes, I verified that in their source code.

  14. udoliess commented on May 3, 2016

    @udoliess
    Contributor

    This change breaks nearly all of my code as I'm used to generate numbered parameter names. Isn't a starting "@" or ":" in SQL command enough to detect a parameter?

  15. roji commented on May 3, 2016

    @roji
    Member

    @udoliess don't worry, later today I'm publishing 3.0.7 which undoes this change, hang on.

  16. roji commented on May 3, 2016

    @roji
    Member

    FYI, 3.0.7 has been released and undoes this fix - parameter names can start with digits. #1056 has been applied instead.

  17. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions