Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQL max() in nested subquery fails #30764

Closed
malachany opened this issue Apr 3, 2024 · 13 comments · Fixed by #30935
Closed

MySQL max() in nested subquery fails #30764

malachany opened this issue Apr 3, 2024 · 13 comments · Fixed by #30935

Comments

@malachany
Copy link

malachany commented Apr 3, 2024

Bug Report

For English only, other languages will not accept.

Before report a bug, make sure you have:

Please pay attention on issues you submitted, because we maybe need more details.
If no response anymore and we cannot reproduce it on current information, we will close it.

Please answer these questions before submitting your issue. Thanks!

Which version of ShardingSphere did you use?

Tested in v5.41 and v5.4.2-SNAPSHOT

Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?

ShardingSphere-Proxy

Expected behavior

Expected to return a list of recent chat_messages

Actual behavior

In 5.41: General error: 30000 Unknown exception: Cannot convert 2022-09-14T18:25:06 to long
In 5.4.2-SNAPSHOT: SQL federation doesn't support SQL

Reason analyze (If you can)

It looks like the MAX(created_at) isn't working correctly, and instead of showing the max DATETIME, it's trying to convert it to LONG.

One thing to note is that if I run the subquery by itself, it works fine.. it's only with it in the subquery that seems to be a problem:

SELECT
    ->     MAX(id) max_id,
    ->     chat_id,
    ->     MAX(created_at) created_at
    ->   FROM chats_messages
    ->   GROUP BY chat_id;
+--------+---------+---------------------+
| max_id | chat_id | created_at          |
+--------+---------+---------------------+
|      1 |       1 | 2022-09-14 18:25:06 |
|      2 |       2 | 2022-09-15 21:37:17 |
|     32 |       3 | 2022-10-26 09:06:24 |
|     34 |       4 | 2022-10-26 14:41:22 |
|      9 |       5 | 2022-09-16 19:18:45 |
|     11 |       6 | 2022-09-26 09:11:29 |
|     12 |       7 | 2022-09-30 19:05:20 |
|     13 |       8 | 2022-10-07 16:40:07 |

this works fine

Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.

This is the query:

SELECT
  members.chat_id,
  mess_max.created_at message_sent
FROM chats_members members 
JOIN (
  SELECT
    MAX(id) max_id,
    chat_id,
    MAX(created_at) created_at
  FROM chats_messages
  GROUP BY chat_id
) mess_max on mess_max.chat_id = members.chat_id
LEFT JOIN chats_messages_read_by_recipient is_read
  ON mess_max.max_id = is_read.chat_message_id AND is_read.created_by = 5094
WHERE (members.user_id = 5094 OR members.group_id IN('13'))
  AND is_read.id IS NULL;

Example codes for reproduce this issue (such as a github link).

@strongduanmu
Copy link
Member

Hi @malachany, thank you for your feedback. Are you using SQL Federation? Can you provide your YAML configuration and table initialization SQL? I want to try to reproduce this exception.

@malachany
Copy link
Author

@strongduanmu , thank you for the fast reply. I'll create an example project with the same settings to replicate it. Will get that to you later today. 🙏🏻

@malachany
Copy link
Author

@strongduanmu , I created a new repo with the example reproducible:

https://github.com/malachany/shardingsphere-convert-to-long-example

All in docker, so just run docker-compose up and then use the creds and SQL statement to reproduce.

Thank you for looking into this

@strongduanmu
Copy link
Member

@malachany Thank you for your help, I will try to reproduce and fix the problem.

@malachany
Copy link
Author

Sounds good, thank you

@malachany
Copy link
Author

Hey @strongduanmu. Checking in to see if there's any update in regards to reproducing the problem. Thanks

@malachany
Copy link
Author

Hey @strongduanmu , checking in on this. Hope you had a good weekend

@strongduanmu
Copy link
Member

@malachany Sorry, I'm very busy recently, I will take some time this week to investigate this issue.

@malachany
Copy link
Author

No worries. Thanks for the update

@zihaoAK47
Copy link
Member

@malachany Hello, we have investigated the issue and currently you can resolve it using any of the following methods:

Change the data type of your table field from datetime to another date type.
Try downgrading your MySQL driver, or use mysql-connector-java as the MySQL driver. The version I tested with is 8.0.13.
Ref: https://mvnrepository.com/artifact/mysql/mysql-connector-java

test

@malachany
Copy link
Author

thanks @strongduanmu and @zihaoAK47 for looking into this

@strongduanmu
Copy link
Member

@malachany You are welcome to help us test sql federation. This feature is very important to ShardingSphere and can greatly improve the usage scenarios of ShardingSphere.

@malachany
Copy link
Author

@strongduanmu I'll be pulling latest and testing this out soon and will update you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants