Skip to content

Commit ef5a371

Browse files
committed
First step to generalizing literal expression
1 parent 814abcc commit ef5a371

24 files changed

+52
-44
lines changed

enginetest/server_engine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ func prepareBindingArgs(ctx *sql.Context, bindings map[string]sqlparser.Expr) ([
650650
if !ok {
651651
return nil, fmt.Errorf("cannot get binding value")
652652
}
653-
args[i] = lit.Value()
653+
args[i] = lit.LiteralValue()
654654
}
655655
return args, nil
656656
}

memory/exponential_dist_table.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,23 @@ func (s ExponentialDistTable) NewInstance(ctx *sql.Context, db sql.Database, arg
3636
if !ok {
3737
return nil, fmt.Errorf("normal_dist table expects arguments to be literal expressions")
3838
}
39-
colCnt, inBounds, _ := types.Int64.Convert(ctx, colCntLit.Value())
39+
colCnt, inBounds, _ := types.Int64.Convert(ctx, colCntLit.LiteralValue())
4040
if !inBounds {
4141
return nil, fmt.Errorf("normal_dist table expects 1st argument to be column count")
4242
}
4343
rowCntLit, ok := args[1].(*expression.Literal)
4444
if !ok {
4545
return nil, fmt.Errorf("normal_dist table expects arguments to be literal expressions")
4646
}
47-
rowCnt, inBounds, _ := types.Int64.Convert(ctx, rowCntLit.Value())
47+
rowCnt, inBounds, _ := types.Int64.Convert(ctx, rowCntLit.LiteralValue())
4848
if !inBounds {
4949
return nil, fmt.Errorf("normal_dist table expects 2nd argument to be row count")
5050
}
5151
lambdaLit, ok := args[2].(*expression.Literal)
5252
if !ok {
5353
return nil, fmt.Errorf("exponential_dist table expects arguments to be literal expressions")
5454
}
55-
lambda, inBounds, _ := types.Float64.Convert(ctx, lambdaLit.Value())
55+
lambda, inBounds, _ := types.Float64.Convert(ctx, lambdaLit.LiteralValue())
5656
if !inBounds {
5757
return nil, fmt.Errorf("exponential_dist table expects 3rd argument to be row count")
5858
}

memory/normal_dist_table.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,31 @@ func (s NormalDistTable) NewInstance(ctx *sql.Context, db sql.Database, args []s
3737
if !ok {
3838
return nil, fmt.Errorf("normal_dist table expects arguments to be literal expressions")
3939
}
40-
colCnt, inBounds, _ := types.Int64.Convert(ctx, colCntLit.Value())
40+
colCnt, inBounds, _ := types.Int64.Convert(ctx, colCntLit.LiteralValue())
4141
if !inBounds {
4242
return nil, fmt.Errorf("normal_dist table expects 1st argument to be column count")
4343
}
4444
rowCntLit, ok := args[1].(*expression.Literal)
4545
if !ok {
4646
return nil, fmt.Errorf("normal_dist table expects arguments to be literal expressions")
4747
}
48-
rowCnt, inBounds, _ := types.Int64.Convert(ctx, rowCntLit.Value())
48+
rowCnt, inBounds, _ := types.Int64.Convert(ctx, rowCntLit.LiteralValue())
4949
if !inBounds {
5050
return nil, fmt.Errorf("normal_dist table expects 2nd argument to be row count")
5151
}
5252
meanLit, ok := args[2].(*expression.Literal)
5353
if !ok {
5454
return nil, fmt.Errorf("normal_dist table expects arguments to be literal expressions")
5555
}
56-
mean, inBounds, _ := types.Float64.Convert(ctx, meanLit.Value())
56+
mean, inBounds, _ := types.Float64.Convert(ctx, meanLit.LiteralValue())
5757
if !inBounds {
5858
return nil, fmt.Errorf("normal_dist table expects 3rd argument to be row count")
5959
}
6060
stdLit, ok := args[3].(*expression.Literal)
6161
if !ok {
6262
return nil, fmt.Errorf("normal_dist table expects arguments to be literal expressions")
6363
}
64-
std, inBounds, _ := types.Float64.Convert(ctx, stdLit.Value())
64+
std, inBounds, _ := types.Float64.Convert(ctx, stdLit.LiteralValue())
6565
if !inBounds {
6666
return nil, fmt.Errorf("normal_dist table expects 4th argument to be row count")
6767
}

memory/sequence_table.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ func (s IntSequenceTable) NewInstance(ctx *sql.Context, db sql.Database, args []
3737
if !ok {
3838
return nil, fmt.Errorf("sequence table expects arguments to be literal expressions")
3939
}
40-
name, ok := nameExp.Value().(string)
40+
name, ok := nameExp.LiteralValue().(string)
4141
if !ok {
4242
return nil, fmt.Errorf("sequence table expects 1st argument to be column name")
4343
}
4444
lenExp, ok := args[1].(*expression.Literal)
4545
if !ok {
4646
return nil, fmt.Errorf("sequence table expects arguments to be literal expressions")
4747
}
48-
length, _, err := types.Int64.Convert(ctx, lenExp.Value())
48+
length, _, err := types.Int64.Convert(ctx, lenExp.LiteralValue())
4949
if !ok {
5050
return nil, fmt.Errorf("%w; sequence table expects 2nd argument to be a sequence length integer", err)
5151
}

memory/table_function.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ func (s TableFunc) NewInstance(ctx *sql.Context, db sql.Database, args []sql.Exp
2727
if !ok {
2828
return nil, fmt.Errorf("table_func table expects arguments to be literal expressions")
2929
}
30-
name, ok := nameExp.Value().(string)
30+
name, ok := nameExp.LiteralValue().(string)
3131
if !ok {
3232
return nil, fmt.Errorf("table_func table expects 1st argument to be column name")
3333
}
3434
valueExpr, ok := args[1].(*expression.Literal)
3535
if !ok {
3636
return nil, fmt.Errorf("table_func table expects arguments to be literal expressions")
3737
}
38-
value, _, err := types.Int64.Convert(ctx, valueExpr.Value())
38+
value, _, err := types.Int64.Convert(ctx, valueExpr.LiteralValue())
3939
if !ok {
4040
return nil, fmt.Errorf("%w; table_func table expects 2nd argument to be a table_func length integer", err)
4141
}

sql/analyzer/costed_index_scan_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ func (i *indexSearchableTable) LookupForExpressions(ctx *sql.Context, exprs ...s
913913
if eq, ok := exprs[0].(*expression.Equals); ok {
914914
if gf, ok := eq.Left().(*expression.GetField); ok && strings.EqualFold(gf.Name(), "x") {
915915
if lit, ok := eq.Right().(*expression.Literal); ok {
916-
ranges := sql.MySQLRangeCollection{{sql.ClosedRangeColumnExpr(lit.Value(), lit.Value(), lit.Type())}}
916+
ranges := sql.MySQLRangeCollection{{sql.ClosedRangeColumnExpr(lit.LiteralValue(), lit.LiteralValue(), lit.Type())}}
917917
return sql.IndexLookup{Index: xIdx, Ranges: ranges}, nil, nil, true, nil
918918
}
919919
}

sql/analyzer/indexed_joins.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ func makeIndexScan(ctx *sql.Context, statsProv sql.StatsProvider, tab plan.Table
13111311
if found && lit == nil {
13121312
break
13131313
}
1314-
rang[j] = sql.ClosedRangeColumnExpr(lit.Value(), lit.Value(), idx.SqlIdx().ColumnExpressionTypes()[j].Type)
1314+
rang[j] = sql.ClosedRangeColumnExpr(lit.LiteralValue(), lit.LiteralValue(), idx.SqlIdx().ColumnExpressionTypes()[j].Type)
13151315
j++
13161316
if found {
13171317
break

sql/analyzer/inserts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func isZero(ctx *sql.Context, lit *expression.Literal) bool {
243243
return false
244244
}
245245

246-
convert, inRange, err := types.Int8.Convert(ctx, lit.Value())
246+
convert, inRange, err := types.Int8.Convert(ctx, lit.LiteralValue())
247247
if err != nil {
248248
// Ignore any conversion errors, since that means the value isn't 0
249249
// and the values are validated in other parts of the analyzer anyway.

sql/analyzer/optimization_rules.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ func expressionSources(expr sql.Expression) (sql.FastIntSet, bool) {
178178
case *expression.NullSafeEquals:
179179
nullRejecting = false
180180
case *expression.Equals:
181-
if lit, ok := e.Left().(*expression.Literal); ok && lit.Value() == nil {
181+
if lit, ok := e.Left().(*expression.Literal); ok && lit.LiteralValue() == nil {
182182
nullRejecting = false
183183
}
184-
if lit, ok := e.Right().(*expression.Literal); ok && lit.Value() == nil {
184+
if lit, ok := e.Right().(*expression.Literal); ok && lit.LiteralValue() == nil {
185185
nullRejecting = false
186186
}
187187
case *plan.Subquery:
@@ -194,10 +194,10 @@ func expressionSources(expr sql.Expression) (sql.FastIntSet, bool) {
194194
case *expression.NullSafeEquals:
195195
nullRejecting = false
196196
case *expression.Equals:
197-
if lit, ok := e.Left().(*expression.Literal); ok && lit.Value() == nil {
197+
if lit, ok := e.Left().(*expression.Literal); ok && lit.LiteralValue() == nil {
198198
nullRejecting = false
199199
}
200-
if lit, ok := e.Right().(*expression.Literal); ok && lit.Value() == nil {
200+
if lit, ok := e.Right().(*expression.Literal); ok && lit.LiteralValue() == nil {
201201
nullRejecting = false
202202
}
203203
}
@@ -289,7 +289,7 @@ func simplifyFilters(ctx *sql.Context, a *Analyzer, node sql.Node, scope *plan.S
289289
if e.Escape != nil {
290290
return e, transform.SameTree, nil
291291
}
292-
val := r.Value()
292+
val := r.LiteralValue()
293293
valStr, ok := val.(string)
294294
if !ok {
295295
return e, transform.SameTree, nil
@@ -369,8 +369,8 @@ func simplifyFilters(ctx *sql.Context, a *Analyzer, node sql.Node, scope *plan.S
369369

370370
func isFalse(e sql.Expression) bool {
371371
lit, ok := e.(*expression.Literal)
372-
if ok && lit != nil && lit.Type() == types.Boolean && lit.Value() != nil {
373-
switch v := lit.Value().(type) {
372+
if ok && lit != nil && lit.Type() == types.Boolean && lit.LiteralValue() != nil {
373+
switch v := lit.LiteralValue().(type) {
374374
case bool:
375375
return !v
376376
case int8:
@@ -382,8 +382,8 @@ func isFalse(e sql.Expression) bool {
382382

383383
func isTrue(e sql.Expression) bool {
384384
lit, ok := e.(*expression.Literal)
385-
if ok && lit != nil && lit.Type() == types.Boolean && lit.Value() != nil {
386-
switch v := lit.Value().(type) {
385+
if ok && lit != nil && lit.Type() == types.Boolean && lit.LiteralValue() != nil {
386+
switch v := lit.LiteralValue().(type) {
387387
case bool:
388388
return v
389389
case int8:

sql/core.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ type Expression interface {
4545
WithChildren(children ...Expression) (Expression, error)
4646
}
4747

48+
// LiteralExpression is a marker interface to indicate that an expression is a literal expression
49+
type LiteralExpression interface {
50+
Expression
51+
// LiteralValue returns the literal value of the expression.
52+
LiteralValue() interface{}
53+
}
54+
4855
// ExpressionWithNodes is an expression that contains nodes as children.
4956
type ExpressionWithNodes interface {
5057
Expression

0 commit comments

Comments
 (0)