-
Notifications
You must be signed in to change notification settings - Fork 919
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[KYUUBI #5464] JDBC Engine supports MySQL
### _Why are the changes needed?_ To close #5464. To support JDBC engine use MySQL Dialect (kyuubi.engine.jdbc.type=mysql). ### _How was this patch tested?_ - [x] Add some test cases that check the changes thoroughly including negative and positive cases if possible - [ ] Add screenshots for manual tests if appropriate - [ ] [Run test](https://kyuubi.readthedocs.io/en/master/contributing/code/testing.html#running-tests) locally before make a pull request ### _Was this patch authored or co-authored using generative AI tooling?_ No. Closes #5588 from Kwafoor/kyuubi_5464. Closes #5464 1019a61 [wangjunbo] [KYUUBI #5464]rename function name `getProviderClass` to `getDriverClass` 9901bba [wangjunbo] [KYUUBI #5464]handle properly to keep compatiblity b33d79e [wangjunbo] [KYUUBI #5464]handle properly to keep compatiblity 86e6ee2 [wangjunbo] [KYUUBI #5464]handle properly to keep compatiblity d76cb32 [wangjunbo] [KYUUBI #5464]update the docs 4a1acff [wangjunbo] [KYUUBI #5464]update the docs 1aff55e [wangjunbo] [KYUUBI #5464]update the docs of kyuubi.engine.type 84202ea [wangjunbo] [KYUUBI #5464] update the docs of kyuubi.engine.type e3c1e94 [wangjunbo] [KYUUBI #5464] fix check cdf820d [wangjunbo] [KYUUBI #5464] fix check ff0f30a [wangjunbo] [KYUUBI #5464] fix check 01321dc [wangjunbo] [KYUUBI #5464] JDBC Engine supports MySQL 756f530 [wangjunbo] [KYUUBI #5464] JDBC Engine supports MySQL Authored-by: wangjunbo <[email protected]> Signed-off-by: Cheng Pan <[email protected]>
- Loading branch information
Showing
20 changed files
with
905 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
...yuubi-jdbc-engine/src/main/scala/org/apache/kyuubi/engine/jdbc/dialect/MySQLDialect.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.kyuubi.engine.jdbc.dialect | ||
import java.sql.{Connection, ResultSet, Statement} | ||
import java.util | ||
|
||
import scala.collection.JavaConverters._ | ||
import scala.collection.mutable.ArrayBuffer | ||
|
||
import org.apache.commons.lang3.StringUtils | ||
|
||
import org.apache.kyuubi.engine.jdbc.mysql.{MySQLRowSetHelper, MySQLSchemaHelper} | ||
import org.apache.kyuubi.engine.jdbc.schema.{RowSetHelper, SchemaHelper} | ||
import org.apache.kyuubi.operation.meta.ResultSetSchemaConstant._ | ||
import org.apache.kyuubi.session.Session | ||
|
||
class MySQLDialect extends JdbcDialect { | ||
override def createStatement(connection: Connection, fetchSize: Int): Statement = { | ||
val statement = | ||
connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY) | ||
statement.setFetchSize(Integer.MIN_VALUE) | ||
statement | ||
} | ||
|
||
override def getTablesQuery( | ||
catalog: String, | ||
schema: String, | ||
tableName: String, | ||
tableTypes: util.List[String]): String = { | ||
val tTypes = | ||
if (tableTypes == null || tableTypes.isEmpty) { | ||
Set("BASE TABLE", "SYSTEM VIEW", "VIEW") | ||
} else { | ||
tableTypes.asScala.toSet | ||
} | ||
val query = new StringBuilder( | ||
s""" | ||
|SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, ENGINE, | ||
|TABLE_ROWS, AVG_ROW_LENGTH, DATA_LENGTH, | ||
|CREATE_TIME, UPDATE_TIME, TABLE_COLLATION, TABLE_COMMENT | ||
|FROM INFORMATION_SCHEMA.TABLES | ||
|""".stripMargin) | ||
|
||
val filters = ArrayBuffer[String]() | ||
if (StringUtils.isNotBlank(catalog)) { | ||
filters += s"$TABLE_CATALOG = '$catalog'" | ||
} | ||
|
||
if (StringUtils.isNotBlank(schema)) { | ||
filters += s"$TABLE_SCHEMA LIKE '$schema'" | ||
} | ||
|
||
if (StringUtils.isNotBlank(tableName)) { | ||
filters += s"$TABLE_NAME LIKE '$tableName'" | ||
} | ||
|
||
if (tTypes.nonEmpty) { | ||
filters += s"(${ | ||
tTypes.map { tableType => s"$TABLE_TYPE = '$tableType'" } | ||
.mkString(" OR ") | ||
})" | ||
} | ||
|
||
if (filters.nonEmpty) { | ||
query.append(" WHERE ") | ||
query.append(filters.mkString(" AND ")) | ||
} | ||
|
||
query.toString() | ||
} | ||
|
||
override def getColumnsQuery( | ||
session: Session, | ||
catalogName: String, | ||
schemaName: String, | ||
tableName: String, | ||
columnName: String): String = { | ||
val query = new StringBuilder( | ||
""" | ||
|SELECT | ||
|`TABLE_CATALOG`,`TABLE_SCHEMA`,`TABLE_NAME`, `COLUMN_NAME`,`ORDINAL_POSITION`, | ||
|`COLUMN_DEFAULT`,`IS_NULLABLE`,`DATA_TYPE`,`CHARACTER_MAXIMUM_LENGTH`, | ||
|`CHARACTER_OCTET_LENGTH`,`NUMERIC_PRECISION`,`NUMERIC_SCALE`,`DATETIME_PRECISION`, | ||
|`CHARACTER_SET_NAME`,`COLLATION_NAME`,`COLUMN_TYPE`,`COLUMN_KEY`,`EXTRA`,`PRIVILEGES`, | ||
|`COLUMN_COMMENT`,`GENERATION_EXPRESSION` | ||
|FROM information_schema.columns | ||
|""".stripMargin) | ||
|
||
val filters = ArrayBuffer[String]() | ||
if (StringUtils.isNotEmpty(catalogName)) { | ||
filters += s"$TABLE_CATALOG = '$catalogName'" | ||
} | ||
if (StringUtils.isNotEmpty(schemaName)) { | ||
filters += s"$TABLE_SCHEMA LIKE '$schemaName'" | ||
} | ||
if (StringUtils.isNotEmpty(tableName)) { | ||
filters += s"$TABLE_NAME LIKE '$tableName'" | ||
} | ||
if (StringUtils.isNotEmpty(columnName)) { | ||
filters += s"$COLUMN_NAME LIKE '$columnName'" | ||
} | ||
|
||
if (filters.nonEmpty) { | ||
query.append(" WHERE ") | ||
query.append(filters.mkString(" AND ")) | ||
} | ||
|
||
query.toString() | ||
} | ||
|
||
override def getRowSetHelper(): RowSetHelper = { | ||
new MySQLRowSetHelper | ||
} | ||
|
||
override def getSchemaHelper(): SchemaHelper = { | ||
new MySQLSchemaHelper | ||
} | ||
|
||
override def name(): String = { | ||
"mysql" | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...c-engine/src/main/scala/org/apache/kyuubi/engine/jdbc/mysql/MySQLConnectionProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.kyuubi.engine.jdbc.mysql | ||
|
||
class MySQLConnectionProvider extends Mysql8ConnectionProvider { | ||
|
||
override val name: String = classOf[MySQLConnectionProvider].getSimpleName | ||
} |
69 changes: 69 additions & 0 deletions
69
...bi-jdbc-engine/src/main/scala/org/apache/kyuubi/engine/jdbc/mysql/MySQLRowSetHelper.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.kyuubi.engine.jdbc.mysql | ||
|
||
import java.sql.Types | ||
|
||
import org.apache.hive.service.rpc.thrift.{TColumn, TColumnValue} | ||
|
||
import org.apache.kyuubi.engine.jdbc.schema.RowSetHelper | ||
|
||
class MySQLRowSetHelper extends RowSetHelper { | ||
|
||
override def toTinyIntTColumn(rows: Seq[Seq[Any]], ordinal: Int): TColumn = | ||
toIntegerTColumn(rows, ordinal) | ||
|
||
override def toSmallIntTColumn(rows: Seq[Seq[Any]], ordinal: Int): TColumn = | ||
toIntegerTColumn(rows, ordinal) | ||
|
||
override def toTinyIntTColumnValue(row: List[Any], ordinal: Int): TColumnValue = | ||
toIntegerTColumnValue(row, ordinal) | ||
|
||
override def toSmallIntTColumnValue(row: List[Any], ordinal: Int): TColumnValue = | ||
toIntegerTColumnValue(row, ordinal) | ||
|
||
override protected def toIntegerTColumn(rows: Seq[Seq[Any]], ordinal: Int): TColumn = { | ||
val colHead = if (rows.isEmpty) None else rows.head(ordinal) | ||
colHead match { | ||
case v: Integer => super.toIntegerTColumn(rows, ordinal) | ||
case v: java.lang.Long => super.toBigIntTColumn(rows, ordinal) | ||
case _ => super.toDefaultTColumn(rows, ordinal, Types.INTEGER) | ||
} | ||
} | ||
|
||
override protected def toIntegerTColumnValue(row: List[Any], ordinal: Int): TColumnValue = { | ||
row(ordinal) match { | ||
case v: Integer => super.toIntegerTColumnValue(row, ordinal) | ||
case v: java.lang.Long => super.toBigIntTColumnValue(row, ordinal) | ||
case _ => super.toDefaultTColumnValue(row, ordinal, Types.INTEGER) | ||
} | ||
} | ||
|
||
override protected def toBigIntTColumn(rows: Seq[Seq[Any]], ordinal: Int): TColumn = { | ||
val colHead = if (rows.isEmpty) None else rows.head(ordinal) | ||
colHead match { | ||
case v: java.lang.Long => super.toBigIntTColumn(rows, ordinal) | ||
case _ => super.toDefaultTColumn(rows, ordinal, Types.BIGINT) | ||
} | ||
} | ||
|
||
override protected def toBigIntTColumnValue(row: List[Any], ordinal: Int): TColumnValue = | ||
row(ordinal) match { | ||
case v: java.lang.Long => super.toBigIntTColumnValue(row, ordinal) | ||
case _ => super.toDefaultTColumnValue(row, ordinal, Types.BIGINT) | ||
} | ||
} |
Oops, something went wrong.