Skip to content

Commit

Permalink
[Fix](PreparedStatement) fix incorrect query result with prepared sta…
Browse files Browse the repository at this point in the history
…tement (apache#46994)

This pull request includes several changes to improve the handling of
placeholders and prepared statements, as well as some minor code
adjustments and additions to test cases.

* Added an `equals` method to the `Placeholder` class to allow
comparison of `Placeholder` objects.Otherwise `between ? and ?` which is
between predicate, will be rewriten to `= ?` which is equal predicate

* Added multiple test cases to validate the functionality of prepared
statements with various data types and structures, including arrays,
maps, and structs.

* Incremented `_field_pos` in `open_dynamic_mode` method of
`MysqlRowBuffer` class to ensure correct position tracking.
  • Loading branch information
eldenmoon committed Jan 15, 2025
1 parent aad4ad9 commit a626762
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 6 deletions.
1 change: 1 addition & 0 deletions be/src/util/mysql_row_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ void MysqlRowBuffer<is_binary_format>::open_dynamic_mode() {
// write length when dynamic mode close
_len_pos = (_pos - _buf);
_pos = _pos + 8;
_field_pos++;
}
_dynamic_mode++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.doris.nereids.trees.expressions;

import org.apache.doris.analysis.PlaceHolderExpr;
import org.apache.doris.catalog.MysqlColType;
import org.apache.doris.nereids.exceptions.UnboundException;
import org.apache.doris.nereids.trees.expressions.shape.LeafExpression;
Expand Down Expand Up @@ -91,4 +92,13 @@ public boolean isUnsigned() {
public MysqlColType getMysqlColType() {
return mysqlColType.get();
}

@Override
public boolean equals(Object o) {
if (!(o instanceof PlaceHolderExpr)) {
return false;
}
Placeholder other = (Placeholder) o;
return placeholderId == other.placeholderId;
}
}
12 changes: 6 additions & 6 deletions regression-test/data/point_query_p0/test_point_query.out
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !point_select --
1231 119291.110000000 ddd laooq \N 2020-01-01T12:36:38 \N 1022-01-01 \N 1.111 \N [119181.111100000, 819019.119100000, null]
1231 119291.110000000 ddd laooq \N 2020-01-01T12:36:38 \N 1022-01-01 \N 1.111 [119181.111100000, 819019.119100000, null] \N

-- !point_select --
1231 119291.110000000 ddd laooq \N 2020-01-01T12:36:38 \N 1022-01-01 \N 1.111 \N [119181.111100000, 819019.119100000, null]
1231 119291.110000000 ddd laooq \N 2020-01-01T12:36:38 \N 1022-01-01 \N 1.111 [119181.111100000, 819019.119100000, null] \N

-- !point_select --
1237 120939.111300000 a ddd laooq 2030-01-02 2020-01-01T12:36:38 22.822 7022-01-01 false 90696620686827832.374 [1.100000000, 2.200000000, 3.300000000, 4.400000000, 5.500000000] []
Expand All @@ -15,7 +15,7 @@
251 120939.111300000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa laooq 2030-01-02 2020-01-01T12:36:38 251.0 7022-01-01 true 90696620686827832.374 [11111.000000000] []

-- !point_select --
252 120939.111300000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa laooq 2030-01-02 2020-01-01T12:36:38 252.0 7022-01-01 false 90696620686827832.374 \N [0.000000000]
252 120939.111300000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa laooq 2030-01-02 2020-01-01T12:36:38 252.0 7022-01-01 false 90696620686827832.374 [0.000000000] \N

-- !point_select --
298 120939.111300000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa laooq 2030-01-02 2020-01-01T12:36:38 298.0 7022-01-01 true 90696620686827832.374 [] []
Expand Down Expand Up @@ -75,10 +75,10 @@
0 1 2 3

-- !point_select --
1231 119291.110000000 ddd laooq \N 2020-01-01T12:36:38 \N 1022-01-01 \N 1.111 \N [119181.111100000, 819019.119100000, null]
1231 119291.110000000 ddd laooq \N 2020-01-01T12:36:38 \N 1022-01-01 \N 1.111 [119181.111100000, 819019.119100000, null] \N

-- !point_select --
1231 119291.110000000 ddd laooq \N 2020-01-01T12:36:38 \N 1022-01-01 \N 1.111 \N [119181.111100000, 819019.119100000, null]
1231 119291.110000000 ddd laooq \N 2020-01-01T12:36:38 \N 1022-01-01 \N 1.111 [119181.111100000, 819019.119100000, null] \N

-- !point_select --
1237 120939.111300000 a ddd laooq 2030-01-02 2020-01-01T12:36:38 22.822 7022-01-01 false 90696620686827832.374 [1.100000000, 2.200000000, 3.300000000, 4.400000000, 5.500000000] []
Expand All @@ -90,7 +90,7 @@
251 120939.111300000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa laooq 2030-01-02 2020-01-01T12:36:38 251.0 7022-01-01 true 90696620686827832.374 [11111.000000000] []

-- !point_select --
252 120939.111300000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa laooq 2030-01-02 2020-01-01T12:36:38 252.0 7022-01-01 false 90696620686827832.374 \N [0.000000000]
252 120939.111300000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa laooq 2030-01-02 2020-01-01T12:36:38 252.0 7022-01-01 false 90696620686827832.374 [0.000000000] \N

-- !point_select --
298 120939.111300000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa laooq 2030-01-02 2020-01-01T12:36:38 298.0 7022-01-01 true 90696620686827832.374 [] []
Expand Down
30 changes: 30 additions & 0 deletions regression-test/data/prepared_stmt_p0/prepared_stmt.out
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,33 @@ a
1236 100320.111390000 laa ddd Will we ignore LIMIT ?,? 2021-01-01 2020-01-01T12:36:38 2.7692 6022-01-01 [null]
1237 120939.111300000 a ddd Will we ignore LIMIT ?,? 2021-01-01 2020-01-01T12:36:38 22.822 7022-01-01 ["2025-01-01 11:30:38"]

-- !select17 --
3

-- !select18 --
1 [1, 2, 3] \N ["1"] \N \N [1.111]

-- !select18_1 --
1 [1, 2, 3] \N

-- !select18_2 --
1 [1, null, 3] \N [null] \N \N [null, null]

-- !select19 --
1 {"a":1} \N

-- !select20 --
1 {"col1":"a", "col2":1, "col3":"doris", "col4":"aaaaa", "col5":1.32} \N

-- !select21 --
1 [[1, 2], [3, 4]] \N

-- !select22 --
1 {"a":{"b":2}} \N

-- !select23 --
1 {"col1":"name", "col2":"doris", "col3":"values", "col4":[1, 2, 3]} \N

-- !select24 --
1 \N [{"id":"1", "name":"doris"}, {"id":"2", "name":"apache"}, null] \N

54 changes: 54 additions & 0 deletions regression-test/suites/prepared_stmt_p0/prepared_stmt.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ suite("test_prepared_stmt", "nonConcurrent") {
stmt_read2.close()

sql "DROP TABLE IF EXISTS mytable1"
sql "DROP TABLE IF EXISTS mytable2"
sql """
CREATE TABLE mytable1
(
Expand Down Expand Up @@ -255,5 +256,58 @@ suite("test_prepared_stmt", "nonConcurrent") {
sql """set enable_server_side_prepared_statement = false"""
def stmt_insert = prepareStatement "insert into mytable1 values(?, ?, ?, ?)"
assertEquals(stmt_insert.class, com.mysql.cj.jdbc.ClientPreparedStatement);

// test prepared with between, test placeholder equal
sql """insert into mytable2 values(3,1,'user1',10);"""
stmt_read = prepareStatement "SELECT COUNT() from mytable2 WHERE siteid between ? and ?"
assertEquals(com.mysql.cj.jdbc.ServerPreparedStatement, stmt_read.class)
stmt_read.setInt(1, 0)
stmt_read.setInt(2, 3)
qe_select17 stmt_read


// test array1
stmt_read = prepareStatement """SELECT 1, [1, 2, 3], null, ["1"], null, null, [1.111]"""
assertEquals(com.mysql.cj.jdbc.ServerPreparedStatement, stmt_read.class)
qe_select18 stmt_read

// test array2
stmt_read = prepareStatement "SELECT 1, [1, 2, 3], null"
assertEquals(com.mysql.cj.jdbc.ServerPreparedStatement, stmt_read.class)
qe_select18_1 stmt_read

// test array3
stmt_read = prepareStatement """SELECT 1, [1, null, 3], null, [null], null, null, [null, null]"""
assertEquals(com.mysql.cj.jdbc.ServerPreparedStatement, stmt_read.class)
qe_select18_2 stmt_read

// test map
stmt_read = prepareStatement """SELECT 1, {"a" : 1}, null"""
assertEquals(com.mysql.cj.jdbc.ServerPreparedStatement, stmt_read.class)
qe_select19 stmt_read

// test struct
stmt_read = prepareStatement """SELECT 1, struct('a', 1, 'doris', 'aaaaa', 1.32), null"""
assertEquals(com.mysql.cj.jdbc.ServerPreparedStatement, stmt_read.class)
qe_select20 stmt_read

// test nested array
stmt_read = prepareStatement("""SELECT 1, [[1, 2], [3, 4]], null""")
assertEquals(com.mysql.cj.jdbc.ServerPreparedStatement, stmt_read.class)
qe_select21 stmt_read

// test nested map
stmt_read = prepareStatement("""SELECT 1, {"a": {"b": 2}}, null""")
assertEquals(com.mysql.cj.jdbc.ServerPreparedStatement, stmt_read.class)
qe_select22 stmt_read

// test struct with array
stmt_read = prepareStatement("""SELECT 1, struct('name', 'doris', 'values', [1, 2, 3]), null""")
assertEquals(com.mysql.cj.jdbc.ServerPreparedStatement, stmt_read.class)
qe_select23 stmt_read

stmt_read = prepareStatement("""SELECT 1, null, [{'id': 1, 'name' : 'doris'}, {'id': 2, 'name': 'apache'}, null], null""")
assertEquals(com.mysql.cj.jdbc.ServerPreparedStatement, stmt_read.class)
qe_select24 stmt_read
}
}

0 comments on commit a626762

Please sign in to comment.