Skip to content

Commit b2ed6d0

Browse files
imback82cloud-fan
authored andcommitted
[SPARK-30214][SQL][FOLLOWUP] Remove statement logical plans for namespace commands
### What changes were proposed in this pull request? This is a follow-up to address the following comment: #27095 (comment) Currently, a SQL command string is parsed to a "statement" logical plan, converted to a logical plan with catalog/namespace, then finally converted to a physical plan. With the new resolution framework, there is no need to create a "statement" logical plan; a logical plan can contain `UnresolvedNamespace` which will be resolved to a `ResolvedNamespace`. This should simply the code base and make it a bit easier to add a new command. ### Why are the changes needed? Clean up codebase. ### Does this PR introduce any user-facing change? No ### How was this patch tested? Existing tests should cover the changes. Closes #27125 from imback82/SPARK-30214-followup. Authored-by: Terry Kim <yuminkim@gmail.com> Signed-off-by: Wenchen Fan <wenchen@databricks.com>
1 parent 0d589f4 commit b2ed6d0

8 files changed

Lines changed: 91 additions & 159 deletions

File tree

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ class Analyzer(
730730
case class ResolveNamespace(catalogManager: CatalogManager)
731731
extends Rule[LogicalPlan] with LookupCatalog {
732732
def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
733-
case s @ ShowTablesStatement(UnresolvedNamespace(Seq()), _) =>
733+
case s @ ShowTables(UnresolvedNamespace(Seq()), _) =>
734734
s.copy(namespace =
735735
ResolvedNamespace(currentCatalog.asNamespaceCatalog, catalogManager.currentNamespace))
736736
case UnresolvedNamespace(Seq()) =>

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveCatalogs.scala

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,6 @@ class ResolveCatalogs(val catalogManager: CatalogManager)
106106
s"Can not specify catalog `${catalog.name}` for view ${tbl.quoted} " +
107107
s"because view support in catalog has not been implemented yet")
108108

109-
case AlterNamespaceSetPropertiesStatement(
110-
NonSessionCatalogAndNamespace(catalog, ns), properties) =>
111-
AlterNamespaceSetProperties(catalog, ns, properties)
112-
113-
case AlterNamespaceSetLocationStatement(
114-
NonSessionCatalogAndNamespace(catalog, ns), location) =>
115-
AlterNamespaceSetProperties(catalog, ns,
116-
Map(SupportsNamespaces.PROP_LOCATION -> location))
117-
118109
case RenameTableStatement(NonSessionCatalogAndTable(catalog, oldName), newNameParts, isView) =>
119110
if (isView) {
120111
throw new AnalysisException("Renaming view is not supported in v2 catalogs.")
@@ -194,18 +185,6 @@ class ResolveCatalogs(val catalogManager: CatalogManager)
194185
if !isSessionCatalog(catalog) =>
195186
CreateNamespace(catalog.asNamespaceCatalog, ns, c.ifNotExists, c.properties)
196187

197-
case DropNamespaceStatement(NonSessionCatalogAndNamespace(catalog, ns), ifExists, cascade) =>
198-
DropNamespace(catalog, ns, ifExists, cascade)
199-
200-
case DescribeNamespaceStatement(NonSessionCatalogAndNamespace(catalog, ns), extended) =>
201-
DescribeNamespace(catalog, ns, extended)
202-
203-
case ShowNamespacesStatement(NonSessionCatalogAndNamespace(catalog, ns), pattern) =>
204-
ShowNamespaces(catalog, ns, pattern)
205-
206-
case ShowTablesStatement(NonSessionCatalogAndNamespace(catalog, ns), pattern) =>
207-
ShowTables(catalog.asTableCatalog, ns, pattern)
208-
209188
case UseStatement(isNamespaceSet, nameParts) =>
210189
if (isNamespaceSet) {
211190
SetCatalogAndNamespace(catalogManager, None, Some(nameParts))
@@ -231,13 +210,4 @@ class ResolveCatalogs(val catalogManager: CatalogManager)
231210
case _ => None
232211
}
233212
}
234-
235-
object NonSessionCatalogAndNamespace {
236-
def unapply(resolved: ResolvedNamespace): Option[(SupportsNamespaces, Seq[String])] =
237-
if (!isSessionCatalog(resolved.catalog)) {
238-
Some(resolved.catalog -> resolved.namespace)
239-
} else {
240-
None
241-
}
242-
}
243213
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,22 +2563,22 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
25632563
}
25642564

25652565
/**
2566-
* Create a [[DropNamespaceStatement]] command.
2566+
* Create a [[DropNamespace]] command.
25672567
*
25682568
* For example:
25692569
* {{{
25702570
* DROP (DATABASE|SCHEMA|NAMESPACE) [IF EXISTS] ns1.ns2 [RESTRICT|CASCADE];
25712571
* }}}
25722572
*/
25732573
override def visitDropNamespace(ctx: DropNamespaceContext): LogicalPlan = withOrigin(ctx) {
2574-
DropNamespaceStatement(
2574+
DropNamespace(
25752575
UnresolvedNamespace(visitMultipartIdentifier(ctx.multipartIdentifier)),
25762576
ctx.EXISTS != null,
25772577
ctx.CASCADE != null)
25782578
}
25792579

25802580
/**
2581-
* Create an [[AlterNamespaceSetPropertiesStatement]] logical plan.
2581+
* Create an [[AlterNamespaceSetProperties]] logical plan.
25822582
*
25832583
* For example:
25842584
* {{{
@@ -2588,14 +2588,14 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
25882588
*/
25892589
override def visitSetNamespaceProperties(ctx: SetNamespacePropertiesContext): LogicalPlan = {
25902590
withOrigin(ctx) {
2591-
AlterNamespaceSetPropertiesStatement(
2591+
AlterNamespaceSetProperties(
25922592
UnresolvedNamespace(visitMultipartIdentifier(ctx.multipartIdentifier)),
25932593
visitPropertyKeyValues(ctx.tablePropertyList))
25942594
}
25952595
}
25962596

25972597
/**
2598-
* Create an [[AlterNamespaceSetLocationStatement]] logical plan.
2598+
* Create an [[AlterNamespaceSetLocation]] logical plan.
25992599
*
26002600
* For example:
26012601
* {{{
@@ -2604,28 +2604,28 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
26042604
*/
26052605
override def visitSetNamespaceLocation(ctx: SetNamespaceLocationContext): LogicalPlan = {
26062606
withOrigin(ctx) {
2607-
AlterNamespaceSetLocationStatement(
2607+
AlterNamespaceSetLocation(
26082608
UnresolvedNamespace(visitMultipartIdentifier(ctx.multipartIdentifier)),
26092609
visitLocationSpec(ctx.locationSpec))
26102610
}
26112611
}
26122612

26132613
/**
2614-
* Create a [[ShowNamespacesStatement]] command.
2614+
* Create a [[ShowNamespaces]] command.
26152615
*/
26162616
override def visitShowNamespaces(ctx: ShowNamespacesContext): LogicalPlan = withOrigin(ctx) {
26172617
if (ctx.DATABASES != null && ctx.multipartIdentifier != null) {
26182618
throw new ParseException(s"FROM/IN operator is not allowed in SHOW DATABASES", ctx)
26192619
}
26202620

26212621
val multiPart = Option(ctx.multipartIdentifier).map(visitMultipartIdentifier)
2622-
ShowNamespacesStatement(
2622+
ShowNamespaces(
26232623
UnresolvedNamespace(multiPart.getOrElse(Seq.empty[String])),
26242624
Option(ctx.pattern).map(string))
26252625
}
26262626

26272627
/**
2628-
* Create a [[DescribeNamespaceStatement]].
2628+
* Create a [[DescribeNamespace]].
26292629
*
26302630
* For example:
26312631
* {{{
@@ -2634,7 +2634,7 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
26342634
*/
26352635
override def visitDescribeNamespace(ctx: DescribeNamespaceContext): LogicalPlan =
26362636
withOrigin(ctx) {
2637-
DescribeNamespaceStatement(
2637+
DescribeNamespace(
26382638
UnresolvedNamespace(visitMultipartIdentifier(ctx.multipartIdentifier())),
26392639
ctx.EXTENDED != null)
26402640
}
@@ -2802,11 +2802,11 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
28022802
}
28032803

28042804
/**
2805-
* Create a [[ShowTablesStatement]] command.
2805+
* Create a [[ShowTables]] command.
28062806
*/
28072807
override def visitShowTables(ctx: ShowTablesContext): LogicalPlan = withOrigin(ctx) {
28082808
val multiPart = Option(ctx.multipartIdentifier).map(visitMultipartIdentifier)
2809-
ShowTablesStatement(
2809+
ShowTables(
28102810
UnresolvedNamespace(multiPart.getOrElse(Seq.empty[String])),
28112811
Option(ctx.pattern).map(string))
28122812
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statements.scala

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,6 @@ case class DescribeTableStatement(
298298
partitionSpec: TablePartitionSpec,
299299
isExtended: Boolean) extends ParsedStatement
300300

301-
/**
302-
* A DESCRIBE NAMESPACE statement, as parsed from SQL.
303-
*/
304-
case class DescribeNamespaceStatement(
305-
namespace: LogicalPlan,
306-
extended: Boolean) extends ParsedStatement {
307-
override def children: Seq[LogicalPlan] = Seq(namespace)
308-
}
309-
310301
/**
311302
* A DESCRIBE TABLE tbl_name col_name statement, as parsed from SQL.
312303
*/
@@ -345,15 +336,6 @@ case class InsertIntoStatement(
345336
override def children: Seq[LogicalPlan] = query :: Nil
346337
}
347338

348-
/**
349-
* A SHOW TABLES statement, as parsed from SQL.
350-
*/
351-
case class ShowTablesStatement(
352-
namespace: LogicalPlan,
353-
pattern: Option[String]) extends ParsedStatement {
354-
override def children: Seq[LogicalPlan] = Seq(namespace)
355-
}
356-
357339
/**
358340
* A SHOW TABLE EXTENDED statement, as parsed from SQL.
359341
*/
@@ -371,43 +353,6 @@ case class CreateNamespaceStatement(
371353
ifNotExists: Boolean,
372354
properties: Map[String, String]) extends ParsedStatement
373355

374-
/**
375-
* A DROP NAMESPACE statement, as parsed from SQL.
376-
*/
377-
case class DropNamespaceStatement(
378-
namespace: LogicalPlan,
379-
ifExists: Boolean,
380-
cascade: Boolean) extends ParsedStatement {
381-
override def children: Seq[LogicalPlan] = Seq(namespace)
382-
}
383-
384-
/**
385-
* ALTER (DATABASE|SCHEMA|NAMESPACE) ... SET (DBPROPERTIES|PROPERTIES) command, as parsed from SQL.
386-
*/
387-
case class AlterNamespaceSetPropertiesStatement(
388-
namespace: LogicalPlan,
389-
properties: Map[String, String]) extends ParsedStatement {
390-
override def children: Seq[LogicalPlan] = Seq(namespace)
391-
}
392-
393-
/**
394-
* ALTER (DATABASE|SCHEMA|NAMESPACE) ... SET LOCATION command, as parsed from SQL.
395-
*/
396-
case class AlterNamespaceSetLocationStatement(
397-
namespace: LogicalPlan,
398-
location: String) extends ParsedStatement {
399-
override def children: Seq[LogicalPlan] = Seq(namespace)
400-
}
401-
402-
/**
403-
* A SHOW NAMESPACES statement, as parsed from SQL.
404-
*/
405-
case class ShowNamespacesStatement(
406-
namespace: LogicalPlan,
407-
pattern: Option[String]) extends ParsedStatement {
408-
override def children: Seq[LogicalPlan] = Seq(namespace)
409-
}
410-
411356
/**
412357
* A USE statement, as parsed from SQL.
413358
*/

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/v2Commands.scala

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,18 +250,19 @@ case class CreateNamespace(
250250
* The logical plan of the DROP NAMESPACE command that works for v2 catalogs.
251251
*/
252252
case class DropNamespace(
253-
catalog: CatalogPlugin,
254-
namespace: Seq[String],
253+
namespace: LogicalPlan,
255254
ifExists: Boolean,
256-
cascade: Boolean) extends Command
255+
cascade: Boolean) extends Command {
256+
override def children: Seq[LogicalPlan] = Seq(namespace)
257+
}
257258

258259
/**
259260
* The logical plan of the DESCRIBE NAMESPACE command that works for v2 catalogs.
260261
*/
261262
case class DescribeNamespace(
262-
catalog: SupportsNamespaces,
263-
namespace: Seq[String],
263+
namespace: LogicalPlan,
264264
extended: Boolean) extends Command {
265+
override def children: Seq[LogicalPlan] = Seq(namespace)
265266

266267
override def output: Seq[Attribute] = Seq(
267268
AttributeReference("name", StringType, nullable = false,
@@ -275,17 +276,29 @@ case class DescribeNamespace(
275276
* command that works for v2 catalogs.
276277
*/
277278
case class AlterNamespaceSetProperties(
278-
catalog: SupportsNamespaces,
279-
namespace: Seq[String],
280-
properties: Map[String, String]) extends Command
279+
namespace: LogicalPlan,
280+
properties: Map[String, String]) extends Command {
281+
override def children: Seq[LogicalPlan] = Seq(namespace)
282+
}
283+
284+
/**
285+
* The logical plan of the ALTER (DATABASE|SCHEMA|NAMESPACE) ... SET LOCATION
286+
* command that works for v2 catalogs.
287+
*/
288+
case class AlterNamespaceSetLocation(
289+
namespace: LogicalPlan,
290+
location: String) extends Command {
291+
override def children: Seq[LogicalPlan] = Seq(namespace)
292+
}
281293

282294
/**
283295
* The logical plan of the SHOW NAMESPACES command that works for v2 catalogs.
284296
*/
285297
case class ShowNamespaces(
286-
catalog: SupportsNamespaces,
287-
namespace: Seq[String],
298+
namespace: LogicalPlan,
288299
pattern: Option[String]) extends Command {
300+
override def children: Seq[LogicalPlan] = Seq(namespace)
301+
289302
override val output: Seq[Attribute] = Seq(
290303
AttributeReference("namespace", StringType, nullable = false)())
291304
}
@@ -412,9 +425,10 @@ case class RenameTable(
412425
* The logical plan of the SHOW TABLE command that works for v2 catalogs.
413426
*/
414427
case class ShowTables(
415-
catalog: TableCatalog,
416-
namespace: Seq[String],
428+
namespace: LogicalPlan,
417429
pattern: Option[String]) extends Command {
430+
override def children: Seq[LogicalPlan] = Seq(namespace)
431+
418432
override val output: Seq[Attribute] = Seq(
419433
AttributeReference("namespace", StringType, nullable = false)(),
420434
AttributeReference("tableName", StringType, nullable = false)())

0 commit comments

Comments
 (0)