github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/dfunctions/hashof_table.go (about)

     1  // Copyright 2024 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  	"errors"
    19  	"fmt"
    20  
    21  	"github.com/dolthub/go-mysql-server/sql"
    22  	"github.com/dolthub/go-mysql-server/sql/expression"
    23  	"github.com/dolthub/go-mysql-server/sql/types"
    24  
    25  	"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
    26  	"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess"
    27  )
    28  
    29  const HashOfTableFuncName = "dolt_hashof_table"
    30  
    31  type HashOfTable struct {
    32  	expression.UnaryExpression
    33  }
    34  
    35  var _ sql.FunctionExpression = (*HashOfTable)(nil)
    36  
    37  // NewHashOfTable creates a new HashOfTable expression.
    38  func NewHashOfTable(e sql.Expression) sql.Expression {
    39  	return &HashOfTable{expression.UnaryExpression{Child: e}}
    40  }
    41  
    42  // Eval implements the Expression interface.
    43  func (t *HashOfTable) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    44  	val, err := t.Child.Eval(ctx, row)
    45  
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	if val == nil {
    51  		return nil, nil
    52  	}
    53  
    54  	tableName, ok := val.(string)
    55  
    56  	if !ok {
    57  		return nil, errors.New("table name is not a string")
    58  	}
    59  
    60  	dbName := ctx.GetCurrentDatabase()
    61  	ds := dsess.DSessFromSess(ctx.Session)
    62  	roots, ok := ds.GetRoots(ctx, dbName)
    63  	if !ok {
    64  		return nil, sql.ErrDatabaseNotFound.New(dbName)
    65  	}
    66  
    67  	tbl, ok, err := roots.Working.GetTable(ctx, doltdb.TableName{Name: tableName})
    68  
    69  	if err != nil {
    70  		return nil, fmt.Errorf("error getting table %s: %w", tableName, err)
    71  	} else if !ok {
    72  		return nil, sql.ErrTableNotFound.New(tableName)
    73  	}
    74  
    75  	h, err := tbl.HashOf()
    76  	if err != nil {
    77  		return nil, fmt.Errorf("error getting hash of table %s: %w", tableName, err)
    78  	}
    79  
    80  	return h.String(), nil
    81  }
    82  
    83  // String implements the Stringer interface.
    84  func (t *HashOfTable) String() string {
    85  	return fmt.Sprintf("%s(%s)", HashOfTableFuncName, t.Child.String())
    86  }
    87  
    88  // FunctionName implements the FunctionExpression interface
    89  func (t *HashOfTable) FunctionName() string {
    90  	return HashOfTableFuncName
    91  }
    92  
    93  // Description implements the FunctionExpression interface
    94  func (t *HashOfTable) Description() string {
    95  	return "returns a hash of the contents of a table, typically used for detecting if a table's data has changed"
    96  }
    97  
    98  // IsNullable implements the Expression interface.
    99  func (t *HashOfTable) IsNullable() bool {
   100  	return t.Child.IsNullable()
   101  }
   102  
   103  // WithChildren implements the Expression interface.
   104  func (t *HashOfTable) WithChildren(children ...sql.Expression) (sql.Expression, error) {
   105  	if len(children) != 1 {
   106  		return nil, sql.ErrInvalidChildrenNumber.New(t, len(children), 1)
   107  	}
   108  	return NewHashOfTable(children[0]), nil
   109  }
   110  
   111  // Type implements the Expression interface.
   112  func (t *HashOfTable) Type() sql.Type {
   113  	return types.Text
   114  }