Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17359#discussion_r110568613
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/NGrams.scala
 ---
    @@ -0,0 +1,258 @@
    +/*
    + * 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.expressions.aggregate
    +
    +import java.nio.ByteBuffer
    +import java.util.HashMap
    +
    +import org.apache.spark.serializer.KryoSerializer
    +import org.apache.spark.SparkConf
    +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
    +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult._
    +import org.apache.spark.sql.catalyst.expressions.{Expression, 
ExpressionDescription, ImplicitCastInputTypes, Literal}
    +import 
org.apache.spark.sql.catalyst.expressions.aggregate.NGrams.NGramBuffer
    +import org.apache.spark.sql.catalyst.InternalRow
    +import org.apache.spark.sql.catalyst.util.{ArrayBasedMapData, 
GenericArrayData}
    +import org.apache.spark.sql.types._
    +import org.apache.spark.unsafe.types.UTF8String
    +
    +/**
    + * Return the top-k n-grams in rows that consist of sequences of strings.
    + */
    +@ExpressionDescription(
    +  usage = """
    +    _FUNC_(expr, n, k, accuracy) - Estimates the top-k n-grams in rows 
that consist of sequences
    +      of strings, represented as arrays of strings, or arrays of arrays of 
strings. 'accuracy' is an
    +      optional precision factor that controls memory usage.
    +      The parameter 'n' specifies what type of n-grams are being 
estimated. Unigrams are n = 1, and
    +      bigrams are n = 2. Generally, n will not be greater than about 5. 
The 'k' parameter specifies
    +      how many of the highest-frequency n-grams will be returned by the 
UDAF. The optional precision
    +      factor 'accuracy' specifies how much memory to use for estimation; 
more memory will give
    +      more accurate frequency counts, but could crash the JVM. The value 
will be the max between
    +      'accuracy'(0 if it's not specified) and 1000/k, which indicates the 
max number of n-grams
    +      which are kept in the internal HashMap.
    +      The output is an array of maps with the top-k n-grams and 
corresponding frequency.
    +  """,
    +  extended = """
    +    Examples:
    +      > SELECT ngrams(array("abc", "abc", "bcd", "abc", "bcd"), 2, 4);
    +       [{["abc","bcd"]:2.0},
    +       {["abc","abc"]:1.0},
    +       {["bcd","abc"]:1.0}]
    +  """)
    +case class NGrams(
    +    child: Expression,
    +    nExpression: Expression,
    +    kExpression: Expression,
    +    accuracyExpression: Expression,
    +    override val mutableAggBufferOffset: Int,
    +    override val inputAggBufferOffset: Int)
    +  extends TypedImperativeAggregate[NGramBuffer] with 
ImplicitCastInputTypes  {
    +
    +  def this(child: Expression, nExpression: Expression, kExpression: 
Expression,
    +           accuracyExpression: Expression) = {
    +    this(child, nExpression, kExpression, NGrams.getAccuracy(kExpression, 
accuracyExpression), 0, 0)
    +  }
    +
    +  def this(child: Expression, nExpression: Expression, kExpression: 
Expression) = {
    +    this(child, nExpression, kExpression, Literal(0))
    +  }
    +
    +  private lazy val n: Int = nExpression.eval().asInstanceOf[Int]
    +  private lazy val k: Int = kExpression.eval().asInstanceOf[Int]
    +  private lazy val accuracy: Int = 
accuracyExpression.eval().asInstanceOf[Int]
    +
    +  override def inputTypes: Seq[AbstractDataType] = {
    +    Seq(TypeCollection(ArrayType(StringType, false), 
ArrayType(ArrayType(StringType, false))),
    +      IntegerType, IntegerType, IntegerType)
    +  }
    +
    +  val isArrayOfString = child.dataType == ArrayType(StringType, false) ||
    +    child.dataType == ArrayType(StringType, true)
    --- End diff --
    
    what's the behavior of hive? The `ArrayType.containsNull` is just a hint, 
there may be no null values even the `containsNull` is true.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to