Example #1
0
/*
This method qualifies identifiers for all the constituent clauses,
namely the subresult, order, limit and offset within a subquery.
For the subresult of the subquery, call Formalize, for the order
by clause call MapExpressions, for limit and offset call Accept.
*/
func (this *Select) FormalizeSubquery(parent *expression.Formalizer) error {
	f, err := this.subresult.Formalize(parent)
	if err != nil {
		return err
	}

	this.correlated = this.subresult.IsCorrelated()

	if this.order != nil {
		err = this.order.MapExpressions(f)
		if err != nil {
			return err
		}

		if !this.correlated {
			// Determine if this is a correlated subquery
			immediate := f.Allowed().GetValue().Fields()
			for ident, _ := range f.Identifiers() {
				if _, ok := immediate[ident]; !ok {
					this.correlated = true
					break
				}
			}
		}
	}

	if this.limit == nil && this.offset == nil {
		return err
	}

	if !this.correlated {
		prevIdentifiers := parent.Identifiers()
		defer parent.SetIdentifiers(prevIdentifiers)
		parent.SetIdentifiers(make(map[string]bool))
	}

	if this.limit != nil {
		_, err = this.limit.Accept(parent)
		if err != nil {
			return err
		}
	}

	if this.offset != nil {
		_, err = this.offset.Accept(parent)
		if err != nil {
			return err
		}
	}

	if !this.correlated {
		this.correlated = len(parent.Identifiers()) > 0
	}

	return err
}