Package org.apache.derby.impl.sql.compile

Examples of org.apache.derby.impl.sql.compile.FromSubquery


    templateColumns = resultColumns;

    /* Resolve the view, if this is a view */
    if (tableDescriptor.getTableType() == TableDescriptor.VIEW_TYPE)
    {
      FromSubquery                fsq;
      ResultSetNode        rsn;
      ViewDescriptor        vd;
      CreateViewNode        cvn;
      SchemaDescriptor      compSchema;

      /* Get the associated ViewDescriptor so that we can get
       * the view definition text.
       */
      vd = dataDictionary.getViewDescriptor(tableDescriptor);

      /*
      ** Set the default compilation schema to be whatever
      ** this schema this view was originally compiled against.
      ** That way we pick up the same tables no matter what
      ** schema we are running against.
      */
      compSchema = dataDictionary.getSchemaDescriptor(vd.getCompSchemaId(), null);

      compilerContext.pushCompilationSchema(compSchema);
 
      try
      {
   
        /* This represents a view - query is dependent on the ViewDescriptor */
        compilerContext.createDependency(vd);
 
        if (SanityManager.DEBUG)
        {
          SanityManager.ASSERT(vd != null,
            "vd not expected to be null for " + tableName);
        }

        cvn = (CreateViewNode)
                  parseStatement(vd.getViewText(), false);

        rsn = cvn.getParsedQueryExpression();

        /* If the view contains a '*' then we mark the views derived column list
         * so that the view will still work, and return the expected results,
         * if any of the tables referenced in the view have columns added to
         * them via ALTER TABLE.  The expected results means that the view
         * will always return the same # of columns.
         */
        if (rsn.getResultColumns().containsAllResultColumn())
        {
          resultColumns.setCountMismatchAllowed(true);
        }
        //Views execute with definer's privileges and if any one of
        //those privileges' are revoked from the definer, the view gets
        //dropped. So, a view can exist in Derby only if it's owner has
        //all the privileges needed to create one. In order to do a
        //select from a view, a user only needs select privilege on the
        //view and doesn't need any privilege for objects accessed by
        //the view. Hence, when collecting privilege requirement for a
        //sql accessing a view, we only need to look for select privilege
        //on the actual view and that is what the following code is
        //checking.
        for (int i = 0; i < resultColumns.size(); i++) {
          ResultColumn rc = (ResultColumn) resultColumns.elementAt(i);
          if (rc.isPrivilegeCollectionRequired())
            compilerContext.addRequiredColumnPriv( rc.getTableColumnDescriptor());
        }

        fsq = (FromSubquery) getNodeFactory().getNode(
          C_NodeTypes.FROM_SUBQUERY,
          rsn,
                    cvn.getOrderByList(),
                    cvn.getOffset(),
                    cvn.getFetchFirst(),
          (correlationName != null) ?
                        correlationName : getOrigTableName().getTableName(),
          resultColumns,
          tableProperties,
          getContextManager());
        // Transfer the nesting level to the new FromSubquery
        fsq.setLevel(level);
        //We are getting ready to bind the query underneath the view. Since
        //that query is going to run with definer's privileges, we do not
        //need to collect any privilege requirement for that query.
        //Following call is marking the query to run with definer
        //privileges. This marking will make sure that we do not collect
        //any privilege requirement for it.
        fsq.disablePrivilegeCollection();
        fsq.setOrigTableName(this.getOrigTableName());

        // since we reset the compilation schema when we return, we
        // need to save it for use when we bind expressions:
        fsq.setOrigCompilationSchema(compSchema);
        ResultSetNode fsqBound =
          fsq.bindNonVTITables(dataDictionary, fromListParam);

        /* Do error checking on derived column list and update "exposed"
         * column names if valid.
         */
        if (derivedRCL != null) {
View Full Code Here


    templateColumns = resultColumns;

    /* Resolve the view, if this is a view */
    if (tableDescriptor.getTableType() == TableDescriptor.VIEW_TYPE)
    {
      FromSubquery                fsq;
      ResultSetNode        rsn;
      ViewDescriptor        vd;
      CreateViewNode        cvn;
      SchemaDescriptor      compSchema;

      /* Get the associated ViewDescriptor so that we can get
       * the view definition text.
       */
      vd = dataDictionary.getViewDescriptor(tableDescriptor);

      /*
      ** Set the default compilation schema to be whatever
      ** this schema this view was originally compiled against.
      ** That way we pick up the same tables no matter what
      ** schema we are running against.
      */
      compSchema = dataDictionary.getSchemaDescriptor(vd.getCompSchemaId(), null);

      compilerContext.pushCompilationSchema(compSchema);
 
      try
      {
   
        /* This represents a view - query is dependent on the ViewDescriptor */
        compilerContext.createDependency(vd);
 
        if (SanityManager.DEBUG)
        {
          SanityManager.ASSERT(vd != null,
            "vd not expected to be null for " + tableName);
        }
 
        cvn = (CreateViewNode)
                  parseStatement(vd.getViewText(), false);

        rsn = cvn.getParsedQueryExpression();
 
        /* If the view contains a '*' then we mark the views derived column list
         * so that the view will still work, and return the expected results,
         * if any of the tables referenced in the view have columns added to
         * them via ALTER TABLE.  The expected results means that the view
         * will always return the same # of columns.
         */
        if (rsn.getResultColumns().containsAllResultColumn())
        {
          resultColumns.setCountMismatchAllowed(true);
        }
        //Views execute with definer's privileges and if any one of
        //those privileges' are revoked from the definer, the view gets
        //dropped. So, a view can exist in Derby only if it's owner has
        //all the privileges needed to create one. In order to do a
        //select from a view, a user only needs select privilege on the
        //view and doesn't need any privilege for objects accessed by
        //the view. Hence, when collecting privilege requirement for a
        //sql accessing a view, we only need to look for select privilege
        //on the actual view and that is what the following code is
        //checking.
        for (int i = 0; i < resultColumns.size(); i++) {
          ResultColumn rc = (ResultColumn) resultColumns.elementAt(i);
          if (rc.isPrivilegeCollectionRequired())
            compilerContext.addRequiredColumnPriv( rc.getTableColumnDescriptor());
        }

        fsq = (FromSubquery) getNodeFactory().getNode(
          C_NodeTypes.FROM_SUBQUERY,
          rsn,
          (correlationName != null) ?
                        correlationName : getOrigTableName().getTableName(),
          resultColumns,
          tableProperties,
          getContextManager());
        // Transfer the nesting level to the new FromSubquery
        fsq.setLevel(level);
        //We are getting ready to bind the query underneath the view. Since
        //that query is going to run with definer's privileges, we do not
        //need to collect any privilege requirement for that query.
        //Following call is marking the query to run with definer
        //privileges. This marking will make sure that we do not collect
        //any privilege requirement for it.
        fsq.disablePrivilegeCollection();
        fsq.setOrigTableName(this.getOrigTableName());

        // since we reset the compilation schema when we return, we
        // need to save it for use when we bind expressions:
        fsq.setOrigCompilationSchema(compSchema);

        return fsq.bindNonVTITables(dataDictionary, fromListParam);
      }
      finally
      {
        compilerContext.popCompilationSchema();
      }
View Full Code Here

    templateColumns = resultColumns;

    /* Resolve the view, if this is a view */
    if (tableDescriptor.getTableType() == TableDescriptor.VIEW_TYPE)
    {
      FromSubquery                fsq;
      ResultSetNode        rsn;
      ViewDescriptor        vd;
      CreateViewNode        cvn;
      SchemaDescriptor      compSchema;

      /* Get the associated ViewDescriptor so that we can get
       * the view definition text.
       */
      vd = dataDictionary.getViewDescriptor(tableDescriptor);

      /*
      ** Set the default compilation schema to be whatever
      ** this schema this view was originally compiled against.
      ** That way we pick up the same tables no matter what
      ** schema we are running against.
      */
      compSchema = dataDictionary.getSchemaDescriptor(vd.getCompSchemaId(), null);

      compilerContext.pushCompilationSchema(compSchema);
 
      try
      {
   
        /* This represents a view - query is dependent on the ViewDescriptor */
        compilerContext.createDependency(vd);
 
        if (SanityManager.DEBUG)
        {
          SanityManager.ASSERT(vd != null,
            "vd not expected to be null for " + tableName);
        }

        cvn = (CreateViewNode)
                  parseStatement(vd.getViewText(), false);

        rsn = cvn.getParsedQueryExpression();

        /* If the view contains a '*' then we mark the views derived column list
         * so that the view will still work, and return the expected results,
         * if any of the tables referenced in the view have columns added to
         * them via ALTER TABLE.  The expected results means that the view
         * will always return the same # of columns.
         */
        if (rsn.getResultColumns().containsAllResultColumn())
        {
          resultColumns.setCountMismatchAllowed(true);
        }
        //Views execute with definer's privileges and if any one of
        //those privileges' are revoked from the definer, the view gets
        //dropped. So, a view can exist in Derby only if it's owner has
        //all the privileges needed to create one. In order to do a
        //select from a view, a user only needs select privilege on the
        //view and doesn't need any privilege for objects accessed by
        //the view. Hence, when collecting privilege requirement for a
        //sql accessing a view, we only need to look for select privilege
        //on the actual view and that is what the following code is
        //checking.
        for (int i = 0; i < resultColumns.size(); i++) {
          ResultColumn rc = (ResultColumn) resultColumns.elementAt(i);
          if (rc.isPrivilegeCollectionRequired())
            compilerContext.addRequiredColumnPriv( rc.getTableColumnDescriptor());
        }

        fsq = (FromSubquery) getNodeFactory().getNode(
          C_NodeTypes.FROM_SUBQUERY,
          rsn,
                    cvn.getOrderByList(),
                    cvn.getOffset(),
                    cvn.getFetchFirst(),
          (correlationName != null) ?
                        correlationName : getOrigTableName().getTableName(),
          resultColumns,
          tableProperties,
          getContextManager());
        // Transfer the nesting level to the new FromSubquery
        fsq.setLevel(level);
        //We are getting ready to bind the query underneath the view. Since
        //that query is going to run with definer's privileges, we do not
        //need to collect any privilege requirement for that query.
        //Following call is marking the query to run with definer
        //privileges. This marking will make sure that we do not collect
        //any privilege requirement for it.
        fsq.disablePrivilegeCollection();
        fsq.setOrigTableName(this.getOrigTableName());

        // since we reset the compilation schema when we return, we
        // need to save it for use when we bind expressions:
        fsq.setOrigCompilationSchema(compSchema);
        ResultSetNode fsqBound =
          fsq.bindNonVTITables(dataDictionary, fromListParam);

        /* Do error checking on derived column list and update "exposed"
         * column names if valid.
         */
        if (derivedRCL != null) {
View Full Code Here

    templateColumns = resultColumns;

    /* Resolve the view, if this is a view */
    if (tableDescriptor.getTableType() == TableDescriptor.VIEW_TYPE)
    {
      FromSubquery                fsq;
      ResultSetNode        rsn;
      ViewDescriptor        vd;
      CreateViewNode        cvn;
      SchemaDescriptor      compSchema;

      /* Get the associated ViewDescriptor so that we can get
       * the view definition text.
       */
      vd = dataDictionary.getViewDescriptor(tableDescriptor);

      /*
      ** Set the default compilation schema to be whatever
      ** this schema this view was originally compiled against.
      ** That way we pick up the same tables no matter what
      ** schema we are running against.
      */
      compSchema = dataDictionary.getSchemaDescriptor(vd.getCompSchemaId(), null);

      compilerContext.pushCompilationSchema(compSchema);
 
      try
      {
   
        /* This represents a view - query is dependent on the ViewDescriptor */
        compilerContext.createDependency(vd);
 
        if (SanityManager.DEBUG)
        {
          SanityManager.ASSERT(vd != null,
            "vd not expected to be null for " + tableName);
        }
 
        cvn = (CreateViewNode)
                  parseStatement(vd.getViewText(), false);

        rsn = cvn.getParsedQueryExpression();
 
        /* If the view contains a '*' then we mark the views derived column list
         * so that the view will still work, and return the expected results,
         * if any of the tables referenced in the view have columns added to
         * them via ALTER TABLE.  The expected results means that the view
         * will always return the same # of columns.
         */
        if (rsn.getResultColumns().containsAllResultColumn())
        {
          resultColumns.setCountMismatchAllowed(true);
        }
        //Views execute with definer's privileges and if any one of
        //those privileges' are revoked from the definer, the view gets
        //dropped. So, a view can exist in Derby only if it's owner has
        //all the privileges needed to create one. In order to do a
        //select from a view, a user only needs select privilege on the
        //view and doesn't need any privilege for objects accessed by
        //the view. Hence, when collecting privilege requirement for a
        //sql accessing a view, we only need to look for select privilege
        //on the actual view and that is what the following code is
        //checking.
        for (int i = 0; i < resultColumns.size(); i++) {
          ResultColumn rc = (ResultColumn) resultColumns.elementAt(i);
          if (rc.isPrivilegeCollectionRequired())
            compilerContext.addRequiredColumnPriv( rc.getTableColumnDescriptor());
        }

        fsq = (FromSubquery) getNodeFactory().getNode(
          C_NodeTypes.FROM_SUBQUERY,
          rsn,
          (correlationName != null) ?
                        correlationName : getOrigTableName().getTableName(),
          resultColumns,
          tableProperties,
          getContextManager());
        // Transfer the nesting level to the new FromSubquery
        fsq.setLevel(level);
        //We are getting ready to bind the query underneath the view. Since
        //that query is going to run with definer's privileges, we do not
        //need to collect any privilege requirement for that query.
        //Following call is marking the query to run with definer
        //privileges. This marking will make sure that we do not collect
        //any privilege requirement for it.
        fsq.disablePrivilegeCollection();
        fsq.setOrigTableName(this.getOrigTableName());

        // since we reset the compilation schema when we return, we
        // need to save it for use when we bind expressions:
        fsq.setOrigCompilationSchema(compSchema);

        return fsq.bindNonVTITables(dataDictionary, fromListParam);
      }
      finally
      {
        compilerContext.popCompilationSchema();
      }
View Full Code Here

    templateColumns = resultColumns;

    /* Resolve the view, if this is a view */
    if (tableDescriptor.getTableType() == TableDescriptor.VIEW_TYPE)
    {
      FromSubquery                fsq;
      ResultSetNode        rsn;
      ViewDescriptor        vd;
      CreateViewNode        cvn;
      SchemaDescriptor      compSchema;

      /* Get the associated ViewDescriptor so that we can get
       * the view definition text.
       */
      vd = dataDictionary.getViewDescriptor(tableDescriptor);

      /*
      ** Set the default compilation schema to be whatever
      ** this schema this view was originally compiled against.
      ** That way we pick up the same tables no matter what
      ** schema we are running against.
      */
      compSchema = dataDictionary.getSchemaDescriptor(vd.getCompSchemaId(), null);

      compilerContext.pushCompilationSchema(compSchema);
 
      try
      {
   
        /* This represents a view - query is dependent on the ViewDescriptor */
        compilerContext.createDependency(vd);
 
        if (SanityManager.DEBUG)
        {
          SanityManager.ASSERT(vd != null,
            "vd not expected to be null for " + tableName);
        }

        cvn = (CreateViewNode)
                  parseStatement(vd.getViewText(), false);

        rsn = cvn.getParsedQueryExpression();

        /* If the view contains a '*' then we mark the views derived column list
         * so that the view will still work, and return the expected results,
         * if any of the tables referenced in the view have columns added to
         * them via ALTER TABLE.  The expected results means that the view
         * will always return the same # of columns.
         */
        if (rsn.getResultColumns().containsAllResultColumn())
        {
          resultColumns.setCountMismatchAllowed(true);
        }
        //Views execute with definer's privileges and if any one of
        //those privileges' are revoked from the definer, the view gets
        //dropped. So, a view can exist in Derby only if it's owner has
        //all the privileges needed to create one. In order to do a
        //select from a view, a user only needs select privilege on the
        //view and doesn't need any privilege for objects accessed by
        //the view. Hence, when collecting privilege requirement for a
        //sql accessing a view, we only need to look for select privilege
        //on the actual view and that is what the following code is
        //checking.
        for (int i = 0; i < resultColumns.size(); i++) {
          ResultColumn rc = (ResultColumn) resultColumns.elementAt(i);
          if (rc.isPrivilegeCollectionRequired())
            compilerContext.addRequiredColumnPriv( rc.getTableColumnDescriptor());
        }

        fsq = (FromSubquery) getNodeFactory().getNode(
          C_NodeTypes.FROM_SUBQUERY,
          rsn,
                    cvn.getOrderByList(),
                    cvn.getOffset(),
                    cvn.getFetchFirst(),
          (correlationName != null) ?
                        correlationName : getOrigTableName().getTableName(),
          resultColumns,
          tableProperties,
          getContextManager());
        // Transfer the nesting level to the new FromSubquery
        fsq.setLevel(level);
        //We are getting ready to bind the query underneath the view. Since
        //that query is going to run with definer's privileges, we do not
        //need to collect any privilege requirement for that query.
        //Following call is marking the query to run with definer
        //privileges. This marking will make sure that we do not collect
        //any privilege requirement for it.
        fsq.disablePrivilegeCollection();
        fsq.setOrigTableName(this.getOrigTableName());

        // since we reset the compilation schema when we return, we
        // need to save it for use when we bind expressions:
        fsq.setOrigCompilationSchema(compSchema);
        ResultSetNode fsqBound =
          fsq.bindNonVTITables(dataDictionary, fromListParam);

        /* Do error checking on derived column list and update "exposed"
         * column names if valid.
         */
        if (derivedRCL != null) {
View Full Code Here

     */
                if (groupByList != null || havingClause != null)
                {
                        vetSelectList280( selectList );

                        FromSubquery            fromSubquery;
                        ResultColumnList        outerRCL =
                                                                                (ResultColumnList) nodeFactory.getNode(
                                                                                                C_NodeTypes.RESULT_COLUMN_LIST,
                                                                                                getContextManager());

                        /* Wrap the user SELECT in a FromSubquery */
                        fromList = (FromList) nodeFactory.getNode(
                                                                        C_NodeTypes.FROM_LIST,
                                                                        getNodeFactory().doJoinOrderOptimization(),
                                                                        getContextManager());
                        fromSubquery = (FromSubquery) nodeFactory.getNode(
                                                                                                        C_NodeTypes.FROM_SUBQUERY,
                                                                                                        selectNode,
                                                                                                        null,
                                                                                                        null,
                                                                                                        null,
                                                                                                        getContextManager());
                        fromList.addElement(fromSubquery);

                        /* Pull any aggregates out of the HAVING clause and append them to
       * SELECT list in the user's select, replacing the aggregates in
       * the HAVING clause with ColumnReferences to the aggregate.
       * Do NOT replace anything below a ResultSetNode.  This means that
       * we'll replace
       *     FROM x HAVING max(x.x) > 2
       * but not
       *    FROM x HAVING x.x = (select max(y.y) from y)
       * Aggregates under a SELECT are all fixed up correctly
       * later, but here we need to get the aggregates that
       * aren't under result sets.
       *
       * Appended ResultColumns marked as generated so the wrapper
       * select won't see them when the * is expanded.
       *
       * RESOLVE - someday we should try to find matching aggregates
       * instead of just adding them.
             *
             * NOTE: This rewriting of the query tree makes the handling of an ORDER BY
             * clause difficult. See OrderByColumn.pullUpOrderByColumn. It makes specific
             * assumptions about the structure of the generated query tree. Do not make
             * any changes to this transformation without carefully considering the
             * OrderByColumn pullUpOrderByColumn and bindOrderByColumn methods.
       */
                        if (havingClause != null)
                        {
                                ReplaceAggregatesWithCRVisitor visitor =
                                        new ReplaceAggregatesWithCRVisitor(selectList, ResultSetNode.class);
                                havingClause = (ValueNode)havingClause.accept(visitor);

                                // fix for HAVING without GROUP BY, makes sure we get one
                                // aggregate operator by adding a count(*), this fixes beetle 5853, 5890
                                if (groupByList == null) {
                                        ValueNode vn = (ValueNode) nodeFactory.getNode(
                                                                C_NodeTypes.AGGREGATE_NODE,
                                                                null,
                                                                org.apache.derby.impl.sql.compile.CountAggregateDefinition.class,
                                                                Boolean.FALSE, // distinct Boolean.TRUE?
                                                                "COUNT(*)",
                                                                getContextManager());
                                        AggregateNode n = (AggregateNode) vn;
                                        n.replaceAggregatesWithColumnReferences(selectList, 0);
                                }
                        }

                        outerRCL.addResultColumn((ResultColumn) nodeFactory.getNode(
                                                                                                C_NodeTypes.ALL_RESULT_COLUMN,
                                                                                                null,
                                                                                                getContextManager()));
                        // wrap another selectNode on the outside of the subquery
                        selectNode = (SelectNode) nodeFactory.getNode(
                                                                                C_NodeTypes.SELECT_NODE,
                                                                                outerRCL,       /* SELECT * from user SELECT */
                                                                                null,           /* AGGREGATE list */
                                                                                fromList,
                                                                                havingClause,
                                                                                null,           /* GROUP BY list */
                                                                                getContextManager());

                        /* Mark the wrapping FromSubquery and SelectNode as appropriate */
                        if (groupByList != null)
                        {
                                fromSubquery.markAsForGroupByClause();
                                selectNode.markAsForGroupByClause();
                        }
                        if (havingClause != null)
                        {
                                fromSubquery.markAsForHavingClause();
                                selectNode.markAsForHavingClause();
                        }
                }
                {if (true) return selectNode;}
    throw new Error("Missing return statement in function");
View Full Code Here

     * The FromSubquery will have a null name, something that the user cannot do,
     * to enable the binding of any correlated columns in the HAVING clause.
     */
                if (groupByList != null || havingClause != null)
                {
                        FromSubquery            fromSubquery;
                        ResultColumnList        outerRCL =
                                                                                (ResultColumnList) nodeFactory.getNode(
                                                                                                C_NodeTypes.RESULT_COLUMN_LIST,
                                                                                                getContextManager());

                        /* Wrap the user SELECT in a FromSubquery */
                        fromList = (FromList) nodeFactory.getNode(
                                                                        C_NodeTypes.FROM_LIST,
                                                                        getNodeFactory().doJoinOrderOptimization(),
                                                                        getContextManager());
                        fromSubquery = (FromSubquery) nodeFactory.getNode(
                                                                                                        C_NodeTypes.FROM_SUBQUERY,
                                                                                                        selectNode,
                                                                                                        null,
                                                                                                        null,
                                                                                                        null,
                                                                                                        getContextManager());
                        fromList.addElement(fromSubquery);

                        /* Pull any aggregates out of the HAVING clause and append them to
       * SELECT list in the user's select, replacing the aggregates in
       * the HAVING clause with ColumnReferences to the aggregate.
       * Do NOT replace anything below a ResultSetNode.  This means that
       * we'll replace
       *     FROM x HAVING max(x.x) > 2
       * but not
       *    FROM x HAVING x.x = (select max(y.y) from y)
       * Aggregates under a SELECT are all fixed up correctly
       * later, but here we need to get the aggregates that
       * aren't under result sets.
       *
       * Appended ResultColumns marked as generated so the wrapper
       * select won't see them when the * is expanded.
       *
       * RESOLVE - someday we should try to find matching aggregates
       * instead of just adding them.
             *
             * NOTE: This rewriting of the query tree makes the handling of an ORDER BY
             * clause difficult. See OrderByColumn.pullUpOrderByColumn. It makes specific
             * assumptions about the structure of the generated query tree. Do not make
             * any changes to this transformation without carefully considering the
             * OrderByColumn pullUpOrderByColumn and bindOrderByColumn methods.
       */
                        if (havingClause != null)
                        {
                                ReplaceAggregatesWithCRVisitor visitor =
                                        new ReplaceAggregatesWithCRVisitor(selectList, ResultSetNode.class);
                                havingClause = (ValueNode)havingClause.accept(visitor);

                                // fix for HAVING without GROUP BY, makes sure we get one
                                // aggregate operator by adding a count(*), this fixes beetle 5853, 5890
                                if (groupByList == null) {
                                        ValueNode vn = (ValueNode) nodeFactory.getNode(
                                                                C_NodeTypes.AGGREGATE_NODE,
                                                                null,
                                                                org.apache.derby.impl.sql.compile.CountAggregateDefinition.class,
                                                                Boolean.FALSE, // distinct Boolean.TRUE?
                                                                "COUNT(*)",
                                                                getContextManager());
                                        AggregateNode n = (AggregateNode) vn;
                                        n.replaceAggregatesWithColumnReferences(selectList, 0);
                                }
                        }

                        outerRCL.addResultColumn((ResultColumn) nodeFactory.getNode(
                                                                                                C_NodeTypes.ALL_RESULT_COLUMN,
                                                                                                null,
                                                                                                getContextManager()));
                        // wrap another selectNode on the outside of the subquery
                        selectNode = (SelectNode) nodeFactory.getNode(
                                                                                C_NodeTypes.SELECT_NODE,
                                                                                outerRCL,       /* SELECT * from user SELECT */
                                                                                null,           /* AGGREGATE list */
                                                                                fromList,
                                                                                havingClause,
                                                                                null,           /* GROUP BY list */
                                                                                getContextManager());

                        /* Mark the wrapping FromSubquery and SelectNode as appropriate */
                        if (groupByList != null)
                        {
                                fromSubquery.markAsForGroupByClause();
                                selectNode.markAsForGroupByClause();
                        }
                        if (havingClause != null)
                        {
                                fromSubquery.markAsForHavingClause();
                                selectNode.markAsForHavingClause();
                        }
                }
                {if (true) return selectNode;}
    throw new Error("Missing return statement in function");
View Full Code Here

     * The FromSubquery will have a null name, something that the user cannot do,
     * to enable the binding of any correlated columns in the HAVING clause.
     */
                if (groupByList != null || havingClause != null)
                {
                        FromSubquery            fromSubquery;
                        ResultColumnList        outerRCL =
                                                                                (ResultColumnList) nodeFactory.getNode(
                                                                                                C_NodeTypes.RESULT_COLUMN_LIST,
                                                                                                getContextManager());

                        /* Wrap the user SELECT in a FromSubquery */
                        fromList = (FromList) nodeFactory.getNode(
                                                                        C_NodeTypes.FROM_LIST,
                                                                        getNodeFactory().doJoinOrderOptimization(),
                                                                        getContextManager());
                        fromSubquery = (FromSubquery) nodeFactory.getNode(
                                                                                                        C_NodeTypes.FROM_SUBQUERY,
                                                                                                        selectNode,
                                                                                                        null,
                                                                                                        null,
                                                                                                        null,
                                                                                                        getContextManager());
                        fromList.addElement(fromSubquery);

                        /* Pull any aggregates out of the HAVING clause and append them to
       * SELECT list in the user's select, replacing the aggregates in
       * the HAVING clause with ColumnReferences to the aggregate.
       * Do NOT replace anything below a ResultSetNode.  This means that
       * we'll replace
       *     FROM x HAVING max(x.x) > 2
       * but not
       *    FROM x HAVING x.x = (select max(y.y) from y)
       * Aggregates under a SELECT are all fixed up correctly
       * later, but here we need to get the aggregates that
       * aren't under result sets.
       *
       * Appended ResultColumns marked as generated so the wrapper
       * select won't see them when the * is expanded.
       *
       * RESOLVE - someday we should try to find matching aggregates
       * instead of just adding them.
             *
             * NOTE: This rewriting of the query tree makes the handling of an ORDER BY
             * clause difficult. See OrderByColumn.pullUpOrderByColumn. It makes specific
             * assumptions about the structure of the generated query tree. Do not make
             * any changes to this transformation without carefully considering the
             * OrderByColumn pullUpOrderByColumn and bindOrderByColumn methods.
       */
                        if (havingClause != null)
                        {
                                ReplaceAggregatesWithCRVisitor visitor =
                                        new ReplaceAggregatesWithCRVisitor(selectList, ResultSetNode.class);
                                havingClause = (ValueNode)havingClause.accept(visitor);

                                // fix for HAVING without GROUP BY, makes sure we get one
                                // aggregate operator by adding a count(*), this fixes beetle 5853, 5890
                                if (groupByList == null) {
                                        ValueNode vn = (ValueNode) nodeFactory.getNode(
                                                                C_NodeTypes.AGGREGATE_NODE,
                                                                null,
                                                                org.apache.derby.impl.sql.compile.CountAggregateDefinition.class,
                                                                Boolean.FALSE, // distinct Boolean.TRUE?
                                                                "COUNT(*)",
                                                                getContextManager());
                                        AggregateNode n = (AggregateNode) vn;
                                        n.replaceAggregatesWithColumnReferences(selectList, 0);
                                }
                        }

                        outerRCL.addResultColumn((ResultColumn) nodeFactory.getNode(
                                                                                                C_NodeTypes.ALL_RESULT_COLUMN,
                                                                                                null,
                                                                                                getContextManager()));
                        // wrap another selectNode on the outside of the subquery
                        selectNode = (SelectNode) nodeFactory.getNode(
                                                                                C_NodeTypes.SELECT_NODE,
                                                                                outerRCL,       /* SELECT * from user SELECT */
                                                                                null,           /* AGGREGATE list */
                                                                                fromList,
                                                                                havingClause,
                                                                                null,           /* GROUP BY list */
                                                                                getContextManager());

                        /* Mark the wrapping FromSubquery and SelectNode as appropriate */
                        if (groupByList != null)
                        {
                                fromSubquery.markAsForGroupByClause();
                                selectNode.markAsForGroupByClause();
                        }
                        if (havingClause != null)
                        {
                                fromSubquery.markAsForHavingClause();
                                selectNode.markAsForHavingClause();
                        }
                }
                {if (true) return selectNode;}
    throw new Error("Missing return statement in function");
View Full Code Here

     * The FromSubquery will have a null name, something that the user cannot do,
     * to enable the binding of any correlated columns in the HAVING clause.
     */
                if (groupByList != null || havingClause != null)
                {
                        FromSubquery            fromSubquery;
                        ResultColumnList        outerRCL =
                                                                                (ResultColumnList) nodeFactory.getNode(
                                                                                                C_NodeTypes.RESULT_COLUMN_LIST,
                                                                                                getContextManager());

                        /* Wrap the user SELECT in a FromSubquery */
                        fromList = (FromList) nodeFactory.getNode(
                                                                        C_NodeTypes.FROM_LIST,
                                                                        getNodeFactory().doJoinOrderOptimization(),
                                                                        getContextManager());
                        fromSubquery = (FromSubquery) nodeFactory.getNode(
                                                                                                        C_NodeTypes.FROM_SUBQUERY,
                                                                                                        selectNode,
                                                                                                        null,
                                                                                                        null,
                                                                                                        null,
                                                                                                        getContextManager());
                        fromList.addElement(fromSubquery);

                        /* Pull any aggregates out of the HAVING clause and append them to
       * SELECT list in the user's select, replacing the aggregates in
       * the HAVING clause with ColumnReferences to the aggregate.
       * Do NOT replace anything below a ResultSetNode.  This means that
       * we'll replace
       *     FROM x HAVING max(x.x) > 2
       * but not
       *    FROM x HAVING x.x = (select max(y.y) from y)
       * Aggregates under a SELECT are all fixed up correctly
       * later, but here we need to get the aggregates that
       * aren't under result sets.
       *
       * Appended ResultColumns marked as generated so the wrapper
       * select won't see them when the * is expanded.
       *
       * RESOLVE - someday we should try to find matching aggregates
       * instead of just adding them.
             *
             * NOTE: This rewriting of the query tree makes the handling of an ORDER BY
             * clause difficult. See OrderByColumn.pullUpOrderByColumn. It makes specific
             * assumptions about the structure of the generated query tree. Do not make
             * any changes to this transformation without carefully considering the
             * OrderByColumn pullUpOrderByColumn and bindOrderByColumn methods.
       */
                        if (havingClause != null)
                        {
                                ReplaceAggregatesWithCRVisitor visitor =
                                        new ReplaceAggregatesWithCRVisitor(selectList, ResultSetNode.class);
                                havingClause = (ValueNode)havingClause.accept(visitor);

                                // fix for HAVING without GROUP BY, makes sure we get one
                                // aggregate operator by adding a count(*), this fixes beetle 5853, 5890
                                if (groupByList == null) {
                                        ValueNode vn = (ValueNode) nodeFactory.getNode(
                                                                C_NodeTypes.AGGREGATE_NODE,
                                                                null,
                                                                org.apache.derby.impl.sql.compile.CountAggregateDefinition.class,
                                                                Boolean.FALSE, // distinct Boolean.TRUE?
                                                                "COUNT(*)",
                                                                getContextManager());
                                        AggregateNode n = (AggregateNode) vn;
                                        n.replaceAggregatesWithColumnReferences(selectList, 0);
                                }
                        }

                        outerRCL.addResultColumn((ResultColumn) nodeFactory.getNode(
                                                                                                C_NodeTypes.ALL_RESULT_COLUMN,
                                                                                                null,
                                                                                                getContextManager()));
                        // wrap another selectNode on the outside of the subquery
                        selectNode = (SelectNode) nodeFactory.getNode(
                                                                                C_NodeTypes.SELECT_NODE,
                                                                                outerRCL,       /* SELECT * from user SELECT */
                                                                                null,           /* AGGREGATE list */
                                                                                fromList,
                                                                                havingClause,
                                                                                null,           /* GROUP BY list */
                                                                                getContextManager());

                        /* Mark the wrapping FromSubquery and SelectNode as appropriate */
                        if (groupByList != null)
                        {
                                fromSubquery.markAsForGroupByClause();
                                selectNode.markAsForGroupByClause();
                        }
                        if (havingClause != null)
                        {
                                fromSubquery.markAsForHavingClause();
                                selectNode.markAsForHavingClause();
                        }
                }
                {if (true) return selectNode;}
    throw new Error("Missing return statement in function");
View Full Code Here

TOP

Related Classes of org.apache.derby.impl.sql.compile.FromSubquery

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.