mirror of
https://github.com/apache/druid.git
synced 2025-02-23 19:15:02 +00:00
fix bugs with nested column jsonpath parser (#12831)
This commit is contained in:
parent
eabce8a159
commit
6981b1cc12
@ -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;
|
||||
}
|
||||
|
@ -100,6 +100,7 @@ public class FrameStorageAdapterTest
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
|
||||
queryableAdapter = new QueryableIndexStorageAdapter(TestIndex.getMMappedTestIndex());
|
||||
frameSegment = FrameTestUtil.adapterToFrameSegment(queryableAdapter, frameType);
|
||||
frameAdapter = frameSegment.asStorageAdapter();
|
||||
|
@ -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
|
||||
|
@ -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}
|
||||
)
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user