pan3793 commented on code in PR #5870: URL: https://github.com/apache/kyuubi/pull/5870#discussion_r1432813329
########## kyuubi-common/src/main/scala/org/apache/kyuubi/engine/schema/TRowSetColumnGenerator.scala: ########## @@ -0,0 +1,105 @@ +/* + * 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.schema +import java.nio.ByteBuffer +import java.util.{ArrayList => JArrayList, BitSet => JBitSet, List => JList} + +import scala.collection.JavaConverters._ + +import org.apache.kyuubi.shaded.hive.service.rpc.thrift._ + +trait TRowSetColumnGenerator[RowT] extends TRowSetColumnGetter[RowT] { + protected def getColumnToList[T]( + rows: Seq[RowT], + ordinal: Int, + defaultVal: T, + convertFunc: (RowT, Int) => T = null): (JList[T], ByteBuffer) = { + val rowSize = rows.length + val ret = new JArrayList[T](rowSize) + val nulls = new JBitSet() + var idx = 0 + while (idx < rowSize) { + val row = rows(idx) + val isNull = isColumnNullAt(row, ordinal) + if (isNull) { + nulls.set(idx, true) + ret.add(defaultVal) + } else { + val value = Option(convertFunc) match { + case Some(f) => f(row, ordinal) + case _ => getColumnAs[T](row, ordinal) + } + ret.add(value) + } + idx += 1 + } + (ret, ByteBuffer.wrap(nulls.toByteArray)) + } + + def asBooleanColumn(rows: Seq[RowT], ordinal: Int): TColumn = { + val (values, nulls) = getColumnToList[java.lang.Boolean](rows, ordinal, true) Review Comment: use J* alias for all Java primitive types ```suggestion val (values, nulls) = getColumnToList[JBoolean](rows, ordinal, true) ``` ########## kyuubi-common/src/main/scala/org/apache/kyuubi/engine/schema/TRowSetColumnValueGenerator.scala: ########## @@ -0,0 +1,93 @@ +/* + * 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.schema + +import org.apache.kyuubi.shaded.hive.service.rpc.thrift._ + +trait TRowSetColumnValueGenerator[RowT] extends TRowSetColumnGetter[RowT] { + + def asBooleanVal(row: RowT, ordinal: Int): TColumnValue = { Review Comment: ```suggestion def asBooleanTColumnValue(row: RowT, ordinal: Int): TColumnValue = { ``` ########## kyuubi-common/src/main/scala/org/apache/kyuubi/engine/schema/AbstractTRowSetGenerator.scala: ########## @@ -16,28 +16,20 @@ */ package org.apache.kyuubi.engine.schema -import java.nio.ByteBuffer -import java.util.{ArrayList => JArrayList, BitSet => JBitSet, List => JList} - -import scala.collection.JavaConverters._ +import java.util.{ArrayList => JArrayList} 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 -trait AbstractTRowSetGenerator[SchemaT, RowT, ColumnT] { +trait AbstractTRowSetGenerator[SchemaT, RowT, ColumnT] Review Comment: we can simply call it `TRowSetGenerator` ########## kyuubi-common/src/main/scala/org/apache/kyuubi/engine/schema/TRowSetColumnGenerator.scala: ########## @@ -0,0 +1,105 @@ +/* + * 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.schema +import java.nio.ByteBuffer +import java.util.{ArrayList => JArrayList, BitSet => JBitSet, List => JList} + +import scala.collection.JavaConverters._ + +import org.apache.kyuubi.shaded.hive.service.rpc.thrift._ + +trait TRowSetColumnGenerator[RowT] extends TRowSetColumnGetter[RowT] { + protected def getColumnToList[T]( + rows: Seq[RowT], + ordinal: Int, + defaultVal: T, + convertFunc: (RowT, Int) => T = null): (JList[T], ByteBuffer) = { + val rowSize = rows.length + val ret = new JArrayList[T](rowSize) + val nulls = new JBitSet() + var idx = 0 + while (idx < rowSize) { + val row = rows(idx) + val isNull = isColumnNullAt(row, ordinal) + if (isNull) { + nulls.set(idx, true) + ret.add(defaultVal) + } else { + val value = Option(convertFunc) match { + case Some(f) => f(row, ordinal) + case _ => getColumnAs[T](row, ordinal) + } + ret.add(value) + } + idx += 1 + } + (ret, ByteBuffer.wrap(nulls.toByteArray)) + } + + def asBooleanColumn(rows: Seq[RowT], ordinal: Int): TColumn = { + val (values, nulls) = getColumnToList[java.lang.Boolean](rows, ordinal, true) + TColumn.boolVal(new TBoolColumn(values, nulls)) + } + + def asByteColumn(rows: Seq[RowT], ordinal: Int): TColumn = { + val (values, nulls) = getColumnToList[java.lang.Byte](rows, ordinal, 0.toByte) + TColumn.byteVal(new TByteColumn(values, nulls)) + } + + def asShortColumn(rows: Seq[RowT], ordinal: Int): TColumn = { + val (values, nulls) = getColumnToList[java.lang.Short](rows, ordinal, 0.toShort) + TColumn.i16Val(new TI16Column(values, nulls)) + } + + def asIntegerColumn(rows: Seq[RowT], ordinal: Int): TColumn = { + val (values, nulls) = getColumnToList[java.lang.Integer](rows, ordinal, 0) + TColumn.i32Val(new TI32Column(values, nulls)) + } + + def asLongColumn(rows: Seq[RowT], ordinal: Int): TColumn = { + val (values, nulls) = getColumnToList[java.lang.Long](rows, ordinal, 0.toLong) + TColumn.i64Val(new TI64Column(values, nulls)) + } + + def asFloatColumn(rows: Seq[RowT], ordinal: Int): TColumn = { + val (values, nulls) = getColumnToList[java.lang.Float](rows, ordinal, 0.toFloat) + val doubleValues = values.asScala.map(f => java.lang.Double.valueOf(f.toString)).asJava + TColumn.doubleVal(new TDoubleColumn(doubleValues, nulls)) + } + + def asDoubleColumn(rows: Seq[RowT], ordinal: Int): TColumn = { + val (values, nulls) = getColumnToList[java.lang.Double](rows, ordinal, 0.toDouble) + TColumn.doubleVal(new TDoubleColumn(values, nulls)) + } + + def asStringColumn( + rows: Seq[RowT], + ordinal: Int, + defaultVal: String = "", + convertFunc: (RowT, Int) => String = null): TColumn = { + val (values, nulls) = getColumnToList[java.lang.String](rows, ordinal, defaultVal, convertFunc) Review Comment: Scala String is same as Java ```suggestion val (values, nulls) = getColumnToList[String](rows, ordinal, defaultVal, convertFunc) ``` ########## kyuubi-common/src/main/scala/org/apache/kyuubi/engine/schema/AbstractTRowSetGenerator.scala: ########## @@ -16,28 +16,20 @@ */ package org.apache.kyuubi.engine.schema Review Comment: the `schema` seems incorrect to me ```suggestion package org.apache.kyuubi.engine.result ``` ########## kyuubi-common/src/main/scala/org/apache/kyuubi/engine/schema/TRowSetColumnGenerator.scala: ########## @@ -0,0 +1,105 @@ +/* + * 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.schema +import java.nio.ByteBuffer +import java.util.{ArrayList => JArrayList, BitSet => JBitSet, List => JList} + +import scala.collection.JavaConverters._ + +import org.apache.kyuubi.shaded.hive.service.rpc.thrift._ + +trait TRowSetColumnGenerator[RowT] extends TRowSetColumnGetter[RowT] { + protected def getColumnToList[T]( + rows: Seq[RowT], + ordinal: Int, + defaultVal: T, + convertFunc: (RowT, Int) => T = null): (JList[T], ByteBuffer) = { + val rowSize = rows.length + val ret = new JArrayList[T](rowSize) + val nulls = new JBitSet() + var idx = 0 + while (idx < rowSize) { + val row = rows(idx) + val isNull = isColumnNullAt(row, ordinal) + if (isNull) { + nulls.set(idx, true) + ret.add(defaultVal) + } else { + val value = Option(convertFunc) match { + case Some(f) => f(row, ordinal) + case _ => getColumnAs[T](row, ordinal) + } + ret.add(value) + } + idx += 1 + } + (ret, ByteBuffer.wrap(nulls.toByteArray)) + } + + def asBooleanColumn(rows: Seq[RowT], ordinal: Int): TColumn = { Review Comment: keep Txxx for consistency ```suggestion def asBooleanTColumn(rows: Seq[RowT], ordinal: Int): TColumn = { ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
