-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use 'Numeric' instead of 'Float' as BQ column type when field type in…
… the json schema is number (close #112)
- Loading branch information
Showing
10 changed files
with
305 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
.../src/main/scala/com.snowplowanalytics/iglu.schemaddl/jsonschema/suggestion/decimals.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.snowplowanalytics.iglu.schemaddl.jsonschema.suggestion | ||
|
||
import numericType._ | ||
import io.circe.Json | ||
import com.snowplowanalytics.iglu.schemaddl.jsonschema.properties.NumberProperty | ||
import com.snowplowanalytics.iglu.schemaddl.jsonschema.Schema | ||
|
||
private[schemaddl] object decimals { | ||
|
||
def integerType(schema: Schema): NumericType = | ||
(schema.minimum, schema.maximum) match { | ||
case (Some(min), Some(max)) => | ||
val minDecimal = min.getAsDecimal | ||
val maxDecimal = max.getAsDecimal | ||
if (maxDecimal <= Int.MaxValue && minDecimal >= Int.MinValue) NumericType.Int32 | ||
else if (maxDecimal <= Long.MaxValue && minDecimal >= Long.MinValue) NumericType.Int64 | ||
else NumericType.Decimal( | ||
(maxDecimal.precision - maxDecimal.scale).max(minDecimal.precision - minDecimal.scale), 0 | ||
) | ||
case _ => NumericType.Int64 | ||
} | ||
|
||
def numericWithMultiple(mult: NumberProperty.MultipleOf.NumberMultipleOf, | ||
maximum: Option[NumberProperty.Maximum], | ||
minimum: Option[NumberProperty.Minimum]): NumericType = | ||
(maximum, minimum) match { | ||
case (Some(max), Some(min)) => | ||
val topPrecision = max match { | ||
case NumberProperty.Maximum.IntegerMaximum(max) => | ||
BigDecimal(max).precision + mult.value.scale | ||
case NumberProperty.Maximum.NumberMaximum(max) => | ||
max.precision - max.scale + mult.value.scale | ||
} | ||
val bottomPrecision = min match { | ||
case NumberProperty.Minimum.IntegerMinimum(min) => | ||
BigDecimal(min).precision + mult.value.scale | ||
case NumberProperty.Minimum.NumberMinimum(min) => | ||
min.precision - min.scale + mult.value.scale | ||
} | ||
|
||
NumericType.Decimal(topPrecision.max(bottomPrecision), mult.value.scale) | ||
|
||
case _ => | ||
NumericType.Double | ||
} | ||
|
||
|
||
def numericEnum(enums: List[Json]): Option[NullableWrapper] = { | ||
def go(scale: Int, max: BigDecimal, nullable: Boolean, enums: List[Json]): Option[NullableWrapper] = | ||
enums match { | ||
case Nil => | ||
val t = if ((scale == 0) && (max <= Int.MaxValue)) NumericType.Int32 | ||
else if ((scale == 0) && (max <= Long.MaxValue)) NumericType.Int64 | ||
else NumericType.Decimal(max.precision - max.scale + scale, scale) | ||
|
||
Some(if (nullable) NullableWrapper.NullableValue(t) | ||
else NullableWrapper.NotNullValue(t)) | ||
|
||
case Json.Null :: tail => go(scale, max, true, tail) | ||
case h :: tail => | ||
h.asNumber.flatMap(_.toBigDecimal) match { | ||
case Some(bigDecimal) => | ||
val nextScale = scale.max(bigDecimal.scale) | ||
val nextMax = (if (bigDecimal > 0) bigDecimal else -bigDecimal).max(max) | ||
go(nextScale, nextMax, nullable, tail) | ||
case None => None | ||
} | ||
} | ||
|
||
go(0, 0, false, enums) | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...c/main/scala/com.snowplowanalytics/iglu.schemaddl/jsonschema/suggestion/numericType.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.snowplowanalytics.iglu.schemaddl.jsonschema.suggestion | ||
|
||
private[schemaddl] object numericType { | ||
sealed trait NumericType extends Product with Serializable | ||
|
||
object NumericType { | ||
case object Double extends NumericType | ||
|
||
case object Int32 extends NumericType | ||
|
||
case object Int64 extends NumericType | ||
|
||
case class Decimal(precision: Int, scale: Int) extends NumericType | ||
} | ||
|
||
sealed trait NullableWrapper | ||
|
||
object NullableWrapper { | ||
|
||
case class NullableValue(value: NumericType) extends NullableWrapper | ||
|
||
case class NotNullValue(value: NumericType) extends NullableWrapper | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.