github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/isbinary.go (about)

     1  // Copyright 2020-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 function
    16  
    17  import (
    18  	"bytes"
    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  
    26  // IsBinary is a function that returns whether a blob is binary or not.
    27  type IsBinary struct {
    28  	expression.UnaryExpression
    29  }
    30  
    31  var _ sql.FunctionExpression = (*IsBinary)(nil)
    32  var _ sql.CollationCoercible = (*IsBinary)(nil)
    33  
    34  // NewIsBinary creates a new IsBinary expression.
    35  func NewIsBinary(e sql.Expression) sql.Expression {
    36  	return &IsBinary{expression.UnaryExpression{Child: e}}
    37  }
    38  
    39  // FunctionName implements sql.FunctionExpression
    40  func (ib *IsBinary) FunctionName() string {
    41  	return "is_binary"
    42  }
    43  
    44  // Description implements sql.FunctionExpression
    45  func (ib *IsBinary) Description() string {
    46  	return "returns whether a blob is a binary file or not."
    47  }
    48  
    49  // Eval implements the Expression interface.
    50  func (ib *IsBinary) Eval(
    51  	ctx *sql.Context,
    52  	row sql.Row,
    53  ) (interface{}, error) {
    54  	v, err := ib.Child.Eval(ctx, row)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	if v == nil {
    60  		return false, nil
    61  	}
    62  
    63  	blob, _, err := types.LongBlob.Convert(v)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	return isBinary(blob.([]byte)), nil
    69  }
    70  
    71  func (ib *IsBinary) String() string {
    72  	return fmt.Sprintf("%s(%s)", ib.FunctionName(), ib.Child)
    73  }
    74  
    75  // WithChildren implements the Expression interface.
    76  func (ib *IsBinary) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    77  	if len(children) != 1 {
    78  		return nil, sql.ErrInvalidChildrenNumber.New(ib, len(children), 1)
    79  	}
    80  	return NewIsBinary(children[0]), nil
    81  }
    82  
    83  // Type implements the Expression interface.
    84  func (ib *IsBinary) Type() sql.Type {
    85  	return types.Boolean
    86  }
    87  
    88  // CollationCoercibility implements the interface sql.CollationCoercible.
    89  func (*IsBinary) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    90  	return sql.Collation_binary, 5
    91  }
    92  
    93  const sniffLen = 8000
    94  
    95  // isBinary detects if data is a binary value based on:
    96  // http://git.kernel.org/cgit/git/git.git/tree/xdiff-interface.c?id=HEAD#n198
    97  func isBinary(data []byte) bool {
    98  	if len(data) > sniffLen {
    99  		data = data[:sniffLen]
   100  	}
   101  
   102  	if bytes.IndexByte(data, byte(0)) == -1 {
   103  		return false
   104  	}
   105  
   106  	return true
   107  }