Skip to content

Commit

Permalink
add CreateStruct
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxr committed Mar 26, 2015
1 parent 4fc4d03 commit 85f3106
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ class Analyzer(catalog: Catalog,
case o => o :: Nil
}
Alias(c.copy(children = expandedArgs), name)() :: Nil
case Alias(c @ CreateStruct(args), name) if containsStar(args) =>
val expandedArgs = args.flatMap {
case s: Star => s.expand(child.output, resolver)
case o => o :: Nil
}
Alias(c.copy(children = expandedArgs), name)() :: Nil
case o => o :: Nil
},
child)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ case class ArrayGetField(child: Expression, field: StructField, ordinal: Int, co
case class CreateArray(children: Seq[Expression]) extends Expression {
override type EvaluatedType = Any

override def foldable: Boolean = !children.exists(!_.foldable)
override def foldable: Boolean = children.forall(_.foldable)

lazy val childTypes = children.map(_.dataType).distinct

Expand All @@ -142,3 +142,29 @@ case class CreateArray(children: Seq[Expression]) extends Expression {

override def toString: String = s"Array(${children.mkString(",")})"
}

/**
* Returns a Row containing the evaluation of all children expressions.
*/
case class CreateStruct(children: Seq[Expression]) extends Expression {
override type EvaluatedType = Row

override def foldable: Boolean = children.forall(_.foldable)

override lazy val resolved: Boolean = childrenResolved

override def dataType: StructType = {
assert(resolved, s"CreateStruct is called with unresolved children: $children.")
val fields = children.map {
case named: NamedExpression =>
StructField(named.name, named.dataType, named.nullable, named.metadata)
}
StructType(fields)
}

override def nullable: Boolean = false

override def eval(input: Row): EvaluatedType = {
Row(children.map(_.eval(input)): _*)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1080,4 +1080,11 @@ class ExpressionEvaluationSuite extends FunSuite {
checkEvaluation(c1 ^ c2, 3, row)
checkEvaluation(~c1, -2, row)
}

test("CreateStruct") {
val row = Row(1, 2, 3)
val c1 = 'a.int.at(0)
val c3 = 'a.int.at(2)
checkEvaluation(CreateStruct(Seq(c1, c3)), Row(1, 3), row)
}
}

0 comments on commit 85f3106

Please sign in to comment.