SQL Compatibility Issues

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: SQL兼容性问题

| username: Hacker_DaUeI5uX

[TiDB Usage Environment] Production Environment
[TiDB Version] 5.7.25-TiDB-v5.1.2 \ 5.7.25-TiDB-v4.0.16

[Encountered Problem: Phenomenon and Impact] The same SQL executes normally on v4.0.16 but reports a syntax error on v5.1.2

SELECT * from (
  (SELECT vin, veh_series_no, veh_variable_model_no, current_version, vehicle_purpose, vehicle_status, current_vehicle_version_status 
   FROM xx_version 
   WHERE veh_series_no='xx' 
     and current_version in ('4.2.1-1.7.114') 
     and is_delete = 0 
     and current_vehicle_version_status in ("1")
  ) as a 
  JOIN 
  (SELECT row_number() over (PARTITION by vin order by event_time desc) as number, vin, event_type, target_version, event_time, error_type, error_reason, message_id 
   FROM xx_upgrade_status 
   WHERE current_version in ('4.2.1-1.7.114') 
     and is_delete = 0
  ) as b 
  ON a.vin = b.vin and b.number = 1
);

[Resource Configuration]
[Attachments: Screenshots/Logs/Monitoring]


| username: WalterWj | Original post link

It seems to be related to window functions. There should be relevant keywords in SQL. Try debugging and see.

| username: xfworld | Original post link

Compare the execution plans and you’ll see that the differences between 4.X and 5.X are indeed significant.

| username: Hacker_DaUeI5uX | Original post link

Just remove the outermost parentheses, it’s still a compatibility issue with different versions. Can you help explain the principle? Why is the 5.x version not supported?

| username: tracy0984 | Original post link

If you add an alias after the outermost parentheses, will it execute successfully?
I checked the official documentation:
Oracle and TiDB Function and Syntax Differences | PingCAP Documentation Center

This part shows that starting from version 5.4, TiDB requires every derived table to have an alias.

| username: tidb菜鸟一只 | Original post link

Try running this query to see if it still reports an error?

| username: Kongdom | Original post link

Oh, really? You can do it without aliases? We came from SQL Server and have always used aliases.

| username: WalterWj | Original post link

Try adding “as t” without removing the outermost parentheses.

| username: Hacker_DaUeI5uX | Original post link

Yes, it is possible to remove the outermost parentheses.

| username: Hacker_DaUeI5uX | Original post link

This is also fine. Shouldn’t SQL syntax in higher versions be backward compatible? The business encounters problems once the cluster is upgraded. :smile:

| username: WalterWj | Original post link

It should have been added as “as t”. I guess the lack of error in the lower version was unexpected. You can try MySQL.

| username: tidb菜鸟一只 | Original post link

You can check the sqlmod of the two databases to see if the SQL format requirements for the lower version database are lower.

| username: Kongdom | Original post link

It is still necessary to follow development standards.