fix bugs with nested column jsonpath parser (#12831)

This commit is contained in:
Clint Wylie 2022-08-02 11:38:25 -07:00 committed by GitHub
parent eabce8a159
commit 6981b1cc12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 9 deletions

View File

@ -33,21 +33,20 @@ public class NestedPathFinder
public static String toNormalizedJsonPath(List<NestedPathPart> paths)
{
if (paths.isEmpty()) {
return "$.";
return "$";
}
StringBuilder bob = new StringBuilder();
boolean first = true;
for (NestedPathPart partFinder : paths) {
if (partFinder instanceof NestedPathField) {
if (first) {
bob.append("$.");
} else {
bob.append(".");
bob.append("$");
}
final String id = partFinder.getPartIdentifier();
if (id.contains(".") || id.contains("'") || id.contains("\"") || id.contains("[") || id.contains("]")) {
bob.append("['").append(id).append("']");
} else {
bob.append(".");
bob.append(id);
}
} else if (partFinder instanceof NestedPathArrayElement) {
@ -124,6 +123,13 @@ public class NestedPathFinder
quoteMark = i;
partMark = i + 1;
} else if (current == '\'' && quoteMark >= 0 && path.charAt(i - 1) != '\\') {
if (path.charAt(i + 1) != ']') {
if (arrayMark >= 0) {
continue;
}
badFormatJsonPath(path, "closing ' must immediately precede ']'");
}
parts.add(new NestedPathField(getPathSubstring(path, partMark, i)));
dotMark = -1;
quoteMark = -1;
@ -131,9 +137,6 @@ public class NestedPathFinder
if (++i == path.length()) {
break;
}
if (path.charAt(i) != ']') {
badFormatJsonPath(path, "closing ' must immediately precede ']'");
}
partMark = i + 1;
arrayMark = -1;
}

View File

@ -100,6 +100,7 @@ public class FrameStorageAdapterTest
@Before
public void setUp()
{
queryableAdapter = new QueryableIndexStorageAdapter(TestIndex.getMMappedTestIndex());
frameSegment = FrameTestUtil.adapterToFrameSegment(queryableAdapter, frameType);
frameAdapter = frameSegment.asStorageAdapter();

View File

@ -309,9 +309,14 @@ public class NestedPathFinderTest
NestedPathFinder.toNormalizedJqPath(pathParts)
);
Assert.assertEquals(
"$.['x.y.z][\\']][]'].['13234.12[]][23'].['fo.o'].['.b.a.r.']",
"$['x.y.z][\\']][]']['13234.12[]][23']['fo.o']['.b.a.r.']",
NestedPathFinder.toNormalizedJsonPath(pathParts)
);
pathParts = NestedPathFinder.parseJsonPath("$['hell'o']");
Assert.assertEquals(1, pathParts.size());
Assert.assertEquals("hell'o", pathParts.get(0).getPartIdentifier());
Assert.assertEquals("$['hell'o']", NestedPathFinder.toNormalizedJsonPath(pathParts));
}
@Test

View File

@ -1955,7 +1955,7 @@ public class CalciteNestedDataQueryTest extends BaseCalciteQueryTest
.build()
),
ImmutableList.of(
new Object[]{"[\"$.\"]", 5L},
new Object[]{"[\"$\"]", 5L},
new Object[]{"[\"$.n.x\",\"$.array[0]\",\"$.array[1]\"]", 2L}
)
);