[GitHub] [spark] iRakson commented on a change in pull request #27759: [SPARK-31008][SQL] Support json_array_length function

2020-04-06 Thread GitBox
iRakson commented on a change in pull request #27759: [SPARK-31008][SQL] 
Support json_array_length function
URL: https://github.com/apache/spark/pull/27759#discussion_r403847787
 
 

 ##
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
 ##
 @@ -796,3 +796,75 @@ case class SchemaOfJson(
 
   override def prettyName: String = "schema_of_json"
 }
+
+/**
+ * A function that returns the number of elements in the outmost JSON array.
+ */
+@ExpressionDescription(
+  usage = "_FUNC_(jsonArray) - Returns the number of elements in the outmost 
JSON array.",
+  arguments = """
+Arguments:
+  * jsonArray - A JSON array. An exception is thrown if any other valid 
JSON strings are passed.
+  `NULL` is returned in case of `NULL` or an invalid JSON.
+  """,
+  examples = """
+Examples:
+  > SELECT _FUNC_('[1,2,3,4]');
+4
+  > SELECT _FUNC_('[1,2,3,{"f1":1,"f2":[5,6]},4]');
+5
+  > SELECT _FUNC_('[1,2');
+NULL
+  """,
+  since = "3.1.0"
+)
+case class LengthOfJsonArray(child: Expression) extends UnaryExpression
+  with CodegenFallback with ExpectsInputTypes {
+
+  override def inputTypes: Seq[DataType] = Seq(StringType)
+  override def dataType: DataType = IntegerType
+  override def nullable: Boolean = true
+  override def prettyName: String = "json_array_length"
+
+  override def eval(input: InternalRow): Any = {
+val json = child.eval(input).asInstanceOf[UTF8String]
+// return null for null input
+if (json == null) {
+  return null
+}
+
+try {
+  
Utils.tryWithResource(CreateJacksonParser.utf8String(SharedFactory.jsonFactory, 
json)) {
+parser => {
+  // return null if null array is encountered.
+  if (parser.nextToken() == null) {
+return null
+  }
+  // Parse the array to compute its length.
+  parseCounter(parser, input)
+}
+  }
+} catch {
+  case _: JsonProcessingException | _: IOException => null
+}
+  }
+
+  private def parseCounter(parser: JsonParser, input: InternalRow): Int = {
+var length = 0
+// Only JSON array are supported for this function.
+if (parser.currentToken != JsonToken.START_ARRAY) {
+  throw new IllegalArgumentException(s"$prettyName can only be called on 
JSON array.")
+}
+// Keep traversing until the end of JSON array
+while(parser.nextToken() != JsonToken.END_ARRAY) {
+  // Null indicates end of input.
+  if (parser.currentToken == null) {
+throw new IllegalArgumentException("Please provide a valid JSON 
array.")
 
 Review comment:
   Yeah. It is unreachable code. Because if we encounter null before 
`END_ARRAY` then our JSON is invalid. I will remove this check.


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:
us...@infra.apache.org


With regards,
Apache Git Services

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



[GitHub] [spark] iRakson commented on a change in pull request #27759: [SPARK-31008][SQL] Support json_array_length function

2020-04-04 Thread GitBox
iRakson commented on a change in pull request #27759: [SPARK-31008][SQL] 
Support json_array_length function
URL: https://github.com/apache/spark/pull/27759#discussion_r403515624
 
 

 ##
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
 ##
 @@ -796,3 +796,75 @@ case class SchemaOfJson(
 
   override def prettyName: String = "schema_of_json"
 }
+
+/**
+ * A function that returns the number of elements in outer JSON array.
+ */
+@ExpressionDescription(
+  usage = "_FUNC_(jsonArray) - Returns the number of elements in outer JSON 
array.",
+  arguments = """
+Arguments:
+  * jsonArray - A JSON array. An exception is thrown if any other valid 
JSON strings are passed.
+  `NULL` is returned in case of an invalid JSON.
+  """,
+  examples = """
+Examples:
+  > SELECT _FUNC_('[1,2,3,4]');
+4
+  > SELECT _FUNC_('[1,2,3,{"f1":1,"f2":[5,6]},4]');
+5
+  > SELECT _FUNC_('[1,2');
+NULL
+  """,
+  since = "3.1.0"
+)
+case class LengthOfJsonArray(child: Expression) extends UnaryExpression
+  with CodegenFallback with ExpectsInputTypes {
+
+  override def inputTypes: Seq[AbstractDataType] = Seq(StringType)
+  override def dataType: DataType = IntegerType
+  override def nullable: Boolean = true
+  override def prettyName: String = "json_array_length"
+
+  override def eval(input: InternalRow): Any = {
+val json = child.eval(input).asInstanceOf[UTF8String]
+// return null for null input
+if (json == null) {
+  return null
+}
+
+try {
+  
Utils.tryWithResource(CreateJacksonParser.utf8String(SharedFactory.jsonFactory, 
json)) {
+parser => {
+  // return null if null array is encountered.
+  if (parser.nextToken() == null) {
+return null
+  }
+  // Parse the array to compute its length.
+  parseCounter(parser, input)
+}
+  }
+} catch {
+  case _: JsonProcessingException | _: IOException => null
+}
+  }
+
+  private def parseCounter(parser: JsonParser, input: InternalRow): Int = {
+var length = 0;
+// Only JSON array are supported for this function.
+if (parser.currentToken != JsonToken.START_ARRAY) {
+  throw new IllegalArgumentException(s"$prettyName can only be called on 
JSON array.")
+}
+// Keep traversing until the end of JSON array
+while(parser.nextToken() != JsonToken.END_ARRAY) {
+  // Null indicates end of input.
+  if (parser.currentToken == null) {
+throw new IllegalArgumentException("Please provide a valid JSON 
array.")
+  }
+  length += 1
+  // skip all the child of inner object or array
+  parser.skipChildren()
 
 Review comment:
   Yes.
   Actually Jackson parser will throw JsonProcessingException in case of 
invalid JSON. So, it is not required to check here. 


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:
us...@infra.apache.org


With regards,
Apache Git Services

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



[GitHub] [spark] iRakson commented on a change in pull request #27759: [SPARK-31008][SQL] Support json_array_length function

2020-04-04 Thread GitBox
iRakson commented on a change in pull request #27759: [SPARK-31008][SQL] 
Support json_array_length function
URL: https://github.com/apache/spark/pull/27759#discussion_r403478544
 
 

 ##
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
 ##
 @@ -796,3 +796,75 @@ case class SchemaOfJson(
 
   override def prettyName: String = "schema_of_json"
 }
+
+/**
+ * A function that returns the number of elements in outer JSON array.
+ */
+@ExpressionDescription(
+  usage = "_FUNC_(jsonArray) - Returns the number of elements in outer JSON 
array.",
+  arguments = """
+Arguments:
+  * jsonArray - A JSON array. An exception is thrown if any other valid 
JSON strings are passed.
+  `NULL` is returned in case of an invalid JSON.
+  """,
+  examples = """
+Examples:
+  > SELECT _FUNC_('[1,2,3,4]');
+4
+  > SELECT _FUNC_('[1,2,3,{"f1":1,"f2":[5,6]},4]');
+5
+  > SELECT _FUNC_('[1,2');
+NULL
+  """,
+  since = "3.1.0"
+)
+case class LengthOfJsonArray(child: Expression) extends UnaryExpression
+  with CodegenFallback with ExpectsInputTypes {
+
+  override def inputTypes: Seq[AbstractDataType] = Seq(StringType)
 
 Review comment:
   `AbstractDataType` -> `DataType` 
   I will fix this while handling review comments, if any.


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:
us...@infra.apache.org


With regards,
Apache Git Services

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



[GitHub] [spark] iRakson commented on a change in pull request #27759: [SPARK-31008][SQL] Support json_array_length function

2020-04-04 Thread GitBox
iRakson commented on a change in pull request #27759: [SPARK-31008][SQL] 
Support json_array_length function
URL: https://github.com/apache/spark/pull/27759#discussion_r403477043
 
 

 ##
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
 ##
 @@ -781,3 +781,68 @@ case class SchemaOfJson(
 
   override def prettyName: String = "schema_of_json"
 }
+
+/**
+ * A function that returns the number of elements in outer JSON array.
+ */
+@ExpressionDescription(
+  usage = "_FUNC_(jsonArray) - Returns the number of elements in outer JSON 
array.",
+  arguments = """
+Arguments:
+  * jsonArray - A JSON array. An Exception is thrown if any other valid 
JSON strings are passed.
+  `NULL` is returned in case of invalid JSON.
+  """,
+  examples = """
+Examples:
+  > SELECT _FUNC_('[1,2,3,4]');
+4
+  > SELECT _FUNC_('[1,2,3,{"f1":1,"f2":[5,6]},4]');
+5
+  > SELECT _FUNC_('[1,2');
+NULL
+  """,
+  since = "3.1.0"
+)
+case class LengthOfJsonArray(child: Expression)
+  extends UnaryExpression with CodegenFallback {
+  override def dataType: DataType = IntegerType
+  override def nullable: Boolean = true
+  override def prettyName: String = "json_array_length"
+
+  override def eval(input: InternalRow): Any = {
+val json = child.eval(input).asInstanceOf[UTF8String]
+try {
+  
Utils.tryWithResource(CreateJacksonParser.utf8String(SharedFactory.jsonFactory, 
json)) {
+parser => {
+  // return null if null array is encountered.
+  if (parser.nextToken() == null) {
+return null
+  }
+  // Parse the array to compute its length.
+  parseCounter(parser, input)
+}
+  }
+} catch {
+  case _: JsonProcessingException | _: IOException => null
 
 Review comment:
   I think we do not need to catch `NullPointerException` here.


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:
us...@infra.apache.org


With regards,
Apache Git Services

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



[GitHub] [spark] iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support json_array_length function

2020-03-10 Thread GitBox
iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support 
json_array_length function
URL: https://github.com/apache/spark/pull/27759#discussion_r390425608
 
 

 ##
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
 ##
 @@ -781,3 +782,68 @@ case class SchemaOfJson(
 
   override def prettyName: String = "schema_of_json"
 }
+
+/**
+ * A function that returns the number of elements in outer JSON Array.
+ */
+@ExpressionDescription(
+  usage = "_FUNC_(jsonArray) - Returns the number of elements in outer JSON 
Array.",
+  arguments = """
+Arguments:
+  * jsonArray - A JSON array is required as argument. An Exception is 
thrown if any
+  other valid JSON strings are passed. `NULL` is returned in case of 
invalid JSON.
 
 Review comment:
   Ideally we should. I just returned `NULL` because other JSON functions 
returns `NULL` as well.


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:
us...@infra.apache.org


With regards,
Apache Git Services

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



[GitHub] [spark] iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support json_array_length function

2020-03-10 Thread GitBox
iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support 
json_array_length function
URL: https://github.com/apache/spark/pull/27759#discussion_r390368102
 
 

 ##
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
 ##
 @@ -781,3 +782,68 @@ case class SchemaOfJson(
 
   override def prettyName: String = "schema_of_json"
 }
+
+/**
+ * A function that returns the number of elements in outer JSON Array.
+ */
+@ExpressionDescription(
+  usage = "_FUNC_(jsonArray) - Returns the number of elements in outer JSON 
Array.",
+  arguments = """
+Arguments:
+  * jsonArray - A JSON array is required as argument. An Exception is 
thrown if any
+  other valid JSON strings are passed. `NULL` is returned in case of 
invalid JSON.
 
 Review comment:
   Won't it be good if we inform users that this function accepts JSON Array 
only?


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:
us...@infra.apache.org


With regards,
Apache Git Services

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



[GitHub] [spark] iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support json_array_length function

2020-03-10 Thread GitBox
iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support 
json_array_length function
URL: https://github.com/apache/spark/pull/27759#discussion_r390368102
 
 

 ##
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
 ##
 @@ -781,3 +782,68 @@ case class SchemaOfJson(
 
   override def prettyName: String = "schema_of_json"
 }
+
+/**
+ * A function that returns the number of elements in outer JSON Array.
+ */
+@ExpressionDescription(
+  usage = "_FUNC_(jsonArray) - Returns the number of elements in outer JSON 
Array.",
+  arguments = """
+Arguments:
+  * jsonArray - A JSON array is required as argument. An Exception is 
thrown if any
+  other valid JSON strings are passed. `NULL` is returned in case of 
invalid JSON.
 
 Review comment:
   Won't it be good if we inform users that this function excepts JSON Array 
only?


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:
us...@infra.apache.org


With regards,
Apache Git Services

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



[GitHub] [spark] iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support json_array_length function

2020-03-04 Thread GitBox
iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support 
json_array_length function
URL: https://github.com/apache/spark/pull/27759#discussion_r388082157
 
 

 ##
 File path: sql/core/src/test/resources/sql-tests/inputs/json-functions.sql
 ##
 @@ -58,5 +58,16 @@ select schema_of_json('{"c1":01, "c2":0.1}', 
map('allowNumericLeadingZeros', 'tr
 select schema_of_json(null);
 CREATE TEMPORARY VIEW jsonTable(jsonField, a) AS SELECT * FROM VALUES ('{"a": 
1, "b": 2}', 'a');
 SELECT schema_of_json(jsonField) FROM jsonTable;
+
+-- json_array_length
+select json_array_length('');
+select json_array_length('[]');
+select json_array_length('[1,2,3]');
+select json_array_length('[[1,2],[5,6,7]]');
+select json_array_length('[{"a":123},{"b":"hello"}]');
+select json_array_length('[1,2,3,[33,44],{"key":[2,3,4]}]');
+select json_array_length('{"key":"not a json array"}');
 
 Review comment:
   I agree on the fact that `json_length` is generic. I defined it 
array-specific because most of the DBs support array-specific function only. 


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:
us...@infra.apache.org


With regards,
Apache Git Services

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



[GitHub] [spark] iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support json_array_length function

2020-03-04 Thread GitBox
iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support 
json_array_length function
URL: https://github.com/apache/spark/pull/27759#discussion_r388079357
 
 

 ##
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
 ##
 @@ -781,3 +782,67 @@ case class SchemaOfJson(
 
   override def prettyName: String = "schema_of_json"
 }
+
+/**
+ * A function that returns number of elements in outer Json Array.
+ */
+@ExpressionDescription(
+  usage = "_FUNC_(jsonArray) - Returns length of the jsonArray",
+  arguments = """
+jsonArray - A JSON array is required as argument. `Analysis Exception` is 
thrown if any other
+valid JSON expression is passed. `NULL` is returned in case of invalid 
JSON.
 
 Review comment:
   ok. I will make the changes


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:
us...@infra.apache.org


With regards,
Apache Git Services

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



[GitHub] [spark] iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support json_array_length function

2020-03-04 Thread GitBox
iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support 
json_array_length function
URL: https://github.com/apache/spark/pull/27759#discussion_r387471894
 
 

 ##
 File path: sql/core/src/main/scala/org/apache/spark/sql/functions.scala
 ##
 @@ -3954,6 +3954,17 @@ object functions {
   def to_json(e: Column): Column =
 to_json(e, Map.empty[String, String])
 
+  /**
 
 Review comment:
   I believe it should be allowed to be used directly. 


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:
us...@infra.apache.org


With regards,
Apache Git Services

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



[GitHub] [spark] iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support json_array_length function

2020-03-04 Thread GitBox
iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support 
json_array_length function
URL: https://github.com/apache/spark/pull/27759#discussion_r387616235
 
 

 ##
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
 ##
 @@ -781,3 +782,65 @@ case class SchemaOfJson(
 
   override def prettyName: String = "schema_of_json"
 }
+
+/**
+ * A function that returns number of elements in outer Json Array.
+ */
+@ExpressionDescription(
+  usage = "_FUNC_(jsonArray) - Returns length of the jsonArray",
+  arguments = """
+jsonArray - A JSON array is required as argument. `Analysis Exception` is 
thrown if any other
+valid JSON expression is passed. `NULL` is returned in case of invalid 
JSON.
+  """,
+  examples = """
+Examples:
+> SELECT _FUNC_('[1,2,3,4]');
+  4
+> SELECT _FUNC_('[1,2,3,{"f1":1,"f2":[5,6]},4]');
+  5
+> SELECT _FUNC_('[1,2');
+  NULL
+  """,
+  since = "3.1.0"
+)
+case class LengthOfJsonArray(child: Expression)
+  extends UnaryExpression with CodegenFallback {
+  override def dataType: DataType = IntegerType
+  override def nullable: Boolean = true
+  override def prettyName: String = "json_array_length"
+
+  override def eval(input: InternalRow): Any = {
+@transient
+val json = child.eval(input).asInstanceOf[UTF8String]
+try {
+  
Utils.tryWithResource(CreateJacksonParser.utf8String(SharedFactory.jsonFactory, 
json)) {
+parser => {
+  // return null if null array is encountered.
+  if (parser.nextToken() == null) {
+return null
+  }
+  // Parse the array to compute its length.
+  parseCounter(parser, input)
+}
+  }
+} catch {
+  case _: JsonProcessingException => null
+}
+  }
+
+  private def parseCounter(parser: JsonParser, input: InternalRow): Int = {
+// Counter for length of array
+var array_length: Int = 0;
+// Only json array are supported for this function.
+if (parser.getCurrentToken != JsonToken.START_ARRAY) {
+  throw new AnalysisException(s"$prettyName can only be called on Json 
Array.")
+}
+// Keep traversing until the end of Json Array
+while(parser.nextToken() != JsonToken.END_ARRAY) {
 
 Review comment:
   It returns null when end of input is reached.
   If it returns null before returning `END_ARRAY` then our json is invalid. 
Invalid input was already handled.
   Anyway now i will add one more check for null.


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:
us...@infra.apache.org


With regards,
Apache Git Services

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



[GitHub] [spark] iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support json_array_length function

2020-03-04 Thread GitBox
iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support 
json_array_length function
URL: https://github.com/apache/spark/pull/27759#discussion_r387614304
 
 

 ##
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
 ##
 @@ -781,3 +782,65 @@ case class SchemaOfJson(
 
   override def prettyName: String = "schema_of_json"
 }
+
+/**
+ * A function that returns number of elements in outer Json Array.
+ */
+@ExpressionDescription(
+  usage = "_FUNC_(jsonArray) - Returns length of the jsonArray",
+  arguments = """
+jsonArray - A JSON array is required as argument. `Analysis Exception` is 
thrown if any other
+valid JSON expression is passed. `NULL` is returned in case of invalid 
JSON.
+  """,
+  examples = """
+Examples:
+> SELECT _FUNC_('[1,2,3,4]');
+  4
+> SELECT _FUNC_('[1,2,3,{"f1":1,"f2":[5,6]},4]');
 
 Review comment:
   Actually i wanted it to work like other `json_array_length` functions, which 
take any input and result length. I can change its implementation, if required.


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:
us...@infra.apache.org


With regards,
Apache Git Services

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



[GitHub] [spark] iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support json_array_length function

2020-03-04 Thread GitBox
iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support 
json_array_length function
URL: https://github.com/apache/spark/pull/27759#discussion_r387613709
 
 

 ##
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
 ##
 @@ -781,3 +782,65 @@ case class SchemaOfJson(
 
   override def prettyName: String = "schema_of_json"
 }
+
+/**
+ * A function that returns number of elements in outer Json Array.
+ */
+@ExpressionDescription(
+  usage = "_FUNC_(jsonArray) - Returns length of the jsonArray",
+  arguments = """
+jsonArray - A JSON array is required as argument. `Analysis Exception` is 
thrown if any other
+valid JSON expression is passed. `NULL` is returned in case of invalid 
JSON.
+  """,
+  examples = """
+Examples:
+> SELECT _FUNC_('[1,2,3,4]');
+  4
+> SELECT _FUNC_('[1,2,3,{"f1":1,"f2":[5,6]},4]');
+  5
+> SELECT _FUNC_('[1,2');
+  NULL
+  """,
+  since = "3.1.0"
+)
+case class LengthOfJsonArray(child: Expression)
+  extends UnaryExpression with CodegenFallback {
+  override def dataType: DataType = IntegerType
+  override def nullable: Boolean = true
+  override def prettyName: String = "json_array_length"
+
+  override def eval(input: InternalRow): Any = {
+@transient
+val json = child.eval(input).asInstanceOf[UTF8String]
+try {
+  
Utils.tryWithResource(CreateJacksonParser.utf8String(SharedFactory.jsonFactory, 
json)) {
+parser => {
+  // return null if null array is encountered.
+  if (parser.nextToken() == null) {
+return null
+  }
+  // Parse the array to compute its length.
+  parseCounter(parser, input)
+}
+  }
+} catch {
+  case _: JsonProcessingException => null
 
 Review comment:
   I will handle IOException. Missed that. Thanks for pointing out.


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:
us...@infra.apache.org


With regards,
Apache Git Services

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



[GitHub] [spark] iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support json_array_length function

2020-03-04 Thread GitBox
iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support 
json_array_length function
URL: https://github.com/apache/spark/pull/27759#discussion_r387613455
 
 

 ##
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
 ##
 @@ -781,3 +782,65 @@ case class SchemaOfJson(
 
   override def prettyName: String = "schema_of_json"
 }
+
+/**
+ * A function that returns number of elements in outer Json Array.
+ */
+@ExpressionDescription(
+  usage = "_FUNC_(jsonArray) - Returns length of the jsonArray",
+  arguments = """
+jsonArray - A JSON array is required as argument. `Analysis Exception` is 
thrown if any other
+valid JSON expression is passed. `NULL` is returned in case of invalid 
JSON.
+  """,
+  examples = """
+Examples:
+> SELECT _FUNC_('[1,2,3,4]');
+  4
+> SELECT _FUNC_('[1,2,3,{"f1":1,"f2":[5,6]},4]');
+  5
+> SELECT _FUNC_('[1,2');
+  NULL
+  """,
+  since = "3.1.0"
+)
+case class LengthOfJsonArray(child: Expression)
+  extends UnaryExpression with CodegenFallback {
+  override def dataType: DataType = IntegerType
+  override def nullable: Boolean = true
+  override def prettyName: String = "json_array_length"
+
+  override def eval(input: InternalRow): Any = {
+@transient
+val json = child.eval(input).asInstanceOf[UTF8String]
+try {
+  
Utils.tryWithResource(CreateJacksonParser.utf8String(SharedFactory.jsonFactory, 
json)) {
+parser => {
+  // return null if null array is encountered.
+  if (parser.nextToken() == null) {
+return null
+  }
+  // Parse the array to compute its length.
+  parseCounter(parser, input)
+}
+  }
+} catch {
+  case _: JsonProcessingException => null
+}
+  }
+
+  private def parseCounter(parser: JsonParser, input: InternalRow): Int = {
+// Counter for length of array
+var array_length: Int = 0;
 
 Review comment:
   I forgot to change the name. I will rename it and comment will be removed as 
well.


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:
us...@infra.apache.org


With regards,
Apache Git Services

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



[GitHub] [spark] iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support json_array_length function

2020-03-03 Thread GitBox
iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support 
json_array_length function
URL: https://github.com/apache/spark/pull/27759#discussion_r387471894
 
 

 ##
 File path: sql/core/src/main/scala/org/apache/spark/sql/functions.scala
 ##
 @@ -3954,6 +3954,17 @@ object functions {
   def to_json(e: Column): Column =
 to_json(e, Map.empty[String, String])
 
+  /**
 
 Review comment:
   I believe it should be allowed to be used directly. 


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:
us...@infra.apache.org


With regards,
Apache Git Services

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



[GitHub] [spark] iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support json_array_length function

2020-03-03 Thread GitBox
iRakson commented on a change in pull request #27759: [SPARK-31008][SQL]Support 
json_array_length function
URL: https://github.com/apache/spark/pull/27759#discussion_r387458054
 
 

 ##
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
 ##
 @@ -781,3 +782,59 @@ case class SchemaOfJson(
 
   override def prettyName: String = "schema_of_json"
 }
+
+/**
+ * A function that returns number of elements in outer Json Array.
+ */
+@ExpressionDescription(
+  usage = "_FUNC_(jsonArray) - Returns length of the jsonArray",
 
 Review comment:
   arguments description is added.


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:
us...@infra.apache.org


With regards,
Apache Git Services

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