-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use 'Numeric' instead of 'Float' as BQ column type when field type in the json schema is number #162
Open
voropaevp
wants to merge
4
commits into
develop
Choose a base branch
from
fix/bigquery-suggestion-decimal
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Use 'Numeric' instead of 'Float' as BQ column type when field type in the json schema is number #162
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove this entire block starting from line 59, and let everything fall through to the next
case
on line 67?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It won't fall though there, check the onlyNumeric implementation L180.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see. OK how about this then: