Skip to content

Commit

Permalink
some enhance
Browse files Browse the repository at this point in the history
  • Loading branch information
cloud-fan committed Sep 10, 2014
1 parent 95d733f commit 16bc4c6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ class SqlParser extends StandardTokenParsers with PackratParsers {
expression ~ "[" ~ expression <~ "]" ^^ {
case base ~ _ ~ ordinal => GetItem(base, ordinal)
} |
dotExpressionHeader |
expression ~ "." ~ ident ^^ {
case base ~ _ ~ fieldName => GetField(base, fieldName)
} |
Expand All @@ -367,6 +366,7 @@ class SqlParser extends StandardTokenParsers with PackratParsers {
"(" ~> expression <~ ")" |
function |
"-" ~> literal ^^ UnaryMinus |
dotExpressionHeader |
ident ^^ UnresolvedAttribute |
"*" ^^^ Star(None) |
literal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,13 @@ abstract class LogicalPlan extends QueryPlan[LogicalPlan] {
def resolve(name: String): Option[NamedExpression] =
resolve(name, output)

/** Performs attribute resolution given a name and a sequence of possible attributes. */
/**
* Performs attribute resolution given a name and a sequence of possible attributes.
* The only possible formats of name are "ident" and "ident.ident".
*/
protected def resolve(name: String, input: Seq[Attribute]): Option[NamedExpression] = {
def handleResult[A <: NamedExpression](result: Seq[A]) = {
result match {
result.distinct match {
case Seq(a) => Some(a)
case Seq() => None
case ambiguousReferences =>
Expand All @@ -99,13 +102,19 @@ abstract class LogicalPlan extends QueryPlan[LogicalPlan] {
}

name.split("\\.") match {
// the format of name is "ident", it should match the name of a possible attribute
case Array(s) => handleResult(input.filter(_.name == s))

// The format of name is "ident.ident", the first part should matches the scope
// and the second part should matches the name. Or the first part matches the
// name and this attribute is struct type.
case Array(s1, s2) =>
handleResult(input.collect {
case a if a.qualifiers.contains(s1) && a.name == s2 => a
case a if a.name == s1 && a.dataType.isInstanceOf[StructType] =>
case a if (a.qualifiers.contains(s1) && a.name == s2) => a
case a if (a.name == s1 && a.dataType.isInstanceOf[StructType]) =>
Alias(GetField(a, s2), s2)()
})

case _ => None
}
}
Expand Down

0 comments on commit 16bc4c6

Please sign in to comment.