| 1 |
# |
| 2 |
# Tests for the Performance Schema native functions ps_thread_id() |
| 3 |
# and ps_current_thread_id() |
| 4 |
|
| 5 |
# 'connection ID' is the MySQL server processlist ID |
| 6 |
# 'thread ID' is the Performance Schema thread ID assigned to the connection ID |
| 7 |
|
| 8 |
|
| 9 |
USE test; |
| 10 |
# Get the connection ID of this connection |
| 11 |
SELECT connection_id() INTO @cid; |
| 12 |
|
| 13 |
# Get the thread ID for this connection |
| 14 |
SELECT thread_id FROM performance_schema.threads |
| 15 |
WHERE PROCESSLIST_ID = @cid INTO @tid; |
| 16 |
|
| 17 |
# Open another connection, get the connection ID |
| 18 |
|
| 19 |
|
| 20 |
# Get the thread ID for the remote thread |
| 21 |
SELECT thread_id FROM performance_schema.threads |
| 22 |
WHERE PROCESSLIST_ID = @cid_1 INTO @tid_1; |
| 23 |
|
| 24 |
# Verify that the remote thread IDs are different |
| 25 |
SELECT @cid <> @cid_1 AS "Expect 1"; |
| 26 |
Expect 1 |
| 27 |
1 |
| 28 |
SELECT @tid <> @tid_1 AS "Expect 1"; |
| 29 |
Expect 1 |
| 30 |
1 |
| 31 |
|
| 32 |
# Returns the current thread ID |
| 33 |
SELECT ps_current_thread_id() - @tid AS "Expect 0"; |
| 34 |
Expect 0 |
| 35 |
0 |
| 36 |
|
| 37 |
# Bad parameter count |
| 38 |
SELECT ps_current_thread_id(1); |
| 39 |
ERROR 42000: Incorrect parameter count in the call to native function 'ps_current_thread_id' |
| 40 |
|
| 41 |
# A NULL connection ID returns NULL |
| 42 |
SELECT ps_thread_id(NULL) AS "Expect NULL"; |
| 43 |
Expect NULL |
| 44 |
NULL |
| 45 |
|
| 46 |
# The current connection ID returns the current thread ID |
| 47 |
SELECT ps_thread_id(@cid) - @tid AS "Expect 0"; |
| 48 |
Expect 0 |
| 49 |
0 |
| 50 |
|
| 51 |
# Another connection ID returns the corresponding thread ID |
| 52 |
SELECT ps_thread_id(@cid_1) - @tid_1 AS "Expect 0"; |
| 53 |
Expect 0 |
| 54 |
0 |
| 55 |
|
| 56 |
# Hybrid parameter |
| 57 |
SELECT ps_thread_id(@cid + 0) - @tid AS "Expect 0"; |
| 58 |
Expect 0 |
| 59 |
0 |
| 60 |
|
| 61 |
# Return NULL if connection ID not found |
| 62 |
SELECT ps_thread_id(9999999) AS "Expect NULL"; |
| 63 |
Expect NULL |
| 64 |
NULL |
| 65 |
|
| 66 |
# Return NULL if bad parameter |
| 67 |
SELECT ps_thread_id("foo") AS "Expect NULL"; |
| 68 |
Expect NULL |
| 69 |
NULL |
| 70 |
Warnings: |
| 71 |
Warning 1292 Truncated incorrect INTEGER value: 'foo' |
| 72 |
|
| 73 |
# Return NULL if negative number |
| 74 |
SELECT ps_thread_id(-1) AS "Expect NULL"; |
| 75 |
Expect NULL |
| 76 |
NULL |
| 77 |
|
| 78 |
# Error if bad parameter count |
| 79 |
SELECT ps_thread_id(1, 2); |
| 80 |
ERROR 42000: Incorrect parameter count in the call to native function 'ps_thread_id' |
| 81 |
|
| 82 |
# Error if bad parameter count |
| 83 |
SELECT ps_thread_id(); |
| 84 |
ERROR 42000: Incorrect parameter count in the call to native function 'ps_thread_id' |
| 85 |
|
| 86 |
# Name collision warning |
| 87 |
USE test; |
| 88 |
CREATE FUNCTION ps_current_thread_id() RETURNS varchar(100) return "This is function ps_current_thread_id()"; |
| 89 |
Warnings: |
| 90 |
Note 1585 This function 'ps_current_thread_id' has the same name as a native function |
| 91 |
|
| 92 |
SHOW WARNINGS; |
| 93 |
Level Code Message |
| 94 |
Note 1585 This function 'ps_current_thread_id' has the same name as a native function |
| 95 |
|
| 96 |
CREATE FUNCTION ps_thread_id() RETURNS varchar(100) return "This is a function ps_thread_id()"; |
| 97 |
Warnings: |
| 98 |
Note 1585 This function 'ps_thread_id' has the same name as a native function |
| 99 |
|
| 100 |
SHOW WARNINGS; |
| 101 |
Level Code Message |
| 102 |
Note 1585 This function 'ps_thread_id' has the same name as a native function |
| 103 |
|
| 104 |
# Local function should work ok |
| 105 |
SELECT test.ps_current_thread_id(); |
| 106 |
test.ps_current_thread_id() |
| 107 |
This is function ps_current_thread_id() |
| 108 |
Warnings: |
| 109 |
Note 1585 This function 'ps_current_thread_id' has the same name as a native function |
| 110 |
SELECT test.ps_thread_id(); |
| 111 |
test.ps_thread_id() |
| 112 |
This is a function ps_thread_id() |
| 113 |
Warnings: |
| 114 |
Note 1585 This function 'ps_thread_id' has the same name as a native function |
| 115 |
|
| 116 |
# Global function should still work |
| 117 |
SELECT ps_current_thread_id() - @tid AS "Expect 0"; |
| 118 |
Expect 0 |
| 119 |
0 |
| 120 |
SELECT ps_current_thread_id() - @tid AS "Expect 0"; |
| 121 |
Expect 0 |
| 122 |
0 |
| 123 |
|
| 124 |
# Cleanup |
| 125 |
DROP FUNCTION test.ps_current_thread_id; |
| 126 |
DROP FUNCTION test.ps_thread_id; |
| 127 |
|
| 128 |
## Handling of NULL (bug#30525561) |
| 129 |
SELECT PS_CURRENT_THREAD_ID() into @this_tid; |
| 130 |
SELECT CONNECTION_ID() into @this_cid; |
| 131 |
SELECT PS_THREAD_ID(my_cid) = @this_tid AS PS_TID_CHECK, |
| 132 |
sys.ps_thread_id(my_cid) = @this_tid AS SYS_TID_CHECK |
| 133 |
FROM (SELECT @this_cid AS my_cid |
| 134 |
UNION ALL |
| 135 |
SELECT CAST(1234567890 AS UNSIGNED) |
| 136 |
UNION ALL |
| 137 |
SELECT NULL |
| 138 |
UNION ALL |
| 139 |
SELECT @this_cid |
| 140 |
UNION ALL |
| 141 |
SELECT CAST(1234567890 AS UNSIGNED) |
| 142 |
) t; |
| 143 |
PS_TID_CHECK SYS_TID_CHECK |
| 144 |
1 1 |
| 145 |
1 1 |
| 146 |
NULL 1 |
| 147 |
1 1 |
| 148 |
1 1 |
| 149 |
Warnings: |
| 150 |
Note 1585 This function 'ps_thread_id' has the same name as a native function |