Re: [PR] [SPARK-48307][SQL] InlineCTE should keep not-inlined relations in the original WithCTE node [spark]

2024-06-04 Thread via GitHub


cloud-fan closed pull request #46617: [SPARK-48307][SQL] InlineCTE should keep 
not-inlined relations in the original WithCTE node
URL: https://github.com/apache/spark/pull/46617


-- 
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-48307][SQL] InlineCTE should keep not-inlined relations in the original WithCTE node [spark]

2024-06-04 Thread via GitHub


cloud-fan commented on PR #46617:
URL: https://github.com/apache/spark/pull/46617#issuecomment-2148488195

   thanks for the review, merging to master!


-- 
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-48307][SQL] InlineCTE should keep not-inlined relations in the original WithCTE node [spark]

2024-05-20 Thread via GitHub


cloud-fan commented on PR #46617:
URL: https://github.com/apache/spark/pull/46617#issuecomment-2121381949

   > Repartition? Do you mean Relation?
   
   It's Reparition, because we rely on shuffle reuse to reuse CTE relations.


-- 
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-48307][SQL] InlineCTE should keep not-inlined relations in the original WithCTE node [spark]

2024-05-20 Thread via GitHub


viirya commented on PR #46617:
URL: https://github.com/apache/spark/pull/46617#issuecomment-2120577446

   > This is not true anymore after https://github.com/apache/spark/pull/42036 
. It's not a big deal as we replace not-inlined CTE relations with Repartition 
during
   
   `Repartition? Do you mean Relation?


-- 
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-48307][SQL] InlineCTE should keep not-inlined relations in the original WithCTE node [spark]

2024-05-20 Thread via GitHub


viirya commented on code in PR #46617:
URL: https://github.com/apache/spark/pull/46617#discussion_r1606854563


##
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/InlineCTESuite.scala:
##
@@ -0,0 +1,42 @@
+/*
+ * 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 org.apache.spark.sql.catalyst.analysis.TestRelation
+import org.apache.spark.sql.catalyst.dsl.expressions._
+import org.apache.spark.sql.catalyst.dsl.plans._
+import org.apache.spark.sql.catalyst.plans.PlanTest
+import org.apache.spark.sql.catalyst.plans.logical.{AppendData, 
CTERelationDef, CTERelationRef, LogicalPlan, OneRowRelation, WithCTE}
+import org.apache.spark.sql.catalyst.rules.RuleExecutor
+
+class InlineCTESuite extends PlanTest {
+
+  object Optimize extends RuleExecutor[LogicalPlan] {
+val batches = Batch("inline CTE", FixedPoint(100), InlineCTE()) :: Nil
+  }
+
+  test("not-inlined CTE relation in command") {

Review Comment:
   ```suggestion
 test("SPARK-48307: not-inlined CTE relation in command") {
   ```



-- 
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-48307][SQL] InlineCTE should keep not-inlined relations in the original WithCTE node [spark]

2024-05-16 Thread via GitHub


amaliujia commented on code in PR #46617:
URL: https://github.com/apache/spark/pull/46617#discussion_r1603865663


##
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InlineCTE.scala:
##
@@ -74,34 +70,33 @@ case class InlineCTE(alwaysInline: Boolean = false) extends 
Rule[LogicalPlan] {
*
* @param plan The plan to collect the CTEs from
* @param cteMap A mutable map that accumulates the CTEs and their reference 
information by CTE
-   *   ids. The value of the map is tuple whose elements are:
-   *   - The CTE definition
-   *   - The number of incoming references to the CTE. This 
includes references from
-   * other CTEs and regular places.
-   *   - A mutable inner map that tracks outgoing references 
(counts) to other CTEs.
+   *   ids.
* @param outerCTEId While collecting the map we use this optional CTE id to 
identify the
*   current outer CTE.
*/
-  def buildCTEMap(
+  private def buildCTEMap(
   plan: LogicalPlan,
-  cteMap: mutable.Map[Long, (CTERelationDef, Int, mutable.Map[Long, Int])],
+  cteMap: mutable.Map[Long, CTEReferenceInfo],
   outerCTEId: Option[Long] = None): Unit = {
 plan match {
   case WithCTE(child, cteDefs) =>
 cteDefs.foreach { cteDef =>
-  cteMap(cteDef.id) = (cteDef, 0, 
mutable.Map.empty.withDefaultValue(0))
+  cteMap(cteDef.id) = CTEReferenceInfo(
+cteDef = cteDef,
+refCount = 0,
+outgoingRefs = mutable.Map.empty.withDefaultValue(0),
+shouldInline = true
+  )
 }
 cteDefs.foreach { cteDef =>
   buildCTEMap(cteDef, cteMap, Some(cteDef.id))
 }
 buildCTEMap(child, cteMap, outerCTEId)
 
   case ref: CTERelationRef =>
-val (cteDef, refCount, refMap) = cteMap(ref.cteId)
-cteMap(ref.cteId) = (cteDef, refCount + 1, refMap)
+cteMap(ref.cteId) = cteMap(ref.cteId).withRefCountIncreased(1)
 outerCTEId.foreach { cteId =>
-  val (_, _, outerRefMap) = cteMap(cteId)
-  outerRefMap(ref.cteId) += 1
+  cteMap(cteId).recordOutgoingReference(ref.cteId)

Review Comment:
   I personally like to not hide the `+1` implementation details here as I had 
troubles to understand it.
   
   Or the function name can be `IncreaseOutgoingReferenceByOne`?



-- 
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-48307][SQL] InlineCTE should keep not-inlined relations in the original WithCTE node [spark]

2024-05-16 Thread via GitHub


cloud-fan commented on PR #46617:
URL: https://github.com/apache/spark/pull/46617#issuecomment-2115021011

   cc @viirya @amaliujia @MaxGekk 


-- 
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-48307][SQL] InlineCTE should keep not-inlined relations in the original WithCTE node [spark]

2024-05-16 Thread via GitHub


cloud-fan opened a new pull request, #46617:
URL: https://github.com/apache/spark/pull/46617

   
   
   ### What changes were proposed in this pull request?
   
   I noticed an outdated comment in the rule `InlineCTE`
   ```
 // CTEs in SQL Commands have been inlined by `CTESubstitution` 
already, so it is safe to add
 // WithCTE as top node here.
   ```
   
   This is not true anymore after https://github.com/apache/spark/pull/42036 . 
It's not a big deal as we replace not-inlined CTE relations with `Repartition` 
during optimization, so it doesn't matter where we put the `WithCTE` node with 
not-inlined CTE relations, as it will disappear eventually. But it's still 
better to keep it at its original place, as third-party rules may be sensitive 
about the plan shape.
   
   ### Why are the changes needed?
   
   to keep the plan shape as much as can after inlining CTE relations.
   
   ### Does this PR introduce _any_ user-facing change?
   
   no
   
   ### How was this patch tested?
   
   new test
   
   ### 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