Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sql/expression/div.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ func getFinalScale(ctx *sql.Context, row sql.Row, expr sql.Expression, divOpCnt

var fScale uint8
if lit, isLit := expr.(*Literal); isLit {
_, fScale = GetPrecisionAndScale(lit.value)
_, fScale = GetPrecisionAndScale(lit.Val)
}
typ := expr.Type()
if dt, dok := typ.(sql.DecimalType); dok {
Expand Down
45 changes: 27 additions & 18 deletions sql/expression/literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"strings"

"github.com/dolthub/vitess/go/vt/proto/query"
"github.com/dolthub/vitess/go/vt/sqlparser"
"github.com/shopspring/decimal"

"github.com/dolthub/go-mysql-server/sql"
Expand All @@ -27,22 +28,23 @@ import (

// Literal represents a literal expression (string, number, bool, ...).
type Literal struct {
value interface{}
val2 sql.Value
fieldType sql.Type
Val interface{}
Typ sql.Type
val2 sql.Value
}

var _ sql.Expression = &Literal{}
var _ sql.Expression2 = &Literal{}
var _ sql.CollationCoercible = &Literal{}
var _ sqlparser.Injectable = &Literal{}

// NewLiteral creates a new Literal expression.
func NewLiteral(value interface{}, fieldType sql.Type) *Literal {
val2, _ := sql.ConvertToValue(value)
return &Literal{
value: value,
val2: val2,
fieldType: fieldType,
Val: value,
val2: val2,
Typ: fieldType,
}
}

Expand All @@ -53,34 +55,34 @@ func (lit *Literal) Resolved() bool {

// IsNullable implements the Expression interface.
func (lit *Literal) IsNullable() bool {
return lit.value == nil
return lit.Val == nil
}

// Type implements the Expression interface.
func (lit *Literal) Type() sql.Type {
return lit.fieldType
return lit.Typ
}

// CollationCoercibility implements the interface sql.CollationCoercible.
func (lit *Literal) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
if types.IsText(lit.fieldType) {
collation, _ = lit.fieldType.CollationCoercibility(ctx)
if types.IsText(lit.Typ) {
collation, _ = lit.Typ.CollationCoercibility(ctx)
return collation, 4
}
return sql.Collation_binary, 5
}

// Eval implements the Expression interface.
func (lit *Literal) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return lit.value, nil
return lit.Val, nil
}

func (lit *Literal) String() string {
switch litVal := lit.value.(type) {
switch litVal := lit.Val.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return fmt.Sprintf("%d", litVal)
case string:
switch lit.fieldType.Type() {
switch lit.Typ.Type() {
// utf8 charset cannot encode binary string
case query.Type_VARBINARY, query.Type_BINARY:
return fmt.Sprintf("'0x%X'", litVal)
Expand All @@ -102,8 +104,8 @@ func (lit *Literal) String() string {
}

func (lit *Literal) DebugString() string {
typeStr := lit.fieldType.String()
switch v := lit.value.(type) {
typeStr := lit.Typ.String()
switch v := lit.Val.(type) {
case string:
return fmt.Sprintf("%s (%s)", v, typeStr)
case []byte:
Expand Down Expand Up @@ -139,14 +141,21 @@ func (lit *Literal) Eval2(ctx *sql.Context, row sql.Row2) (sql.Value, error) {
}

func (lit *Literal) Type2() sql.Type2 {
t2, ok := lit.fieldType.(sql.Type2)
t2, ok := lit.Typ.(sql.Type2)
if !ok {
panic(fmt.Errorf("expected Type2, but was %T", lit.fieldType))
panic(fmt.Errorf("expected Type2, but was %T", lit.Typ))
}
return t2
}

// Value returns the literal value.
func (p *Literal) Value() interface{} {
return p.value
return p.Val
}

func (lit *Literal) WithResolvedChildren(children []any) (any, error) {
if len(children) != 0 {
return nil, sql.ErrInvalidChildrenNumber.New(lit, len(children), 0)
}
return lit, nil
}
Loading