Re: [PR] [SPARK-43124][SQL] Add ConvertCommandResultToLocalRelation rule [spark]

2024-03-06 Thread via GitHub


cloud-fan commented on code in PR #45397:
URL: https://github.com/apache/spark/pull/45397#discussion_r1515551231


##
sql/core/src/main/scala/org/apache/spark/sql/catalyst/optimizer/ConvertCommandResultToLocalRelation.scala:
##
@@ -0,0 +1,52 @@
+/*
+ * 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.expressions.{AttributeReference, 
Expression, IntegerLiteral, InterpretedMutableProjection, Predicate, 
Unevaluable}
+import org.apache.spark.sql.catalyst.plans.logical.{CommandResult, Filter, 
Limit, LocalRelation, LogicalPlan, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.COMMAND_RESULT
+
+/**
+ * Converts local operations (i.e. ones that don't require data exchange) on 
`CommandResult`
+ * to `LocalRelation`.
+ */
+object ConvertCommandResultToLocalRelation extends Rule[LogicalPlan] {

Review Comment:
   oh I see!



-- 
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-43124][SQL] Add ConvertCommandResultToLocalRelation rule [spark]

2024-03-06 Thread via GitHub


ulysses-you commented on code in PR #45397:
URL: https://github.com/apache/spark/pull/45397#discussion_r1515554208


##
sql/core/src/main/scala/org/apache/spark/sql/catalyst/optimizer/ConvertCommandResultToLocalRelation.scala:
##
@@ -0,0 +1,52 @@
+/*
+ * 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.expressions.{AttributeReference, 
Expression, IntegerLiteral, InterpretedMutableProjection, Predicate, 
Unevaluable}
+import org.apache.spark.sql.catalyst.plans.logical.{CommandResult, Filter, 
Limit, LocalRelation, LogicalPlan, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.COMMAND_RESULT
+
+/**
+ * Converts local operations (i.e. ones that don't require data exchange) on 
`CommandResult`
+ * to `LocalRelation`.
+ */
+object ConvertCommandResultToLocalRelation extends Rule[LogicalPlan] {

Review Comment:
   Probably, we can add a new trait `LocalRelationConverable` to let 
`LocalRelation` and `CommandResult` inherit it.



-- 
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-43124][SQL] Add ConvertCommandResultToLocalRelation rule [spark]

2024-03-06 Thread via GitHub


cloud-fan commented on code in PR #45397:
URL: https://github.com/apache/spark/pull/45397#discussion_r1515557219


##
sql/core/src/main/scala/org/apache/spark/sql/catalyst/optimizer/ConvertCommandResultToLocalRelation.scala:
##
@@ -0,0 +1,52 @@
+/*
+ * 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.expressions.{AttributeReference, 
Expression, IntegerLiteral, InterpretedMutableProjection, Predicate, 
Unevaluable}
+import org.apache.spark.sql.catalyst.plans.logical.{CommandResult, Filter, 
Limit, LocalRelation, LogicalPlan, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.COMMAND_RESULT
+
+/**
+ * Converts local operations (i.e. ones that don't require data exchange) on 
`CommandResult`
+ * to `LocalRelation`.
+ */
+object ConvertCommandResultToLocalRelation extends Rule[LogicalPlan] {
+
+  override def apply(plan: LogicalPlan): LogicalPlan = 
plan.transformWithPruning(
+_.containsPattern(COMMAND_RESULT)) {
+case Project(projectList, CommandResult(output, _, _, rows))
+  if !projectList.exists(hasUnevaluableExpr) =>
+  val projection = new InterpretedMutableProjection(projectList, output)
+  projection.initialize(0)
+  LocalRelation(projectList.map(_.toAttribute), 
rows.map(projection(_).copy()))
+
+case Limit(IntegerLiteral(limit), CommandResult(output, _, _, rows)) =>
+  LocalRelation(output, rows.take(limit))
+
+case Filter(condition, CommandResult(output, _, _, rows))

Review Comment:
   By looking at this rule, I'm on the fence now. The original target of 
`CommandResult` is for better UI support: 
https://github.com/apache/spark/commit/8013f985a4d07a948b0c22638314162819bfb2be
   
   e.g. , if you do `sql("show tables").filter(...)`, we do want to see a 
command result node under a filter node in the UI, even if it means extra jobs.
   
   I think certain DataFrame operations such as `df.show()`, `df.isEmpty` 
should just be exceptions. It looks like a single operation to users and we 
should not have extra jobs. But this should not be general to all operations on 
`CommandResult`
   
   cc @HyukjinKwon 



-- 
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-43124][SQL] Add ConvertCommandResultToLocalRelation rule [spark]

2024-03-06 Thread via GitHub


HyukjinKwon commented on code in PR #45397:
URL: https://github.com/apache/spark/pull/45397#discussion_r1515634282


##
sql/core/src/main/scala/org/apache/spark/sql/catalyst/optimizer/ConvertCommandResultToLocalRelation.scala:
##
@@ -0,0 +1,52 @@
+/*
+ * 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.expressions.{AttributeReference, 
Expression, IntegerLiteral, InterpretedMutableProjection, Predicate, 
Unevaluable}
+import org.apache.spark.sql.catalyst.plans.logical.{CommandResult, Filter, 
Limit, LocalRelation, LogicalPlan, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.COMMAND_RESULT
+
+/**
+ * Converts local operations (i.e. ones that don't require data exchange) on 
`CommandResult`
+ * to `LocalRelation`.
+ */
+object ConvertCommandResultToLocalRelation extends Rule[LogicalPlan] {
+
+  override def apply(plan: LogicalPlan): LogicalPlan = 
plan.transformWithPruning(
+_.containsPattern(COMMAND_RESULT)) {
+case Project(projectList, CommandResult(output, _, _, rows))
+  if !projectList.exists(hasUnevaluableExpr) =>
+  val projection = new InterpretedMutableProjection(projectList, output)
+  projection.initialize(0)
+  LocalRelation(projectList.map(_.toAttribute), 
rows.map(projection(_).copy()))
+
+case Limit(IntegerLiteral(limit), CommandResult(output, _, _, rows)) =>
+  LocalRelation(output, rows.take(limit))
+
+case Filter(condition, CommandResult(output, _, _, rows))

Review Comment:
   ic thanks for explanation.



-- 
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-43124][SQL] Add ConvertCommandResultToLocalRelation rule [spark]

2024-03-06 Thread via GitHub


wForget commented on code in PR #45397:
URL: https://github.com/apache/spark/pull/45397#discussion_r1515650747


##
sql/core/src/main/scala/org/apache/spark/sql/catalyst/optimizer/ConvertCommandResultToLocalRelation.scala:
##
@@ -0,0 +1,52 @@
+/*
+ * 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.expressions.{AttributeReference, 
Expression, IntegerLiteral, InterpretedMutableProjection, Predicate, 
Unevaluable}
+import org.apache.spark.sql.catalyst.plans.logical.{CommandResult, Filter, 
Limit, LocalRelation, LogicalPlan, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.COMMAND_RESULT
+
+/**
+ * Converts local operations (i.e. ones that don't require data exchange) on 
`CommandResult`
+ * to `LocalRelation`.
+ */
+object ConvertCommandResultToLocalRelation extends Rule[LogicalPlan] {
+
+  override def apply(plan: LogicalPlan): LogicalPlan = 
plan.transformWithPruning(
+_.containsPattern(COMMAND_RESULT)) {
+case Project(projectList, CommandResult(output, _, _, rows))
+  if !projectList.exists(hasUnevaluableExpr) =>
+  val projection = new InterpretedMutableProjection(projectList, output)
+  projection.initialize(0)
+  LocalRelation(projectList.map(_.toAttribute), 
rows.map(projection(_).copy()))
+
+case Limit(IntegerLiteral(limit), CommandResult(output, _, _, rows)) =>
+  LocalRelation(output, rows.take(limit))
+
+case Filter(condition, CommandResult(output, _, _, rows))

Review Comment:
   Thanks for the explanation, it makes sense to me, can we continue to review 
#45373?



-- 
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-43124][SQL] Add ConvertCommandResultToLocalRelation rule [spark]

2024-03-07 Thread via GitHub


cloud-fan commented on code in PR #45397:
URL: https://github.com/apache/spark/pull/45397#discussion_r1515785048


##
sql/core/src/main/scala/org/apache/spark/sql/catalyst/optimizer/ConvertCommandResultToLocalRelation.scala:
##
@@ -0,0 +1,52 @@
+/*
+ * 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.expressions.{AttributeReference, 
Expression, IntegerLiteral, InterpretedMutableProjection, Predicate, 
Unevaluable}
+import org.apache.spark.sql.catalyst.plans.logical.{CommandResult, Filter, 
Limit, LocalRelation, LogicalPlan, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.COMMAND_RESULT
+
+/**
+ * Converts local operations (i.e. ones that don't require data exchange) on 
`CommandResult`
+ * to `LocalRelation`.
+ */
+object ConvertCommandResultToLocalRelation extends Rule[LogicalPlan] {
+
+  override def apply(plan: LogicalPlan): LogicalPlan = 
plan.transformWithPruning(
+_.containsPattern(COMMAND_RESULT)) {
+case Project(projectList, CommandResult(output, _, _, rows))
+  if !projectList.exists(hasUnevaluableExpr) =>
+  val projection = new InterpretedMutableProjection(projectList, output)
+  projection.initialize(0)
+  LocalRelation(projectList.map(_.toAttribute), 
rows.map(projection(_).copy()))
+
+case Limit(IntegerLiteral(limit), CommandResult(output, _, _, rows)) =>
+  LocalRelation(output, rows.take(limit))
+
+case Filter(condition, CommandResult(output, _, _, rows))

Review Comment:
   Yea let's back to https://github.com/apache/spark/pull/45373



-- 
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-43124][SQL] Add ConvertCommandResultToLocalRelation rule [spark]

2024-03-07 Thread via GitHub


wForget closed pull request #45397: [SPARK-43124][SQL] Add 
ConvertCommandResultToLocalRelation rule
URL: https://github.com/apache/spark/pull/45397


-- 
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-43124][SQL] Add ConvertCommandResultToLocalRelation rule [spark]

2024-03-07 Thread via GitHub


wForget commented on PR #45397:
URL: https://github.com/apache/spark/pull/45397#issuecomment-1983129621

   Close with comment: 
https://github.com/apache/spark/pull/45397#discussion_r1515557219


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