beliefer commented on a change in pull request #27440: [SPARK-27951][SQL] 
Support ANSI SQL NTH_VALUE window function
URL: https://github.com/apache/spark/pull/27440#discussion_r376885203
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/windowExpressions.scala
 ##########
 @@ -465,6 +470,47 @@ case class Lag(input: Expression, offset: Expression, 
default: Expression)
   override val direction = Descending
 }
 
+/**
+ * The NthValue function returns the value of `input` at the row that is the 
`offset`th row of
+ * the window frame (counting from 1). Offsets start at 0, which is the 
current row. When the
+ * value of `input` is null at the `offset`th row or there is no such an 
`offset`th row, null
+ * is returned.
+ *
+ * @param input expression to evaluate `offset`th row of the window frame.
+ * @param offset rows to jump ahead in the partition.
+ */
+@ExpressionDescription(
+  usage = """
+    _FUNC_(input[, offset]) - Returns the value of `input` at the row that is 
the`offset`th row
+      of the window frame (counting from 1). If the value of `input` at the 
`offset`th row is
+      null, null is returned. If there is no such an offset row (e.g., when 
the offset is 10,
+      size of the window frame less than 10), null is returned.
+  """)
+case class NthValue(input: Expression, offset: Expression)
+    extends OffsetWindowFunction {
+
+  override val default = Literal(null)
+
+  override val isWholeBased = true
+
+  override val direction = Ascending
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+    val check = super.checkInputDataTypes()
+    if (check.isFailure) {
+      check
+    } else {
+      offset.eval() match {
+        case i: Int if i <= 0 => TypeCheckFailure(
+          s"The 'offset' argument of nth_value must be greater than zero but 
it is $i.")
 
 Review comment:
   'offset' must greater than zero.
   The description of Redshift
   ```
   offset
   Determines the row number relative to the first row in the window for which 
to return the expression. The offset can be a constant or an expression and 
must be a positive integer that is greater than 0.
   ```
   The description of Vertica
   `row‑number | Specifies the row to evaluate, where row‑number evaluates to 
an integer ≥ 1.`
   The description of Presto
   ` It is an error for the offset to be zero or negative.`
   The description of Oracle
   `n determines the nth row for which the measure value is to be returned. n 
can be a constant, bind variable, column, or an expression involving them, as 
long as it resolves to a positive integer. The function returns NULL if the 
data source window has fewer than n rows. If n is null, then the function 
returns an error.`
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to