Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ package org.apache.spark.sql.catalyst.analysis
import java.util.Locale

import scala.collection.mutable

import org.apache.spark.sql.AnalysisException
import org.apache.spark.sql.catalyst.expressions.{Ascending, Expression, IntegerLiteral, SortOrder}
import org.apache.spark.sql.catalyst.expressions.{Ascending, Expression, IntegerLiteral, SortOrder, SubqueryExpression}
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.rules.Rule
import org.apache.spark.sql.catalyst.trees.CurrentOrigin
Expand Down Expand Up @@ -165,6 +164,24 @@ object ResolveHints {
hintErrorHandler.hintRelationsNotFound(h.name, h.parameters, unmatchedIdents)
applied
}
case With(child, relations) => resolveCTEHint(child,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch looks like it is specifically designed to fix With comparing to #29062. I am open to more comments.

relations.foldLeft(Seq.empty[(String, LogicalPlan)]) {
case (resolved, (name, relation)) =>
resolved :+ name -> apply(resolveCTEHint(relation, resolved))
})
}

def resolveCTEHint(plan: LogicalPlan, cteRelations: Seq[(String, LogicalPlan)]): LogicalPlan = {
plan resolveOperatorsDown {
case u: UnresolvedRelation =>
cteRelations.find(x => resolver(x._1, u.tableName)).map(_._2).getOrElse(u)
Comment on lines +177 to +178
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch will occur stackoverflow when cte table name is same as table in cte as follows:
sql("create temporary view t as select 1 as id")
sql("with t as (select /*+ BROADCAST(id) */ id from t) select id from t")
@cloud-fan Could you please help me find a way to pass this ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you looked at #29062 ? Seems easier to just run CTE substitution in the very beginning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, put the CTE substitution in the very beginning is an easier way.

case other =>
// This cannot be done in ResolveSubquery because ResolveSubquery does not know the CTE.
other transformExpressions {
case e: SubqueryExpression =>
e.withNewPlan(resolveCTEHint(e.plan, cteRelations))
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2558,6 +2558,13 @@ abstract class SQLQuerySuiteBase extends QueryTest with SQLTestUtils with TestHi
}
}
}

test("SPARK-32347: cte hint regression") {
withTempView("t") {
sql("create temporary view t as select 1 as id")
sql("with cte as (select /*+ BROADCAST(id) */ id from t) select id from cte")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just in case, could you check that the hist is correctly applied?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I need to check the hist, seems something wrong with the patch.

}
}
}

@SlowHiveTest
Expand Down