-
Notifications
You must be signed in to change notification settings - Fork 28.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-49766][SQL] Codegen Support for
json_array_length
(by `Invok…
…e` & `RuntimeReplaceable`) ### What changes were proposed in this pull request? The pr aims to add `Codegen` Support for `json_array_length`. ### Why are the changes needed? - improve codegen coverage. - simplified code. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Pass GA & Existed UT (eg: JsonFunctionsSuite#`json_array_length function`) ### Was this patch authored or co-authored using generative AI tooling? No. Closes #48224 from panbingkun/SPARK-49766. Authored-by: panbingkun <[email protected]> Signed-off-by: Max Gekk <[email protected]>
- Loading branch information
1 parent
6734d48
commit 1244c5a
Showing
3 changed files
with
73 additions
and
41 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...yst/src/main/java/org/apache/spark/sql/catalyst/expressions/json/JsonExpressionUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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.json; | ||
|
||
import java.io.IOException; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
|
||
import org.apache.spark.sql.catalyst.expressions.SharedFactory; | ||
import org.apache.spark.sql.catalyst.json.CreateJacksonParser; | ||
import org.apache.spark.unsafe.types.UTF8String; | ||
|
||
public class JsonExpressionUtils { | ||
|
||
public static Integer lengthOfJsonArray(UTF8String json) { | ||
// return null for null input | ||
if (json == null) { | ||
return null; | ||
} | ||
try (JsonParser jsonParser = | ||
CreateJacksonParser.utf8String(SharedFactory.jsonFactory(), json)) { | ||
if (jsonParser.nextToken() == null) { | ||
return null; | ||
} | ||
// Only JSON array are supported for this function. | ||
if (jsonParser.currentToken() != JsonToken.START_ARRAY) { | ||
return null; | ||
} | ||
// Parse the array to compute its length. | ||
int length = 0; | ||
// Keep traversing until the end of JSON array | ||
while (jsonParser.nextToken() != JsonToken.END_ARRAY) { | ||
length += 1; | ||
// skip all the child of inner object or array | ||
jsonParser.skipChildren(); | ||
} | ||
return length; | ||
} catch (IOException e) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../common/src/test/resources/query-tests/explain-results/function_json_array_length.explain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Project [json_array_length(g#0) AS json_array_length(g)#0] | ||
Project [static_invoke(JsonExpressionUtils.lengthOfJsonArray(g#0)) AS json_array_length(g)#0] | ||
+- LocalRelation <empty>, [id#0L, a#0, b#0, d#0, e#0, f#0, g#0] |