Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-12139][SQL] REGEX Column Specification for Hive Queries #10137

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -222,6 +222,33 @@ case class UnresolvedStar(target: Option[Seq[String]]) extends Star with Unevalu
override def toString: String = target.map(_ + ".").getOrElse("") + "*"
}

/**
* Represents all of the input attributes to a given relational operator, for example in
* "SELECT `(id)?+.+` FROM ...".
*
* @param table an optional table that should be the target of the expansion. If omitted all
* tables' columns are produced.
*/
case class UnresolvedRegex(expr: String, table: Option[String]) extends Star with Unevaluable {

override def expand(input: Seq[Attribute], resolver: Resolver): Seq[NamedExpression] = {
val expandedAttributes: Seq[Attribute] = table match {
// If there is no table specified, use all input attributes that match expr
case None => input.filter(_.name.matches(expr))
// If there is a table, pick out attributes that are part of this table that match expr
case Some(t) => input.filter(_.qualifiers.filter(resolver(_, t)).nonEmpty)
.filter(_.name.matches(expr))
}
expandedAttributes.zip(input).map {
case (n: NamedExpression, _) => n
case (e, originalAttribute) =>
Alias(e, originalAttribute.name)(qualifiers = originalAttribute.qualifiers)
}
}

override def toString: String = table.map(_ + ".").getOrElse("") + expr
}

/**
* Used to assign new names to Generator's output, such as hive udtf.
* For example the SQL expression "stack(2, key, value, key, value) as (a, b)" could be represented
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,11 @@ https://cwiki.apache.org/confluence/display/Hive/Enhanced+Aggregation%2C+Cube%2C
/* Attribute References */
case Token("TOK_TABLE_OR_COL",
Token(name, Nil) :: Nil) =>
UnresolvedAttribute.quoted(cleanIdentifier(name))
name match {
// If the columns is wrapped in backticks, treat it as a regular expression
case escapedIdentifier(i) => UnresolvedRegex(i, None)
case _ => UnresolvedAttribute.quoted(name)
}
case Token(".", qualifier :: Token(attr, Nil) :: Nil) =>
nodeToExpr(qualifier) match {
case UnresolvedAttribute(nameParts) =>
Expand Down