func resolveBarewordSetVariables(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope) (sql.Node, error) {
	_, ok := n.(*plan.Set)
	if !ok || n.Resolved() {
		return n, nil
	}
	return plan.TransformExpressionsUp(n, func(e sql.Expression) (sql.Expression, error) {
		sf, ok := e.(*expression.SetField)
		if !ok {
			return e, nil
		}
		setVal, err := getSetVal(ctx, sf.Left.String(), sf.Right)
		if err != nil {
			return nil, err
		}
		if uc, ok := sf.Left.(*deferredColumn); ok {
			varName := uc.String()
			_, _, ok = sql.SystemVariables.GetGlobal(varName)
			if !ok {
				return sf, nil
			}
			if uc, ok := setVal.(column); ok && uc.Table() == "" {
				_, setScope, _ := sqlparser.VarScope(uc.Name())
				if setScope == sqlparser.SetScope_None {
					setVal = expression.NewLiteral(uc.Name(), sql.LongText)
				}
			}
			return sf.WithChildren(expression.NewSystemVar(varName, sql.SystemVariableScope_Session), setVal)
		}
		return sf, nil
	})
}
resolveUnquotedSetVariables does a similar pass as resolveSetVariables, but handles system vars that were provided
as barewords (vars not prefixed with @@, and string values unquoted). These will have been deferred into
deferredColumns by the resolve_columns rule.
package analyzer
import (
	"fmt"
	"strings"
	"github.com/dolthub/vitess/go/vt/sqlparser"
	"github.com/dolthub/go-mysql-server/sql"
	"github.com/dolthub/go-mysql-server/sql/expression"
	"github.com/dolthub/go-mysql-server/sql/parse"
	"github.com/dolthub/go-mysql-server/sql/plan"
)
func resolveVariables(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope) (sql.Node, error) {
	span, ctx := ctx.Span("resolve_variables")
	defer span.Finish()
	return plan.TransformUp(n, func(n sql.Node) (sql.Node, error) {
		if n.Resolved() {
			return n, nil
		}
		resolveVars := func(e sql.Expression) (sql.Expression, error) {
			uc, ok := e.(column)
			if !ok || e.Resolved() {
				return e, nil
			}
			expr, ok, err := resolveSystemOrUserVariable(ctx, a, uc)
			if err != nil {
				return nil, err
			}
			if ok {
				return expr, nil
			}
			return e, nil
		}
		if n, ok := n.(*plan.Set); ok {
			return plan.TransformExpressionsUp(n, func(e sql.Expression) (sql.Expression, error) {
				sf, ok := e.(*expression.SetField)
				if !ok {
					return e, nil
				}
				nr, err := expression.TransformUp(sf.Right, resolveVars)
				if err != nil {
					return nil, err
				}
				return sf.WithChildren(sf.Left, nr)
			})
		}
		return plan.TransformExpressionsUp(n, resolveVars)
	})
}
func resolveSetVariables(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope) (sql.Node, error) {
	return plan.TransformUp(n, func(n sql.Node) (sql.Node, error) {
		_, ok := n.(*plan.Set)
		if !ok || n.Resolved() {
			return n, nil
		}
		return plan.TransformExpressionsUp(n, func(e sql.Expression) (sql.Expression, error) {
			sf, ok := e.(*expression.SetField)
			if !ok {
				return e, nil
			}
			setExpr := sf.Left
			varName := sf.Left.String()
			setVal, err := getSetVal(ctx, varName, sf.Right)
			if err != nil {
				return nil, err
			}
			if _, ok := sf.Left.(*expression.UnresolvedColumn); ok {
				var scope sqlparser.SetScope
				varName, scope, err = sqlparser.VarScope(varName)
				if err != nil {
					return nil, err
				}
				switch scope {
				case sqlparser.SetScope_None:
					return sf, nil
				case sqlparser.SetScope_Global:
					_, _, ok = sql.SystemVariables.GetGlobal(varName)
					if !ok {
						return nil, sql.ErrUnknownSystemVariable.New(varName)
					}
					setExpr = expression.NewSystemVar(varName, sql.SystemVariableScope_Global)
				case sqlparser.SetScope_Persist:
					return nil, sql.ErrUnsupportedFeature.New("PERSIST")
				case sqlparser.SetScope_PersistOnly:
					return nil, sql.ErrUnsupportedFeature.New("PERSIST_ONLY")
				case sqlparser.SetScope_Session:
					_, err = ctx.GetSessionVariable(ctx, varName)
					if err != nil {
						return nil, err
					}
					setExpr = expression.NewSystemVar(varName, sql.SystemVariableScope_Session)
				case sqlparser.SetScope_User:
					setExpr = expression.NewUserVar(varName)
				default:  
					return nil, fmt.Errorf("unknown set scope %v", scope)
				}
			}
			if _, ok := setExpr.(*expression.SystemVar); ok {
				if uc, ok := setVal.(*expression.UnresolvedColumn); ok && uc.Table() == "" {
					_, setScope, _ := sqlparser.VarScope(uc.Name())
					if setScope == sqlparser.SetScope_None {
						setVal = expression.NewLiteral(uc.Name(), sql.LongText)
					}
				}
			}
			if _, ok := setExpr.(*expression.SystemVar); ok {
				return sf.WithChildren(setExpr, setVal)
			} else if _, ok := setExpr.(*expression.UserVar); ok {
				return sf.WithChildren(setExpr, setVal)
			}
			return sf, nil
		})
	})
}