[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-10-10 Thread gczsjdy
Github user gczsjdy closed the pull request at:

https://github.com/apache/spark/pull/17359


---

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



[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-04-10 Thread gczsjdy
Github user gczsjdy commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r110823102
  
--- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/NGrams.scala
 ---
@@ -0,0 +1,249 @@
+/*
+ * 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-04-10 Thread gczsjdy
Github user gczsjdy commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r110820889
  
--- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/NGrams.scala
 ---
@@ -0,0 +1,249 @@
+/*
+ * 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))),
--- End diff --

If the array contains null, n-gram of that array will be ambiguous to 
evaluate. Should we just ignore the null value? For example regard n-gram of 
`array(‘a’, null, 'b')` the same as `array(‘a’, 'b')`. Maybe we should 
refuse the array containing null.


---
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 n

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-04-09 Thread cloud-fan
Github user cloud-fan commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r110569002
  
--- 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 --

To be honest, I don't there is a way to guarantee no null values at plan 
compile time.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as we

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-04-09 Thread cloud-fan
Github user cloud-fan commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r110568699
  
--- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/NGrams.scala
 ---
@@ -0,0 +1,249 @@
+/*
+ * 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldab

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-04-09 Thread cloud-fan
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 

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-04-09 Thread cloud-fan
Github user cloud-fan commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r110568482
  
--- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/NGrams.scala
 ---
@@ -0,0 +1,249 @@
+/*
+ * 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))),
--- End diff --

why do we have special requirement about nullability?


---
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.
---

---

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread gczsjdy
Github user gczsjdy commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r10755
  
--- 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread gczsjdy
Github user gczsjdy commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107429936
  
--- 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 --

To add, Array of String with null will cause runtime exceptions.


---
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 

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread gczsjdy
Github user gczsjdy commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107429714
  
--- 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 --

Yes, actually I want to refuse users' input with Array of String with null 
so I didn't include it in `inputTypes`, but the tests I have done showed me 
that some Array without null will be of type `A

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread gczsjdy
Github user gczsjdy commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107428643
  
--- 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]
--- End diff --

Right, thanks


---
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



[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread gczsjdy
Github user gczsjdy commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107418753
  
--- 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread gczsjdy
Github user gczsjdy commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107417310
  
--- 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread gczsjdy
Github user gczsjdy commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107415412
  
--- 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107353837
  
--- 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable 

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107353470
  
--- 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]
--- End diff --

also k.


---
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



[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107353157
  
--- 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable 

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107352740
  
--- 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable 

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107351551
  
--- 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable 

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107350936
  
--- 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable 

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107350129
  
--- 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable 

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107350167
  
--- 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable 

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107349865
  
--- 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable 

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107349720
  
--- 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]
--- End diff --

n can't be <= 0.


---
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



[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107349156
  
--- 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 --

`ArrayType(StringType, true)` is not included in `inputTypes`?


---
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

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107348955
  
--- 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 --

nvm...


---
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 feat

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107348550
  
--- 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable 

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107348281
  
--- 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable 

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107347983
  
--- 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable 

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread gczsjdy
Github user gczsjdy commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107347663
  
--- 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
--- End diff --

Yes, please refer to 
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF and 
https://cwiki.apache.org/confluence/display/Hive/StatisticsAndDataMining#StatisticsAndDataMining-Usage


---
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



[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107347661
  
--- 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable 

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107347431
  
--- 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)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+val defaultCheck = super.checkInputDataTypes()
+if (defaultCheck.isFailure) {
+  defaultCheck
+} else if (!nExpression.foldable 

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107347252
  
--- 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 --

Are you meaning `ArrayType(ArrayType(StringType, false))` here?


---
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 no

[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread gczsjdy
Github user gczsjdy commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107345616
  
--- 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},
--- End diff --

Yeah, and actually it's 'bigram' because n equals to 2 here.


---
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



[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107344830
  
--- 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
--- End diff --

does Hive also support `arrays of arrays of strings`?


---
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



[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107343838
  
--- 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},
--- End diff --

oh. i see. your unit of unigram is each element in the array, not a 
character.


---
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



[GitHub] spark pull request #17359: [SPARK-20028][SQL] Add aggreagate expression nGra...

2017-03-22 Thread viirya
Github user viirya commented on a diff in the pull request:

https://github.com/apache/spark/pull/17359#discussion_r107343519
  
--- 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},
--- End diff --

hmm, doesn't "abc" show 3 times?


---
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