Skip to content

Commit 6e37517

Browse files
committed
Address hvanhovell's comments
Blame Rev:
1 parent 7699e87 commit 6e37517

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,19 @@ case class UnresolvedTableValuedFunction(
9090
* @param table an optional table that should be the target of the expansion. If omitted all
9191
* tables' columns are produced.
9292
*/
93-
case class UnresolvedRegex(expr: String, table: Option[String]) extends Star with Unevaluable {
93+
case class UnresolvedRegex(regexPattern: String, table: Option[String])
94+
extends Star with Unevaluable {
9495
override def expand(input: LogicalPlan, resolver: Resolver): Seq[NamedExpression] = {
95-
val expandedAttributes: Seq[Attribute] = table match {
96+
table match {
9697
// If there is no table specified, use all input attributes that match expr
97-
case None => input.output.filter(_.name.matches(expr))
98+
case None => input.output.filter(_.name.matches(regexPattern))
9899
// If there is a table, pick out attributes that are part of this table that match expr
99-
case Some(t) => input.output.filter(_.qualifier.filter(resolver(_, t)).nonEmpty)
100-
.filter(_.name.matches(expr))
101-
}
102-
103-
expandedAttributes.zip(input.output).map {
104-
case (n: NamedExpression, _) => n
105-
case (e, originalAttribute) =>
106-
Alias(e, originalAttribute.name)()
100+
case Some(t) => input.output.filter(_.qualifier.exists(resolver(_, t)))
101+
.filter(_.name.matches(regexPattern))
107102
}
108103
}
109104

110-
override def toString: String = table.map(_ + ".").getOrElse("") + expr
105+
override def toString: String = table.map(_ + ".").getOrElse("") + regexPattern
111106
}
112107

113108
/**

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

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,18 +1239,12 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
12391239
val attr = ctx.fieldName.getText
12401240
expression(ctx.base) match {
12411241
case unresolved_attr @ UnresolvedAttribute(nameParts) =>
1242-
if (conf.supportQuotedIdentifiers) {
1243-
val escapedIdentifier = "`(.+)`".r
1244-
val ret = Option(ctx.fieldName.getStart).map(_.getText match {
1245-
case r@escapedIdentifier(i) =>
1246-
UnresolvedRegex(i, Some(unresolved_attr.name))
1247-
case _ =>
1248-
UnresolvedAttribute(nameParts :+ attr)
1249-
})
1250-
return ret.get
1242+
matchEscapedIdentifier(ctx.fieldName.getStart.getText) match {
1243+
case Some(i) if conf.supportQuotedIdentifiers =>
1244+
UnresolvedRegex(i, Some(unresolved_attr.name))
1245+
case _ =>
1246+
UnresolvedAttribute(nameParts :+ attr)
12511247
}
1252-
1253-
UnresolvedAttribute(nameParts :+ attr)
12541248
case e =>
12551249
UnresolvedExtractValue(e, Literal(attr))
12561250
}
@@ -1261,19 +1255,12 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
12611255
* quoted in ``
12621256
*/
12631257
override def visitColumnReference(ctx: ColumnReferenceContext): Expression = withOrigin(ctx) {
1264-
if (conf.supportQuotedIdentifiers) {
1265-
val escapedIdentifier = "`(.+)`".r
1266-
val ret = Option(ctx.getStart).map(_.getText match {
1267-
case r @ escapedIdentifier(i) =>
1268-
UnresolvedRegex(i, None)
1269-
case _ =>
1270-
UnresolvedAttribute.quoted(ctx.getText)
1271-
})
1272-
1273-
return ret.get
1258+
matchEscapedIdentifier(ctx.getStart.getText) match {
1259+
case Some(i) if conf.supportQuotedIdentifiers =>
1260+
UnresolvedRegex(i, None)
1261+
case _ =>
1262+
UnresolvedAttribute.quoted(ctx.getText)
12741263
}
1275-
1276-
UnresolvedAttribute.quoted(ctx.getText)
12771264
}
12781265

12791266
/**

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,18 @@ object ParserUtils {
177177
sb.toString()
178178
}
179179

180+
val escapedIdentifier = "`(.+)`".r
181+
182+
/**
183+
* Return the substring extracted using regex
184+
*/
185+
def matchEscapedIdentifier(b: String): Option[String] = {
186+
b match {
187+
case escapedIdentifier(i) => Some(i)
188+
case _ => None
189+
}
190+
}
191+
180192
/** Some syntactic sugar which makes it easier to work with optional clauses for LogicalPlans. */
181193
implicit class EnhancedLogicalPlan(val plan: LogicalPlan) extends AnyVal {
182194
/**

0 commit comments

Comments
 (0)