datastax / simulacron

Simulacron - An Apache Cassandra® Native Protocol Server Simulator
Apache License 2.0
32 stars 13 forks source link

Prepared statement match has bug matching the parameters. #98

Closed cthumuluru closed 4 years ago

cthumuluru commented 4 years ago

With Simulacron, while mocking prepared statements with more than one parameters of different types, our tests are failing. I think it's a bug in the way the parameter metadata is stored and checked for a match.

Parameter types and values are two different maps (API doesn't enforce ordered maps) and the match fails. The bug is in the following code (com.datastax.oss.simulacron.common.request.Query) where the iterators won't yield elements in the matching order: `

    Iterator<Object> primedPositionValues = params.values().iterator();
    Iterator<String> primedPositionTypes = paramTypes.values().iterator();
    // iterate over the parameters and make sure they all match
    for (ByteBuffer buffer : options.positionalValues) {
      if (!checkParamsEqual(
          buffer, primedPositionValues.next(), primedPositionTypes.next(), mapper)) {
        return false;
      }
    }

` Replacing Map with LinkedHashMap fixes the issue.

cthumuluru commented 4 years ago

I can work on a fix if someone assert my findings.

jorgebay commented 4 years ago

Nice findings!

Feel free to submit a patch, I can review it :)

cthumuluru commented 4 years ago

@jorgebay — Can you please review this PR and provide your feedback?

In Query.java I think it would be nice to compare the column positions and throw RuntimeException if the param order won't match. I didn't add it in this PR since the existing code was not doing it either. :)

Also, ResultSet suffers from similar ordering issues. I'll send those changes in a different PR.

cthumuluru commented 4 years ago

Added changes to retain the order of column values in a Row.