cloud-fan commented on a change in pull request #29795: URL: https://github.com/apache/spark/pull/29795#discussion_r493196364
########## File path: sql/core/src/test/scala/org/apache/spark/sql/UpdateFieldsPerformanceSuite.scala ########## @@ -0,0 +1,229 @@ +/* + * 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.spark.sql + +import org.apache.spark.sql.functions.{col, lit} +import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.sql.types.{IntegerType, StructField, StructType} + +class UpdateFieldsPerformanceSuite extends QueryTest with SharedSparkSession { + + private def nestedColName(d: Int, colNum: Int): String = s"nested${d}Col$colNum" + + private def nestedStructType( + depths: Seq[Int], colNums: Seq[Int], nullable: Boolean): StructType = { Review comment: nit: 4 space indentation ########## File path: sql/core/src/test/scala/org/apache/spark/sql/UpdateFieldsPerformanceSuite.scala ########## @@ -0,0 +1,229 @@ +/* + * 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.spark.sql + +import org.apache.spark.sql.functions.{col, lit} +import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.sql.types.{IntegerType, StructField, StructType} + +class UpdateFieldsPerformanceSuite extends QueryTest with SharedSparkSession { + + private def nestedColName(d: Int, colNum: Int): String = s"nested${d}Col$colNum" + + private def nestedStructType( + depths: Seq[Int], colNums: Seq[Int], nullable: Boolean): StructType = { + if (depths.length == 1) { + StructType(colNums.map { colNum => + StructField(nestedColName(depths.head, colNum), IntegerType, nullable = false) + }) + } else { + val depth = depths.head + val fields = colNums.foldLeft(Seq.empty[StructField]) { + case (structFields, colNum) if colNum == 0 => + val nested = nestedStructType(depths.tail, colNums, nullable) + structFields :+ StructField(nestedColName(depth, colNum), nested, nullable) + case (structFields, colNum) => + structFields :+ StructField(nestedColName(depth, colNum), IntegerType, nullable = false) + } + StructType(fields) + } + } + + private def nestedRow(depths: Seq[Int], colNums: Seq[Int]): Row = { + if (depths.length == 1) { + Row.fromSeq(colNums) + } else { + val values = colNums.foldLeft(Seq.empty[Any]) { + case (values, colNum) if colNum == 0 => values :+ nestedRow(depths.tail, colNums) + case (values, colNum) => values :+ colNum + } + Row.fromSeq(values) + } + } + + /** + * Utility function for generating a DataFrame with nested columns. + * + * @param depth: The depth to which to create nested columns. + * @param numColsAtEachDepth: The number of columns to create at each depth. The value of each + * column will be the same as its index (IntegerType) at that depth + * unless the index = 0, in which case it may be a StructType which + * represents the next depth. + * @param nullable: This value is used to set the nullability of StructType columns. + */ + private def nestedDf( + depth: Int, numColsAtEachDepth: Int, nullable: Boolean = false): DataFrame = { Review comment: ditto ########## File path: sql/core/src/test/scala/org/apache/spark/sql/UpdateFieldsPerformanceSuite.scala ########## @@ -0,0 +1,229 @@ +/* + * 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.spark.sql + +import org.apache.spark.sql.functions.{col, lit} +import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.sql.types.{IntegerType, StructField, StructType} + +class UpdateFieldsPerformanceSuite extends QueryTest with SharedSparkSession { + + private def nestedColName(d: Int, colNum: Int): String = s"nested${d}Col$colNum" + + private def nestedStructType( + depths: Seq[Int], colNums: Seq[Int], nullable: Boolean): StructType = { + if (depths.length == 1) { + StructType(colNums.map { colNum => + StructField(nestedColName(depths.head, colNum), IntegerType, nullable = false) + }) + } else { + val depth = depths.head + val fields = colNums.foldLeft(Seq.empty[StructField]) { + case (structFields, colNum) if colNum == 0 => + val nested = nestedStructType(depths.tail, colNums, nullable) + structFields :+ StructField(nestedColName(depth, colNum), nested, nullable) + case (structFields, colNum) => + structFields :+ StructField(nestedColName(depth, colNum), IntegerType, nullable = false) + } + StructType(fields) + } + } + + private def nestedRow(depths: Seq[Int], colNums: Seq[Int]): Row = { + if (depths.length == 1) { + Row.fromSeq(colNums) + } else { + val values = colNums.foldLeft(Seq.empty[Any]) { + case (values, colNum) if colNum == 0 => values :+ nestedRow(depths.tail, colNums) + case (values, colNum) => values :+ colNum + } + Row.fromSeq(values) + } + } + + /** + * Utility function for generating a DataFrame with nested columns. + * + * @param depth: The depth to which to create nested columns. + * @param numColsAtEachDepth: The number of columns to create at each depth. The value of each + * column will be the same as its index (IntegerType) at that depth + * unless the index = 0, in which case it may be a StructType which + * represents the next depth. + * @param nullable: This value is used to set the nullability of StructType columns. + */ + private def nestedDf( + depth: Int, numColsAtEachDepth: Int, nullable: Boolean = false): DataFrame = { + require(depth > 0) + require(numColsAtEachDepth > 0) + + val depths = 1 to depth + val colNums = 0 until numColsAtEachDepth + val nestedColumn = nestedRow(depths, colNums) + val nestedColumnDataType = nestedStructType(depths, colNums, nullable) + + spark.createDataFrame( + sparkContext.parallelize(Row(nestedColumn) :: Nil), + StructType(Seq(StructField(nestedColName(0, 0), nestedColumnDataType, nullable)))) + } + + test("nestedDf should generate nested DataFrames") { + checkAnswer( + nestedDf(1, 1), + Row(Row(0)) :: Nil, + StructType(Seq(StructField("nested0Col0", StructType(Seq( + StructField("nested1Col0", IntegerType, nullable = false))), + nullable = false)))) + + checkAnswer( + nestedDf(1, 2), + Row(Row(0, 1)) :: Nil, + StructType(Seq(StructField("nested0Col0", StructType(Seq( + StructField("nested1Col0", IntegerType, nullable = false), + StructField("nested1Col1", IntegerType, nullable = false))), + nullable = false)))) + + checkAnswer( + nestedDf(2, 1), + Row(Row(Row(0))) :: Nil, + StructType(Seq(StructField("nested0Col0", StructType(Seq( + StructField("nested1Col0", StructType(Seq( + StructField("nested2Col0", IntegerType, nullable = false))), + nullable = false))), + nullable = false)))) + + checkAnswer( + nestedDf(2, 2), + Row(Row(Row(0, 1), 1)) :: Nil, + StructType(Seq(StructField("nested0Col0", StructType(Seq( + StructField("nested1Col0", StructType(Seq( + StructField("nested2Col0", IntegerType, nullable = false), + StructField("nested2Col1", IntegerType, nullable = false))), + nullable = false), + StructField("nested1Col1", IntegerType, nullable = false))), + nullable = false)))) + + checkAnswer( + nestedDf(2, 2, nullable = true), + Row(Row(Row(0, 1), 1)) :: Nil, + StructType(Seq(StructField("nested0Col0", StructType(Seq( + StructField("nested1Col0", StructType(Seq( + StructField("nested2Col0", IntegerType, nullable = false), + StructField("nested2Col1", IntegerType, nullable = false))), + nullable = true), + StructField("nested1Col1", IntegerType, nullable = false))), + nullable = true)))) + } + + // simulates how a user would add/drop nested fields in a performant manner + private def addDropNestedColumns( + column: Column, Review comment: ditto ########## File path: sql/core/src/test/scala/org/apache/spark/sql/UpdateFieldsPerformanceSuite.scala ########## @@ -0,0 +1,229 @@ +/* + * 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.spark.sql + +import org.apache.spark.sql.functions.{col, lit} +import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.sql.types.{IntegerType, StructField, StructType} + +class UpdateFieldsPerformanceSuite extends QueryTest with SharedSparkSession { Review comment: if we care about performance, shall we turn it into a benchmark like `OrcReadBenchmark`? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
