Skip to content

Commit f87ef76

Browse files
cxzl25pan3793
authored andcommitted
[KYUUBI #2047] Support more MySQL JDBC driver versions
### _Why are the changes needed?_ Support more MySQL JDBC driver versions. The user may use the 5.x mysql driver to connect to the kyuubi server. #2047 ### _How was this patch tested?_ - [ ] Add some test cases that check the changes thoroughly including negative and positive cases if possible - [ ] Add screenshots for manual tests if appropriate - [x] [Run test](https://kyuubi.apache.org/docs/latest/develop_tools/testing.html#running-tests) locally before make a pull request Closes #2048 from cxzl25/KYUUBI-2047. Closes #2047 03cd15f [sychen] Support more MySQL JDBC driver versions Authored-by: sychen <sychen@trip.com> Signed-off-by: Cheng Pan <chengpan@apache.org> (cherry picked from commit 109569b) Signed-off-by: Cheng Pan <chengpan@apache.org>
1 parent 0ee4af5 commit f87ef76

File tree

1 file changed

+48
-26
lines changed

1 file changed

+48
-26
lines changed

kyuubi-server/src/main/scala/org/apache/kyuubi/server/mysql/MySQLDialectHelper.scala

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,14 @@ object MySQLDialectHelper {
4141
| 'utf8mb4' as `@@character_set_server`,
4242
| 'utf8mb4' as `@@character_set_database`
4343
|""".stripMargin
44-
// mysql-connector-java:8 initialized query
45-
case sql
46-
if sql.contains("select @@session.auto_increment_increment as auto_increment_increment, @@character_set_client as character_set_client, @@character_set_connection as character_set_connection, @@character_set_results as character_set_results, @@character_set_server as character_set_server, @@collation_server as collation_server, @@collation_connection as collation_connection, @@init_connect as init_connect, @@interactive_timeout as interactive_timeout, @@license as license, @@lower_case_table_names as lower_case_table_names, @@max_allowed_packet as max_allowed_packet, @@net_write_timeout as net_write_timeout, @@performance_schema as performance_schema, @@query_cache_size as query_cache_size, @@query_cache_type as query_cache_type, @@sql_mode as sql_mode, @@system_time_zone as system_time_zone, @@time_zone as time_zone, @@transaction_isolation as transaction_isolation, @@wait_timeout as wait_timeout") =>
47-
"""SELECT
48-
| 1 AS auto_increment_increment,
49-
| 'utf8mb4' AS character_set_client,
50-
| 'utf8mb4' AS character_set_connection,
51-
| 'utf8mb4' AS character_set_results,
52-
| 'utf8mb4' AS character_set_server,
53-
| 'utf8mb4_general_ci' AS collation_server,
54-
| 'utf8mb4_general_ci' AS collation_connection,
55-
| '' AS init_connect,
56-
| 28800 AS interactive_timeout,
57-
| 'Apache License 2.0' AS license,
58-
| 0 AS lower_case_table_names,
59-
| 4194304 AS max_allowed_packet,
60-
| 60 AS net_write_timeout,
61-
| 0 AS performance_schema,
62-
| 1048576 AS query_cache_size,
63-
| 'OFF' AS query_cache_type,
64-
| 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' AS sql_mode,
65-
| 'UTC' AS system_time_zone,
66-
| 'SYSTEM' AS time_zone,
67-
| 'REPEATABLE-READ' AS transaction_isolation,
68-
| '28800' AS wait_timeout
69-
|""".stripMargin
7044
// scalastyle:on line.size.limit
45+
// mysql-connector-java:[5-8] version initialized query
46+
case sql
47+
if sql.contains("select @@session.auto_increment_increment") ||
48+
sql.contains("select @@session.auto_increment_increment") =>
49+
convertInitSQL(sql)
50+
case "set names utf8mb4" =>
51+
"SET NAMES=utf8mb4"
7152
case "select @@session.transaction_read_only" =>
7253
"select '0' as `@@session.transaction_read_only`"
7354
case _ => origin
@@ -86,4 +67,45 @@ object MySQLDialectHelper {
8667
}
8768
throw MySQLErrorCode.ER_NOT_SUPPORTED_YET.toKyuubiSQLException
8869
}
70+
71+
private val regexInitSQL = "@@(session\\.)?([^ ]+)".r
72+
73+
private def convertInitSQL(originSQL: String): String = {
74+
regexInitSQL.findAllMatchIn(originSQL).map(m => {
75+
val key = m.group(2)
76+
if (serverVariables.contains(key)) {
77+
s"'${serverVariables(key)}' AS $key"
78+
} else {
79+
return originSQL
80+
}
81+
}).mkString("SELECT ", ",", "")
82+
}
83+
84+
private val serverVariables: Map[String, String] =
85+
Map(
86+
"auto_increment_increment" -> "1",
87+
"character_set_client" -> "utf8mb4",
88+
"character_set_connection" -> "utf8mb4",
89+
"character_set_results" -> "utf8mb4",
90+
"character_set_server" -> "utf8mb4",
91+
"collation_connection" -> "utf8mb4_general_ci",
92+
"collation_server" -> "utf8mb4_general_ci",
93+
"have_query_cache" -> "YES",
94+
"init_connect" -> "",
95+
"interactive_timeout" -> "28800",
96+
"license" -> "Apache License 2.0",
97+
"lower_case_table_names" -> "0",
98+
"max_allowed_packet" -> "4194304",
99+
"net_buffer_length" -> "16384",
100+
"net_write_timeout" -> "60",
101+
"performance_schema" -> "0",
102+
"query_cache_size" -> "1048576",
103+
"query_cache_type" -> "OFF",
104+
"sql_mode" -> ("ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE," +
105+
"NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"),
106+
"system_time_zone" -> "UTC",
107+
"time_zone" -> "SYSTEM",
108+
"transaction_isolation" -> "REPEATABLE-READ",
109+
"tx_isolation" -> "REPEATABLE-READ",
110+
"wait_timeout" -> "28800")
89111
}

0 commit comments

Comments
 (0)