Exemplo n.º 1
0
// Evaluate SubqueryExpr.
// Get the value from v.SubQuery and set it to v.
func (e *Evaluator) subqueryExpr(v *ast.SubqueryExpr) bool {
	if v.SubqueryExec != nil {
		v.SetValue(v.SubqueryExec.GetValue())
	}
	v.Evaluated = true
	return true
}
Exemplo n.º 2
0
// EvalSubquery evaluates a subquery.
func EvalSubquery(ctx context.Context, v *ast.SubqueryExpr) error {
	if v.SubqueryExec != nil {
		rowCount := 2
		if v.MultiRows {
			rowCount = -1
		} else if v.Exists {
			rowCount = 1
		}
		rows, err := v.SubqueryExec.EvalRows(ctx, rowCount)
		if err != nil {
			return errors.Trace(err)
		}
		if v.MultiRows || v.Exists {
			v.GetDatum().SetRow(rows)
			v.Evaluated = true
			return nil
		}
		switch len(rows) {
		case 0:
			v.SetNull()
		case 1:
			v.SetDatum(rows[0])
		default:
			return errors.New("Subquery returns more than 1 row")
		}
	}
	v.Evaluated = true
	return nil
}