Skip to content

Commit ec78760

Browse files
authored
Merge pull request #2959 from dolthub/zachmu/literals-alt
Doltgres literal support
2 parents d3b95b2 + 377894a commit ec78760

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

sql/expression/div.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ func getFinalScale(ctx *sql.Context, row sql.Row, expr sql.Expression, divOpCnt
577577

578578
var fScale uint8
579579
if lit, isLit := expr.(*Literal); isLit {
580-
_, fScale = GetPrecisionAndScale(lit.value)
580+
_, fScale = GetPrecisionAndScale(lit.Val)
581581
}
582582
typ := expr.Type()
583583
if dt, dok := typ.(sql.DecimalType); dok {

sql/expression/literal.go

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"strings"
2020

2121
"github.com/dolthub/vitess/go/vt/proto/query"
22+
"github.com/dolthub/vitess/go/vt/sqlparser"
2223
"github.com/shopspring/decimal"
2324

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

2829
// Literal represents a literal expression (string, number, bool, ...).
2930
type Literal struct {
30-
value interface{}
31-
val2 sql.Value
32-
fieldType sql.Type
31+
Val interface{}
32+
Typ sql.Type
33+
val2 sql.Value
3334
}
3435

3536
var _ sql.Expression = &Literal{}
3637
var _ sql.Expression2 = &Literal{}
3738
var _ sql.CollationCoercible = &Literal{}
39+
var _ sqlparser.Injectable = &Literal{}
3840

3941
// NewLiteral creates a new Literal expression.
4042
func NewLiteral(value interface{}, fieldType sql.Type) *Literal {
4143
val2, _ := sql.ConvertToValue(value)
4244
return &Literal{
43-
value: value,
44-
val2: val2,
45-
fieldType: fieldType,
45+
Val: value,
46+
val2: val2,
47+
Typ: fieldType,
4648
}
4749
}
4850

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

5456
// IsNullable implements the Expression interface.
5557
func (lit *Literal) IsNullable() bool {
56-
return lit.value == nil
58+
return lit.Val == nil
5759
}
5860

5961
// Type implements the Expression interface.
6062
func (lit *Literal) Type() sql.Type {
61-
return lit.fieldType
63+
return lit.Typ
6264
}
6365

6466
// CollationCoercibility implements the interface sql.CollationCoercible.
6567
func (lit *Literal) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
66-
if types.IsText(lit.fieldType) {
67-
collation, _ = lit.fieldType.CollationCoercibility(ctx)
68+
if types.IsText(lit.Typ) {
69+
collation, _ = lit.Typ.CollationCoercibility(ctx)
6870
return collation, 4
6971
}
7072
return sql.Collation_binary, 5
7173
}
7274

7375
// Eval implements the Expression interface.
7476
func (lit *Literal) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
75-
return lit.value, nil
77+
return lit.Val, nil
7678
}
7779

7880
func (lit *Literal) String() string {
79-
switch litVal := lit.value.(type) {
81+
switch litVal := lit.Val.(type) {
8082
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
8183
return fmt.Sprintf("%d", litVal)
8284
case string:
83-
switch lit.fieldType.Type() {
85+
switch lit.Typ.Type() {
8486
// utf8 charset cannot encode binary string
8587
case query.Type_VARBINARY, query.Type_BINARY:
8688
return fmt.Sprintf("'0x%X'", litVal)
@@ -102,8 +104,8 @@ func (lit *Literal) String() string {
102104
}
103105

104106
func (lit *Literal) DebugString() string {
105-
typeStr := lit.fieldType.String()
106-
switch v := lit.value.(type) {
107+
typeStr := lit.Typ.String()
108+
switch v := lit.Val.(type) {
107109
case string:
108110
return fmt.Sprintf("%s (%s)", v, typeStr)
109111
case []byte:
@@ -139,14 +141,21 @@ func (lit *Literal) Eval2(ctx *sql.Context, row sql.Row2) (sql.Value, error) {
139141
}
140142

141143
func (lit *Literal) Type2() sql.Type2 {
142-
t2, ok := lit.fieldType.(sql.Type2)
144+
t2, ok := lit.Typ.(sql.Type2)
143145
if !ok {
144-
panic(fmt.Errorf("expected Type2, but was %T", lit.fieldType))
146+
panic(fmt.Errorf("expected Type2, but was %T", lit.Typ))
145147
}
146148
return t2
147149
}
148150

149151
// Value returns the literal value.
150152
func (p *Literal) Value() interface{} {
151-
return p.value
153+
return p.Val
154+
}
155+
156+
func (lit *Literal) WithResolvedChildren(children []any) (any, error) {
157+
if len(children) != 0 {
158+
return nil, sql.ErrInvalidChildrenNumber.New(lit, len(children), 0)
159+
}
160+
return lit, nil
152161
}

0 commit comments

Comments
 (0)