srielau commented on code in PR #59874:
URL: https://github.com/apache/spark/pull/59874#discussion_r4632715711
##########
sql/core/src/main/scala/org/apache/spark/sql/catalyst/parser/ParseSqlResult.scala:
##########
@@ -926,30 +1231,33 @@ object ParseSqlResult {
sessionIdent(last)
}
} else {
- qualifyWithCurrentNamespace(parts, session)
+ qualifyWithCurrentNamespace(parts, scope)
}
}
private def qualifyRoutine(
parts: Seq[String],
- session: ClassicSparkSession): Seq[String] = {
- val catalog = session.sessionState.catalog
- // Same order as FunctionResolution: `first` is session, builtin,
persistent;
- // `second` (default) is builtin, session, persistent; `last` is builtin,
persistent, session.
- val sessionOrder =
-
session.sessionState.conf.getConf(SQLConf.SESSION_FUNCTION_RESOLUTION_ORDER)
+ scope: AnalysisScope): Seq[String] = {
+ val catalog = scope.catalog
def isTemp(name: String): Boolean =
catalog.isTemporaryFunction(FunctionIdentifier(name))
- // Persistent in the catalog the statement resolves names in, as
FunctionResolution does.
- def isPersistent(name: String): Boolean = {
- val mgr = session.sessionState.catalogManager
- mgr.currentCatalog match {
- case _ if
mgr.currentCatalog.name().equalsIgnoreCase(CatalogManager.SESSION_CATALOG_NAME)
=>
- catalog.isPersistentFunction(FunctionIdentifier(name))
- case fc: FunctionCatalog =>
- Try(fc.functionExists(Identifier.of(mgr.currentNamespace,
name))).getOrElse(false)
- case _ => false
- }
+ // The identity of routine `name` in path entry `entry`, spelled as the
catalog spells it.
+ def persistentIn(entry: Seq[String], name: String): Option[Seq[String]] =
entry match {
+ case Seq(cat, db) if
cat.equalsIgnoreCase(CatalogManager.SESSION_CATALOG_NAME) =>
+ val ident = FunctionIdentifier(name, Some(db))
+ if (Try(catalog.isPersistentFunction(ident)).getOrElse(false)) {
Review Comment:
Addressed in 4c69a8a9caf. The walk skips the metastore probe for the last
path entry when it equals the current catalog and schema, since the fallback
below qualifies to the same name. Under `last` the persistent entry is not
last, so its probe stays.
##########
sql/core/src/main/scala/org/apache/spark/sql/catalyst/parser/ParseSqlResult.scala:
##########
@@ -173,13 +234,140 @@ object ParseSqlResult {
/** Analyze `plan`, including nested EXPLAIN / DESCRIBE QUERY payloads. */
private def analyzePlan(
session: ClassicSparkSession,
+ analyzerOpt: Option[Analyzer],
plan: LogicalPlan): LogicalPlan = {
+ // Checked here, before QueryExecution, because QueryExecution executes a
script (also
+ // when named parameter markers wrap it) as soon as analysis starts. A
script under a
+ // supervising command is refused by the recursive call below.
+ if (QueryExecution.isUnresolvedPlanSqlScript(plan)) {
+ throw QueryExecutionErrors.parseSqlAnalyzeUnsupportedScript()
+ }
val toAnalyze = plan match {
case s: SupervisingCommand =>
- s.withTransformedSupervisedPlan(inner => analyzePlan(session, inner))
+ s.withTransformedSupervisedPlan(inner => analyzePlan(session,
analyzerOpt, inner))
case other => other
}
- session.sessionState.executePlan(toAnalyze,
CommandExecutionMode.SKIP).analyzed
+ new QueryExecution(
+ session,
+ toAnalyze,
+ mode = CommandExecutionMode.SKIP,
+ analyzerOpt = analyzerOpt).analyzed
+ }
+
+ /**
+ * Run `f` under the call's conf when the call gives its own SQL path. The
copy enables the
+ * path feature for this call only; the invoker's conf is not changed.
+ */
+ private def inCallConf[T](conf: Option[SQLConf])(f: => T): T = conf match {
+ case Some(c) => SQLConf.withExistingConf(c)(f)
+ case None => f
+ }
+
+ /**
+ * The analyzer for one ANALYZE call with `current_catalog` /
`current_schema` /
+ * `current_path` overrides, and the conf to run it under when the call
gives a path.
+ * The session is not changed: the call gets its own catalog manager, v1
catalog and conf.
+ */
+ private def contextAnalyzer(
+ session: ClassicSparkSession,
+ context: ParseSqlAnalyzeContext): (Analyzer, Option[SQLConf]) = {
+ val state = session.sessionState
+ val mgr = state.catalogManager
+ val invokerCatalog = mgr.currentCatalog.name()
+ val target = context.catalog.map(checkedContextValue("current_catalog", _))
+ .map(mgr.catalog)
+ .getOrElse(mgr.currentCatalog)
+ val catalogName = target.name()
+ val isSession = CatalogV2Util.isSessionCatalog(target)
+ val switched = catalogName != invokerCatalog
+ val namespace: Array[String] = context.schema match {
+ case Some(schema) => checkedNamespace(schema, isSession)
+ case None if switched =>
+ if (isSession) Array(state.conf.defaultDatabase) else
target.defaultNamespace()
+ case None => mgr.currentNamespace
+ }
+ target match {
+ case ns: SupportsNamespaces if !isSession &&
!ns.namespaceExists(namespace) =>
+ throw QueryCompilationErrors.noSuchNamespaceError(catalogName +:
namespace)
+ case _ =>
+ }
+ val v1 = perCallSessionCatalog(session)
+ if (isSession) v1.setCurrentDatabase(namespace.head)
+ val callConf = context.path.map { _ =>
+ val copy = state.conf.clone()
+ copy.setConf(SQLConf.PATH_ENABLED, true)
+ copy
+ }
+ // PATH in the call's path means the invoker's path, as in SET PATH.
CURRENT_SCHEMA
+ // stays a marker that resolves against the call's schema.
+ val pathOverride = (context.path, callConf) match {
+ case (Some(path), Some(conf)) => callPathOverride(path, conf, mgr)
+ case _ => None
+ }
+ val analyzer = state.analyzer.withCatalogManager(
Review Comment:
Fixed in 4c69a8a9caf. The call's analyzer is published through
`CallAnalyzerRegistry` while the batch is analyzed, and
`ResolveExecuteImmediate` analyzes the payload with it. So `EXECUTE IMMEDIATE
'SELECT a FROM t'` under `current_schema => 's'` resolves `s.t`, as the direct
statements do. The usage text now says so.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/VariableResolution.scala:
##########
@@ -153,23 +161,51 @@ class VariableResolution(
varDef
)
}
- .orElse(
- if (maybeTempVariableName(nameParts) &&
- allowUnqualifiedSessionTempVariableLookup(nameParts)) {
- tempVariableManager
- .get(namePartsCaseAdjusted)
- .map { varDef =>
- VariableReference(
- nameParts,
- FakeSystemCatalog,
- Identifier.of(Array(CatalogManager.SESSION_NAMESPACE),
namePartsCaseAdjusted.last),
- varDef
- )
- }
- } else {
- None
+ }
+
+ private def lookupSuppliedLocal(
+ nameParts: Seq[String],
+ namePartsCaseAdjusted: Seq[String]): Option[VariableReference] = {
+ if (SuppliedVariableRegistry.isSessionName(nameParts)) {
+ None
+ } else {
+ SuppliedVariableRegistry.lookup(nameParts, conf.resolver).map { dataType
=>
Review Comment:
Fixed in 4c69a8a9caf. The payload runs under
`SuppliedVariableRegistry.withoutLocals`, which keeps session-shaped names and
drops the supplied locals, so `proc.p` no longer resolves in the body. Covered
by the new test 'ANALYZE hides supplied locals from an EXECUTE IMMEDIATE body'.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]