Skip to content

Commit f06d885

Browse files
authored
Merge pull request #3308 from dolthub/angela/date_functions
make `IsNullable` return true for nullable datetime functions
2 parents 11f255c + 0d1594c commit f06d885

File tree

6 files changed

+84
-17
lines changed

6 files changed

+84
-17
lines changed

enginetest/queries/script_queries.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13441,16 +13441,23 @@ select * from t1 except (
1344113441
},
1344213442
{
1344313443
// https://github.com/dolthub/dolt/issues/10070
13444+
// https://github.com/dolthub/dolt/issues/10092
1344413445
Name: "NOT EXISTS with nullable filter",
1344513446
SetUpScript: []string{
1344613447
"CREATE TABLE t0(c0 INT , c1 INT);",
1344713448
"INSERT INTO t0(c0, c1) VALUES (1, -2);",
13449+
"create table t1(c0 int, primary key(c0))",
13450+
"insert into t1 values (1)",
1344813451
},
1344913452
Assertions: []ScriptTestAssertion{
1345013453
{
1345113454
Query: `SELECT * FROM t0 WHERE NOT EXISTS (SELECT 1 FROM (SELECT 1) alias0 WHERE (CASE -1 WHEN t0.c1 THEN false END));`,
1345213455
Expected: []sql.Row{{1, -2}},
1345313456
},
13457+
{
13458+
Query: "select * from t1 where not exists (select 1 from (select 1) as subquery where weekday(t1.c0))",
13459+
Expected: []sql.Row{{1}},
13460+
},
1345413461
},
1345513462
},
1345613463
}

enginetest/queries/tpch_plans.go

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sql/expression/function/extract.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ func (td *Extract) Description() string {
5454
// Type implements the Expression interface.
5555
func (td *Extract) Type() sql.Type { return types.Int64 }
5656

57+
// IsNullable implements the Expression interface
58+
func (td *Extract) IsNullable() bool {
59+
return true
60+
}
61+
5762
// CollationCoercibility implements the interface sql.CollationCoercible.
5863
func (*Extract) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
5964
return sql.Collation_binary, 5

sql/expression/function/time.go

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ func (q *Quarter) String() string { return fmt.Sprintf("%s(%s)", q.FunctionName(
146146
// Type implements the Expression interface.
147147
func (q *Quarter) Type() sql.Type { return types.Int32 }
148148

149+
// IsNullable implements the Expression interface
150+
func (q *Quarter) IsNullable() bool {
151+
return true
152+
}
153+
149154
// CollationCoercibility implements the interface sql.CollationCoercible.
150155
func (q *Quarter) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
151156
return sql.Collation_binary, 5
@@ -192,6 +197,11 @@ func (m *Month) String() string { return fmt.Sprintf("%s(%s)", m.FunctionName(),
192197
// Type implements the Expression interface.
193198
func (m *Month) Type() sql.Type { return types.Int32 }
194199

200+
// IsNullable implements the Expression interface
201+
func (d *Month) IsNullable() bool {
202+
return true
203+
}
204+
195205
// CollationCoercibility implements the interface sql.CollationCoercible.
196206
func (*Month) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
197207
return sql.Collation_binary, 5
@@ -238,6 +248,11 @@ func (d *Day) String() string { return fmt.Sprintf("%s(%s)", d.FunctionName(), d
238248
// Type implements the Expression interface.
239249
func (d *Day) Type() sql.Type { return types.Int32 }
240250

251+
// IsNullable implements the Expression interface
252+
func (d *Day) IsNullable() bool {
253+
return true
254+
}
255+
241256
// CollationCoercibility implements the interface sql.CollationCoercible.
242257
func (*Day) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
243258
return sql.Collation_binary, 5
@@ -285,6 +300,11 @@ func (d *Weekday) String() string { return fmt.Sprintf("%s(%s)", d.FunctionName(
285300
// Type implements the Expression interface.
286301
func (d *Weekday) Type() sql.Type { return types.Int32 }
287302

303+
// IsNullable implements the Expression interface
304+
func (d *Weekday) IsNullable() bool {
305+
return true
306+
}
307+
288308
// CollationCoercibility implements the interface sql.CollationCoercible.
289309
func (*Weekday) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
290310
return sql.Collation_binary, 5
@@ -470,6 +490,11 @@ func (d *DayOfWeek) String() string { return fmt.Sprintf("DAYOFWEEK(%s)", d.Chil
470490
// Type implements the Expression interface.
471491
func (d *DayOfWeek) Type() sql.Type { return types.Int32 }
472492

493+
// IsNullable implements the Expression interface
494+
func (d *DayOfWeek) IsNullable() bool {
495+
return true
496+
}
497+
473498
// CollationCoercibility implements the interface sql.CollationCoercible.
474499
func (*DayOfWeek) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
475500
return sql.Collation_binary, 5
@@ -516,6 +541,11 @@ func (d *DayOfYear) String() string { return fmt.Sprintf("DAYOFYEAR(%s)", d.Chil
516541
// Type implements the Expression interface.
517542
func (d *DayOfYear) Type() sql.Type { return types.Int32 }
518543

544+
// IsNullable implements the Expression interface
545+
func (d *DayOfYear) IsNullable() bool {
546+
return true
547+
}
548+
519549
// CollationCoercibility implements the interface sql.CollationCoercible.
520550
func (*DayOfYear) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
521551
return sql.Collation_binary, 5
@@ -653,7 +683,7 @@ func (d *YearWeek) Children() []sql.Expression { return []sql.Expression{d.date,
653683

654684
// IsNullable implements the Expression interface.
655685
func (d *YearWeek) IsNullable() bool {
656-
return d.date.IsNullable()
686+
return true
657687
}
658688

659689
// WithChildren implements the Expression interface.
@@ -781,7 +811,7 @@ func (d *Week) Children() []sql.Expression { return []sql.Expression{d.date, d.m
781811

782812
// IsNullable implements the Expression interface.
783813
func (d *Week) IsNullable() bool {
784-
return d.date.IsNullable()
814+
return true
785815
}
786816

787817
// WithChildren implements the Expression interface.
@@ -1288,6 +1318,11 @@ func (d *Date) String() string { return fmt.Sprintf("DATE(%s)", d.Child) }
12881318
// Type implements the Expression interface.
12891319
func (d *Date) Type() sql.Type { return types.Date }
12901320

1321+
// IsNullable implements the Expression interface
1322+
func (d *Date) IsNullable() bool {
1323+
return true
1324+
}
1325+
12911326
// CollationCoercibility implements the interface sql.CollationCoercible.
12921327
func (*Date) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
12931328
return sql.Collation_binary, 5
@@ -1370,6 +1405,11 @@ func (dtf *UnaryDatetimeFunc) String() string {
13701405
return fmt.Sprintf("%s(%s)", strings.ToUpper(dtf.Name), dtf.Child.String())
13711406
}
13721407

1408+
// IsNullable implements the Expression interface
1409+
func (dtf *UnaryDatetimeFunc) IsNullable() bool {
1410+
return true
1411+
}
1412+
13731413
// Type implements the Expression interface.
13741414
func (dtf *UnaryDatetimeFunc) Type() sql.Type {
13751415
return dtf.SQLType
@@ -1401,6 +1441,11 @@ func (*DayName) CollationCoercibility(ctx *sql.Context) (collation sql.Collation
14011441
return ctx.GetCollation(), 4
14021442
}
14031443

1444+
// IsNullable implements the Expression interface
1445+
func (d *DayName) IsNullable() bool {
1446+
return true
1447+
}
1448+
14041449
func (d *DayName) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
14051450
val, err := d.EvalChild(ctx, row)
14061451
if err != nil {
@@ -1487,6 +1532,11 @@ func (*MonthName) CollationCoercibility(ctx *sql.Context) (collation sql.Collati
14871532
return ctx.GetCollation(), 4
14881533
}
14891534

1535+
// IsNullable implements the Expression interface
1536+
func (d *MonthName) IsNullable() bool {
1537+
return true
1538+
}
1539+
14901540
func (d *MonthName) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
14911541
val, err := d.EvalChild(ctx, row)
14921542
if err != nil {

sql/expression/function/time_math.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ func (d *DateDiff) String() string {
6363
// Type implements the sql.Expression interface.
6464
func (d *DateDiff) Type() sql.Type { return types.Int64 }
6565

66+
// IsNullable implements the Expression interface
67+
func (d *DateDiff) IsNullable() bool {
68+
return true
69+
}
70+
6671
// CollationCoercibility implements the interface sql.CollationCoercible.
6772
func (*DateDiff) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
6873
return sql.Collation_binary, 5
@@ -600,7 +605,7 @@ func (t *TimestampDiff) Resolved() bool {
600605

601606
// IsNullable implements the sql.Expression interface.
602607
func (t *TimestampDiff) IsNullable() bool {
603-
return t.unit.IsNullable() && t.expr1.IsNullable() && t.expr2.IsNullable()
608+
return true
604609
}
605610

606611
// Type implements the sql.Expression interface.

sql/types/datetime.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ var (
6666
// datetimeMaxTime is the maximum representable time value, MYSQL: 9999-12-31 23:59:59.999999 (microseconds)
6767
datetimeMaxTime = time.Date(9999, 12, 31, 23, 59, 59, 999999000, time.UTC)
6868

69-
// datetimeMinTime is the minimum representable time value, MYSQL: 0000-01-01 00:00:00.000000 (microseconds)
70-
datetimeMinTime = time.Date(0000, 0, 0, 0, 0, 0, 0, time.UTC)
69+
// datetimeMinTime is the minimum representable time value, MYSQL: 0000-00-00 00:00:00.000000 (microseconds)
70+
datetimeMinTime = ZeroTime
7171

7272
DateOnlyLayouts = []string{
7373
"2006-01-02",

0 commit comments

Comments
 (0)