Skip to content

Commit

Permalink
Internal types can be merged.
Browse files Browse the repository at this point in the history
  • Loading branch information
Spiro Michaylov committed Jan 29, 2015
1 parent 66bf58c commit 0b03795
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 6 deletions.
20 changes: 19 additions & 1 deletion src/main/scala/nsmc/conversion/types/Merger.scala
@@ -1,7 +1,25 @@
package nsmc.conversion.types

import scala.collection.mutable.HashMap

object Merger {
def merge(l: ConversionType, r: ConversionType) : ConversionType = {
null
(l, r) match {
case (StructureType(lMap), StructureType(rMap)) => {
val keys = lMap.keySet ++ rMap.keySet
val pairs = keys.map(k => {
val inLeft = lMap.isDefinedAt(k)
val inRight = rMap.isDefinedAt(k)
(inLeft, inRight) match {
case (true, true) => (k, merge(lMap.getOrElse(k, null), rMap.getOrElse(k, null)))
case (true, false) => (k, lMap.getOrElse(k, null))
case (false, true) => (k, rMap.getOrElse(k, null))
}
})
val ct = new StructureType(HashMap[String, ConversionType](pairs.toSeq:_*))
ct
}
case (_, _) => l // TODO: assume for now they're equal
}
}
}
104 changes: 99 additions & 5 deletions src/test/scala/nsmc/conversion/MergeInternalTests.scala
@@ -1,15 +1,109 @@
package nsmc.conversion

import nsmc.conversion.types._

import org.apache.spark.sql.catalyst.types.{IntegerType, StringType}
import org.scalatest.{Matchers, FlatSpec}

import scala.collection.mutable

class MergeInternalTests extends FlatSpec with Matchers {
// TODO: no overlap

// TODO: partial overlap
"flat types without field overlap" should "merge correctly" in {
val t1 = new mutable.HashMap[String, ConversionType]
t1 += "a" -> new AtomicType(StringType)
t1 += "b" -> new AtomicType(IntegerType)

val t2 = new mutable.HashMap[String, ConversionType]
t2 += "c" -> new AtomicType(StringType)
t2 += "d" -> new AtomicType(IntegerType)

val merged = Merger.merge(new StructureType(t1), new StructureType(t2))

merged shouldBe a [StructureType]
val hm = merged.asInstanceOf[StructureType].fields

hm should have size (4)
hm("a") should be (new AtomicType(StringType))
hm("b") should be (new AtomicType(IntegerType))
hm("c") should be (new AtomicType(StringType))
hm("d") should be (new AtomicType(IntegerType))
}

"flat types with partial equal type overlap" should "merge correctly" in {
val t1 = new mutable.HashMap[String, ConversionType]
t1 += "a" -> new AtomicType(StringType)
t1 += "b" -> new AtomicType(IntegerType)

val t2 = new mutable.HashMap[String, ConversionType]
t2 += "b" -> new AtomicType(IntegerType)
t2 += "c" -> new AtomicType(StringType)

val merged = Merger.merge(new StructureType(t1), new StructureType(t2))

merged shouldBe a [StructureType]
val hm = merged.asInstanceOf[StructureType].fields

hm should have size (3)
hm("a") should be (new AtomicType(StringType))
hm("b") should be (new AtomicType(IntegerType))
hm("c") should be (new AtomicType(StringType))
}

"flat types with full equal type overlap" should "merge correctly" in {
val t1 = new mutable.HashMap[String, ConversionType]
t1 += "a" -> new AtomicType(StringType)
t1 += "b" -> new AtomicType(IntegerType)

val t2 = new mutable.HashMap[String, ConversionType]
t2 += "a" -> new AtomicType(StringType)
t2 += "b" -> new AtomicType(IntegerType)

val merged = Merger.merge(new StructureType(t1), new StructureType(t2))

merged shouldBe a [StructureType]
val hm = merged.asInstanceOf[StructureType].fields

hm should have size (2)
hm("a") should be (new AtomicType(StringType))
hm("b") should be (new AtomicType(IntegerType))
}

"flat types with incompatibly typed overlaps" should "merge correctly" ignore {

}

"nested types with overlaps" should "merge correctly" in {

val inner1 = new mutable.HashMap[String, ConversionType]
inner1 += "x" -> new AtomicType(StringType)
inner1 += "y" -> new AtomicType(IntegerType)

val t1 = new mutable.HashMap[String, ConversionType]
t1 += "a" -> new AtomicType(StringType)
t1 += "b" -> new StructureType(inner1)

val inner2 = new mutable.HashMap[String, ConversionType]
inner2 += "y" -> new AtomicType(IntegerType)
inner2 += "z" -> new AtomicType(StringType)

val t2 = new mutable.HashMap[String, ConversionType]
t2 += "b" -> new StructureType(inner2)
t2 += "c" -> new AtomicType(StringType)

// TODO: fully overlapped
val merged = Merger.merge(new StructureType(t1), new StructureType(t2))

// TODO: compatible types
merged shouldBe a [StructureType]
val hm = merged.asInstanceOf[StructureType].fields

// TODO: incompatible types
hm should have size (3)
hm("a") should be (new AtomicType(StringType))
hm("b") shouldBe a [StructureType]
val inner = hm("b").asInstanceOf[StructureType].fields
inner should have size 3
inner("x") should be (new AtomicType(StringType))
inner("y") should be (new AtomicType(IntegerType))
inner("z") should be (new AtomicType(StringType))
hm("c") should be (new AtomicType(StringType))
}
}

0 comments on commit 0b03795

Please sign in to comment.