Skip to content

Commit aa9a2a3

Browse files
committed
Use jsonb instead of varchar for complex enums (close #31)
1 parent 3dd180b commit aa9a2a3

File tree

3 files changed

+4
-13
lines changed

3 files changed

+4
-13
lines changed

modules/common/src/main/scala/com/snowplowanalytics/snowplow/postgres/shredding/Type.scala

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ object Type {
5757
/** Derive a Postgres type, given JSON Schema */
5858
def getDataType(properties: Schema, suggestions: List[DataTypeSuggestion]): Type =
5959
suggestions match {
60-
case Nil => Type.Varchar(DefaultVarcharSize) // Generic
60+
case Nil => Type.Jsonb // Generic
6161
case suggestion :: tail =>
6262
suggestion(properties) match {
6363
case Some(format) => format
@@ -69,8 +69,7 @@ object Type {
6969
val complexEnumSuggestion: DataTypeSuggestion = properties =>
7070
properties.enum match {
7171
case Some(enums) if isComplexEnum(enums.value) =>
72-
val longest = excludeNull(enums.value).map(_.noSpaces.length).maximumOption.getOrElse(16)
73-
Some(Type.Varchar(longest))
72+
Some(Type.Jsonb)
7473
case _ => None
7574
}
7675

@@ -167,11 +166,6 @@ object Type {
167166
case Some(max) => Some(Type.Varchar(max))
168167
case None => Some(Type.Varchar(DefaultVarcharSize))
169168
}
170-
case (_, _, Some(e)) =>
171-
e.value.map(jsonLength).maximumOption match {
172-
case Some(max) => Some(Type.Varchar(max))
173-
case None => Some(Type.Varchar(DefaultVarcharSize))
174-
}
175169
case _ => None
176170
}
177171
}
@@ -189,9 +183,6 @@ object Type {
189183
varcharSuggestion
190184
)
191185

192-
private def jsonLength(json: Json): Int =
193-
json.fold(0, b => b.toString.length, _ => json.noSpaces.length, _.length, _ => json.noSpaces.length, _ => json.noSpaces.length)
194-
195186
/**
196187
* Get set of types or enum as string excluding null
197188
*

modules/common/src/test/scala/com/snowplowanalytics/snowplow/postgres/shredding/TypeSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class TypeSpec extends Specification {
9595

9696
"return jsonb type for a complex enum field" >> {
9797
val properties = Schema(enum = Some(CommonProperties.Enum(List(Json.fromInt(12345), Json.fromString("y")))))
98-
Type.getDataType(properties, Type.dataTypeSuggestions) must_== Type.Varchar(5)
98+
Type.getDataType(properties, Type.dataTypeSuggestions) must_== Type.Jsonb
9999
}
100100

101101
}

modules/common/src/test/scala/com/snowplowanalytics/snowplow/postgres/streaming/sinkspec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class sinkspec extends Database {
9898
ColumnInfo("required_union", None, false, "jsonb", None),
9999
ColumnInfo("id", None, true, "uuid", None),
100100
ColumnInfo("nested.a", None, true, "double precision", None),
101-
ColumnInfo("nested.b", None, true, "character varying", Some(4096)),
101+
ColumnInfo("nested.b", None, true, "jsonb", None),
102102
ColumnInfo("some_array", None, true, "jsonb", None),
103103
ColumnInfo("nested.c", None, true, "bigint", None),
104104
ColumnInfo("some_date", None, true, "timestamp without time zone", None),

0 commit comments

Comments
 (0)