Skip to content

Commit

Permalink
Schema DDL: add support for 'date' format (close #256)
Browse files Browse the repository at this point in the history
  • Loading branch information
miike authored and oguzhanunlu committed Dec 14, 2017
1 parent 2744dac commit 4a9e3e9
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ object StringProperties {
case object Ipv6Format extends Format { val asString = "ipv6" }
case object EmailFormat extends Format { val asString = "email" }
case object DateTimeFormat extends Format { val asString = "date-time" }
case object DateFormat extends Format { val asString = "date" }
case object HostNameFormat extends Format { val asString = "hostname" }

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ object StringSerializers {
case JString(format) if format == "email" => EmailFormat
case JString(format) if format == "hostname" => HostNameFormat
case JString(format) if format == "date-time" => DateTimeFormat
case JString(format) if format == "date" => DateFormat
case JString(format) => CustomFormat(format)
case x => throw new MappingException("Format must be string")
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ case object RedshiftTimestamp extends DataType {
def toDdl = "TIMESTAMP"
}

case object RedshiftDate extends DataType {
def toDdl = "DATE"
}

case object RedshiftSmallInt extends DataType {
def toDdl = "SMALLINT"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ object DdlGenerator {
complexEnumSuggestion,
productSuggestion,
timestampSuggestion,
dateSuggestion,
arraySuggestion,
integerSuggestion,
numberSuggestion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ object TypeSuggestions {
case _ => None
}

val dateSuggestion: DataTypeSuggestion = (properties, columnName) =>
(properties.get("type"), properties.get("format")) match {
case(Some(types), Some("date")) if types.contains("string") =>
Some(RedshiftDate)
case _ => None
}

val arraySuggestion: DataTypeSuggestion = (properties, columnName) =>
properties.get("type") match {
case Some(types) if types.contains("array") =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class TypeSuggestionsSpec extends Specification { def is = s2"""
recognize string,null maxLength == minLength as CHAR $e4
recognize number with product type $e5
recognize integer with product type $e6
recognize timestamp $e7
recognize full date $e8
"""

def e1 = {
Expand Down Expand Up @@ -55,4 +57,14 @@ class TypeSuggestionsSpec extends Specification { def is = s2"""
val props = Map("type" -> "integer,null")
DdlGenerator.getDataType(props, 16, "somecolumn") must beEqualTo(RedshiftBigInt)
}

def e7 = {
val props = Map("type" -> "string", "format" -> "date-time")
DdlGenerator.getDataType(props, 16, "somecolumn") must beEqualTo(RedshiftTimestamp)
}

def e8 = {
val props = Map("type" -> "string", "format" -> "date")
DdlGenerator.getDataType(props, 16, "somecolumn") must beEqualTo(RedshiftDate)
}
}

0 comments on commit 4a9e3e9

Please sign in to comment.