Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Commit

Permalink
Some vet tools fixes (#688)
Browse files Browse the repository at this point in the history
Some vet tools fixes
  • Loading branch information
ajnavarro authored Apr 24, 2019
2 parents 2db6776 + 04aa0ac commit edd578f
Show file tree
Hide file tree
Showing 27 changed files with 127 additions and 106 deletions.
28 changes: 15 additions & 13 deletions auth/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,22 @@ func testAuthentication(

for _, c := range tests {
t.Run(fmt.Sprintf("%s-%s", c.user, c.password), func(t *testing.T) {
req := require.New(t)
r := require.New(t)

db, err := dsql.Open("mysql", connString(c.user, c.password))
req.NoError(err)
var db *dsql.DB
db, err = dsql.Open("mysql", connString(c.user, c.password))
r.NoError(err)
_, err = db.Query("SELECT 1")

if c.success {
req.NoError(err)
r.NoError(err)
} else {
req.Error(err)
req.Contains(err.Error(), "Access denied")
r.Error(err)
r.Contains(err.Error(), "Access denied")
}

err = db.Close()
req.NoError(err)
r.NoError(err)

if extra != nil {
extra(t, c)
Expand Down Expand Up @@ -196,20 +197,21 @@ func testAudit(

for _, c := range tests {
t.Run(c.user, func(t *testing.T) {
req := require.New(t)
r := require.New(t)

db, err := dsql.Open("mysql", connString(c.user, ""))
req.NoError(err)
var db *dsql.DB
db, err = dsql.Open("mysql", connString(c.user, ""))
r.NoError(err)
_, err = db.Query(c.query)

if c.success {
req.NoError(err)
r.NoError(err)
} else {
req.Error(err)
r.Error(err)
}

err = db.Close()
req.NoError(err)
r.NoError(err)

if extra != nil {
extra(t, c)
Expand Down
2 changes: 1 addition & 1 deletion mem/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (t *Table) HandledFilters(filters []sql.Expression) []sql.Expression {
var handled []sql.Expression
for _, f := range filters {
var hasOtherFields bool
f.TransformUp(func(e sql.Expression) (sql.Expression, error) {
_, _ = f.TransformUp(func(e sql.Expression) (sql.Expression, error) {
if e, ok := e.(*expression.GetField); ok {
if e.Table() != t.name || !t.schema.Contains(e.Name(), t.name) {
hasOtherFields = true
Expand Down
37 changes: 20 additions & 17 deletions mem/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,24 @@ var tests = []struct {
},
indexColumns: []string{"col1", "col3"},
expectedKeyValues: []*indexKeyValue{
&indexKeyValue{sql.NewRow("a", int64(100)), &indexValue{Key: "0", Pos: 0}},
&indexKeyValue{sql.NewRow("c", int64(100)), &indexValue{Key: "0", Pos: 1}},
&indexKeyValue{sql.NewRow("e", int64(200)), &indexValue{Key: "0", Pos: 2}},
&indexKeyValue{sql.NewRow("b", int64(100)), &indexValue{Key: "1", Pos: 0}},
&indexKeyValue{sql.NewRow("d", int64(200)), &indexValue{Key: "1", Pos: 1}},
&indexKeyValue{sql.NewRow("f", int64(200)), &indexValue{Key: "1", Pos: 2}},
{sql.NewRow("a", int64(100)), &indexValue{Key: "0", Pos: 0}},
{sql.NewRow("c", int64(100)), &indexValue{Key: "0", Pos: 1}},
{sql.NewRow("e", int64(200)), &indexValue{Key: "0", Pos: 2}},
{sql.NewRow("b", int64(100)), &indexValue{Key: "1", Pos: 0}},
{sql.NewRow("d", int64(200)), &indexValue{Key: "1", Pos: 1}},
{sql.NewRow("f", int64(200)), &indexValue{Key: "1", Pos: 2}},
},
lookup: &dummyLookup{
values: map[string][]*indexValue{
"0": []*indexValue{
&indexValue{Key: "0", Pos: 0},
&indexValue{Key: "0", Pos: 1},
&indexValue{Key: "0", Pos: 2},
"0": {
{Key: "0", Pos: 0},
{Key: "0", Pos: 1},
{Key: "0", Pos: 2},
},
"1": []*indexValue{
&indexValue{Key: "1", Pos: 0},
&indexValue{Key: "1", Pos: 1},
&indexValue{Key: "1", Pos: 2},
"1": {
{Key: "1", Pos: 0},
{Key: "1", Pos: 1},
{Key: "1", Pos: 2},
},
},
},
Expand All @@ -194,13 +194,16 @@ func TestTable(t *testing.T) {
require.NoError(err)

for i := 0; i < test.numPartitions; i++ {
p, err := pIter.Next()
var p sql.Partition
p, err = pIter.Next()
require.NoError(err)

iter, err := table.PartitionRows(sql.NewEmptyContext(), p)
var iter sql.RowIter
iter, err = table.PartitionRows(sql.NewEmptyContext(), p)
require.NoError(err)

rows, err := sql.RowIterToRows(iter)
var rows []sql.Row
rows, err = sql.RowIterToRows(iter)
require.NoError(err)

expected := table.partitions[string(p.Key())]
Expand Down
4 changes: 2 additions & 2 deletions sql/analyzer/optimization_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func optimizeDistinct(ctx *sql.Context, a *Analyzer, node sql.Node) (sql.Node, e
defer span.Finish()

a.Log("optimize distinct, node of type: %T", node)
if node, ok := node.(*plan.Distinct); ok {
if n, ok := node.(*plan.Distinct); ok {
var isSorted bool
_, _ = node.TransformUp(func(node sql.Node) (sql.Node, error) {
a.Log("checking for optimization in node of type: %T", node)
Expand All @@ -45,7 +45,7 @@ func optimizeDistinct(ctx *sql.Context, a *Analyzer, node sql.Node) (sql.Node, e

if isSorted {
a.Log("distinct optimized for ordered output")
return plan.NewOrderedDistinct(node.Child), nil
return plan.NewOrderedDistinct(n.Child), nil
}
}

Expand Down
4 changes: 2 additions & 2 deletions sql/analyzer/prune_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func pruneColumns(ctx *sql.Context, a *Analyzer, n sql.Node) (sql.Node, error) {
}

if describe, ok := n.(*plan.DescribeQuery); ok {
n, err := pruneColumns(ctx, a, describe.Child)
pruned, err := pruneColumns(ctx, a, describe.Child)
if err != nil {
return nil, err
}

return plan.NewDescribeQuery(describe.Format, n), nil
return plan.NewDescribeQuery(describe.Format, pruned), nil
}

columns := make(usedColumns)
Expand Down
16 changes: 8 additions & 8 deletions sql/analyzer/pushdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ func pushdown(ctx *sql.Context, a *Analyzer, n sql.Node) (sql.Node, error) {
// First step is to find all col exprs and group them by the table they mention.
// Even if they appear multiple times, only the first one will be used.
plan.InspectExpressions(n, func(e sql.Expression) bool {
if e, ok := e.(*expression.GetField); ok {
tf := tableField{e.Table(), e.Name()}
if gf, ok := e.(*expression.GetField); ok {
tf := tableField{gf.Table(), gf.Name()}
if _, ok := tableFields[tf]; !ok {
a.Log("found used column %s.%s", e.Table(), e.Name())
a.Log("found used column %s.%s", gf.Table(), gf.Name())
tableFields[tf] = struct{}{}
fieldsByTable[e.Table()] = append(fieldsByTable[e.Table()], e.Name())
exprsByTable[e.Table()] = append(exprsByTable[e.Table()], e)
fieldsByTable[gf.Table()] = append(fieldsByTable[gf.Table()], gf.Name())
exprsByTable[gf.Table()] = append(exprsByTable[gf.Table()], gf)
}
}
return true
Expand All @@ -59,14 +59,14 @@ func pushdown(ctx *sql.Context, a *Analyzer, n sql.Node) (sql.Node, error) {

// then find all filters, also by table. Note that filters that mention
// more than one table will not be passed to neither.
filters := make(filters)
filt := make(filters)
plan.Inspect(n, func(node sql.Node) bool {
a.Log("inspecting node of type: %T", node)
switch node := node.(type) {
case *plan.Filter:
fs := exprToTableFilters(node.Expression)
a.Log("found filters for %d tables %s", len(fs), node.Expression)
filters.merge(fs)
filt.merge(fs)
}
return true
})
Expand Down Expand Up @@ -118,7 +118,7 @@ func pushdown(ctx *sql.Context, a *Analyzer, n sql.Node) (sql.Node, error) {
var table = node.Table

if ft, ok := table.(sql.FilteredTable); ok {
tableFilters := filters[node.Name()]
tableFilters := filt[node.Name()]
handled := ft.HandledFilters(tableFilters)
handledFilters = append(handledFilters, handled...)
schema := node.Schema()
Expand Down
4 changes: 2 additions & 2 deletions sql/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ func (c *Catalog) UnlockTables(ctx *Context, id uint32) error {
table, err := c.dbs.Table(db, t)
if err == nil {
if lockable, ok := table.(Lockable); ok {
if err := lockable.Unlock(ctx, id); err != nil {
errors = append(errors, err.Error())
if e := lockable.Unlock(ctx, id); e != nil {
errors = append(errors, e.Error())
}
}
} else {
Expand Down
12 changes: 6 additions & 6 deletions sql/expression/comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,32 @@ func (c *comparison) evalLeftAndRight(ctx *sql.Context, row sql.Row) (interface{
func (c *comparison) castLeftAndRight(left, right interface{}) (interface{}, interface{}, error) {
if sql.IsNumber(c.Left().Type()) || sql.IsNumber(c.Right().Type()) {
if sql.IsDecimal(c.Left().Type()) || sql.IsDecimal(c.Right().Type()) {
left, right, err := convertLeftAndRight(left, right, ConvertToDecimal)
l, r, err := convertLeftAndRight(left, right, ConvertToDecimal)
if err != nil {
return nil, nil, err
}

c.compareType = sql.Float64
return left, right, nil
return l, r, nil
}

if sql.IsSigned(c.Left().Type()) || sql.IsSigned(c.Right().Type()) {
left, right, err := convertLeftAndRight(left, right, ConvertToSigned)
l, r, err := convertLeftAndRight(left, right, ConvertToSigned)
if err != nil {
return nil, nil, err
}

c.compareType = sql.Int64
return left, right, nil
return l, r, nil
}

left, right, err := convertLeftAndRight(left, right, ConvertToUnsigned)
l, r, err := convertLeftAndRight(left, right, ConvertToUnsigned)
if err != nil {
return nil, nil, err
}

c.compareType = sql.Uint64
return left, right, nil
return l, r, nil
}

left, right, err := convertLeftAndRight(left, right, ConvertToChar)
Expand Down
6 changes: 4 additions & 2 deletions sql/expression/function/ceil_round_floor.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ func (r *Round) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
dVal := float64(0)

if r.Right != nil {
dTemp, err := r.Right.Eval(ctx, row)
var dTemp interface{}
dTemp, err = r.Right.Eval(ctx, row)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -280,7 +281,8 @@ func (r *Round) TransformUp(f sql.TransformExprFunc) (sql.Expression, error) {

args[1] = nil
if r.Right != nil {
arg, err := r.Right.TransformUp(f)
var arg sql.Expression
arg, err = r.Right.TransformUp(f)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions sql/expression/function/concat.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ func (f *Concat) String() string {
func (f *Concat) TransformUp(fn sql.TransformExprFunc) (sql.Expression, error) {
var args = make([]sql.Expression, len(f.args))
for i, arg := range f.args {
arg, err := arg.TransformUp(fn)
a, err := arg.TransformUp(fn)
if err != nil {
return nil, err
}
args[i] = arg
args[i] = a
}

expr, err := NewConcat(args...)
Expand Down
4 changes: 2 additions & 2 deletions sql/expression/function/concat_ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ func (f *ConcatWithSeparator) String() string {
func (f *ConcatWithSeparator) TransformUp(fn sql.TransformExprFunc) (sql.Expression, error) {
var args = make([]sql.Expression, len(f.args))
for i, arg := range f.args {
arg, err := arg.TransformUp(fn)
a, err := arg.TransformUp(fn)
if err != nil {
return nil, err
}
args[i] = arg
args[i] = a
}

expr, err := NewConcatWithSeparator(args...)
Expand Down
4 changes: 2 additions & 2 deletions sql/expression/function/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,10 @@ func TestCalcWeek(t *testing.T) {
func TestNow(t *testing.T) {
require := require.New(t)
date := time.Date(2018, time.December, 2, 16, 25, 0, 0, time.Local)
clock := clock(func() time.Time {
clk := clock(func() time.Time {
return date
})
f := &Now{clock}
f := &Now{clk}

result, err := f.Eval(nil, nil)
require.NoError(err)
Expand Down
3 changes: 2 additions & 1 deletion sql/expression/like.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ func (l *Like) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
)
// eval right and convert to text
if !l.cached || l.pool == nil {
v, err := l.Right.Eval(ctx, row)
var v interface{}
v, err = l.Right.Eval(ctx, row)
if err != nil || v == nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions sql/expression/unresolved.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ func (*UnresolvedFunction) Eval(ctx *sql.Context, r sql.Row) (interface{}, error
func (uf *UnresolvedFunction) TransformUp(f sql.TransformExprFunc) (sql.Expression, error) {
var rc []sql.Expression
for _, c := range uf.Arguments {
c, err := c.TransformUp(f)
ct, err := c.TransformUp(f)
if err != nil {
return nil, err
}
rc = append(rc, c)
rc = append(rc, ct)
}

return f(NewUnresolvedFunction(uf.name, uf.IsAggregate, rc...))
Expand Down
7 changes: 4 additions & 3 deletions sql/index/pilosa/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (d *Driver) loadIndex(db, table, id string) (*pilosaIndex, error) {

dir := filepath.Join(d.root, db, table, id)
config := d.configFilePath(db, table, id)
if _, err := os.Stat(config); err != nil {
if _, err = os.Stat(config); err != nil {
return nil, errCorruptedIndex.New(dir)
}

Expand All @@ -202,7 +202,7 @@ func (d *Driver) loadIndex(db, table, id string) (*pilosaIndex, error) {
"dir": dir,
})
log.Warn("could not read index file, index is corrupt and will be deleted")
if err := os.RemoveAll(dir); err != nil {
if err = os.RemoveAll(dir); err != nil {
log.Warn("unable to remove corrupted index: " + dir)
}

Expand Down Expand Up @@ -304,7 +304,8 @@ func (d *Driver) savePartition(
continue
}

rowID, err := mapping.getRowID(field.Name(), values[i])
var rowID uint64
rowID, err = mapping.getRowID(field.Name(), values[i])
if err != nil {
return 0, err
}
Expand Down
Loading

0 comments on commit edd578f

Please sign in to comment.