-
Notifications
You must be signed in to change notification settings - Fork 400
fix: Add JSON column type for SQL Views #23773
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
Changes from all commits
b0b4635
81c5778
0827543
085f4f3
d629e34
6f6feaa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Copyright (c) 2004-2022, University of Oslo | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are met: | ||
| * | ||
| * 1. Redistributions of source code must retain the above copyright notice, this | ||
| * list of conditions and the following disclaimer. | ||
| * | ||
| * 2. Redistributions in binary form must reproduce the above copyright notice, | ||
| * this list of conditions and the following disclaimer in the documentation | ||
| * and/or other materials provided with the distribution. | ||
| * | ||
| * 3. Neither the name of the copyright holder nor the names of its contributors | ||
| * may be used to endorse or promote products derived from this software without | ||
| * specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
| * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
| * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
| * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
| * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package org.hisp.dhis.common; | ||
|
|
||
| /** Wraps a raw JSON string so that serializers can emit it as inline JSON rather than a string. */ | ||
| public record RawJsonValue(String value) {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * Copyright (c) 2004-2022, University of Oslo | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are met: | ||
| * | ||
| * 1. Redistributions of source code must retain the above copyright notice, this | ||
| * list of conditions and the following disclaimer. | ||
| * | ||
| * 2. Redistributions in binary form must reproduce the above copyright notice, | ||
| * this list of conditions and the following disclaimer in the documentation | ||
| * and/or other materials provided with the distribution. | ||
| * | ||
| * 3. Neither the name of the copyright holder nor the names of its contributors | ||
| * may be used to endorse or promote products derived from this software without | ||
| * specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
| * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
| * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
| * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
| * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package org.hisp.dhis.common.adapter; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonFactory; | ||
| import com.fasterxml.jackson.core.JsonGenerator; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import java.io.StringWriter; | ||
| import java.util.List; | ||
| import org.hisp.dhis.common.RawJsonValue; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class JacksonRowDataSerializerTest { | ||
|
|
||
| private final JacksonRowDataSerializer serializer = new JacksonRowDataSerializer(); | ||
|
|
||
| @Test | ||
| void regularStringValuesAreWrappedInJsonStrings() throws Exception { | ||
| List<List<Object>> rows = List.of(List.of("hello", "world")); | ||
|
|
||
| assertEquals("[[\"hello\",\"world\"]]", serialize(rows)); | ||
| } | ||
|
|
||
| @Test | ||
| void rawJsonValueIsEmittedAsInlineJson() throws Exception { | ||
| List<List<Object>> rows = List.of(List.of("Alice", new RawJsonValue("{\"key\":\"value\"}"))); | ||
|
|
||
| assertEquals("[[\"Alice\",{\"key\":\"value\"}]]", serialize(rows)); | ||
| } | ||
|
|
||
| @Test | ||
| void rawJsonArrayIsEmittedAsInlineJsonArray() throws Exception { | ||
| List<List<Object>> rows = List.of(List.of(new RawJsonValue("[1,2,3]"))); | ||
|
|
||
| assertEquals("[[[1,2,3]]]", serialize(rows)); | ||
| } | ||
|
|
||
| @Test | ||
| void nullRawJsonValueIsEmittedAsEmptyString() throws Exception { | ||
| List<Object> row = new java.util.ArrayList<>(); | ||
| row.add(null); | ||
| List<List<Object>> rows = List.of(row); | ||
|
|
||
| assertEquals("[[\"\"]]", serialize(rows)); | ||
| } | ||
|
|
||
| private String serialize(List<List<Object>> rows) throws Exception { | ||
| StringWriter writer = new StringWriter(); | ||
| JsonGenerator generator = new JsonFactory().createGenerator(writer); | ||
| serializer.serialize(rows, generator, new ObjectMapper().getSerializerProvider()); | ||
| generator.flush(); | ||
| return writer.toString(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,7 @@ | |
| import org.apache.commons.lang3.StringUtils; | ||
| import org.apache.commons.lang3.math.NumberUtils; | ||
| import org.hisp.dhis.common.Grid; | ||
| import org.hisp.dhis.common.RawJsonValue; | ||
| import org.hisp.dhis.common.TransactionMode; | ||
| import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore; | ||
| import org.hisp.dhis.query.QueryParserException; | ||
|
|
@@ -56,6 +57,7 @@ | |
| import org.springframework.jdbc.BadSqlGrammarException; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
| import org.springframework.jdbc.support.rowset.SqlRowSet; | ||
| import org.springframework.jdbc.support.rowset.SqlRowSetMetaData; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| /** | ||
|
|
@@ -141,7 +143,28 @@ public void populateSqlViewGrid( | |
| log.debug("Get view SQL: " + sql + ", max limit: " + maxLimit); | ||
|
|
||
| grid.addHeaders(rs); | ||
| grid.addRows(rs, maxLimit); | ||
| populateRows(grid, rs, maxLimit); | ||
| } | ||
|
|
||
| private static void populateRows(Grid grid, SqlRowSet rs, int maxLimit) { | ||
| SqlRowSetMetaData rsmd = rs.getMetaData(); | ||
| int cols = rsmd.getColumnCount(); | ||
| boolean[] isJson = new boolean[cols + 1]; | ||
| for (int i = 1; i <= cols; i++) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to keep the code a bit more condense, you could move the content of this for loop into the next, and keep everything in one, instead of spreading the column related code in two blocks. Performance wise, I don't see any advantage of pre-calculating these booleans |
||
| String typeName = rsmd.getColumnTypeName(i); | ||
| isJson[i] = "json".equalsIgnoreCase(typeName) || "jsonb".equalsIgnoreCase(typeName); | ||
| } | ||
| while (rs.next()) { | ||
| grid.addRow(); | ||
| for (int i = 1; i <= cols; i++) { | ||
| Object value = rs.getObject(i); | ||
| grid.addValue(isJson[i] && value != null ? new RawJsonValue(value.toString()) : value); | ||
| if (maxLimit > 0 && i > maxLimit) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this is a bug that's already present, but caught my attention looking at the code: i represent the number of columns, while the exception highlights the number of rows. This is however also the same problem in the old code (addRows). |
||
| throw new IllegalStateException( | ||
| "Number of rows produced by query is larger than the max limit: " + maxLimit); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
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.
from this PR, as far as I can tell in the code, this piece of code is redundant.
Since you're already adding a condition in JacksonRowDataSerializer to not use maybeFormat if the field is an instanceof RawJsonValue, you will in theory never get it in this code execution.
I'd say remove either or, unless there's some reason I cant see we need both checks :)