SQL: Fix multi-line CLI commands to join correctly (elastic/x-pack-elasticsearch#3516)

Previously multi-line CLI SQL statements were joined, but the space command was
missing, so a command like:

```
sql> SHOW
   | functions;
```

Would incorrectly parse as "showfunctions" and throw an error.

This fixes the behavior and adds a test for multi-line commands.

Resolves elastic/x-pack-elasticsearch#3410

Original commit: elastic/x-pack-elasticsearch@3870924ccd
This commit is contained in:
Lee Hinman 2018-01-09 16:55:30 -07:00 committed by GitHub
parent 8257d8d76a
commit 0865063740
3 changed files with 12 additions and 2 deletions

View File

@ -8,6 +8,7 @@ package org.elasticsearch.xpack.qa.sql.cli;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.SpecialPermission;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.logging.Loggers;
import java.io.BufferedReader;
@ -23,6 +24,7 @@ import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;
@ -161,7 +163,8 @@ public class RemoteCli implements Closeable {
* terminal in unix mode to make the tests consistent. */
out.print(command + ";\n");
out.flush();
String firstResponse = "[?1h=[33msql> [0m" + command + ";";
final String firstResponse = "[?1h=[33msql> [0m" +
Strings.collectionToDelimitedString(Strings.splitSmart(command, "\n", false), "[?1h=[33m | [0m") + ";";
String firstLine = readLine();
assertThat(firstLine, startsWith(firstResponse));
return firstLine.substring(firstResponse.length());

View File

@ -19,6 +19,13 @@ public abstract class SelectTestCase extends CliIntegrationTestCase {
assertThat(readLine(), containsString("test_value"));
assertEquals("", readLine());
}
public void testMultiLineSelect() throws IOException {
index("test", body -> body.field("test_field", "test_value"));
assertThat(command("SELECT *\nFROM\ntest"), containsString("test_field"));
assertThat(readLine(), containsString("----------"));
assertThat(readLine(), containsString("test_value"));
assertEquals("", readLine());
}
public void testSelectWithWhere() throws IOException {
index("test", body -> body.field("test_field", "test_value1").field("i", 1));

View File

@ -40,8 +40,8 @@ public class CliRepl {
line = line.trim();
if (!line.endsWith(";")) {
multiLine.append(" ");
multiLine.append(line);
multiLine.append(" ");
prompt = MULTI_LINE_PROMPT;
continue;
}