You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2014/07/24 11:28:31 UTC
git commit: SqlRun: allow '!plan' after '!ok' for same SQL statement.
Repository: incubator-optiq
Updated Branches:
refs/heads/master 1781c8581 -> a10a8d778
SqlRun: allow '!plan' after '!ok' for same SQL statement.
Project: http://git-wip-us.apache.org/repos/asf/incubator-optiq/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-optiq/commit/a10a8d77
Tree: http://git-wip-us.apache.org/repos/asf/incubator-optiq/tree/a10a8d77
Diff: http://git-wip-us.apache.org/repos/asf/incubator-optiq/diff/a10a8d77
Branch: refs/heads/master
Commit: a10a8d778bcf6d042fa8637a5d3f34e1466324f3
Parents: 1781c85
Author: Julian Hyde <ju...@gmail.com>
Authored: Thu Jul 24 02:12:45 2014 -0700
Committer: Julian Hyde <ju...@gmail.com>
Committed: Thu Jul 24 02:12:45 2014 -0700
----------------------------------------------------------------------
.../java/net/hydromatic/optiq/tools/SqlRun.java | 197 ++++++++++---------
.../net/hydromatic/optiq/tools/SqlRunTest.java | 46 +++++
2 files changed, 151 insertions(+), 92 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-optiq/blob/a10a8d77/core/src/main/java/net/hydromatic/optiq/tools/SqlRun.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/net/hydromatic/optiq/tools/SqlRun.java b/core/src/main/java/net/hydromatic/optiq/tools/SqlRun.java
index 577ac4e..a19030a 100644
--- a/core/src/main/java/net/hydromatic/optiq/tools/SqlRun.java
+++ b/core/src/main/java/net/hydromatic/optiq/tools/SqlRun.java
@@ -179,79 +179,80 @@ public class SqlRun {
private Command nextCommand() throws IOException {
lines.clear();
- String line = nextLine();
- if (line == null) {
- return null;
- }
- if (line.startsWith("#") || line.isEmpty()) {
- return new CommentCommand(lines);
- }
- if (line.startsWith("!")) {
- line = line.substring(1);
- while (line.startsWith(" ")) {
- line = line.substring(1);
- }
- if (line.startsWith("use")) {
- String[] parts = line.split(" ");
- return new UseCommand(lines, parts[1]);
- }
- if (line.startsWith("ok")) {
- SqlCommand command = previousSqlCommand();
- return new CheckResultCommand(lines, command);
- }
- if (line.startsWith("plan")) {
- SqlCommand command = previousSqlCommand();
- return new ExplainCommand(lines, command);
- }
- if (line.startsWith("skip")) {
- return new SkipCommand(lines);
- }
- if (line.startsWith("set outputformat")) {
- String[] parts = line.split(" ");
- final OutputFormat outputFormat =
- OutputFormat.valueOf(parts[2].toUpperCase());
- return new SetCommand(lines, Property.OUTPUTFORMAT, outputFormat);
+ ImmutableList<String> content = ImmutableList.of();
+ for (;;) {
+ String line = nextLine();
+ if (line == null) {
+ return null;
}
- if (line.equals("if (false) {")) {
- List<String> ifLines = ImmutableList.copyOf(lines);
- lines.clear();
- Command command = new Parser().parse();
- return new IfCommand(ifLines, lines, command);
+ if (line.startsWith("#") || line.isEmpty()) {
+ return new CommentCommand(lines);
}
- if (line.equals("}")) {
- return null;
+ if (line.startsWith("!")) {
+ line = line.substring(1);
+ while (line.startsWith(" ")) {
+ line = line.substring(1);
+ }
+ if (line.startsWith("use")) {
+ String[] parts = line.split(" ");
+ return new UseCommand(lines, parts[1]);
+ }
+ if (line.startsWith("ok")) {
+ SqlCommand command = previousSqlCommand();
+ return new CheckResultCommand(lines, command, content);
+ }
+ if (line.startsWith("plan")) {
+ SqlCommand command = previousSqlCommand();
+ return new ExplainCommand(lines, command, content);
+ }
+ if (line.startsWith("skip")) {
+ return new SkipCommand(lines);
+ }
+ if (line.startsWith("set outputformat")) {
+ String[] parts = line.split(" ");
+ final OutputFormat outputFormat =
+ OutputFormat.valueOf(parts[2].toUpperCase());
+ return new SetCommand(lines, Property.OUTPUTFORMAT, outputFormat);
+ }
+ if (line.equals("if (false) {")) {
+ List<String> ifLines = ImmutableList.copyOf(lines);
+ lines.clear();
+ Command command = new Parser().parse();
+ return new IfCommand(ifLines, lines, command);
+ }
+ if (line.equals("}")) {
+ return null;
+ }
+ throw new RuntimeException("Unknown command: " + line);
}
- throw new RuntimeException("Unknown command: " + line);
- }
- buf.setLength(0);
- for (;;) {
+ buf.setLength(0);
boolean last = false;
- if (line.endsWith(";")) {
- last = true;
- line = line.substring(0, line.length() - 1);
+ for (;;) {
+ if (line.endsWith(";")) {
+ last = true;
+ line = line.substring(0, line.length() - 1);
+ }
+ buf.append(line).append("\n");
+ if (last) {
+ break;
+ }
+ line = nextLine();
+ if (line == null) {
+ throw new RuntimeException(
+ "end of file reached before end of SQL command");
+ }
+ if (line.startsWith("!") || line.startsWith("#")) {
+ pushLine();
+ break;
+ }
}
- buf.append(line).append("\n");
+ content = ImmutableList.copyOf(lines);
+ lines.clear();
if (last) {
- break;
- }
- line = nextLine();
- if (line == null) {
- throw new RuntimeException(
- "end of file reached before end of SQL command");
- }
- }
- final ImmutableList<String> sqlLines = ImmutableList.copyOf(lines);
- String sql = buf.toString();
- for (;;) {
- line = nextLine();
- if (line == null || line.startsWith("!") || line.startsWith("#")) {
- pushLine();
- break;
+ String sql = buf.toString();
+ return new SqlCommand(content, sql, null);
}
}
- final List<String> outputLines =
- lines.subList(sqlLines.size(), lines.size());
- return new SqlCommand(sqlLines, sql, outputLines);
}
private SqlCommand previousSqlCommand() {
@@ -492,16 +493,38 @@ public class SqlRun {
/** Command that executes a SQL statement and checks its result. */
class CheckResultCommand extends SimpleCommand {
private final SqlCommand sqlCommand;
+ private final ImmutableList<String> output;
- public CheckResultCommand(List<String> lines, SqlCommand sqlCommand) {
+ public CheckResultCommand(List<String> lines, SqlCommand sqlCommand,
+ ImmutableList<String> output) {
super(lines);
this.sqlCommand = sqlCommand;
+ this.output = output;
}
public void execute(boolean execute) throws Exception {
if (execute) {
- OutputFormat format = (OutputFormat) map.get(Property.OUTPUTFORMAT);
+ if (connection == null) {
+ throw new RuntimeException("no connection");
+ }
+ final Statement statement = connection.createStatement();
+ if (resultSet != null) {
+ resultSet.close();
+ }
+ try {
+ if (OptiqPrepareImpl.DEBUG) {
+ System.out.println("sql=" + sqlCommand.sql);
+ }
+ resultSet = null;
+ resultSetException = null;
+ resultSet = statement.executeQuery(sqlCommand.sql);
+ sort = !sqlCommand.sql.toUpperCase().contains("ORDER BY");
+ } catch (SQLException e) {
+ resultSetException = e;
+ }
if (resultSet != null) {
+ final OutputFormat format =
+ (OutputFormat) map.get(Property.OUTPUTFORMAT);
final List<String> headerLines = new ArrayList<String>();
final List<String> bodyLines = new ArrayList<String>();
final List<String> footerLines = new ArrayList<String>();
@@ -512,7 +535,7 @@ public class SqlRun {
// Strip the header and footer from the actual output.
// We assume that original and actual header have the same line count.
// Ditto footers.
- final List<String> lines = new ArrayList<String>(sqlCommand.output);
+ final List<String> lines = new ArrayList<String>(output);
for (String line : headerLines) {
if (!lines.isEmpty()) {
lines.remove(0);
@@ -551,6 +574,8 @@ public class SqlRun {
}
resultSet = null;
resultSetException = null;
+ } else {
+ echo(output);
}
echo(lines);
}
@@ -559,10 +584,14 @@ public class SqlRun {
/** Command that prints the plan for the current query. */
class ExplainCommand extends SimpleCommand {
private final SqlCommand sqlCommand;
+ private final ImmutableList<String> content;
- public ExplainCommand(List<String> lines, SqlCommand sqlCommand) {
+ public ExplainCommand(List<String> lines,
+ SqlCommand sqlCommand,
+ ImmutableList<String> content) {
super(lines);
this.sqlCommand = sqlCommand;
+ this.content = content;
}
public void execute(boolean execute) throws Exception {
@@ -578,6 +607,8 @@ public class SqlRun {
throw new AssertionError("explain returned more than 1 record");
}
printWriter.flush();
+ } else {
+ echo(content);
}
echo(lines);
}
@@ -586,36 +617,14 @@ public class SqlRun {
/** Command that executes a SQL statement. */
private class SqlCommand extends SimpleCommand {
private final String sql;
- private final ImmutableList<String> output;
protected SqlCommand(List<String> lines, String sql, List<String> output) {
super(lines);
this.sql = Preconditions.checkNotNull(sql);
- this.output = ImmutableList.copyOf(output);
}
public void execute(boolean execute) throws Exception {
echo(lines);
- if (execute) {
- if (connection == null) {
- throw new RuntimeException("no connection");
- }
- final Statement statement = connection.createStatement();
- if (resultSet != null) {
- resultSet.close();
- }
- try {
- if (OptiqPrepareImpl.DEBUG) {
- System.out.println("sql=" + sql);
- }
- resultSet = statement.executeQuery(sql);
- sort = !sql.toUpperCase().contains("ORDER BY");
- } catch (SQLException e) {
- resultSetException = e;
- }
- } else {
- echo(output);
- }
}
}
@@ -631,7 +640,11 @@ public class SqlRun {
OUTPUTFORMAT
}
- /** Command that executes a SQL statement and checks its result. */
+ /** Command that defines the current SQL statement.
+ *
+ * @see CheckResultCommand
+ * @see ExplainCommand
+ */
class SetCommand extends SimpleCommand {
private final Property property;
private final Object value;
http://git-wip-us.apache.org/repos/asf/incubator-optiq/blob/a10a8d77/core/src/test/java/net/hydromatic/optiq/tools/SqlRunTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/net/hydromatic/optiq/tools/SqlRunTest.java b/core/src/test/java/net/hydromatic/optiq/tools/SqlRunTest.java
index 8314c70..d1e5168 100644
--- a/core/src/test/java/net/hydromatic/optiq/tools/SqlRunTest.java
+++ b/core/src/test/java/net/hydromatic/optiq/tools/SqlRunTest.java
@@ -44,6 +44,7 @@ public class SqlRunTest {
+ "!set outputformat mysql\n"
+ "select count(*) as c1 from \"foodmart\".\"days\";\n"
+ "!ok\n"
+ + "!plan\n"
+ "\n",
"!use foodmart\n"
+ "select count(*) as c1 from \"foodmart\".\"days\";\n"
@@ -60,6 +61,11 @@ public class SqlRunTest {
+ "(1 row)\n"
+ "\n"
+ "!ok\n"
+ + "JdbcToEnumerableConverter\n"
+ + " JdbcAggregateRel(group=[{}], C1=[COUNT()])\n"
+ + " JdbcProjectRel(DUMMY=[0])\n"
+ + " JdbcTableScan(table=[[foodmart, days]])\n"
+ + "!plan\n"
+ "\n");
}
@@ -134,6 +140,46 @@ public class SqlRunTest {
+ "\n"));
}
+ /** Content inside a '!ok' command, that needs to be matched. */
+ @Test public void testOkContent() {
+ check(
+ "!use foodmart\n"
+ + "values (1), (2);\n"
+ + "baz\n"
+ + "!ok\n"
+ + "\n",
+ containsString(
+ "!use foodmart\n"
+ + "values (1), (2);\n"
+ + "EXPR$0\n"
+ + "1\n"
+ + "2\n"
+ + "!ok\n"
+ + "\n"));
+ }
+
+ /** Content inside a '!plan' command, that needs to be matched. */
+ @Test public void testPlanContent() {
+ check(
+ "!use foodmart\n"
+ + "values (1), (2);\n"
+ + "foo\n"
+ + "!plan\n"
+ + "baz\n"
+ + "!ok\n"
+ + "\n",
+ containsString(
+ "!use foodmart\n"
+ + "values (1), (2);\n"
+ + "EnumerableValuesRel(tuples=[[{ 1 }, { 2 }]])\n"
+ + "!plan\n"
+ + "EXPR$0\n"
+ + "1\n"
+ + "2\n"
+ + "!ok\n"
+ + "\n"));
+ }
+
@Test public void testPlanDisabled() {
check(
"!use foodmart\n"