Skip to content

Commit e994bc8

Browse files
committed
[SQL] Add compiler flag for file to contain errors
Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
1 parent c00a52b commit e994bc8

4 files changed

Lines changed: 56 additions & 12 deletions

File tree

sql-to-dbsp-compiler/SQL-compiler/src/main/java/org/dbsp/sqlCompiler/CompilerMain.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import org.apache.calcite.adapter.jdbc.JdbcSchema;
3030
import org.apache.calcite.jdbc.CalciteConnection;
3131
import org.apache.calcite.schema.SchemaPlus;
32+
import org.apache.calcite.util.Pair;
3233
import org.apache.commons.io.output.NullPrintStream;
34+
import org.codehaus.janino.Compiler;
3335
import org.dbsp.sqlCompiler.circuit.DBSPCircuit;
3436
import org.dbsp.sqlCompiler.compiler.CompilerOptions;
3537
import org.dbsp.sqlCompiler.compiler.DBSPCompiler;
@@ -47,6 +49,8 @@
4749
import javax.annotation.Nullable;
4850
import javax.sql.DataSource;
4951
import java.io.File;
52+
import java.io.FileNotFoundException;
53+
import java.io.FileOutputStream;
5054
import java.io.IOException;
5155
import java.io.InputStream;
5256
import java.io.PrintStream;
@@ -271,21 +275,41 @@ CompilerMessages run() throws SQLException {
271275
return compiler.messages;
272276
}
273277

274-
public static CompilerMessages execute(String... argv) throws SQLException {
278+
public static Pair<CompilerMessages, CompilerOptions> run(String... argv) throws SQLException {
275279
CompilerMain main = new CompilerMain();
276280
int exitCode = main.parseOptions(argv);
281+
CompilerOptions options = main.options;
277282
if (exitCode != 0) {
278283
// return empty messages
279284
CompilerMessages result = new CompilerMessages(new DBSPCompiler(new CompilerOptions()));
280285
result.setExitCode(exitCode);
281-
return result;
286+
return new Pair<>(result, options);
287+
}
288+
return new Pair<>(main.run(), options);
289+
}
290+
291+
public static CompilerMessages execute(String... argv) throws SQLException {
292+
return run(argv).left;
293+
}
294+
295+
public static CompilerMessages runAndReportErrors(String... argv) throws SQLException, IOException {
296+
var result = run(argv);
297+
PrintStream errorStream = System.err;
298+
FileOutputStream errorFile = null;
299+
if (!result.right.ioOptions.errorFile.isEmpty()) {
300+
errorFile = new FileOutputStream(result.right.ioOptions.errorFile);
301+
errorStream = new PrintStream(errorFile);
302+
}
303+
result.left.show(errorStream);
304+
if (errorFile != null) {
305+
errorFile.close();
306+
errorStream.close();
282307
}
283-
return main.run();
308+
return result.left;
284309
}
285310

286-
public static void main(String[] argv) throws SQLException {
287-
CompilerMessages messages = execute(argv);
288-
messages.show(System.err);
311+
public static void main(String[] argv) throws SQLException, IOException {
312+
CompilerMessages messages = runAndReportErrors(argv);
289313
System.exit(messages.exitCode);
290314
}
291315
}

sql-to-dbsp-compiler/SQL-compiler/src/main/java/org/dbsp/sqlCompiler/compiler/CompilerOptions.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,10 @@ public static class IO implements IDiff<IO>, IValidate {
152152
public boolean noRust = false;
153153
@Parameter(names = "--enterprise", description = "Generate code supporting enterprise features")
154154
public boolean enterprise = false;
155-
@Parameter(names="-o", description = "Output file; stdout if null")
155+
@Parameter(names="-o", description = "Output file; stdout if not specified")
156156
public String outputFile = "";
157+
@Parameter(names="--errors", description = "Error output file; stderr if not specified")
158+
public String errorFile = "";
157159
@Parameter(names = {"--jpg", "-jpg"}, description = "Emit a jpg image of the circuit instead of Rust")
158160
public boolean emitJpeg = false;
159161
@Parameter(names = {"--png", "-png"}, description = "Emit a png image of the circuit instead of Rust")
@@ -162,7 +164,7 @@ public static class IO implements IDiff<IO>, IValidate {
162164
public String emitPlan = null;
163165
@Nullable @Parameter(names = "--dataflow", description = "Emit the Dataflow graph of the program in the specified JSON file")
164166
public String emitDataflow = null;
165-
@Parameter(names = {"--je", "-je"}, description = "Emit error messages as a JSON array to stderr")
167+
@Parameter(names = {"--je", "-je"}, description = "Emit error messages as a JSON array to the error output")
166168
public boolean emitJsonErrors = false;
167169
@Parameter(names = {"--js", "-js"},
168170
description = "Emit a JSON file containing the schema of all views and tables in the specified file.")
@@ -231,6 +233,7 @@ public String toString() {
231233
return "IO{" +
232234
"outputFile=" + Utilities.singleQuote(this.outputFile) +
233235
", metadataSource=" + this.metadataSource +
236+
", errorFile=" + Utilities.singleQuote(this.errorFile) +
234237
", emitHandles=" + this.emitHandles +
235238
", emitJpeg=" + this.emitJpeg +
236239
", emitPng=" + this.emitPng +

sql-to-dbsp-compiler/SQL-compiler/src/test/java/org/dbsp/sqlCompiler/compiler/sql/MetadataTests.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@ DECLARE RECURSIVE VIEW v(
123123
}
124124
}
125125

126+
@Test
127+
public void issue4183() throws IOException, SQLException {
128+
String sql = "WRONG";
129+
File file = createInputScript(sql);
130+
File errorFile = File.createTempFile("err", ".txt", new File("."));
131+
errorFile.deleteOnExit();
132+
CompilerMain.runAndReportErrors("--noRust", file.getPath(), "--errors", errorFile.getPath());
133+
String str = Utilities.readFile(errorFile.getPath());
134+
Assert.assertTrue(str.contains("Error parsing SQL"));
135+
}
136+
126137
@Test
127138
public void lineageTest() throws SQLException, IOException {
128139
// Check that the calcite property in the dataflow graph is never "null" for this program
@@ -702,6 +713,9 @@ public void testHelpMessage() throws SQLException {
702713
--enterprise
703714
Generate code supporting enterprise features
704715
Default: false
716+
--errors
717+
Error output file; stderr if not specified
718+
Default: <empty string>
705719
--handles
706720
Use handles (true) or Catalog (false) in the emitted Rust code
707721
Default: false
@@ -714,7 +728,7 @@ Use handles (true) or Catalog (false) in the emitted Rust code
714728
Connection string to a database that contains table metadata
715729
Default: <empty string>
716730
--je, -je
717-
Emit error messages as a JSON array to stderr
731+
Emit error messages as a JSON array to the error output
718732
Default: false
719733
--jpg, -jpg
720734
Emit a jpg image of the circuit instead of Rust
@@ -756,7 +770,7 @@ Optimization level (0, 1, or 2)
756770
Generate an incremental circuit
757771
Default: false
758772
-o
759-
Output file; stdout if null
773+
Output file; stdout if not specified
760774
Default: <empty string>
761775
-q
762776
Quiet: do not print warnings

sql-to-dbsp-compiler/using.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ Usage: sql-to-dbsp [options] Input file to compile
5050
--enterprise
5151
Generate code supporting enterprise features
5252
Default: false
53+
--errors
54+
Error output file; stderr if not specified
55+
Default: <empty string>
5356
--crates
5457
Followed by a program name. Generates code using multiple crates;
5558
`outputFile` is interpreted as a directory.
@@ -66,7 +69,7 @@ Usage: sql-to-dbsp [options] Input file to compile
6669
Connection string to a database that contains table metadata
6770
Default: <empty string>
6871
--je, -je
69-
Emit error messages as a JSON array to stderr
72+
Emit error messages as a JSON array to the error output
7073
Default: false
7174
--jpg, -jpg
7275
Emit a jpg image of the circuit instead of Rust
@@ -108,7 +111,7 @@ Usage: sql-to-dbsp [options] Input file to compile
108111
Generate an incremental circuit
109112
Default: false
110113
-o
111-
Output file; stdout if null
114+
Output file; stdout if not specified
112115
Default: <empty string>
113116
-q
114117
Quiet: do not print warnings

0 commit comments

Comments
 (0)