Search in sources :

Example 6 with CompilerContext

use of org.apache.derby.iapi.sql.compile.CompilerContext in project derby by apache.

the class ResultSetNode method parseDefault.

/**
 *	Parse a default and turn it into a query tree.
 *
 *	@param	defaultText			Text of Default.
 *
 * @return	The parsed default as a query tree.
 *
 * @exception StandardException		Thrown on failure
 */
public ValueNode parseDefault(String defaultText) throws StandardException {
    Parser p;
    ValueNode defaultTree;
    LanguageConnectionContext lcc = getLanguageConnectionContext();
    /* Get a Statement to pass to the parser */
    /* We're all set up to parse. We have to build a compilable SQL statement
		 * before we can parse -  So, we goober up a VALUES defaultText.
		 */
    String values = "VALUES " + defaultText;
    /*
		** Get a new compiler context, so the parsing of the select statement
		** doesn't mess up anything in the current context (it could clobber
		** the ParameterValueSet, for example).
		*/
    CompilerContext newCC = lcc.pushCompilerContext();
    p = newCC.getParser();
    /* Finally, we can call the parser */
    // Since this is always nested inside another SQL statement, so topLevel flag
    // should be false
    Visitable qt = p.parseStatement(values);
    if (SanityManager.DEBUG) {
        if (!(qt instanceof CursorNode)) {
            SanityManager.THROWASSERT("qt expected to be instanceof CursorNode, not " + qt.getClass().getName());
        }
        CursorNode cn = (CursorNode) qt;
        if (!(cn.getResultSetNode() instanceof RowResultSetNode)) {
            SanityManager.THROWASSERT("cn.getResultSetNode() expected to be instanceof RowResultSetNode, not " + cn.getResultSetNode().getClass().getName());
        }
    }
    defaultTree = ((CursorNode) qt).getResultSetNode().getResultColumns().elementAt(0).getExpression();
    lcc.popCompilerContext(newCC);
    return defaultTree;
}
Also used : LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) CompilerContext(org.apache.derby.iapi.sql.compile.CompilerContext) Visitable(org.apache.derby.iapi.sql.compile.Visitable) Parser(org.apache.derby.iapi.sql.compile.Parser)

Example 7 with CompilerContext

use of org.apache.derby.iapi.sql.compile.CompilerContext in project derby by apache.

the class MergeNode method addColumnPrivilege.

/**
 * <p>
 * Add SELECT privilege on the indicated column.
 * </p>
 */
private void addColumnPrivilege(ColumnReference cr) throws StandardException {
    CompilerContext cc = getCompilerContext();
    ResultColumn rc = cr.getSource();
    if (rc != null) {
        ColumnDescriptor colDesc = rc.getColumnDescriptor();
        if (colDesc != null) {
            cc.pushCurrentPrivType(Authorizer.SELECT_PRIV);
            cc.addRequiredColumnPriv(colDesc);
            cc.popCurrentPrivType();
        }
    }
}
Also used : CompilerContext(org.apache.derby.iapi.sql.compile.CompilerContext) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)

Example 8 with CompilerContext

use of org.apache.derby.iapi.sql.compile.CompilerContext in project derby by apache.

the class OrderByList method generate.

/**
 *    	generate the sort result set operating over the source
 *		expression.
 *
 *		@param acb the tool for building the class
 *		@param mb	the method the generated code is to go into
 *		@exception StandardException thrown on failure
 */
void generate(ActivationClassBuilder acb, MethodBuilder mb, ResultSetNode child) throws StandardException {
    /*
		** If sorting is not required, don't generate a sort result set -
		** just return the child result set.
		*/
    if (!sortNeeded) {
        child.generate(acb, mb);
        return;
    }
    /* Get the next ResultSet#, so we can number this ResultSetNode, its
		 * ResultColumnList and ResultSet.
		 *
		 * REMIND: to do this properly (if order bys can live throughout
		 * the tree) there ought to be an OrderByNode that holds its own
		 * ResultColumnList that is a lsit of virtual column nodes pointing
		 * to the source's result columns.  But since we know it is outermost,
		 * we just gloss over that and get ourselves a resultSetNumber
		 * directly.
		 */
    CompilerContext cc = getCompilerContext();
    /*
			create the orderItem and stuff it in.
		 */
    int orderItem = acb.addItem(acb.getColumnOrdering(this));
    /* Generate the SortResultSet:
		 *	arg1: childExpress - Expression for childResultSet
		 *  arg2: distinct - always false, we have a separate node
		 *				for distincts
		 *  arg3: isInSortedOrder - is the source result set in sorted order
		 *  arg4: orderItem - entry in saved objects for the ordering
		 *  arg5: rowAllocator - method to construct rows for fetching
		 *			from the sort
		 *  arg6: row size
		 *  arg7: resultSetNumber
		 *  arg8: estimated row count
		 *  arg9: estimated cost
		 */
    acb.pushGetResultSetFactoryExpression(mb);
    child.generate(acb, mb);
    resultSetNumber = cc.getNextResultSetNumber();
    // is a distinct query
    mb.push(false);
    // not in sorted order
    mb.push(false);
    mb.push(orderItem);
    // row allocator
    mb.push(acb.addItem(child.getResultColumns().buildRowTemplate()));
    mb.push(child.getResultColumns().getTotalColumnSize());
    mb.push(resultSetNumber);
    // Get the cost estimate for the child
    // RESOLVE - we will eventually include the cost of the sort
    CostEstimate costEstimate = child.getFinalCostEstimate();
    mb.push(costEstimate.rowCount());
    mb.push(costEstimate.getEstimatedCost());
    mb.callMethod(VMOpcode.INVOKEINTERFACE, (String) null, "getSortResultSet", ClassName.NoPutResultSet, 9);
}
Also used : CompilerContext(org.apache.derby.iapi.sql.compile.CompilerContext) CostEstimate(org.apache.derby.iapi.sql.compile.CostEstimate)

Example 9 with CompilerContext

use of org.apache.derby.iapi.sql.compile.CompilerContext in project derby by apache.

the class JoinNode method deferredBindExpressions.

private void deferredBindExpressions(FromList fromListParam) throws StandardException {
    ContextManager cm = getContextManager();
    CompilerContext cc = getCompilerContext();
    /* Bind the expressions in the join clause */
    subqueryList = new SubqueryList(cm);
    aggregates = new ArrayList<AggregateNode>();
    /* ON clause */
    if (joinClause != null) {
        joinClause = bindExpression(joinClause, true, true, "ON");
    } else /* USING clause */
    if (usingClause != null) {
        /* Build a join clause from the usingClause, using the
			 * exposed names in the left and right RSNs.
			 * For each column in the list, we generate 2 ColumnReferences,
			 * 1 for the left and 1 for the right.  We bind each of these
			 * to the appropriate side and build an equality predicate
			 * between the 2.  We bind the = and AND nodes by hand because
			 * we have to bind the ColumnReferences a side at a time.
			 * We need to bind the CRs a side at a time to ensure that
			 * we don't find an bogus ambiguous column reference. (Bug 377)
			 */
        joinClause = new BooleanConstantNode(true, cm);
        for (ResultColumn rc : usingClause) {
            BinaryComparisonOperatorNode equalsNode;
            ColumnReference leftCR;
            ColumnReference rightCR;
            /* Create and bind the left CR */
            fromListParam.insertElementAt(leftResultSet, 0);
            leftCR = new ColumnReference(rc.getName(), ((FromTable) leftResultSet).getTableName(), cm);
            leftCR = (ColumnReference) leftCR.bindExpression(fromListParam, subqueryList, aggregates);
            fromListParam.removeElementAt(0);
            /* Create and bind the right CR */
            fromListParam.insertElementAt(rightResultSet, 0);
            rightCR = new ColumnReference(rc.getName(), ((FromTable) rightResultSet).getTableName(), cm);
            rightCR = (ColumnReference) rightCR.bindExpression(fromListParam, subqueryList, aggregates);
            fromListParam.removeElementAt(0);
            /* Create and insert the new = condition */
            equalsNode = new BinaryRelationalOperatorNode(BinaryRelationalOperatorNode.K_EQUALS, leftCR, rightCR, false, cm);
            equalsNode.bindComparisonOperator();
            // Create a new join clause by ANDing the new = condition and
            // the old join clause.
            AndNode newJoinClause = new AndNode(equalsNode, joinClause, cm);
            newJoinClause.postBindFixup();
            joinClause = newJoinClause;
        }
    }
    if (joinClause != null) {
        /* If joinClause is a parameter, (where ?), then we assume
			 * it will be a nullable boolean.
			 */
        if (joinClause.requiresTypeFromContext()) {
            joinClause.setType(new DataTypeDescriptor(TypeId.BOOLEAN_ID, true));
        }
        /*
			** Is the datatype of the JOIN clause BOOLEAN?
			**
			** NOTE: This test is not necessary in SQL92 entry level, because
			** it is syntactically impossible to have a non-Boolean JOIN clause
			** in that level of the standard.  But we intend to extend the
			** language to allow Boolean user functions in the JOIN clause,
			** so we need to test for the error condition.
			*/
        TypeId joinTypeId = joinClause.getTypeId();
        /* If the where clause is not a built-in type, then generate a bound 
			 * conversion tree to a built-in type.
			 */
        if (joinTypeId.userType()) {
            joinClause = joinClause.genSQLJavaSQLTree();
        }
        if (!joinClause.getTypeServices().getTypeId().equals(TypeId.BOOLEAN_ID)) {
            throw StandardException.newException(SQLState.LANG_NON_BOOLEAN_JOIN_CLAUSE, joinClause.getTypeServices().getTypeId().getSQLTypeName());
        }
    }
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) CompilerContext(org.apache.derby.iapi.sql.compile.CompilerContext) ContextManager(org.apache.derby.iapi.services.context.ContextManager)

Example 10 with CompilerContext

use of org.apache.derby.iapi.sql.compile.CompilerContext in project derby by apache.

the class JoinNode method bindExpression.

/**
 * Bind an expression against the child tables of the JoinNode. May
 * update the subquery and aggregate lists in the JoinNode. Assumes that
 * the subquery and aggregate lists for the JoinNode have already been created.
 *
 * @return the bound expression
 */
public ValueNode bindExpression(ValueNode expression, boolean useLeftChild, boolean useRightChild, String expressionType) throws StandardException {
    ContextManager cm = getContextManager();
    CompilerContext cc = getCompilerContext();
    /* Create a new fromList with only left and right children before
         * binding the join clause. Valid column references in the join clause
         * are limited to columns from the 2 tables being joined. This
         * algorithm enforces that.
         */
    FromList fromList = makeFromList(useLeftChild, useRightChild);
    int previousReliability = orReliability(CompilerContext.ON_CLAUSE_RESTRICTION);
    expression = expression.bindExpression(fromList, subqueryList, aggregates);
    cc.setReliability(previousReliability);
    // SQL 2003, section 7.7 SR 5
    SelectNode.checkNoWindowFunctions(expression, expressionType);
    /*
        ** We cannot have aggregates in the ON clause.
        ** In the future, if we relax this, we'll need
        ** to be able to pass the list of aggregates up
        ** the tree.
        */
    if (!aggregates.isEmpty()) {
        throw StandardException.newException(SQLState.LANG_NO_AGGREGATES_IN_ON_CLAUSE);
    }
    return expression;
}
Also used : ContextManager(org.apache.derby.iapi.services.context.ContextManager) CompilerContext(org.apache.derby.iapi.sql.compile.CompilerContext)

Aggregations

CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)53 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)12 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)10 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)9 Parser (org.apache.derby.iapi.sql.compile.Parser)8 Visitable (org.apache.derby.iapi.sql.compile.Visitable)6 TypeId (org.apache.derby.iapi.types.TypeId)6 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)5 ProviderList (org.apache.derby.iapi.sql.depend.ProviderList)5 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)5 ArrayList (java.util.ArrayList)4 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)4 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)4 ContextManager (org.apache.derby.iapi.services.context.ContextManager)3 StandardException (org.apache.derby.shared.common.error.StandardException)3 UUID (org.apache.derby.catalog.UUID)2 DefaultInfoImpl (org.apache.derby.catalog.types.DefaultInfoImpl)2 ClassFactory (org.apache.derby.iapi.services.loader.ClassFactory)2 CostEstimate (org.apache.derby.iapi.sql.compile.CostEstimate)2 TypeCompilerFactory (org.apache.derby.iapi.sql.compile.TypeCompilerFactory)2