jmalkin commented on code in PR #7: URL: https://github.com/apache/datasketches-spark/pull/7#discussion_r1906150680
########## src/main/scala/org/apache/spark/sql/aggregate/ThetaUnion.scala: ########## @@ -0,0 +1,133 @@ +/* + * 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.aggregate + +import org.apache.datasketches.memory.Memory +import org.apache.datasketches.theta.{Sketch, SetOperation} + +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.expressions.{ExpectsInputTypes, Expression, ExpressionDescription, Literal} +import org.apache.spark.sql.catalyst.expressions.aggregate.TypedImperativeAggregate +import org.apache.spark.sql.catalyst.trees.BinaryLike +import org.apache.spark.sql.types.{AbstractDataType, DataType, IntegerType, ThetaSketchWrapper, ThetaSketchType} +import org.apache.spark.SparkUnsupportedOperationException + +/** + * Theta Union operation. + * + * See [[https://datasketches.apache.org/docs/Theta/ThetaSketches.html]] for more information. + * + * @param child child expression, on which to perform the union operation Review Comment: not sure how useful javadoc comments are here, but it uses unarylike naming rather than binarylike ########## src/main/scala/org/apache/spark/sql/types/ThetaSketchType.scala: ########## @@ -0,0 +1,37 @@ +/* + * 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.types + +class ThetaSketchType extends UserDefinedType[ThetaSketchWrapper] { Review Comment: I think when we get to python compatibility we'll want to make sure the type shows up as some sort of ThetaSketch, not a wrapper ########## src/main/scala/org/apache/spark/sql/aggregate/ThetaSketchBuild.scala: ########## @@ -0,0 +1,137 @@ +/* + * 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.aggregate + +import org.apache.datasketches.theta.{UpdateSketch, SetOperation} +import org.apache.spark.SparkUnsupportedOperationException +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.expressions.{ExpectsInputTypes, Expression, ExpressionDescription, Literal} +import org.apache.spark.sql.catalyst.expressions.aggregate.TypedImperativeAggregate +import org.apache.spark.sql.catalyst.trees.BinaryLike +import org.apache.spark.sql.types.{AbstractDataType, DataType, IntegerType, LongType, NumericType, FloatType, DoubleType, ThetaSketchWrapper, ThetaSketchType} + +/** + * The ThetaSketchBuild function creates a Theta sketch from a column of values + * which can be used to estimate distinct count. + * + * See [[https://datasketches.apache.org/docs/Theta/ThetaSketches.html]] for more information. + * + * @param child child expression, from which to build a sketch + * @param lgk the size-accraucy trade-off parameter for the sketch + */ +// scalastyle:off line.size.limit +@ExpressionDescription( + usage = """ + _FUNC_(expr, lgk) - Creates a Theta Sketch and returns the binary representation. + `lgk` (optional, default: 12) the size-accuracy trade-off parameter.""", + examples = """ + Example: + > SELECT theta_sketch_get_estimate(_FUNC_(col, 12)) FROM VALUES (1), (2), (3), (4), (5) tab(col); + 5.0 + """, +) +// scalastyle:on line.size.limit +case class ThetaSketchBuild( + left: Expression, + right: Expression, + mutableAggBufferOffset: Int = 0, + inputAggBufferOffset: Int = 0) + extends TypedImperativeAggregate[ThetaSketchWrapper] + with BinaryLike[Expression] + with ExpectsInputTypes { + + lazy val lgk: Int = { + right.eval() match { + case null => 12 + case lgk: Int => lgk + case _ => throw new SparkUnsupportedOperationException( + s"Unsupported input type ${right.dataType.catalogString}", + Map("dataType" -> dataType.toString)) + } + } + + def this(child: Expression) = this(child, Literal(12), 0, 0) + def this(child: Expression, lgk: Expression) = this(child, lgk, 0, 0) + def this(child: Expression, lgk: Int) = this(child, Literal(lgk), 0, 0) + + override def withNewMutableAggBufferOffset(newMutableAggBufferOffset: Int): ThetaSketchBuild = + copy(mutableAggBufferOffset = newMutableAggBufferOffset) + + override def withNewInputAggBufferOffset(newInputAggBufferOffset: Int): ThetaSketchBuild = + copy(inputAggBufferOffset = newInputAggBufferOffset) + + override protected def withNewChildrenInternal(newLeft: Expression, newRight: Expression): ThetaSketchBuild = { + copy(left = newLeft, right = newRight) + } + + override def prettyName: String = "theta_sketch_build" + + override def dataType: DataType = ThetaSketchType + + override def nullable: Boolean = false + + override def inputTypes: Seq[AbstractDataType] = Seq(NumericType, IntegerType, LongType, FloatType, DoubleType) + + override def createAggregationBuffer(): ThetaSketchWrapper = new ThetaSketchWrapper(updateSketch = Some(UpdateSketch.builder().setLogNominalEntries(lgk).build())) + + override def update(wrapper: ThetaSketchWrapper, input: InternalRow): ThetaSketchWrapper = { + val value = left.eval(input) + if (value != null) { + left.dataType match { Review Comment: This seems borrowed from KLL and it won't handle things like strings, so that'll certainly need to change ########## src/main/scala/org/apache/spark/sql/aggregate/ThetaSketchBuild.scala: ########## @@ -0,0 +1,137 @@ +/* + * 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.aggregate + +import org.apache.datasketches.theta.{UpdateSketch, SetOperation} +import org.apache.spark.SparkUnsupportedOperationException +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.expressions.{ExpectsInputTypes, Expression, ExpressionDescription, Literal} +import org.apache.spark.sql.catalyst.expressions.aggregate.TypedImperativeAggregate +import org.apache.spark.sql.catalyst.trees.BinaryLike +import org.apache.spark.sql.types.{AbstractDataType, DataType, IntegerType, LongType, NumericType, FloatType, DoubleType, ThetaSketchWrapper, ThetaSketchType} + +/** + * The ThetaSketchBuild function creates a Theta sketch from a column of values + * which can be used to estimate distinct count. + * + * See [[https://datasketches.apache.org/docs/Theta/ThetaSketches.html]] for more information. + * + * @param child child expression, from which to build a sketch + * @param lgk the size-accraucy trade-off parameter for the sketch + */ +// scalastyle:off line.size.limit +@ExpressionDescription( + usage = """ + _FUNC_(expr, lgk) - Creates a Theta Sketch and returns the binary representation. + `lgk` (optional, default: 12) the size-accuracy trade-off parameter.""", + examples = """ + Example: + > SELECT theta_sketch_get_estimate(_FUNC_(col, 12)) FROM VALUES (1), (2), (3), (4), (5) tab(col); + 5.0 + """, +) +// scalastyle:on line.size.limit +case class ThetaSketchBuild( + left: Expression, + right: Expression, + mutableAggBufferOffset: Int = 0, + inputAggBufferOffset: Int = 0) + extends TypedImperativeAggregate[ThetaSketchWrapper] + with BinaryLike[Expression] + with ExpectsInputTypes { + + lazy val lgk: Int = { + right.eval() match { + case null => 12 + case lgk: Int => lgk + case _ => throw new SparkUnsupportedOperationException( + s"Unsupported input type ${right.dataType.catalogString}", + Map("dataType" -> dataType.toString)) + } + } + + def this(child: Expression) = this(child, Literal(12), 0, 0) + def this(child: Expression, lgk: Expression) = this(child, lgk, 0, 0) + def this(child: Expression, lgk: Int) = this(child, Literal(lgk), 0, 0) + + override def withNewMutableAggBufferOffset(newMutableAggBufferOffset: Int): ThetaSketchBuild = + copy(mutableAggBufferOffset = newMutableAggBufferOffset) + + override def withNewInputAggBufferOffset(newInputAggBufferOffset: Int): ThetaSketchBuild = + copy(inputAggBufferOffset = newInputAggBufferOffset) + + override protected def withNewChildrenInternal(newLeft: Expression, newRight: Expression): ThetaSketchBuild = { + copy(left = newLeft, right = newRight) + } + + override def prettyName: String = "theta_sketch_build" + + override def dataType: DataType = ThetaSketchType + + override def nullable: Boolean = false + + override def inputTypes: Seq[AbstractDataType] = Seq(NumericType, IntegerType, LongType, FloatType, DoubleType) Review Comment: The first item here should capture a notion of any type (not sure how) and there should be only 2 since the number of input expressions is 2 -- 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: dev-unsubscr...@datasketches.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@datasketches.apache.org For additional commands, e-mail: dev-h...@datasketches.apache.org