Skip to content

Panic When Comparing Decimal against NaN #3264

@kylebrandt

Description

@kylebrandt

When creating a table with a NaN value, and then a WHERE condition, panic is triggered on: Cannot create a Decimal from NaN

Example

func main() {
	pro := createTestDatabase()
	engine := sqle.NewDefault(pro)

	session := memory.NewSession(sql.NewBaseSession(), pro)
	ctx := sql.NewContext(context.Background(), sql.WithSession(session))
	ctx.SetCurrentDatabase(dbName)

	config := server.Config{
		Protocol: "tcp",
		Address:  fmt.Sprintf("%s:%d", address, port),
	}
	s, err := server.NewServer(config, engine, sql.NewContext, memory.NewSessionBuilder(pro), nil)
	if err != nil {
		panic(err)
	}
	if err = s.Start(); err != nil {
		panic(err)
	}
}

func createTestDatabase() *memory.DbProvider {
	db := memory.NewDatabase(dbName)
	db.BaseDatabase.EnablePrimaryKeyIndexes()

	pro := memory.NewDBProvider(db)
	session := memory.NewSession(sql.NewBaseSession(), pro)
	ctx := sql.NewContext(context.Background(), sql.WithSession(session))

	table := memory.NewTable(db, tableName, sql.NewPrimaryKeySchema(sql.Schema{
		{Name: "num", Type: types.Float64, Nullable: false, Source: tableName, PrimaryKey: true},
	}), db.GetForeignKeyCollection())
	db.AddTable(tableName, table)

	_ = table.Insert(ctx, sql.NewRow(math.NaN()))

	return pro
}

and

mysql --host=127.0.0.1 --port=3306 --user=root mydb --execute="SELECT * FROM mytable WHERE num > 0.2"

In my context where I am using GMS as a dependency, the trace looks like:

/home/kbrandt/go/pkg/mod/github.com/shopspring/[email protected]/decimal.go:327 (0x323c6e4)
	newFromFloat: panic(fmt.Sprintf(\"Cannot create a Decimal from %v\", val))
/home/kbrandt/go/pkg/mod/github.com/shopspring/[email protected]/decimal.go:303 (0x323c03e)
	NewFromFloat: return newFromFloat(value, math.Float64bits(value), &float64info)
/home/kbrandt/go/pkg/mod/github.com/dolthub/[email protected]/sql/types/decimal.go:202 (0x3508cc4)
	DecimalType_.ConvertToNullDecimal: return t.ConvertToNullDecimal(decimal.NewFromFloat(value))
/home/kbrandt/go/pkg/mod/github.com/dolthub/[email protected]/sql/types/decimal.go:143 (0x35076d7)
	DecimalType_.Convert: dec, err := t.ConvertToNullDecimal(v)
/home/kbrandt/go/pkg/mod/github.com/dolthub/[email protected]/sql/expression/convert.go:345 (0x35da51d)
	convertValue: d, _, err := dt.Convert(ctx, value)
/home/kbrandt/go/pkg/mod/github.com/dolthub/[email protected]/sql/expression/comparison.go:292 (0x35cf9d1)
	convertLeftAndRight: l, err := convertValue(ctx, left, convertTo, nil, 0, 0)
/home/kbrandt/go/pkg/mod/github.com/dolthub/[email protected]/sql/expression/comparison.go:236 (0x35ce949)
	(*comparison).castLeftAndRight: l, r, err := convertLeftAndRight(ctx, left, right, ConvertToDecimal)
/home/kbrandt/go/pkg/mod/github.com/dolthub/[email protected]/sql/expression/comparison.go:176 (0x35cd82e)
	(*comparison).Compare: left, right, compareType, err = c.castLeftAndRight(ctx, left, right)
/home/kbrandt/go/pkg/mod/github.com/dolthub/[email protected]/sql/expression/comparison.go:496 (0x35d1d0d)
	(*GreaterThan).Eval: result, err := gt.Compare(ctx, row)
/home/kbrandt/go/pkg/mod/github.com/dolthub/[email protected]/sql/core.go:347 (0x326b09b)
	EvaluateCondition: v, err := cond.Eval(ctx, row)
/home/kbrandt/go/pkg/mod/github.com/dolthub/[email protected]/sql/plan/filter.go:125 (0x3777abb)
	(*FilterIter).Next: res, err := sql.EvaluateCondition(ctx, i.cond, row)
/home/kbrandt/go/pkg/mod/github.com/dolthub/[email protected]/sql/session.go:609 (0x32b1d33)
	(*spanIter).Next: row, err := i.iter.Next(ctx)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions