Skip to content

Commit e608431

Browse files
Try: fix bwc tests
1 parent 2cabfcc commit e608431

File tree

1 file changed

+44
-4
lines changed
  • x-pack/plugin/esql/qa/server/multi-clusters/src/javaRestTest/java/org/elasticsearch/xpack/esql/ccq

1 file changed

+44
-4
lines changed

x-pack/plugin/esql/qa/server/multi-clusters/src/javaRestTest/java/org/elasticsearch/xpack/esql/ccq/MultiClusterSpecIT.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,12 @@ static CsvSpecReader.CsvTestCase convertToRemoteIndices(CsvSpecReader.CsvTestCas
258258
String[] localIndices = fromStatement.substring("FROM ".length()).split(",");
259259
final String remoteIndices;
260260
if (canUseRemoteIndicesOnly() && randomBoolean()) {
261-
remoteIndices = Arrays.stream(localIndices).map(index -> "*:" + index.trim()).collect(Collectors.joining(","));
261+
remoteIndices = Arrays.stream(localIndices)
262+
.map(index -> unquoteAndRequoteAsRemote(index.trim(), true))
263+
.collect(Collectors.joining(","));
262264
} else {
263265
remoteIndices = Arrays.stream(localIndices)
264-
.map(index -> "*:" + index.trim() + "," + index.trim())
266+
.map(index -> unquoteAndRequoteAsRemote(index.trim(), false))
265267
.collect(Collectors.joining(","));
266268
}
267269
var newFrom = "FROM " + remoteIndices + " " + commands[0].substring(fromStatement.length());
@@ -272,9 +274,13 @@ static CsvSpecReader.CsvTestCase convertToRemoteIndices(CsvSpecReader.CsvTestCas
272274
assert parts.length >= 2 : commands[0];
273275
String[] indices = parts[1].split(",");
274276
if (canUseRemoteIndicesOnly() && randomBoolean()) {
275-
parts[1] = Arrays.stream(indices).map(index -> "*:" + index.trim()).collect(Collectors.joining(","));
277+
parts[1] = Arrays.stream(indices)
278+
.map(index -> unquoteAndRequoteAsRemote(index.trim(), true))
279+
.collect(Collectors.joining(","));
276280
} else {
277-
parts[1] = Arrays.stream(indices).map(index -> "*:" + index.trim() + "," + index.trim()).collect(Collectors.joining(","));
281+
parts[1] = Arrays.stream(indices)
282+
.map(index -> unquoteAndRequoteAsRemote(index.trim(), false))
283+
.collect(Collectors.joining(","));
278284
}
279285
String newNewMetrics = String.join(" ", parts);
280286
testCase.query = newNewMetrics + query.substring(first.length());
@@ -307,6 +313,40 @@ static boolean hasIndexMetadata(String query) {
307313
return false;
308314
}
309315

316+
/**
317+
* Since partial quoting is prohibited, we need to take the index name, unquote it,
318+
* convert it to a remote index, and then requote it. For example, "employees" is unquoted,
319+
* turned into the remote index *:employees, and then requoted to get "*:employees".
320+
* @param index Name of the index.
321+
* @param asRemoteIndexOnly If the return needs to be in the form of "*:idx,idx" or "*:idx".
322+
* @return A remote index pattern that's requoted.
323+
*/
324+
private static String unquoteAndRequoteAsRemote(String index, boolean asRemoteIndexOnly) {
325+
index = index.trim();
326+
327+
int numOfQuotes = 0;
328+
for (; numOfQuotes < index.length(); numOfQuotes++) {
329+
if (index.charAt(numOfQuotes) != '"') {
330+
break;
331+
}
332+
}
333+
334+
String unquoted = unquote(index, numOfQuotes);
335+
if (asRemoteIndexOnly) {
336+
return quote("*:" + unquoted, numOfQuotes);
337+
} else {
338+
return quote("*:" + unquoted + "," + unquoted, numOfQuotes);
339+
}
340+
}
341+
342+
private static String quote(String index, int numOfQuotes) {
343+
return "\"".repeat(numOfQuotes) + index + "\"".repeat(numOfQuotes);
344+
}
345+
346+
private static String unquote(String index, int numOfQuotes) {
347+
return index.substring(numOfQuotes, index.length() - numOfQuotes);
348+
}
349+
310350
@Override
311351
protected boolean enableRoundingDoubleValuesOnAsserting() {
312352
return true;

0 commit comments

Comments
 (0)