github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/sqle/dfunctions/squash.go (about) 1 // Copyright 2021 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 dfunctions 16 17 import ( 18 "fmt" 19 20 "github.com/dolthub/go-mysql-server/sql" 21 "github.com/dolthub/go-mysql-server/sql/expression" 22 23 "github.com/dolthub/dolt/go/libraries/doltcore/merge" 24 "github.com/dolthub/dolt/go/libraries/doltcore/sqle" 25 ) 26 27 const SquashFuncName = "squash" 28 29 type SquashFunc struct { 30 expression.UnaryExpression 31 } 32 33 func NewSquashFunc(ctx *sql.Context, child sql.Expression) sql.Expression { 34 return &SquashFunc{expression.UnaryExpression{Child: child}} 35 } 36 37 func (s SquashFunc) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 38 sess := sqle.DSessFromSess(ctx.Session) 39 40 branchVal, err := s.Child.Eval(ctx, row) 41 if err != nil { 42 return nil, err 43 } 44 45 branchName, ok := branchVal.(string) 46 if !ok { 47 return nil, fmt.Errorf("error: SQUASH() was given a non-string branch name") 48 } 49 50 dbName := sess.GetCurrentDatabase() 51 ddb, ok := sess.GetDoltDB(dbName) 52 if !ok { 53 return nil, sql.ErrDatabaseNotFound.New(dbName) 54 } 55 56 root, ok := sess.GetRoot(dbName) 57 if !ok { 58 return nil, sql.ErrDatabaseNotFound.New(dbName) 59 } 60 61 head, _, headRoot, err := getHead(ctx, sess, dbName) 62 if err != nil { 63 return nil, err 64 } 65 66 err = checkForUncommittedChanges(root, headRoot) 67 if err != nil { 68 return nil, err 69 } 70 71 cm, _, err := getBranchCommit(ctx, branchName, ddb) 72 if err != nil { 73 return nil, err 74 } 75 76 mergeRoot, _, err := merge.MergeCommits(ctx, head, cm) 77 if err != nil { 78 return nil, err 79 } 80 81 h, err := ddb.WriteRootValue(ctx, mergeRoot) 82 if err != nil { 83 return nil, err 84 } 85 86 return h.String(), nil 87 } 88 89 func (s SquashFunc) Resolved() bool { 90 return s.Child.Resolved() 91 } 92 93 func (s SquashFunc) String() string { 94 return fmt.Sprintf("SQUASH(%s)", s.Child.String()) 95 } 96 97 func (s SquashFunc) Type() sql.Type { 98 return sql.Text 99 } 100 101 func (s SquashFunc) IsNullable() bool { 102 return false 103 } 104 105 func (s SquashFunc) Children() []sql.Expression { 106 return []sql.Expression{s.Child} 107 } 108 109 func (s SquashFunc) WithChildren(ctx *sql.Context, children ...sql.Expression) (sql.Expression, error) { 110 if len(children) != 1 { 111 return nil, sql.ErrInvalidChildrenNumber.New(s, len(children), 1) 112 } 113 114 return NewSquashFunc(ctx, children[0]), nil 115 }