Bringing tree up to date.
[galago.git] / java / pig-galago / src / com / yahoo / pig / StandAloneParser.java
blobeb4e6454dd93296ec329864c65f99156e15f5ab5
1 /*
2 * Copyright (c) 2007 Yahoo! Inc. All rights reserved.
3 * See accompanying LICENSE file.
4 */
5 package com.yahoo.pig;
7 import java.io.*;
8 import java.util.*;
10 import com.yahoo.pig.impl.PigContext;
11 import com.yahoo.pig.PigServer.ExecType;
12 import com.yahoo.pig.impl.logicalLayer.LogicalPlan;
13 import com.yahoo.pig.impl.logicalLayer.LogicalPlanBuilder;
14 import com.yahoo.pig.impl.logicalLayer.parser.ParseException;
15 import com.yahoo.pig.impl.logicalLayer.parser.QueryParser;
16 import com.yahoo.pig.impl.physicalLayer.IntermedResult;
18 public class StandAloneParser {
19 public static ClassLoader classloader = LogicalPlanBuilder.class.getClassLoader();
21 public static void main(String args[]) throws IOException {
23 Map<String, IntermedResult> aliases = new HashMap<String, IntermedResult>();
25 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
27 while (true) {
28 System.out.print("> ");
30 String line;
31 try {
32 line = in.readLine();
33 } catch (IOException e) {
34 e.printStackTrace();
35 return;
38 if (line.toLowerCase().equals("quit")) break;
39 if (line.toLowerCase().startsWith("#")) continue;
40 else tryParse(line, aliases);
46 private static void tryParse(String query, Map<String, IntermedResult> aliases) {
47 if (query.trim().equals(""))
48 return;
49 ByteArrayInputStream in = new ByteArrayInputStream(query.getBytes());
50 QueryParser parser = new QueryParser(in, new PigContext(ExecType.LOCAL), aliases);
51 try {
52 LogicalPlan lp = parser.Parse();
53 System.out.println("--- Query parsed successfully ---");
54 if (lp.alias != null) aliases.put(lp.alias, new IntermedResult(lp));
55 System.out.print("Current aliases: ");
56 for (Iterator<String> it = aliases.keySet().iterator(); it.hasNext(); ) {
57 String alias = it.next();
58 IntermedResult ir = aliases.get(alias);
59 System.out.print(alias + "->" + ir.lp.root().outputSchema());
60 if (it.hasNext()) System.out.print(", \n");
61 else System.out.print("\n");
63 } catch (ParseException e) {
64 System.out.println("Parse error: " + e);