Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxr committed Oct 7, 2014
1 parent c194d5e commit 367d237
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ object ScalaReflection {
params.head.map { p =>
val Schema(dataType, nullable) =
schemaFor(p.typeSignature.substituteTypes(formalTypeArgs, actualTypeArgs))
StructField(p.name.toString, dataType, nullable)
StructField(p.name.toString, dataType, nullable, Map.empty)
}), nullable = true)
// Need to decide if we actually need a special type here.
case t if t <:< typeOf[Array[Byte]] => Schema(BinaryType, nullable = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ abstract class Expression extends TreeNode[Expression] {
*/
def foldable: Boolean = false
def nullable: Boolean
def metadata: Map[String, Any] = Map.empty
def references: AttributeSet = AttributeSet(children.flatMap(_.references.iterator))

/** Returns the result of evaluating this expression on a given input Row */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ abstract class Generator extends Expression {
override type EvaluatedType = TraversableOnce[Row]

override lazy val dataType =
ArrayType(StructType(output.map(a => StructField(a.name, a.dataType, a.nullable))))
ArrayType(StructType(output.map(a => StructField(a.name, a.dataType, a.nullable, a.metadata))))

override def nullable = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ abstract class NamedExpression extends Expression {
def name: String
def exprId: ExprId
def qualifiers: Seq[String]
def metadata: Map[String, Any] = Map.empty

def toAttribute: Attribute

Expand Down Expand Up @@ -99,6 +98,8 @@ case class Alias(child: Expression, name: String)
override def toString: String = s"$child AS $name#${exprId.id}$typeSuffix"

override protected final def otherCopyArgs = exprId :: qualifiers :: Nil

override def metadata: Map[String, Any] = child.metadata
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ case class LowerCaseSchema(child: LogicalPlan) extends UnaryNode {
protected def lowerCaseSchema(dataType: DataType): DataType = dataType match {
case StructType(fields) =>
StructType(fields.map(f =>
StructField(f.name.toLowerCase(), lowerCaseSchema(f.dataType), f.nullable)))
StructField(f.name.toLowerCase(), lowerCaseSchema(f.dataType), f.nullable, f.metadata)))
case ArrayType(elemType, containsNull) => ArrayType(lowerCaseSchema(elemType), containsNull)
case otherType => otherType
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ object DataType extends RegexParsers {
protected lazy val structField: Parser[StructField] =
("StructField(" ~> "[a-zA-Z0-9_]*".r) ~ ("," ~> dataType) ~ ("," ~> boolVal <~ ")") ^^ {
case name ~ tpe ~ nullable =>
StructField(name, tpe, nullable = nullable)
// TODO: parse metadata
StructField(name, tpe, nullable = nullable, Map.empty)
}

protected lazy val boolVal: Parser[Boolean] =
Expand Down
27 changes: 27 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/MetadataSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.apache.spark.sql

import org.apache.spark.sql.test.TestSQLContext
import org.scalatest.FunSuite

case class Person(name: String, age: Int)

class MetadataSuite extends FunSuite {

test("metadata") {
val sqlContext = TestSQLContext
import sqlContext._
val members = sqlContext.sparkContext.makeRDD(Seq(
Person("mike", 10),
Person("jim", 20)))
val table: SchemaRDD = sqlContext.createSchemaRDD(members)
val schema: StructType = table.schema
println("schema: " + schema)
val ageField = schema("age").copy(metadata = Map("desc" -> "age (must be nonnegative)"))
val newSchema = schema.copy(Seq(schema("name"), ageField))
val newTable = sqlContext.applySchema(table, newSchema)
val selectByExprAgeField = newTable.select('age).schema("age")
assert(selectByExprAgeField.metadata.nonEmpty)
val selectByNameAttrAgeField = newTable.select("age".attr).schema("age")
assert(selectByNameAttrAgeField.metadata.nonEmpty)
}
}

0 comments on commit 367d237

Please sign in to comment.