reset password
Author Message
se1k1h1mawar1
Posts: 121
Posted 13:32 Nov 13, 2015 |


1)  In the method createPlan (please see below), is the line of code, marked by an arrow (   Plan p = plans.remove(0);    <----- ) chaining the Plan interface to the array list of plans?
Can this line be replaced by Plan p = new ArrayList<Plan>(); , without having any practical differences by any chance?
If not, what exactly is this line of code doing?

    /**
        * Creates a query plan as follows.  It first takes
        * the product of all tables and views; it then selects on the predicate;
        * and finally it projects on the field list.
        */
       public Plan createPlan(QueryData data, Transaction tx) {
          //Step 1: Create a plan for each mentioned table or view
          List<Plan> plans = new ArrayList<Plan>();
          for (String tblname : data.tables()) {
             String viewdef = SimpleDB.mdMgr().getViewDef(tblname, tx);
             if (viewdef != null)
                plans.add(SimpleDB.planner().createQueryPlan(viewdef, tx));
             else
                plans.add(new TablePlan(tblname, tx));
          }
          
          //Step 2: Create the product of all table plans
          Plan p = plans.remove(0);                        <-----
          for (Plan nextplan : plans)
             p = new ProductPlan(p, nextplan);
          
          //Step 3: Add a selection plan for the predicate
          p = new SelectPlan(p, data.pred());
          
          //Step 4: Project on the field names
          p = new ProjectPlan(p, data.fields());
          return p;
       }

2)  Considering how computers only executes our commands sequentialy in a forward direction, it is unlikely, but I thought I'd ask.
When running a debuger on eclipse, is there a functionality that lets us go back to the previous execution of a line of code in a similar way as a browser would let us go back to what we were viewing previously by clicking on the browser's back button ?


3)  Does a StreamTokenizer treat ' . ' as a number 0.0?
I would imaging that "tok.ordinaryChar('.');" is done in the Lexer.java class (please see below) in order to
classify periods as a single character token so that the parser will view the character as a string or char so that we can use it in places like, " ... s.SName, c.pId ... "?


public Lexer(String s) {
      initKeywords();
      tok = new StreamTokenizer(new StringReader(s));
      tok.ordinaryChar('.');           <-----
      tok.lowerCaseMode(true); //ids and keywords are converted
      nextToken();
   }


4)  What is the point of having the following method if it should never be called as the comment suggest?
Is this simply giving us a framework so that we can create a method to allow use of aliases of field names in SQL command as a practice?


/**
    * This method should never be called.
    * Throws a ClassCastException.
    * @see simpledb.query.Expression#asFieldName()
    */
   public String asFieldName() {
      throw new ClassCastException();
   }

5) There is an author comments that reads, "BadSyntaxException: simply extends RuntimeException ...".
Does this mean that the author created a subclass of RuntimeException? If so, is the reason for doing so to make it easier for us to narrow down which part of the code created the exception (i.e. BadSyntaxException would only be created within the SimpleDB code wherares RuntimeException might come from JAVA library code)? If not, what does the author mean by that line?

Any insights will be greatly appreciated.

Thank you,

 

cysun
Posts: 2935
Posted 17:55 Nov 13, 2015 |

1. Please read the API doc to understand what the remove(int) method does.

Basically after "Step 1", plans will contain the list of tables to be joined. remove(0) takes the first plan, which is then joined (i.e. product) with the rest of the plans in the list one by one.

2. No, for the simple reason that not all operations are reversible.

3. No. You can take StreamTokenizerTest, modify the code and run it a few times to see the effect of ordinaryChar('.').

4. My guess is that the class is implementing some interface which requires an asFieldName() method.

5. In most cases the purpose of creating a subclass of Exception or RuntimeException is for better readability of the error message. For example, you'd have a much better idea of what went wrong if you see a BadSyntaxException than a generic RuntimeException.

se1k1h1mawar1
Posts: 121
Posted 18:29 Nov 13, 2015 |

I see where I went wrong in reading the code!
Thank you for your reply!  Sincerely,