github.com/dolthub/go-mysql-server@v0.18.0/sql/planbuilder/subquery.go (about)

     1  // Copyright 2023 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package planbuilder
    16  
    17  import "github.com/dolthub/go-mysql-server/sql"
    18  
    19  type subquery struct {
    20  	parent     *subquery
    21  	correlated sql.ColSet
    22  	volatile   bool
    23  }
    24  
    25  // addOutOfScope is used to tag subqueries that failed to resolve a column.
    26  // The way this works is that we have a scope where we initiate a resolve
    27  // (scope_0), and we will work upwards through the scope chain until we find
    28  // the scope with the column (ex: scope_n). After finding the column, we wind
    29  // back upwards through the callstack to return the column. During the unwind,
    30  // check and tag every scope in between scope_0...scope_n-1 that is a subquery.
    31  // This prevents propagating the correlation tag above the scope where the
    32  // column is defined.
    33  func (s *subquery) addOutOfScope(c columnId) {
    34  	if s == nil {
    35  		return
    36  	}
    37  	s.correlated.Add(sql.ColumnId(c))
    38  }
    39  
    40  // markVolatile marks this and every parent subquery as volatile.
    41  func (s *subquery) markVolatile() {
    42  	s.volatile = true
    43  	if s.parent != nil {
    44  		s.parent.markVolatile()
    45  	}
    46  }