Class FromClause


  • public class FromClause
    extends java.lang.Object
    This class represents a hierarchy of one or more base and derived relations that produce the rows considered by SELECT clauses.

    The "join condition type" specified in the condType field indicates the way that the join condition was specified in the original SQL expression. This value may be null if the join specified no condition, which would occur with these SQL expressions:

    • SELECT * FROM t1, t2 WHERE t1.a = t2.a;
    • SELECT * FROM t1 JOIN t2 WHERE t1.a = t2.a;

    If condType is set to JoinConditionType.JOIN_ON_EXPR then the join condition was specified in an ON clause, such as:

    • SELECT * FROM t1 JOIN t2 ON t1.a = t2.a;
    The ON predicate is available via the getOnExpression() method.

    Finally, if the condType is either JoinConditionType.JOIN_USING or JoinConditionType.NATURAL_JOIN then the join condition was implicitly specified via a SQL USING clause, or via a natural join, such as:

    • SELECT * FROM t1 JOIN t2 USING (a);
    • SELECT * FROM t1 NATURAL JOIN t2;
    In these cases the join predicate must be generated from examining the schemas of the participating tables. The generated predicate is only available after the computeSchema(edu.caltech.nanodb.storage.TableManager) method has been called (which is indirectly invoked by the SelectCommand and InsertCommand classes before planning and executing a query), and is available via the getComputedJoinExpr() method.
    See Also:
    SelectClause
    • Constructor Detail

      • FromClause

        public FromClause​(java.lang.String tableName,
                          java.lang.String aliasName)
        Construct a new FROM clause of type FromClause.ClauseType.BASE_TABLE. The alias name may be null in the case of base-tables.
        Parameters:
        tableName - the name of the table being selected from
        aliasName - an optional alias used to refer to the table in the rest of the query
      • FromClause

        public FromClause​(FunctionCall tableFunction,
                          java.lang.String aliasName)
      • FromClause

        public FromClause​(SelectClause derivedTable,
                          java.lang.String aliasName)
        Construct a new FROM clause of type FromClause.ClauseType.SELECT_SUBQUERY. The alias name must be specified for derived tables.
        Parameters:
        derivedTable - the subquery that is referenced by this from-clause
        aliasName - the relation-name given for this subquery
        Throws:
        java.lang.NullPointerException - if either derivedTable or aliasName is null
      • FromClause

        public FromClause​(FromClause left,
                          FromClause right,
                          JoinType type)
        Construct a new FROM clause of type FromClause.ClauseType.JOIN_EXPR. The two sub-relations being joined are specified as FromClause instances.
        Parameters:
        left - the left relation being joined
        right - the right relation being joined
        type - the kind of join being performed
        Throws:
        java.lang.NullPointerException - if either sub-relation is null, or if the join-type is null.
    • Method Detail

      • getClauseType

        public FromClause.ClauseType getClauseType()
        Returns the kind of FROM clause this object represents.
        Returns:
        the clause-type. This value will never be null.
      • isBaseTable

        public boolean isBaseTable()
        Returns true if this from clause specifies a base table, as opposed to a derived table or a join expression.
        Returns:
        true if this clause specifies a base table.
      • isDerivedTable

        public boolean isDerivedTable()
        Returns true if this from clause specifies a derived table, as opposed to a base table or a join expression.
        Returns:
        true if this clause is a derived table specified as a SELECT statement.
      • isJoinExpr

        public boolean isJoinExpr()
        Returns true if this from clause specifies a join expression, as opposed to a base table or a join expression.
        Returns:
        true if this clause is a join expression.
      • getTableName

        public java.lang.String getTableName()
        Returns the table name. This value will be null if this is a derived table or a join expression.
        Returns:
        the name of the table.
        Throws:
        java.lang.IllegalStateException - if the from-clause's type is not a FromClause.ClauseType.BASE_TABLE.
      • getSelectClause

        public SelectClause getSelectClause()
        Returns the select clause for a derived table. This value will be null if this is not derived.
        Returns:
        the derived select clause
        Throws:
        java.lang.IllegalStateException - if the from-clause's type is not a FromClause.ClauseType.SELECT_SUBQUERY.
      • getLeftChild

        public FromClause getLeftChild()
        If the from-clause is a join expression, this method returns the left child-clause.
        Returns:
        the left child-clause of this join clause
        Throws:
        java.lang.IllegalStateException - if this from-clause is not a join expression.
      • getRightChild

        public FromClause getRightChild()
        If the from-clause is a join expression, this method returns the right child-clause.
        Returns:
        the right child-clause of this join clause
        Throws:
        java.lang.IllegalStateException - if this from-clause is not a join expression.
      • getResultName

        public java.lang.String getResultName()
        Returns the name of the relation that the results should go into. In the case where an alias exists, this returns the alias. If an alias does not exist, this must return the table name for the base table.
        Returns:
        the result relation name
        Throws:
        java.lang.IllegalStateException - if the clause-type is a join expression, since that kind of from-clause doesn't have a result name.
      • isRenamed

        public boolean isRenamed()
      • getJoinType

        public JoinType getJoinType()
        Returns the join type of a join clause.
        Returns:
        the join type
        Throws:
        java.lang.IllegalStateException - if the clause-type is not a join clause. Only join clauses have a join type.
      • isOuterJoin

        public boolean isOuterJoin()
        Returns true if this join clause is an outer join, false otherwise.
        Returns:
        true if this join clause is an outer join, false otherwise.
        Throws:
        java.lang.IllegalStateException - if the clause-type is not a join clause. Only join clauses have a join type.
      • hasOuterJoinOnLeft

        public boolean hasOuterJoinOnLeft()
      • hasOuterJoinOnRight

        public boolean hasOuterJoinOnRight()
      • getConditionType

        public FromClause.JoinConditionType getConditionType()
        Returns the join condition type. This value will be null if there is only 1 table in the outer array of from clauses.
        Returns:
        the join condition type
        Throws:
        java.lang.IllegalStateException - if the clause-type is not a join clause. Only join clauses have a join-condition type.
      • setConditionType

        public void setConditionType​(FromClause.JoinConditionType type)
        Sets the join condition type.
        Parameters:
        type - the join condition type
        Throws:
        java.lang.IllegalStateException - if the clause-type is not a join clause. Only join clauses have a join-condition type.
      • getUsingNames

        public java.util.List<java.lang.String> getUsingNames()
        For join conditions of the form USING (col1, col2, ...), this method returns the list of column names that were specified. If this join expression does not use a USING expression then this method returns null.
        Returns:
        the list of column names specified in the USING clause
        Throws:
        java.lang.IllegalStateException - if the clause-type is not a join clause. Only join clauses have a join condition.
      • addUsingName

        public void addUsingName​(java.lang.String name)
        For join conditions of the form USING (col1, col2, ...), this method allows the column names to be added to this from clause. This method is used by the parser to construct the from clause as it is being parsed.
        Parameters:
        name - the name of the column to use in the join operation
        Throws:
        java.lang.IllegalStateException - if the clause-type is not a join clause. Only join clauses have a join condition.
      • getOnExpression

        public Expression getOnExpression()
        Returns the join predicate if this from clause specifies an ON clause. Returns null if there is no ON clause.
        Returns:
        the expression specified in the ON clause
        Throws:
        java.lang.IllegalStateException - if the clause-type is not a join clause. Only join clauses have a join condition.
      • setOnExpression

        public void setOnExpression​(Expression expr)
        Sets the join predicate if this from clause specifies an ON clause. Returns null if there is no ON clause.
        Parameters:
        expr - the expression specified in the ON clause
        Throws:
        java.lang.IllegalStateException - if the clause-type is not a join clause. Only join clauses have a join condition.
      • computeSchema

        public Schema computeSchema​(TableManager tableManager)
        This function prepares the from-clause for use by the planner to generate an actual execution plan from the clause. This includes several important tasks:
        • The schema of the from-clause's output is determined.
        • If the from-clause is a join, and the join specifies NATURAL, USING (col, ...), or an ON clause, the join condition is determined

        Since a from-clause can be a SQL subquery or a join expression, it should be evident that the method will recursively prepare its children as well.

        This method really shouldn't be called directly. Instead, the enclosing SelectClause will calls this method when the clause's SelectClause.computeSchema(edu.caltech.nanodb.storage.TableManager, edu.caltech.nanodb.queryast.SelectClause) method is invoked.

        Returns:
        the schema of the from-clause's output
      • buildJoinSchema

        private Schema buildJoinSchema​(java.lang.String context,
                                       Schema leftSchema,
                                       Schema rightSchema,
                                       java.util.Set<java.lang.String> commonCols)

        This helper function handles a NATURAL join or a join with a USING clause, determining what columns to use in the join, computing the join predicate to use, and computing the schema of the join result.

        The schema of the result follows the SQL standard:

        • Common columns first, in the order specified, if any.
        • Columns that appear only in the left table, if any.
        • Columns that appear only in the right table, if any.

        Of course, any of these sets of columns could be empty.

        Parameters:
        context - a string used in logging messages and exceptions to indicate the context in which this method was called.
        leftSchema - the schema of the table on the left side of the join
        rightSchema - the schema of the table on the right side of the join
        commonCols - the set of common columns to use
        Returns:
        the schema of the result, as produced by the join operation.
      • checkJoinColumn

        private void checkJoinColumn​(java.lang.String context,
                                     java.lang.String side,
                                     Schema schema,
                                     java.lang.String colName)
      • getSchema

        public Schema getSchema()
      • getComputedJoinExpr

        public Expression getComputedJoinExpr()
      • getComputedSelectValues

        public java.util.ArrayList<SelectValue> getComputedSelectValues()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object