public static void main()

in tools/query_breakdown/src/main/java/com/google/bigquery/Main.java [39:193]


  public static void main(String[] args) {
    // starts the timer for runtime measurement
    long start = System.nanoTime();

    String inputFile = null;
    int runtimeLimit = 100000; // default value for runtime limit, measured in seconds
    int replacementLimit = 3; // default value for number of recommended replacements
    boolean jsonOutput = false;
    CommandLine cl = createCommand(args);

    // if there is an error in parsing the commandline
    if (cl == null) {
      exit(1);
    }

    // sets the variables accordingly from the flag arguments
    if (cl.hasOption("i")) {
      inputFile = cl.getOptionValue("i");
    }
    if (cl.hasOption("j")) {
      jsonOutput = true;
    }
    if (cl.hasOption("l")) {
      runtimeLimit = Integer.parseInt(cl.getOptionValue("l"));
    }
    if (cl.hasOption("r")) {
      replacementLimit = Integer.parseInt(cl.getOptionValue("r"));
    }

    InputReader ir = null;
    // this is where we will put the file I/O logic through the input reader.
    try {
      ir = new InputReader(inputFile);
    } catch (IOException e) {
      System.out.println("there was an I/O error while reading the input");
      e.printStackTrace();
      exit(1);
    }

    if (ir.getLocationTrackers().size() != ir.getQueries().size()) {
      System.out.println("there was an error in input parsing: wrong number of queries and "
          + "location trackers");
      exit(1);
    }

    // we initialize a file to output to
    FileWriter writer = null;
    try {
      String absPath = new File("").getAbsolutePath();
      File outputFile = new File(absPath + "/output.txt");
      outputFile.createNewFile();
      writer = new FileWriter(outputFile);
    } catch (IOException e) {
      System.out.println("there was an I/O error while creating an output file");
      e.printStackTrace();
      exit(1);
    }

    /* this is where we feed in each of the original queries to QueryBreakdown, which will find
       all the unparseable components of the query. We then get the results and add them to the
       endResult list. We also output the results in the txt file created before.
     */

    // gets queries and locationTrackers initialized by the InputReader
    List<String> queries = ir.getQueries();
    List<LocationTracker> locationTrackers = ir.getLocationTrackers();

    // contains all the nodes to output as results
    List<Node> endResult = new ArrayList<>();
    for (int i = 0; i < queries.size(); i++) {
      QueryBreakdown qb = new QueryBreakdown(new CalciteParser());
      List<Node> result = qb.run(queries.get(i), runtimeLimit, replacementLimit,
          locationTrackers.get(i));
      endResult.addAll(result);
      try {
        writer.write("Original Query: " + queries.get(i) + "\n\n");
        if (result.isEmpty()) {
          writer.write("Resulting Query: " + "the entire query can be parsed without error"
              + "\n\n");
        }
        else {
          writer.write("Resulting Query: " + qb.getFinalString() + "\n\n");
        }
      } catch (IOException e) {
        System.out.println("there was an I/O error while writing to an output file");
        e.printStackTrace();
        exit(1);
      }
    }

    try {
      writer.close();
    } catch (IOException e) {
      System.out.println("there was an I/O error while closing the writer");
      e.printStackTrace();
      exit(1);
    }

    // keeps track of total characters of unparesable components for performance analysis
    int totalUnparseable = 0;

    // outputs the results accordingly as json or user-readable format
    if (jsonOutput) {
      // all queries paresable
      if (endResult.isEmpty()) {
        System.out.println("[]");
        return;
      }
      JSONArray jsonArray = new JSONArray();
      for (Node node: endResult) {
        jsonArray.add(node.toJSON());
        totalUnparseable += node.getUnparseableCount();
      }

      // add performance metric
      DecimalFormat df = new DecimalFormat("##.#");
      double x = 100 - (double) totalUnparseable / ir.getDocLength() * 100;
      JSONObject performance = new JSONObject();
      performance.put("performance", df.format(x));
      jsonArray.add(performance);

      // add runtime
      long end = System.nanoTime();
      float runtimeSeconds = TimeUnit.NANOSECONDS.toSeconds(end - start);
      JSONObject runtime = new JSONObject();
      runtime.put("runtime", runtimeSeconds);
      jsonArray.add(runtime);
      System.out.println(jsonArray);
    }
    else {
      if (endResult.isEmpty()) {
        System.out.println("The entire query can be parsed without error");
        return;
      }

      // print out results
      for (Node node: endResult) {
        System.out.println(node.toString());
        totalUnparseable += node.getUnparseableCount();
      }

      // print out performance metric
      DecimalFormat df = new DecimalFormat("##.#");
      double x = 100 - (double) totalUnparseable / ir.getDocLength() * 100;
      System.out.println("Percentage of Parseable Components: " + df.format(x) + "%");

      // print out runtime
      long end = System.nanoTime();
      float runtimeSeconds = TimeUnit.NANOSECONDS.toSeconds(end - start);
      System.out.println("Runtime: " + runtimeSeconds + " seconds");
    }

    // to deal with a bug where program does not terminate in CLI without explicit exit call
    System.exit(0);
  }