@@ -422,20 +422,101 @@ public void dayName() {
422422 public void dayOfMonth () {
423423 when (nullRef .type ()).thenReturn (DATE );
424424 when (missingRef .type ()).thenReturn (DATE );
425- assertEquals (nullValue (), eval (DSL .dayofmonth (nullRef )));
426- assertEquals (missingValue (), eval (DSL .dayofmonth (missingRef )));
425+ assertEquals (nullValue (), eval (DSL .dayofmonth (functionProperties , nullRef )));
426+ assertEquals (missingValue (), eval (DSL .dayofmonth (functionProperties , missingRef )));
427427
428- FunctionExpression expression = DSL .dayofmonth (DSL .literal (new ExprDateValue ("2020-08-07" )));
428+ FunctionExpression expression = DSL .dayofmonth (
429+ functionProperties , DSL .literal (new ExprDateValue ("2020-08-07" )));
429430 assertEquals (INTEGER , expression .type ());
430431 assertEquals ("dayofmonth(DATE '2020-08-07')" , expression .toString ());
431432 assertEquals (integerValue (7 ), eval (expression ));
432433
433- expression = DSL .dayofmonth (DSL .literal ("2020-07-08" ));
434+ expression = DSL .dayofmonth (functionProperties , DSL .literal ("2020-07-08" ));
434435 assertEquals (INTEGER , expression .type ());
435436 assertEquals ("dayofmonth(\" 2020-07-08\" )" , expression .toString ());
436437 assertEquals (integerValue (8 ), eval (expression ));
437438 }
438439
440+ private void testDayOfMonthWithUnderscores (FunctionExpression dateExpression , int dayOfMonth ) {
441+ assertEquals (INTEGER , dateExpression .type ());
442+ assertEquals (integerValue (dayOfMonth ), eval (dateExpression ));
443+ }
444+
445+ @ Test
446+ public void dayOfMonthWithUnderscores () {
447+ lenient ().when (nullRef .valueOf (env )).thenReturn (nullValue ());
448+ lenient ().when (missingRef .valueOf (env )).thenReturn (missingValue ());
449+
450+
451+ FunctionExpression expression1 = DSL .dayofmonth (
452+ functionProperties , DSL .literal (new ExprDateValue ("2020-08-07" )));
453+ FunctionExpression expression2 = DSL .dayofmonth (functionProperties , DSL .literal ("2020-07-08" ));
454+
455+ assertAll (
456+ () -> testDayOfMonthWithUnderscores (expression1 , 7 ),
457+ () -> assertEquals ("dayofmonth(DATE '2020-08-07')" , expression1 .toString ()),
458+
459+ () -> testDayOfMonthWithUnderscores (expression2 , 8 ),
460+ () -> assertEquals ("dayofmonth(\" 2020-07-08\" )" , expression2 .toString ())
461+
462+ );
463+ }
464+
465+ @ Test
466+ public void testDayOfMonthWithTimeType () {
467+ lenient ().when (nullRef .valueOf (env )).thenReturn (nullValue ());
468+ lenient ().when (missingRef .valueOf (env )).thenReturn (missingValue ());
469+ FunctionExpression expression = DSL .day_of_month (
470+ functionProperties , DSL .literal (new ExprTimeValue ("12:23:34" )));
471+
472+ assertEquals (INTEGER , eval (expression ).type ());
473+ assertEquals (
474+ LocalDate .now (functionProperties .getQueryStartClock ()).getDayOfMonth (),
475+ eval (expression ).integerValue ());
476+ assertEquals ("day_of_month(TIME '12:23:34')" , expression .toString ());
477+ }
478+
479+ private void testInvalidDayOfMonth (String date ) {
480+ FunctionExpression expression = DSL .day_of_month (
481+ functionProperties , DSL .literal (new ExprDateValue (date )));
482+ eval (expression );
483+ }
484+
485+ @ Test
486+ public void dayOfMonthWithUnderscoresLeapYear () {
487+ lenient ().when (nullRef .valueOf (env )).thenReturn (nullValue ());
488+ lenient ().when (missingRef .valueOf (env )).thenReturn (missingValue ());
489+
490+ //Feb. 29 of a leap year
491+ testDayOfMonthWithUnderscores (DSL .day_of_month (
492+ functionProperties , DSL .literal ("2020-02-29" )), 29 );
493+
494+ //Feb. 29 of a non-leap year
495+ assertThrows (SemanticCheckException .class , () -> testInvalidDayOfMonth ("2021-02-29" ));
496+ }
497+
498+ @ Test
499+ public void dayOfMonthWithUnderscoresInvalidArguments () {
500+ lenient ().when (nullRef .type ()).thenReturn (DATE );
501+ lenient ().when (missingRef .type ()).thenReturn (DATE );
502+
503+ assertAll (
504+ () -> assertEquals (nullValue (), eval (DSL .day_of_month (functionProperties , nullRef ))),
505+ () -> assertEquals (
506+ missingValue (), eval (DSL .day_of_month (functionProperties , missingRef ))),
507+
508+ //40th day of the month
509+ () -> assertThrows (
510+ SemanticCheckException .class , () -> testInvalidDayOfMonth ("2021-02-40" )),
511+ //13th month of the year
512+ () -> assertThrows (
513+ SemanticCheckException .class , () -> testInvalidDayOfMonth ("2021-13-40" )),
514+ //incorrect format
515+ () -> assertThrows (
516+ SemanticCheckException .class , () -> testInvalidDayOfMonth ("asdfasdfasdf" ))
517+ );
518+ }
519+
439520 private void dayOfWeekQuery (
440521 FunctionExpression dateExpression ,
441522 int dayOfWeek ,
0 commit comments