Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: the timestamp function should convert to a datetime type #2625

Merged
merged 1 commit into from
Aug 8, 2024
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
4 changes: 2 additions & 2 deletions enginetest/engine_only_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,11 @@ var analyzerTestCases = []analyzerTestCase{
planGenerator: func(t *testing.T, ctx *sql.Context, engine enginetest.QueryEngine) sql.Node {
db, err := engine.EngineAnalyzer().Catalog.Database(ctx, "foo")
require.NoError(t, err)
timestamp, err := function.NewTimestamp(
datetime, err := function.NewDatetime(
expression.NewLiteral("20200101:120000Z", types.LongText),
)
require.NoError(t, err)
return plan.NewShowTables(db, false, timestamp)
return plan.NewShowTables(db, false, datetime)
},
},
{
Expand Down
7 changes: 7 additions & 0 deletions enginetest/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -3680,6 +3680,13 @@ Select * from (
Query: "select i from datetime_table where timestamp_col = timestamp('2020-01-02T12:00:00')",
Expected: []sql.Row{{1}},
},
{
// The timestamp function actually converts to a datetime type, so check that we can convert values
// outside of the timestamp range, but inside the datetime range.
// https://github.com/dolthub/dolt/issues/8236
Query: "SELECT timestamp('1001-01-01 00:00:00');",
Expected: []sql.Row{{time.Date(1001, time.January, 1, 0, 0, 0, 0, time.UTC)}},
},
{
Query: "select i from datetime_table where timestamp_col = '2020-01-02T12:00:01'",
Expected: []sql.Row{},
Expand Down
72 changes: 4 additions & 68 deletions sql/expression/function/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,72 +322,6 @@ func (d *DateSub) String() string {
return fmt.Sprintf("%s(%s,%s)", d.FunctionName(), d.Date, d.Interval)
}

// TimestampConversion is a shorthand function for CONVERT(expr, TIMESTAMP)
type TimestampConversion struct {
Date sql.Expression
}

var _ sql.FunctionExpression = (*TimestampConversion)(nil)
var _ sql.CollationCoercible = (*TimestampConversion)(nil)

// FunctionName implements sql.FunctionExpression
func (t *TimestampConversion) FunctionName() string {
return "timestamp"
}

// Description implements sql.FunctionExpression
func (t *TimestampConversion) Description() string {
return "returns a timestamp value for the expression given (e.g. the string '2020-01-02')."
}

func (t *TimestampConversion) Resolved() bool {
return t.Date == nil || t.Date.Resolved()
}

func (t *TimestampConversion) String() string {
return fmt.Sprintf("%s(%s)", t.FunctionName(), t.Date)
}

func (t *TimestampConversion) Type() sql.Type {
return t.Date.Type()
}

// CollationCoercibility implements the interface sql.CollationCoercible.
func (*TimestampConversion) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return sql.Collation_binary, 5
}

func (t *TimestampConversion) IsNullable() bool {
return false
}

func (t *TimestampConversion) Eval(ctx *sql.Context, r sql.Row) (interface{}, error) {
e, err := t.Date.Eval(ctx, r)
if err != nil {
return nil, err
}
ret, _, err := types.TimestampMaxPrecision.Convert(e)
return ret, err
}

func (t *TimestampConversion) Children() []sql.Expression {
if t.Date == nil {
return nil
}
return []sql.Expression{t.Date}
}

func (t *TimestampConversion) WithChildren(children ...sql.Expression) (sql.Expression, error) {
return NewTimestamp(children...)
}

func NewTimestamp(args ...sql.Expression) (sql.Expression, error) {
if len(args) != 1 {
return nil, sql.ErrInvalidArgumentNumber.New("TIMESTAMP", 1, len(args))
}
return &TimestampConversion{args[0]}, nil
}

// DatetimeConversion is a shorthand function for CONVERT(expr, DATETIME)
type DatetimeConversion struct {
Date sql.Expression
Expand Down Expand Up @@ -447,8 +381,10 @@ func (t *DatetimeConversion) WithChildren(children ...sql.Expression) (sql.Expre
return NewDatetime(children...)
}

// NewDatetime returns a DatetimeConversion instance to handle the sql function "datetime". This is
// not a standard mysql function, but provides a shorthand for datetime conversions.
// NewDatetime returns a DatetimeConversion instance to handle the sql function "datetime". The standard
// MySQL function associated with this function is "timestamp", which actually returns a datetime type
// instead of a timestamp type.
// https://dev.mysql.com/doc/refman/8.4/en/date-and-time-functions.html#function_timestamp
func NewDatetime(args ...sql.Expression) (sql.Expression, error) {
if len(args) != 1 {
return nil, sql.ErrInvalidArgumentNumber.New("DATETIME", 1, len(args))
Expand Down
2 changes: 1 addition & 1 deletion sql/expression/function/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ var BuiltIns = []sql.Function{
sql.Function2{Name: "time_format", Fn: NewTimeFormat},
sql.Function1{Name: "time_to_sec", Fn: NewTimeToSec},
sql.Function2{Name: "timediff", Fn: NewTimeDiff},
sql.FunctionN{Name: "timestamp", Fn: NewTimestamp},
sql.FunctionN{Name: "timestamp", Fn: NewDatetime},
sql.Function3{Name: "timestampdiff", Fn: NewTimestampDiff},
sql.Function1{Name: "to_base64", Fn: NewToBase64},
sql.Function1{Name: "to_days", Fn: NewToDays},
Expand Down
Loading