Re: [PR] [SPARK-42789][SQL] Rewrite multiple GetJsonObject that consumes same JSON to single JsonTuple [spark]

2024-05-16 Thread via GitHub


github-actions[bot] closed pull request #45020: [SPARK-42789][SQL] Rewrite 
multiple GetJsonObject that consumes same JSON to single JsonTuple
URL: https://github.com/apache/spark/pull/45020


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

To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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



Re: [PR] [SPARK-42789][SQL] Rewrite multiple GetJsonObject that consumes same JSON to single JsonTuple [spark]

2024-05-16 Thread via GitHub


AngersZh commented on code in PR #45020:
URL: https://github.com/apache/spark/pull/45020#discussion_r1602762575


##
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteGetJsonObject.scala:
##
@@ -0,0 +1,81 @@
+/*
+ * 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.optimizer
+
+import scala.collection.mutable
+
+import org.apache.spark.sql.catalyst.expressions.{AttributeReference, 
Expression, GetJsonObject, JsonTuple, NamedExpression}
+import org.apache.spark.sql.catalyst.plans.logical.{Generate, LogicalPlan, 
Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.GET_JSON_OBJECT
+import org.apache.spark.sql.internal.SQLConf.REWRITE_TO_JSON_TUPLE_THRESHOLD
+import org.apache.spark.sql.types.StringType
+
+/**
+ * This rule rewrites multiple GetJsonObjects to a JsonTuple if their json 
expression is the same.
+ */
+object RewriteGetJsonObject extends Rule[LogicalPlan] {
+  def apply(plan: LogicalPlan): LogicalPlan = 
conf.getConf(REWRITE_TO_JSON_TUPLE_THRESHOLD) match {
+case None => plan
+case Some(threshold) =>
+  plan.transformUpWithPruning(_.containsPattern(GET_JSON_OBJECT), ruleId) 
{ case p: Project =>
+val getJsonObjects = p.projectList.flatMap {
+  _.collect { case gjo: GetJsonObject if 
gjo.rewriteToJsonTuplePath.nonEmpty => gjo }
+}

Review Comment:
   If there are some if/else such expression, just extract get_json_object will 
causing heavy performance regression



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

To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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



Re: [PR] [SPARK-42789][SQL] Rewrite multiple GetJsonObject that consumes same JSON to single JsonTuple [spark]

2024-05-15 Thread via GitHub


github-actions[bot] commented on PR #45020:
URL: https://github.com/apache/spark/pull/45020#issuecomment-2113687220

   We're closing this PR because it hasn't been updated in a while. This isn't 
a judgement on the merit of the PR in any way. It's just a way of keeping the 
PR queue manageable.
   If you'd like to revive this PR, please reopen it and ask a committer to 
remove the Stale tag!


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

To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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



Re: [PR] [SPARK-42789][SQL] Rewrite multiple GetJsonObject that consumes same JSON to single JsonTuple [spark]

2024-02-04 Thread via GitHub


pan3793 commented on PR #45020:
URL: https://github.com/apache/spark/pull/45020#issuecomment-1926355059

   cc @wangyum @cloud-fan @yaooqinn @ulysses-you 


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

To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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



[PR] [SPARK-42789][SQL] Rewrite multiple GetJsonObject that consumes same JSON to single JsonTuple [spark]

2024-02-04 Thread via GitHub


pan3793 opened a new pull request, #45020:
URL: https://github.com/apache/spark/pull/45020

   This PR takes over https://github.com/apache/spark/pull/40419 as the 
original author @wangyum is busy with other works.
   
   
   
   ### What changes were proposed in this pull request?
   
   This PR adds a new rule to rewrite multiple `GetJsonObject` to one 
`JsonTuple` if their json expressions are the same and the path is `.`. 
For example:
   
   ```
   === Applying Rule 
org.apache.spark.sql.catalyst.optimizer.RewriteGetJsonObject ===
   !Project [get_json_object(a#0, $.c1) AS c1#3, get_json_object(a#0, $.c2) AS 
c2#4, get_json_object(b#1, $.c1) AS c3#5, get_json_object(b#1, $.c2) AS c4#6, 
get_json_object(c#2, $.c1) AS c5#7]   Project [c1#13 AS c1#3, c2#14 AS c2#4, 
c1#15 AS c3#5, c2#16 AS c4#6, get_json_object(c#2, $.c1) AS c5#7]
   !+- LocalRelation , [a#0, b#1, c#2]   

   +- Generate json_tuple(b#1, c1, c2), false, 
b, [c1#15, c2#16]
   !

  +- Generate json_tuple(a#0, c1, c2), 
false, a, [c1#13, c2#14]
   !

 +- LocalRelation , [a#0, b#1, 
c#2]
   ```
   
   Please note that it needs to rewrite the path, otherwise it will not get the 
correct value. For example:
   ```
   spark-sql (default)> SELECT get_json_object('{"name": "alice", "age": 5}', 
'$.name') AS name;
   alice
   spark-sql (default)> SELECT name lateral view json_tuple('{"name": "alice", 
"age": 5}', '$.name') a AS name;
   NULL
   spark-sql (default)> select name lateral view json_tuple('{"name": "alice", 
"age": 5}', 'name') a AS name;
   alice
   ```
   
   ### Why are the changes needed?
   
   Improve query performance by eliminating multiple-time expensive JSON 
parsing.
   
   ### Does this PR introduce _any_ user-facing change?
   
   A new configuration 
`spark.sql.optimizer.rewriteGetJsonObjectToJsonTupleThreshold` is introduced, 
while the default value `None` keeps the current behavior.
   
   ### How was this patch tested?
   
   Unit test and benchmark test.
   
   Benchmark result:
   
   No. of GetJsonObjects | Default(ms) | Rewrite to JsonTuple(ms) | Relative
   -- | -- | -- | --
   2 | 37914 | 24887 | 1.5X
   3 | 52862 | 26752 | 2.0X
   4 | 71680 | 28452 | 2.5X
   5 | 108479 | 36830 | 2.9X
   10 | 153952 | 32436 | 4.7X
   15 | 224950 | 34806 | 6.5X
   20 | 293155 | 38148 | 7.7X
   25 | 366037 | 45725 | 8.0X
   30 | 439567 | 52949 | 8.3X
   35 | 526481 | 60586 | 8.7X
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   No.


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

To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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