Skip to content

Commit

Permalink
fix: prepare param mismatch (#5025)
Browse files Browse the repository at this point in the history
* fix: prepare param mismatch

* test: clear state

* fix: minus 1
  • Loading branch information
CookiePieWw authored Nov 22, 2024
1 parent 5f8d849 commit 1578c00
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/servers/src/mysql/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,6 @@ impl MysqlInstanceShim {
dummy_params(param_num)?
};

debug_assert_eq!(params.len(), param_num - 1);

let columns = schema
.as_ref()
.map(|schema| {
Expand All @@ -205,14 +203,26 @@ impl MysqlInstanceShim {
.transpose()?
.unwrap_or_default();

self.save_plan(
SqlPlan {
query: query.to_string(),
plan,
schema,
},
stmt_key,
);
// DataFusion may optimize the plan so that some parameters are not used.
if params.len() != param_num - 1 {
self.save_plan(
SqlPlan {
query: query.to_string(),
plan: None,
schema: None,
},
stmt_key,
);
} else {
self.save_plan(
SqlPlan {
query: query.to_string(),
plan,
schema,
},
stmt_key,
);
}

Ok((params, columns))
}
Expand Down
72 changes: 72 additions & 0 deletions tests/cases/standalone/common/prepare/mysql_prepare.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
-- invalid prepare, from
-- https://github.com/duckdb/duckdb/blob/00a605270719941ca0412ad5d0a14b1bdfbf9eb5/test/sql/prepared/invalid_prepare.test
-- SQLNESS PROTOCOL MYSQL
SELECT ?;

Failed to execute query, err: MySqlError { ERROR 1815 (HY000): (PlanQuery): Failed to plan SQL: Error during planning: Placeholder type could not be resolved. Make sure that the placeholder is bound to a concrete type, e.g. by providing parameter values. }

-- SQLNESS PROTOCOL MYSQL
PREPARE stmt FROM 'SELECT ?::int;';

affected_rows: 0

-- SQLNESS PROTOCOL MYSQL
EXECUTE stmt USING 1;

+----------+
| Int64(1) |
+----------+
| 1 |
+----------+

-- SQLNESS PROTOCOL MYSQL
EXECUTE stmt USING 'a';

Failed to parse query result, err: MySqlError { ERROR 1815 (HY000): (EngineExecuteQuery): Cast error: Cannot cast string 'a' to value of Int32 type }

-- SQLNESS PROTOCOL MYSQL
DEALLOCATE stmt;

affected_rows: 0

-- SQLNESS PROTOCOL MYSQL
PREPARE stmt FROM 'SELECT ?::int WHERE 1=0;';

affected_rows: 0

-- SQLNESS PROTOCOL MYSQL
EXECUTE stmt USING 1;

affected_rows: 0

-- SQLNESS PROTOCOL MYSQL
EXECUTE stmt USING 'a';

affected_rows: 0

-- SQLNESS PROTOCOL MYSQL
DEALLOCATE stmt;

affected_rows: 0

-- parameter variants, from:
-- https://github.com/duckdb/duckdb/blob/2360dd00f193b5d0850f9379d0c3794eb2084f36/test/sql/prepared/parameter_variants.test
-- SQLNESS PROTOCOL MYSQL
PREPARE stmt FROM 'SELECT CAST(? AS INTEGER), CAST(? AS STRING);';

affected_rows: 0

-- SQLNESS PROTOCOL MYSQL
EXECUTE stmt USING 1, 'hello';

+----------+---------------+
| Int64(1) | Utf8("hello") |
+----------+---------------+
| 1 | hello |
+----------+---------------+

-- SQLNESS PROTOCOL MYSQL
DEALLOCATE stmt;

affected_rows: 0

39 changes: 39 additions & 0 deletions tests/cases/standalone/common/prepare/mysql_prepare.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
-- invalid prepare, from
-- https://github.com/duckdb/duckdb/blob/00a605270719941ca0412ad5d0a14b1bdfbf9eb5/test/sql/prepared/invalid_prepare.test
-- SQLNESS PROTOCOL MYSQL
SELECT ?;

-- SQLNESS PROTOCOL MYSQL
PREPARE stmt FROM 'SELECT ?::int;';

-- SQLNESS PROTOCOL MYSQL
EXECUTE stmt USING 1;

-- SQLNESS PROTOCOL MYSQL
EXECUTE stmt USING 'a';

-- SQLNESS PROTOCOL MYSQL
DEALLOCATE stmt;

-- SQLNESS PROTOCOL MYSQL
PREPARE stmt FROM 'SELECT ?::int WHERE 1=0;';

-- SQLNESS PROTOCOL MYSQL
EXECUTE stmt USING 1;

-- SQLNESS PROTOCOL MYSQL
EXECUTE stmt USING 'a';

-- SQLNESS PROTOCOL MYSQL
DEALLOCATE stmt;

-- parameter variants, from:
-- https://github.com/duckdb/duckdb/blob/2360dd00f193b5d0850f9379d0c3794eb2084f36/test/sql/prepared/parameter_variants.test
-- SQLNESS PROTOCOL MYSQL
PREPARE stmt FROM 'SELECT CAST(? AS INTEGER), CAST(? AS STRING);';

-- SQLNESS PROTOCOL MYSQL
EXECUTE stmt USING 1, 'hello';

-- SQLNESS PROTOCOL MYSQL
DEALLOCATE stmt;

0 comments on commit 1578c00

Please sign in to comment.