You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CREATE TABLE `table1` (
`id` int unsigned NOT NULL,
`join_id` bigint unsigned NOT NULL,
`created_at` timestamp NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_join_id` (`join_id`)
) ENGINE InnoDB,
CHARSET utf8mb4,
COLLATE utf8mb4_0900_ai_ci;
CREATE TABLE `table2` (
`id` bigint unsigned NOT NULL,
`join_id` int unsigned NOT NULL,
`created_at` timestamp NOT NULL,
`updated_at` timestamp NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_join_id` (`join_id`)
) ENGINE InnoDB,
CHARSET utf8mb4,
COLLATE utf8mb4_0900_ai_ci;
Running a query like
SELECT
GREATEST(`table1`.`created_at`, `table2`.`updated_at`) AS `created_at`
FROM `table1`
STRAIGHT_JOIN `table2`
ON `table1`.`join_id` = `table2`.`join_id`
ORDER BY 1;
results in a different data-type returned by Vitess than in plain MySQL
for column created_at field types do not match
Not equal:
MySQL: TIMESTAMP
Vitess: VARCHAR
Query (SELECT
GREATEST(`table1`.`created_at`, `table2`.`updated_at`) AS `created_at`
FROM `table1`
STRAIGHT_JOIN `table2`
ON `table1`.`join_id` = `table2`.`join_id`
ORDER BY 1;) results mismatched.
Vitess Results:
[VARCHAR("2024-02-12 10:30:00")]
[VARCHAR("2024-02-12 10:31:00")]
[VARCHAR("2024-02-12 10:32:00")]
[VARCHAR("2024-02-12 10:33:00")]
[VARCHAR("2024-02-12 10:34:00")]
[VARCHAR("2024-02-12 10:35:00")]
[VARCHAR("2024-02-12 10:36:00")]
[VARCHAR("2024-02-12 10:37:00")]
[VARCHAR("2024-02-12 10:38:00")]
[VARCHAR("2024-02-12 10:39:00")]
[VARCHAR("2024-02-12 10:40:00")]
[VARCHAR("2024-02-12 10:41:00")]
[VARCHAR("2024-02-12 10:42:00")]
[VARCHAR("2024-02-12 10:43:00")]
[VARCHAR("2024-02-12 10:44:00")]
[VARCHAR("2024-02-12 10:45:00")]
[VARCHAR("2024-02-12 10:46:00")]
[VARCHAR("2024-02-12 10:47:00")]
[VARCHAR("2024-02-12 10:48:00")]
[VARCHAR("2024-02-12 10:49:00")]
Vitess RowsAffected: 0
MySQL Results:
[TIMESTAMP("2024-02-12 10:30:00")]
[TIMESTAMP("2024-02-12 10:31:00")]
[TIMESTAMP("2024-02-12 10:32:00")]
[TIMESTAMP("2024-02-12 10:33:00")]
[TIMESTAMP("2024-02-12 10:34:00")]
[TIMESTAMP("2024-02-12 10:35:00")]
[TIMESTAMP("2024-02-12 10:36:00")]
[TIMESTAMP("2024-02-12 10:37:00")]
[TIMESTAMP("2024-02-12 10:38:00")]
[TIMESTAMP("2024-02-12 10:39:00")]
[TIMESTAMP("2024-02-12 10:40:00")]
[TIMESTAMP("2024-02-12 10:41:00")]
[TIMESTAMP("2024-02-12 10:42:00")]
[TIMESTAMP("2024-02-12 10:43:00")]
[TIMESTAMP("2024-02-12 10:44:00")]
[TIMESTAMP("2024-02-12 10:45:00")]
[TIMESTAMP("2024-02-12 10:46:00")]
[TIMESTAMP("2024-02-12 10:47:00")]
[TIMESTAMP("2024-02-12 10:48:00")]
[TIMESTAMP("2024-02-12 10:49:00")]
MySQL RowsAffected: 0
As a work-around you could use a CAST() function to cast the result-type to DATETIME, for example:
SELECT
CAST(GREATEST(`table1`.`created_at`, `table2`.`updated_at`) as DATETIME) AS `created_at`
FROM `table1`
STRAIGHT_JOIN `table2`
ON `table1`.`join_id` = `table2`.`join_id`
ORDER BY 1;
Reproduction Steps
providing a vt test testcase
Testcase output:
➜ vitess-tester git:(main) ✗ ./vt test --sharded --number-of-shards 2 t/oss-bug-greatest-wrong-datatype.t.txt
starting sharded keyspace: 'mysqltest' with shards [-80 80-]
E0212 11:11:41.517472 15414 vtorc_process.go:105] configuration - {
"instance-poll-time": "10h"
}
t/oss-bug-greatest-wrong-datatype.t.txt: ok! Ran 6 queries, 5 successfully and 1 failures take time 2.208355125 s
Error: some tests failed 😭
see errors in errors
verified on v20
vttablet version Version: 20.0.6-SNAPSHOT (Git revision b71e9ac397f40807574534ce00ee50378c819d4d branch 'release-20.0') built on Wed Jan 22 15:41:39 CET 2025 by [email protected] using go1.22.11 darwin/arm64
Operating System and Environment details
MacOS Sequoia 15.3
Log Fragments
The text was updated successfully, but these errors were encountered:
So there are essentially two problems here in one. The first is that we don't correctly handle temporal types (datetime, timestamp, date & time) correctly when they are compared in GREATEST and LEAST. We either end up doing varchar comparisons or can even panic in cases.
Secondly, there's a separate problem here. For sharded environments, we end up sending down queries to the different shards and in cases like here we need to use a bind parameter to combine the values from different shards. This is visible in the query plan for this query:
select GREATEST(CAST(:table1_created_at AS DATETIME), table2.updated_at) as created_at from table2 where table2.join_id = CAST(:table1_join_id AS UNSIGNED)
What you can see here, is that we explicitly cast the bind variable as datetime and not as timestamp, which means the values here even after #17826 come out as datetime and not as timestamp. So why does it cast as datetime? First of all, we need to cast here since by default if we interpolate a bind variable, it would interpreted as varchar. That's the wrong type then.
But MySQL has no support for CAST(... AS TIMESTAMP). In fact, there is no possible way to create a timestamp literal or value in any way except for values directly loaded from a table. This means it's impossible here to return the timestamp type and returning a datetime is the best we can do here and the only option.
Overview of the Issue
Given the following table definitions
Running a query like
results in a different data-type returned by Vitess than in plain MySQL
As a work-around you could use a
CAST()
function to cast the result-type toDATETIME
, for example:Reproduction Steps
providing a
vt test
testcaseTestcase output:
Test case definition:
Binary Version
Operating System and Environment details
Log Fragments
The text was updated successfully, but these errors were encountered: