-
Notifications
You must be signed in to change notification settings - Fork 929
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[KYUUBI #5861] Generalize TRowSet generator for JDBC engine with dial…
…ects # 🔍 Description ## Issue References 🔗 As described. ## Describe Your Solution 🔧 - Introduced JdbcTRowSetGenerator extending `AbstractTRowSetGenerator ` introduced in #5851 in JDBC engine. - Provide a DefaultJdbcTRowSetGenerator as default implementation for mapping the JDBC data types to TRowSet generation - Make JDBC dialect providing TRowSetGenerator extending DefaultJdbcTRowSetGenerator to adapt detailed differences ## Types of changes 🔖 - [ ] Bugfix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) ## Test Plan 🧪 #### Behavior Without This Pull Request ⚰️ #### Behavior With This Pull Request 🎉 #### Related Unit Tests --- # Checklists ## 📝 Author Self Checklist - [x] My code follows the [style guidelines](https://kyuubi.readthedocs.io/en/master/contributing/code/style.html) of this project - [x] I have performed a self-review - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] This patch was not authored or co-authored using [Generative Tooling](https://www.apache.org/legal/generative-tooling.html) ## 📝 Committer Pre-Merge Checklist - [ ] Pull request title is okay. - [ ] No license issues. - [ ] Milestone correctly set? - [ ] Test coverage is ok - [ ] Assignees are selected. - [ ] Minimum number of approvals - [ ] No changes are requested **Be nice. Be informative.** Closes #5861 from bowenliang123/jdbc-rowgen. Closes #5861 7f8658d [Bowen Liang] generalize jdbc TRowSet generator Authored-by: Bowen Liang <[email protected]> Signed-off-by: liangbowen <[email protected]>
- Loading branch information
1 parent
acdd74d
commit 09febce
Showing
13 changed files
with
211 additions
and
386 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
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
151 changes: 151 additions & 0 deletions
151
...ine/src/main/scala/org/apache/kyuubi/engine/jdbc/schema/DefaultJdbcTRowSetGenerator.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,151 @@ | ||
/* | ||
* 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.schema | ||
|
||
import java.sql.Date | ||
import java.sql.Types._ | ||
import java.time.LocalDateTime | ||
import java.util | ||
|
||
import org.apache.kyuubi.shaded.hive.service.rpc.thrift._ | ||
import org.apache.kyuubi.shaded.hive.service.rpc.thrift.TTypeId._ | ||
import org.apache.kyuubi.util.RowSetUtils.{bitSetToBuffer, formatDate, formatLocalDateTime} | ||
|
||
class DefaultJdbcTRowSetGenerator extends JdbcTRowSetGenerator { | ||
|
||
override def toTColumn(rows: Seq[Seq[_]], ordinal: Int, sqlType: Int): TColumn = | ||
sqlType match { | ||
case BIT => toBitTColumn(rows, ordinal) | ||
case TINYINT => toTinyIntTColumn(rows, ordinal) | ||
case SMALLINT => toSmallIntTColumn(rows, ordinal) | ||
case INTEGER => toIntegerTColumn(rows, ordinal) | ||
case BIGINT => toBigIntTColumn(rows, ordinal) | ||
case REAL => toRealTColumn(rows, ordinal) | ||
case DOUBLE => toDoubleTColumn(rows, ordinal) | ||
case CHAR => toCharTColumn(rows, ordinal) | ||
case VARCHAR => toVarcharTColumn(rows, ordinal) | ||
case _ => toDefaultTColumn(rows, ordinal, sqlType) | ||
} | ||
|
||
override def toTColumnValue(ordinal: Int, row: Seq[_], types: Seq[Column]): TColumnValue = | ||
getColumnType(types, ordinal) match { | ||
case BIT => toBitTColumnValue(row, ordinal) | ||
case TINYINT => toTinyIntTColumnValue(row, ordinal) | ||
case SMALLINT => toSmallIntTColumnValue(row, ordinal) | ||
case INTEGER => toIntegerTColumnValue(row, ordinal) | ||
case BIGINT => toBigIntTColumnValue(row, ordinal) | ||
case REAL => toRealTColumnValue(row, ordinal) | ||
case DOUBLE => toDoubleTColumnValue(row, ordinal) | ||
case CHAR => toCharTColumnValue(row, ordinal) | ||
case VARCHAR => toVarcharTColumnValue(row, ordinal) | ||
case otherType => toDefaultTColumnValue(row, ordinal, otherType) | ||
} | ||
|
||
protected def toDefaultTColumn(rows: Seq[Seq[_]], ordinal: Int, sqlType: Int): TColumn = { | ||
val nulls = new java.util.BitSet() | ||
val rowSize = rows.length | ||
val values = new util.ArrayList[String](rowSize) | ||
var i = 0 | ||
while (i < rowSize) { | ||
val row = rows(i) | ||
nulls.set(i, row(ordinal) == null) | ||
val value = | ||
if (row(ordinal) == null) { | ||
"" | ||
} else { | ||
toHiveString(row(ordinal), sqlType) | ||
} | ||
values.add(value) | ||
i += 1 | ||
} | ||
TColumn.stringVal(new TStringColumn(values, nulls)) | ||
} | ||
|
||
protected def toBitTColumn(rows: Seq[Seq[_]], ordinal: Int): TColumn = | ||
toTTypeColumn(BOOLEAN_TYPE, rows, ordinal) | ||
|
||
protected def toTinyIntTColumn(rows: Seq[Seq[_]], ordinal: Int): TColumn = | ||
toTTypeColumn(TINYINT_TYPE, rows, ordinal) | ||
|
||
protected def toSmallIntTColumn(rows: Seq[Seq[_]], ordinal: Int): TColumn = | ||
toTTypeColumn(SMALLINT_TYPE, rows, ordinal) | ||
|
||
protected def toIntegerTColumn(rows: Seq[Seq[_]], ordinal: Int): TColumn = | ||
toTTypeColumn(INT_TYPE, rows, ordinal) | ||
|
||
protected def toBigIntTColumn(rows: Seq[Seq[_]], ordinal: Int): TColumn = | ||
toTTypeColumn(BIGINT_TYPE, rows, ordinal) | ||
|
||
protected def toRealTColumn(rows: Seq[Seq[_]], ordinal: Int): TColumn = | ||
toTTypeColumn(FLOAT_TYPE, rows, ordinal) | ||
|
||
protected def toDoubleTColumn(rows: Seq[Seq[_]], ordinal: Int): TColumn = | ||
toTTypeColumn(DOUBLE_TYPE, rows, ordinal) | ||
|
||
protected def toCharTColumn(rows: Seq[Seq[_]], ordinal: Int): TColumn = | ||
toTTypeColumn(CHAR_TYPE, rows, ordinal) | ||
|
||
protected def toVarcharTColumn(rows: Seq[Seq[_]], ordinal: Int): TColumn = | ||
toTTypeColumn(STRING_TYPE, rows, ordinal) | ||
|
||
// ========================================================== | ||
|
||
protected def toBitTColumnValue(row: Seq[_], ordinal: Int): TColumnValue = | ||
toTTypeColumnVal(BOOLEAN_TYPE, row, ordinal) | ||
|
||
protected def toTinyIntTColumnValue(row: Seq[_], ordinal: Int): TColumnValue = | ||
toTTypeColumnVal(TINYINT_TYPE, row, ordinal) | ||
|
||
protected def toSmallIntTColumnValue(row: Seq[_], ordinal: Int): TColumnValue = | ||
toTTypeColumnVal(SMALLINT_TYPE, row, ordinal) | ||
|
||
protected def toIntegerTColumnValue(row: Seq[_], ordinal: Int): TColumnValue = | ||
toTTypeColumnVal(INT_TYPE, row, ordinal) | ||
|
||
protected def toBigIntTColumnValue(row: Seq[_], ordinal: Int): TColumnValue = | ||
toTTypeColumnVal(BIGINT_TYPE, row, ordinal) | ||
|
||
protected def toRealTColumnValue(row: Seq[_], ordinal: Int): TColumnValue = | ||
toTTypeColumnVal(FLOAT_TYPE, row, ordinal) | ||
|
||
protected def toDoubleTColumnValue(row: Seq[_], ordinal: Int): TColumnValue = | ||
toTTypeColumnVal(DOUBLE_TYPE, row, ordinal) | ||
|
||
protected def toCharTColumnValue(row: Seq[_], ordinal: Int): TColumnValue = | ||
toTTypeColumnVal(STRING_TYPE, row, ordinal) | ||
|
||
protected def toVarcharTColumnValue(row: Seq[_], ordinal: Int): TColumnValue = | ||
toTTypeColumnVal(STRING_TYPE, row, ordinal) | ||
|
||
protected def toDefaultTColumnValue(row: Seq[_], ordinal: Int, sqlType: Int): TColumnValue = { | ||
val tStrValue = new TStringValue | ||
if (row(ordinal) != null) { | ||
tStrValue.setValue( | ||
toHiveString(row(ordinal), sqlType)) | ||
} | ||
TColumnValue.stringVal(tStrValue) | ||
} | ||
|
||
protected def toHiveString(data: Any, sqlType: Int): String = | ||
(data, sqlType) match { | ||
case (date: Date, DATE) => formatDate(date) | ||
case (dateTime: LocalDateTime, TIMESTAMP) => formatLocalDateTime(dateTime) | ||
case (decimal: java.math.BigDecimal, DECIMAL) => decimal.toPlainString | ||
case (bigint: java.math.BigInteger, BIGINT) => bigint.toString() | ||
case (other, _) => other.toString | ||
} | ||
} |
Oops, something went wrong.