Skip to content

Commit

Permalink
Parsing complex structs
Browse files Browse the repository at this point in the history
  • Loading branch information
ptiurin committed Dec 12, 2024
1 parent 36b7ad2 commit 6b1fdc6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class ColumnType {
private static final Set<String> TIMEZONES = Arrays.stream(TimeZone.getAvailableIDs())
.collect(Collectors.toCollection(HashSet::new));
private static final Pattern COMMA_WITH_SPACES = Pattern.compile("\\s*,\\s*");
// Regex to split on comma and ignoring commas that are between parenthesis
private static final String COMPLEX_TYPE_PATTERN = ",(?![^()]*\\))";
@EqualsAndHashCode.Exclude
String name;
FireboltDataType dataType;
Expand Down Expand Up @@ -109,10 +111,9 @@ private static List<ColumnType> getCollectionSubType(FireboltDataType fireboltDa
}

if (fireboltDataType.equals(TUPLE)) {
types = typeWithoutNullKeyword.split(",(?![^()]*\\))"); // Regex to split on comma and ignoring comma that are between
// parenthesis
types = typeWithoutNullKeyword.split(COMPLEX_TYPE_PATTERN);
} else if (fireboltDataType.equals(STRUCT)) {
types = typeWithoutNullKeyword.split(",");
types = typeWithoutNullKeyword.split(COMPLEX_TYPE_PATTERN);
} else {
types = new String[] {typeWithoutNullKeyword};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1490,8 +1490,8 @@ void shouldReturnStruct() throws SQLException {
assertEquals("{\"a\":\"1\"}", resultSet.getObject("a_struct"));
assertEquals("{\"a\":[1,2,3]}", resultSet.getObject(5));
assertEquals("{\"a\":[1,2,3]}", resultSet.getObject("array_struct"));
assertEquals("{\"x\":\"2\",\"a\":{\"b\":\"1\"}}", resultSet.getObject(6));
assertEquals("{\"x\":\"2\",\"a\":{\"b\":\"1\"}}", resultSet.getObject("nested_struct"));
assertEquals("{\"x\":\"2\",\"a\":{\"b\":\"1\",\"c\":\"3\"}}", resultSet.getObject(6));
assertEquals("{\"x\":\"2\",\"a\":{\"b\":\"1\",\"c\":\"3\"}}", resultSet.getObject("nested_struct"));
// Returns native JDBC type
for (int i = 2; i <= 6; i++) {
assertEquals(Types.VARCHAR, resultSet.getMetaData().getColumnType(i));
Expand All @@ -1501,7 +1501,7 @@ void shouldReturnStruct() throws SQLException {
assertEquals("STRUCT(A INT)", resultSet.getMetaData().getColumnTypeName(3));
assertEquals("STRUCT(A INT)", resultSet.getMetaData().getColumnTypeName(4));
assertEquals("STRUCT(A ARRAY(INT))", resultSet.getMetaData().getColumnTypeName(5));
assertEquals("STRUCT(X INT, A STRUCT(B INT))", resultSet.getMetaData().getColumnTypeName(6));
assertEquals("STRUCT(X INT, A STRUCT(B INT, C INT))", resultSet.getMetaData().getColumnTypeName(6));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
id null_struct an_empty_struct a_struct array_struct nested_struct
Int64 struct(a int null) struct(a int) struct(a int) struct(a array(int)) struct(x int, a struct(b int))
1 {"a":null} {"a":"1"} {"a":[1,2,3]} {"x":"2","a":{"b":"1"}}
2 {"a":null} {"a":"2"} {"a":[1,2,3]} {"x":"2","a":{"b":"1"}}
Int64 struct(a int null) struct(a int) struct(a int) struct(a array(int)) struct(x int, a struct(b int, c int))
1 {"a":null} {"a":"1"} {"a":[1,2,3]} {"x":"2","a":{"b":"1","c":"3"}}
2 {"a":null} {"a":"2"} {"a":[1,2,3]} {"x":"2","a":{"b":"1","c":"3"}}

0 comments on commit 6b1fdc6

Please sign in to comment.