Skip to content

Commit 0a42955

Browse files
mpotterwilwade
andauthored
Use the Double primative for JSON Schema "number" type (#111)
Problem ======= The JSON Schema stipulates that [a "number" type can be an integer, floating point, or exponential notation](https://json-schema.org/understanding-json-schema/reference/numeric). Currently, the "number" type is treated the same as an integer when instantiating `fromJSONSchema`. In those cases, providing a float value of e.g. `2.5` will fail because it can't be converted into a BigInt. Solution ======== I changed the primitive to Double for "number" types when creating a schema from JSON. Co-authored-by: Wil Wade <[email protected]>
1 parent e0f1ebd commit 0a42955

5 files changed

+54
-8
lines changed

lib/jsonSchema.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ const fromJsonSchemaArray = (fieldValue: SupportedJSONSchema4, optionalFieldList
6969
}
7070
return fields.createListField('UTF8', optionalFieldList);
7171
case 'integer':
72-
case 'number':
7372
return fields.createListField('INT64', optionalFieldList);
73+
case 'number':
74+
return fields.createListField('DOUBLE', optionalFieldList);
7475
case 'boolean':
7576
return fields.createListField('BOOLEAN', optionalFieldList);
7677
case 'object':
@@ -96,8 +97,9 @@ const fromJsonSchemaField = (jsonSchema: JSONSchema4) => (fieldName: string, fie
9697
}
9798
return fields.createStringField(optional);
9899
case 'integer':
99-
case 'number':
100100
return fields.createIntField(64, optional);
101+
case 'number':
102+
return fields.createDoubleField(optional);
101103
case 'boolean':
102104
return fields.createBooleanField(optional);
103105
case 'array':

test/jsonSchema.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ describe("Json Schema Conversion Test File", async function () {
5858
"properties": {
5959
"string_field": { "type": "string" },
6060
"int_field": { "type": "integer" },
61+
"number_field": { "type": "number" },
6162
"array_field": {
6263
"type": "array",
6364
"items": { "type": "string" },
@@ -118,6 +119,7 @@ describe("Json Schema Conversion Test File", async function () {
118119
const row1 = {
119120
string_field: 'string value',
120121
int_field: 10n,
122+
number_field: 2.5,
121123
timestamp_array_field: { list: [{ element: new Date("2023-01-01 GMT") }] },
122124

123125
timestamp_field: new Date("2023-01-01 GMT"),

test/test-files/array.schema.result.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"fields": {
1010
"element": {
1111
"optional": true,
12-
"type": "INT64",
12+
"type": "DOUBLE",
1313
"encoding": "PLAIN",
1414
"compression": "UNCOMPRESSED"
1515
}
@@ -61,7 +61,7 @@
6161
"fields": {
6262
"element": {
6363
"name": "element",
64-
"primitiveType": "INT64",
64+
"primitiveType": "DOUBLE",
6565
"path": [
6666
"numberArray",
6767
"list",
@@ -148,7 +148,7 @@
148148
"fields": {
149149
"element": {
150150
"name": "element",
151-
"primitiveType": "INT64",
151+
"primitiveType": "DOUBLE",
152152
"path": [
153153
"numberArray",
154154
"list",
@@ -179,7 +179,7 @@
179179
"fields": {
180180
"element": {
181181
"name": "element",
182-
"primitiveType": "INT64",
182+
"primitiveType": "DOUBLE",
183183
"path": [
184184
"numberArray",
185185
"list",
@@ -195,7 +195,7 @@
195195
},
196196
{
197197
"name": "element",
198-
"primitiveType": "INT64",
198+
"primitiveType": "DOUBLE",
199199
"path": [
200200
"numberArray",
201201
"list",

test/test-files/json-schema-test-file.result.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type_length": null,
55
"repetition_type": null,
66
"name": "root",
7-
"num_children": 7,
7+
"num_children": 8,
88
"converted_type": null,
99
"scale": null,
1010
"precision": null,
@@ -35,6 +35,18 @@
3535
"field_id": null,
3636
"logicalType": null
3737
},
38+
{
39+
"type": 5,
40+
"type_length": null,
41+
"repetition_type": 1,
42+
"name": "number_field",
43+
"num_children": null,
44+
"converted_type": null,
45+
"scale": null,
46+
"precision": null,
47+
"field_id": null,
48+
"logicalType": null
49+
},
3850
{
3951
"type": null,
4052
"type_length": null,

test/test-files/json-schema-test-file.schema.result.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
"encoding": "PLAIN",
1313
"compression": "UNCOMPRESSED"
1414
},
15+
"number_field": {
16+
"optional": true,
17+
"type": "DOUBLE",
18+
"encoding": "PLAIN",
19+
"compression": "UNCOMPRESSED"
20+
},
1521
"array_field": {
1622
"type": "LIST",
1723
"optional": true,
@@ -157,6 +163,18 @@
157163
"rLevelMax": 0,
158164
"dLevelMax": 1
159165
},
166+
"number_field": {
167+
"name": "number_field",
168+
"primitiveType": "DOUBLE",
169+
"path": [
170+
"number_field"
171+
],
172+
"repetitionType": "OPTIONAL",
173+
"encoding": "PLAIN",
174+
"compression": "UNCOMPRESSED",
175+
"rLevelMax": 0,
176+
"dLevelMax": 1
177+
},
160178
"array_field": {
161179
"name": "array_field",
162180
"path": [
@@ -501,6 +519,18 @@
501519
"rLevelMax": 0,
502520
"dLevelMax": 1
503521
},
522+
{
523+
"name": "number_field",
524+
"primitiveType": "DOUBLE",
525+
"path": [
526+
"number_field"
527+
],
528+
"repetitionType": "OPTIONAL",
529+
"encoding": "PLAIN",
530+
"compression": "UNCOMPRESSED",
531+
"rLevelMax": 0,
532+
"dLevelMax": 1
533+
},
504534
{
505535
"name": "array_field",
506536
"path": [

0 commit comments

Comments
 (0)