-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CALCITE-6832] Redundant fields/nullable entries in STRUCT serializat…
…ion to JSON The JSON serialization of a RelDatatypeField of type STRUCT contains redundant fields and nullable entries along with unnecessary nesting. The redundancy leads to wasted space and convoluted representation that is hard to follow especially when there are nested STRUCTS.
- Loading branch information
Showing
4 changed files
with
107 additions
and
19 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
53 changes: 53 additions & 0 deletions
53
core/src/test/java/org/apache/calcite/rel/externalize/RelJsonTest.java
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,53 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.calcite.rel.externalize; | ||
|
||
import org.apache.calcite.rel.type.RelDataType; | ||
import org.apache.calcite.rel.type.RelDataTypeFactory; | ||
import org.apache.calcite.rel.type.RelDataTypeField; | ||
import org.apache.calcite.rel.type.RelDataTypeFieldImpl; | ||
import org.apache.calcite.rel.type.RelDataTypeSystem; | ||
import org.apache.calcite.sql.type.SqlTypeFactoryImpl; | ||
import org.apache.calcite.sql.type.SqlTypeName; | ||
import org.apache.calcite.test.DiffRepository; | ||
import org.apache.calcite.util.JsonBuilder; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Unit tests for @{@link RelJson}. | ||
*/ | ||
public class RelJsonTest { | ||
|
||
private static final DiffRepository REPO = DiffRepository.lookup(RelJsonTest.class); | ||
|
||
@Test void testToJsonWithStructRelDatatypeField() { | ||
RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT); | ||
RelDataType type = typeFactory.builder() | ||
.add("street", SqlTypeName.VARCHAR, 50) | ||
.add("number", SqlTypeName.INTEGER) | ||
.add("building", SqlTypeName.VARCHAR, 20).nullable(true) | ||
.build(); | ||
RelDataTypeField address = | ||
new RelDataTypeFieldImpl("address", 0, type); | ||
|
||
JsonBuilder builder = new JsonBuilder(); | ||
Object jsonObj = RelJson.create().withJsonBuilder(builder).toJson(address); | ||
REPO.assertEquals("content", "${content}", builder.toJsonString(jsonObj)); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
core/src/test/resources/org/apache/calcite/rel/externalize/RelJsonTest.xml
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,46 @@ | ||
<?xml version="1.0" ?> | ||
<!-- | ||
~ Licensed to the Apache Software Foundation (ASF) under one or more | ||
~ contributor license agreements. See the NOTICE file distributed with | ||
~ this work for additional information regarding copyright ownership. | ||
~ The ASF licenses this file to you under the Apache License, Version 2.0 | ||
~ (the "License"); you may not use this file except in compliance with | ||
~ the License. You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
--> | ||
<Root> | ||
<TestCase name="testToJsonWithStructRelDatatypeField"> | ||
<Resource name="content"> | ||
<![CDATA[{ | ||
"fields": [ | ||
{ | ||
"type": "VARCHAR", | ||
"nullable": false, | ||
"precision": 50, | ||
"name": "street" | ||
}, | ||
{ | ||
"type": "INTEGER", | ||
"nullable": false, | ||
"name": "number" | ||
}, | ||
{ | ||
"type": "VARCHAR", | ||
"nullable": true, | ||
"precision": 20, | ||
"name": "building" | ||
} | ||
], | ||
"nullable": false, | ||
"name": "address" | ||
}]]> | ||
</Resource> | ||
</TestCase> | ||
</Root> |