maropu commented on a change in pull request #30334: URL: https://github.com/apache/spark/pull/30334#discussion_r532045253
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/UnionEstimation.scala ########## @@ -0,0 +1,97 @@ +/* + * 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.catalyst.plans.logical.statsEstimation + +import scala.collection.mutable.ArrayBuffer + +import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeMap} +import org.apache.spark.sql.catalyst.plans.logical.{ColumnStat, Statistics, Union} +import org.apache.spark.sql.types.{ByteType, DataType, DateType, DecimalType, DoubleType, FloatType, IntegerType, LongType, ShortType, TimestampType} Review comment: nit: `import org.apache.spark.sql.types._` looks okay. ########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/UnionEstimation.scala ########## @@ -0,0 +1,97 @@ +/* + * 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.catalyst.plans.logical.statsEstimation + +import scala.collection.mutable.ArrayBuffer + +import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeMap} +import org.apache.spark.sql.catalyst.plans.logical.{ColumnStat, Statistics, Union} +import org.apache.spark.sql.types.{ByteType, DataType, DateType, DecimalType, DoubleType, FloatType, IntegerType, LongType, ShortType, TimestampType} + +/** + * Estimate the number of output rows by doing the sum of output rows for each child of union, + * and estimate min and max stats for each column by finding the overall min and max of that + * column coming from its children. + */ +object UnionEstimation { + import EstimationUtils._ + + def compare(a: Any, b: Any, dataType: DataType): Boolean = { + dataType match { + case dt: IntegerType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: LongType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: FloatType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DoubleType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: ShortType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: ByteType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DateType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: TimestampType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DecimalType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case _ => false + } + } + + def estimate(union: Union): Option[Statistics] = { + val sizeInBytes = union.children.map(_.stats.sizeInBytes).sum + val outputRows: Option[BigInt] = if (rowCountsExist(union.children: _*)) { + Some(union.children.map(_.stats.rowCount.get).sum) + } else { + None + } + + val output = union.output + val outputAttrStats = new ArrayBuffer[(Attribute, ColumnStat)]() + + union.children.map(_.output).transpose.zipWithIndex.foreach { + case (attrs, outputIndex) => + val validStat = attrs.zipWithIndex.forall { + case (attr, childIndex) => + val attrStats = union.children(childIndex).stats.attributeStats + attrStats.get(attr).isDefined && attrStats(attr).hasMinMaxStats + } + if (validStat) { + val dataType = output(outputIndex).dataType + val minStart: Option[Any] = None + val maxStart: Option[Any] = None + val result = attrs.zipWithIndex.foldLeft((minStart, maxStart)) { + case ((minVal, maxVal), (attr, childIndex)) => + val colStat = union.children(childIndex).stats.attributeStats(attr) + val min = if (minVal.isEmpty || compare(colStat.min.get, minVal.get, dataType)) { + colStat.min + } else { + minVal + } + val max = if (maxVal.isEmpty || compare(maxVal.get, colStat.max.get, dataType)) { Review comment: ditto ########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/UnionEstimation.scala ########## @@ -0,0 +1,97 @@ +/* + * 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.catalyst.plans.logical.statsEstimation + +import scala.collection.mutable.ArrayBuffer + +import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeMap} +import org.apache.spark.sql.catalyst.plans.logical.{ColumnStat, Statistics, Union} +import org.apache.spark.sql.types.{ByteType, DataType, DateType, DecimalType, DoubleType, FloatType, IntegerType, LongType, ShortType, TimestampType} + +/** + * Estimate the number of output rows by doing the sum of output rows for each child of union, + * and estimate min and max stats for each column by finding the overall min and max of that + * column coming from its children. + */ +object UnionEstimation { + import EstimationUtils._ + + def compare(a: Any, b: Any, dataType: DataType): Boolean = { Review comment: private ########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/UnionEstimation.scala ########## @@ -0,0 +1,97 @@ +/* + * 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.catalyst.plans.logical.statsEstimation + +import scala.collection.mutable.ArrayBuffer + +import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeMap} +import org.apache.spark.sql.catalyst.plans.logical.{ColumnStat, Statistics, Union} +import org.apache.spark.sql.types.{ByteType, DataType, DateType, DecimalType, DoubleType, FloatType, IntegerType, LongType, ShortType, TimestampType} + +/** + * Estimate the number of output rows by doing the sum of output rows for each child of union, + * and estimate min and max stats for each column by finding the overall min and max of that + * column coming from its children. + */ +object UnionEstimation { + import EstimationUtils._ + + def compare(a: Any, b: Any, dataType: DataType): Boolean = { + dataType match { + case dt: IntegerType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: LongType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: FloatType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DoubleType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: ShortType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: ByteType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DateType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: TimestampType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DecimalType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case _ => false + } + } + + def estimate(union: Union): Option[Statistics] = { + val sizeInBytes = union.children.map(_.stats.sizeInBytes).sum + val outputRows: Option[BigInt] = if (rowCountsExist(union.children: _*)) { + Some(union.children.map(_.stats.rowCount.get).sum) + } else { + None + } + + val output = union.output Review comment: nit: `output` -> `unionOutput` ########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/UnionEstimation.scala ########## @@ -0,0 +1,97 @@ +/* + * 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.catalyst.plans.logical.statsEstimation + +import scala.collection.mutable.ArrayBuffer + +import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeMap} +import org.apache.spark.sql.catalyst.plans.logical.{ColumnStat, Statistics, Union} +import org.apache.spark.sql.types.{ByteType, DataType, DateType, DecimalType, DoubleType, FloatType, IntegerType, LongType, ShortType, TimestampType} + +/** + * Estimate the number of output rows by doing the sum of output rows for each child of union, + * and estimate min and max stats for each column by finding the overall min and max of that + * column coming from its children. + */ +object UnionEstimation { + import EstimationUtils._ + + def compare(a: Any, b: Any, dataType: DataType): Boolean = { + dataType match { + case dt: IntegerType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: LongType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: FloatType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DoubleType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: ShortType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: ByteType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DateType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: TimestampType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DecimalType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) Review comment: Instead of casting `ordering` into `Ordering[Any]`, how about creating a comparator `(Any, Any) => Boolean` in advance? ``` private def createStatComparator(dt: DataType) = dt match { case ByteType => (a: Any, b: Any) => ByteType.ordering.lt(a.asInstanceOf[Byte], b.asInstanceOf[Byte]) case ShortType => (a: Any, b: Any) => ShortType.ordering.lt(a.asInstanceOf[Short], b.asInstanceOf[Short]) // Lists up all the supported types... case _ => throw new IllegalStateException(s"Unsupported data type: ${dt.catalogString}") } ``` ########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/UnionEstimation.scala ########## @@ -0,0 +1,97 @@ +/* + * 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.catalyst.plans.logical.statsEstimation + +import scala.collection.mutable.ArrayBuffer + +import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeMap} +import org.apache.spark.sql.catalyst.plans.logical.{ColumnStat, Statistics, Union} +import org.apache.spark.sql.types.{ByteType, DataType, DateType, DecimalType, DoubleType, FloatType, IntegerType, LongType, ShortType, TimestampType} + +/** + * Estimate the number of output rows by doing the sum of output rows for each child of union, + * and estimate min and max stats for each column by finding the overall min and max of that + * column coming from its children. + */ +object UnionEstimation { + import EstimationUtils._ + + def compare(a: Any, b: Any, dataType: DataType): Boolean = { + dataType match { + case dt: IntegerType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: LongType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: FloatType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DoubleType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: ShortType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: ByteType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DateType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: TimestampType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DecimalType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case _ => false + } + } + + def estimate(union: Union): Option[Statistics] = { + val sizeInBytes = union.children.map(_.stats.sizeInBytes).sum + val outputRows: Option[BigInt] = if (rowCountsExist(union.children: _*)) { + Some(union.children.map(_.stats.rowCount.get).sum) + } else { + None + } + + val output = union.output + val outputAttrStats = new ArrayBuffer[(Attribute, ColumnStat)]() + + union.children.map(_.output).transpose.zipWithIndex.foreach { + case (attrs, outputIndex) => + val validStat = attrs.zipWithIndex.forall { + case (attr, childIndex) => + val attrStats = union.children(childIndex).stats.attributeStats + attrStats.get(attr).isDefined && attrStats(attr).hasMinMaxStats Review comment: How about checking if we need to compute min/max stats before computing them? ``` val childrenToComputeMinMaxStats = union.children.map(_.output) .transpose.zipWithIndex.filter { case (attrs, _) => supportedType(attrs.head.dataType) && // Checks if all the children have min/max stats attrs.zipWithIndex.forall { case (attr, childIndex) => val attrStats = union.children(childIndex).stats.attributeStats attrStats.get(attr).isDefined && attrStats(attr).hasMinMaxStats } } val newAttrStats = if (childrenToComputeMinMaxStats.nonEmpty) { // Compute min/max stats } else { AttributeMap.empty[ColumnStat] } ``` ########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/UnionEstimation.scala ########## @@ -0,0 +1,97 @@ +/* + * 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.catalyst.plans.logical.statsEstimation + +import scala.collection.mutable.ArrayBuffer + +import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeMap} +import org.apache.spark.sql.catalyst.plans.logical.{ColumnStat, Statistics, Union} +import org.apache.spark.sql.types.{ByteType, DataType, DateType, DecimalType, DoubleType, FloatType, IntegerType, LongType, ShortType, TimestampType} + +/** + * Estimate the number of output rows by doing the sum of output rows for each child of union, + * and estimate min and max stats for each column by finding the overall min and max of that + * column coming from its children. + */ +object UnionEstimation { + import EstimationUtils._ + + def compare(a: Any, b: Any, dataType: DataType): Boolean = { + dataType match { + case dt: IntegerType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: LongType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: FloatType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DoubleType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: ShortType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: ByteType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DateType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: TimestampType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DecimalType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case _ => false + } + } + + def estimate(union: Union): Option[Statistics] = { + val sizeInBytes = union.children.map(_.stats.sizeInBytes).sum + val outputRows: Option[BigInt] = if (rowCountsExist(union.children: _*)) { + Some(union.children.map(_.stats.rowCount.get).sum) + } else { + None + } + + val output = union.output + val outputAttrStats = new ArrayBuffer[(Attribute, ColumnStat)]() + + union.children.map(_.output).transpose.zipWithIndex.foreach { + case (attrs, outputIndex) => + val validStat = attrs.zipWithIndex.forall { + case (attr, childIndex) => + val attrStats = union.children(childIndex).stats.attributeStats + attrStats.get(attr).isDefined && attrStats(attr).hasMinMaxStats + } + if (validStat) { + val dataType = output(outputIndex).dataType + val minStart: Option[Any] = None + val maxStart: Option[Any] = None + val result = attrs.zipWithIndex.foldLeft((minStart, maxStart)) { + case ((minVal, maxVal), (attr, childIndex)) => + val colStat = union.children(childIndex).stats.attributeStats(attr) + val min = if (minVal.isEmpty || compare(colStat.min.get, minVal.get, dataType)) { Review comment: It seems we don't need `minVal.isEmpty`. ########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/UnionEstimation.scala ########## @@ -0,0 +1,97 @@ +/* + * 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.catalyst.plans.logical.statsEstimation + +import scala.collection.mutable.ArrayBuffer + +import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeMap} +import org.apache.spark.sql.catalyst.plans.logical.{ColumnStat, Statistics, Union} +import org.apache.spark.sql.types.{ByteType, DataType, DateType, DecimalType, DoubleType, FloatType, IntegerType, LongType, ShortType, TimestampType} + +/** + * Estimate the number of output rows by doing the sum of output rows for each child of union, + * and estimate min and max stats for each column by finding the overall min and max of that + * column coming from its children. + */ +object UnionEstimation { + import EstimationUtils._ + + def compare(a: Any, b: Any, dataType: DataType): Boolean = { + dataType match { + case dt: IntegerType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: LongType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: FloatType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DoubleType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: ShortType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: ByteType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DateType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: TimestampType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case dt: DecimalType => dt.ordering.asInstanceOf[Ordering[Any]].lt(a, b) + case _ => false + } + } + + def estimate(union: Union): Option[Statistics] = { + val sizeInBytes = union.children.map(_.stats.sizeInBytes).sum + val outputRows: Option[BigInt] = if (rowCountsExist(union.children: _*)) { + Some(union.children.map(_.stats.rowCount.get).sum) + } else { + None + } + + val output = union.output + val outputAttrStats = new ArrayBuffer[(Attribute, ColumnStat)]() + + union.children.map(_.output).transpose.zipWithIndex.foreach { + case (attrs, outputIndex) => + val validStat = attrs.zipWithIndex.forall { + case (attr, childIndex) => + val attrStats = union.children(childIndex).stats.attributeStats + attrStats.get(attr).isDefined && attrStats(attr).hasMinMaxStats + } + if (validStat) { + val dataType = output(outputIndex).dataType + val minStart: Option[Any] = None + val maxStart: Option[Any] = None + val result = attrs.zipWithIndex.foldLeft((minStart, maxStart)) { Review comment: nit: `result` -> `minMaxValue` ########## File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/UnionEstimationSuite.scala ########## @@ -0,0 +1,194 @@ +/* + * 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.catalyst.statsEstimation + +import org.apache.spark.sql.catalyst.expressions.{AttributeMap, AttributeReference} +import org.apache.spark.sql.catalyst.plans.logical +import org.apache.spark.sql.catalyst.plans.logical.{ColumnStat, Union} +import org.apache.spark.sql.types.{ByteType, DateType, Decimal, DecimalType, DoubleType, FloatType, IntegerType, LongType, ShortType, TimestampType} + +class UnionEstimationSuite extends StatsEstimationTestBase { + + test("test row size estimation") { + val attrInt = AttributeReference("cint", IntegerType)() + + val sz: Option[BigInt] = Some(1024) Review comment: nit: `val sz = Some(BigInt(1024))`? ########## File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/UnionEstimationSuite.scala ########## @@ -0,0 +1,194 @@ +/* + * 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.catalyst.statsEstimation + +import org.apache.spark.sql.catalyst.expressions.{AttributeMap, AttributeReference} +import org.apache.spark.sql.catalyst.plans.logical +import org.apache.spark.sql.catalyst.plans.logical.{ColumnStat, Union} +import org.apache.spark.sql.types.{ByteType, DateType, Decimal, DecimalType, DoubleType, FloatType, IntegerType, LongType, ShortType, TimestampType} + +class UnionEstimationSuite extends StatsEstimationTestBase { + + test("test row size estimation") { + val attrInt = AttributeReference("cint", IntegerType)() + + val sz: Option[BigInt] = Some(1024) + val child1 = StatsTestPlan( + outputList = Seq(attrInt), + rowCount = 2, + attributeStats = AttributeMap(Nil), + size = sz) + + val child2 = StatsTestPlan( + outputList = Seq(attrInt), + rowCount = 2, + attributeStats = AttributeMap(Nil), + size = sz) + + val union = Union(Seq(child1, child2)) + val expectedStats = logical.Statistics(sizeInBytes = 2 * 1024, rowCount = Some(4)) + assert(union.stats === expectedStats) + } + + test("col stats estimation") { + val sz: Option[BigInt] = Some(1024) + + val attrInt = AttributeReference("cint", IntegerType)() + val attrDouble = AttributeReference("cdouble", DoubleType)() + val attrShort = AttributeReference("cshort", ShortType)() + val attrLong = AttributeReference("clong", LongType)() + val attrByte = AttributeReference("cbyte", ByteType)() + val attrFloat = AttributeReference("cfloat", FloatType)() + val attrDecimal = AttributeReference("cdecimal", DecimalType.SYSTEM_DEFAULT)() + val attrDate = AttributeReference("cdate", DateType)() + val attrTimestamp = AttributeReference("ctimestamp", TimestampType)() + + val s1: Short = 1 + val s2: Short = 4 + val b1: Byte = 1 + val b2: Byte = 4 + val columnInfo: AttributeMap[ColumnStat] = AttributeMap( + Seq( + attrInt -> ColumnStat( + distinctCount = Some(2), + min = Some(1), + max = Some(4), + nullCount = Some(0), + avgLen = Some(4), + maxLen = Some(4)), + attrDouble -> ColumnStat( + distinctCount = Some(2), + min = Some(5.0), + max = Some(4.0), + nullCount = Some(0), + avgLen = Some(4), + maxLen = Some(4)), + attrShort -> ColumnStat(min = Some(s1), max = Some(s2)), + attrLong -> ColumnStat(min = Some(1L), max = Some(4L)), + attrByte -> ColumnStat(min = Some(b1), max = Some(b2)), + attrFloat -> ColumnStat(min = Some(1.1f), max = Some(4.1f)), + attrDecimal -> ColumnStat(min = Some(Decimal(13.5)), max = Some(Decimal(19.5))), + attrDate -> ColumnStat(min = Some(1), max = Some(4)), + attrTimestamp -> ColumnStat(min = Some(1L), max = Some(4L)))) + + val s3: Short = 2 Review comment: nit: `val s3 = 2.toShort`? ########## File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/UnionEstimationSuite.scala ########## @@ -0,0 +1,194 @@ +/* + * 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.catalyst.statsEstimation + +import org.apache.spark.sql.catalyst.expressions.{AttributeMap, AttributeReference} +import org.apache.spark.sql.catalyst.plans.logical +import org.apache.spark.sql.catalyst.plans.logical.{ColumnStat, Union} +import org.apache.spark.sql.types.{ByteType, DateType, Decimal, DecimalType, DoubleType, FloatType, IntegerType, LongType, ShortType, TimestampType} + +class UnionEstimationSuite extends StatsEstimationTestBase { + + test("test row size estimation") { + val attrInt = AttributeReference("cint", IntegerType)() + + val sz: Option[BigInt] = Some(1024) + val child1 = StatsTestPlan( + outputList = Seq(attrInt), + rowCount = 2, + attributeStats = AttributeMap(Nil), + size = sz) + + val child2 = StatsTestPlan( + outputList = Seq(attrInt), + rowCount = 2, + attributeStats = AttributeMap(Nil), + size = sz) + + val union = Union(Seq(child1, child2)) + val expectedStats = logical.Statistics(sizeInBytes = 2 * 1024, rowCount = Some(4)) + assert(union.stats === expectedStats) + } + + test("col stats estimation") { + val sz: Option[BigInt] = Some(1024) Review comment: ditto ########## File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/UnionEstimationSuite.scala ########## @@ -0,0 +1,194 @@ +/* + * 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.catalyst.statsEstimation + +import org.apache.spark.sql.catalyst.expressions.{AttributeMap, AttributeReference} +import org.apache.spark.sql.catalyst.plans.logical +import org.apache.spark.sql.catalyst.plans.logical.{ColumnStat, Union} +import org.apache.spark.sql.types.{ByteType, DateType, Decimal, DecimalType, DoubleType, FloatType, IntegerType, LongType, ShortType, TimestampType} + +class UnionEstimationSuite extends StatsEstimationTestBase { + + test("test row size estimation") { + val attrInt = AttributeReference("cint", IntegerType)() + + val sz: Option[BigInt] = Some(1024) + val child1 = StatsTestPlan( + outputList = Seq(attrInt), + rowCount = 2, + attributeStats = AttributeMap(Nil), + size = sz) + + val child2 = StatsTestPlan( + outputList = Seq(attrInt), + rowCount = 2, + attributeStats = AttributeMap(Nil), + size = sz) + + val union = Union(Seq(child1, child2)) + val expectedStats = logical.Statistics(sizeInBytes = 2 * 1024, rowCount = Some(4)) + assert(union.stats === expectedStats) + } + + test("col stats estimation") { + val sz: Option[BigInt] = Some(1024) + + val attrInt = AttributeReference("cint", IntegerType)() + val attrDouble = AttributeReference("cdouble", DoubleType)() + val attrShort = AttributeReference("cshort", ShortType)() + val attrLong = AttributeReference("clong", LongType)() + val attrByte = AttributeReference("cbyte", ByteType)() + val attrFloat = AttributeReference("cfloat", FloatType)() + val attrDecimal = AttributeReference("cdecimal", DecimalType.SYSTEM_DEFAULT)() + val attrDate = AttributeReference("cdate", DateType)() + val attrTimestamp = AttributeReference("ctimestamp", TimestampType)() + + val s1: Short = 1 + val s2: Short = 4 + val b1: Byte = 1 + val b2: Byte = 4 + val columnInfo: AttributeMap[ColumnStat] = AttributeMap( + Seq( + attrInt -> ColumnStat( + distinctCount = Some(2), + min = Some(1), + max = Some(4), + nullCount = Some(0), + avgLen = Some(4), + maxLen = Some(4)), + attrDouble -> ColumnStat( + distinctCount = Some(2), + min = Some(5.0), + max = Some(4.0), + nullCount = Some(0), + avgLen = Some(4), + maxLen = Some(4)), + attrShort -> ColumnStat(min = Some(s1), max = Some(s2)), + attrLong -> ColumnStat(min = Some(1L), max = Some(4L)), + attrByte -> ColumnStat(min = Some(b1), max = Some(b2)), + attrFloat -> ColumnStat(min = Some(1.1f), max = Some(4.1f)), + attrDecimal -> ColumnStat(min = Some(Decimal(13.5)), max = Some(Decimal(19.5))), + attrDate -> ColumnStat(min = Some(1), max = Some(4)), + attrTimestamp -> ColumnStat(min = Some(1L), max = Some(4L)))) + + val s3: Short = 2 + val s4: Short = 6 + val b3: Byte = 2 Review comment: nit: `val b3 = 2.toByte`? ---------------------------------------------------------------- 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]
