@@ -2680,3 +2680,183 @@ g.test_datetime_33_3 = function()
2680
2680
t .assert_equals (box .execute (sql ).rows , {{dt1 }})
2681
2681
end )
2682
2682
end
2683
+
2684
+ -- Make sure cast from MAP to INTERVAL works as intended.
2685
+
2686
+ --
2687
+ -- The result of CAST() from MAP value to INTERVAL must be equal to the result
2688
+ -- of calling require('datetime').interval.new() with the corresponding table as
2689
+ -- an argument.
2690
+ --
2691
+ g .test_datetime_34_1 = function ()
2692
+ g .server :exec (function ()
2693
+ local t = require (' luatest' )
2694
+ local itv = require (' datetime' ).interval
2695
+ local v = setmetatable ({}, { __serialize = ' map' })
2696
+ local sql = [[ SELECT CAST(#v AS INTERVAL);]]
2697
+ t .assert_equals (box .execute (sql , {{[' #v' ] = v }}).rows , {{itv .new (v )}})
2698
+
2699
+ v .something = 1
2700
+ t .assert_equals (box .execute (sql , {{[' #v' ] = v }}).rows , {{itv .new (v )}})
2701
+
2702
+ v = {year = 1 , month = 1 , week = 1 , day = 1 , hour = 1 , min = 1 , sec = 1 ,
2703
+ nsec = 1 , adjust = ' none' }
2704
+ t .assert_equals (box .execute (sql , {{[' #v' ] = v }}).rows , {{itv .new (v )}})
2705
+ end )
2706
+ end
2707
+
2708
+ --
2709
+ -- Make sure an error is thrown if the INTERVAL value cannot be constructed from
2710
+ -- the corresponding table.
2711
+ --
2712
+ g .test_datetime_34_2 = function ()
2713
+ g .server :exec (function ()
2714
+ local t = require (' luatest' )
2715
+ local sql = [[ SELECT CAST(#v AS INTERVAL);]]
2716
+
2717
+ -- "year" cannot be more than 11759221.
2718
+ local v = {year = 11759222 }
2719
+ local _ , err = box .execute (sql , {{[' #v' ] = v }})
2720
+ local res = [[ Type mismatch: can not convert ]] ..
2721
+ [[ map({"year": 11759222}) to interval]]
2722
+ t .assert_equals (err .message , res )
2723
+
2724
+ -- "year" cannot be less than -11759221.
2725
+ v = {year = - 11759222 }
2726
+ _ , err = box .execute (sql , {{[' #v' ] = v }})
2727
+ res = [[ Type mismatch: can not convert ]] ..
2728
+ [[ map({"year": -11759222}) to interval]]
2729
+ t .assert_equals (err .message , res )
2730
+
2731
+ -- "month" cannot be more than 141110652.
2732
+ v = {month = 141110653 }
2733
+ _ , err = box .execute (sql , {{[' #v' ] = v }})
2734
+ res = [[ Type mismatch: can not convert ]] ..
2735
+ [[ map({"month": 141110653}) to interval]]
2736
+ t .assert_equals (err .message , res )
2737
+
2738
+ -- "month" cannot be less than -141110652.
2739
+ v = {month = - 141110653 }
2740
+ _ , err = box .execute (sql , {{[' #v' ] = v }})
2741
+ res = [[ Type mismatch: can not convert ]] ..
2742
+ [[ map({"month": -141110653}) to interval]]
2743
+ t .assert_equals (err .message , res )
2744
+
2745
+ -- "week" cannot be more than 613579352.
2746
+ v = {week = 613579353 }
2747
+ _ , err = box .execute (sql , {{[' #v' ] = v }})
2748
+ res = [[ Type mismatch: can not convert ]] ..
2749
+ [[ map({"week": 613579353}) to interval]]
2750
+ t .assert_equals (err .message , res )
2751
+
2752
+ -- "week" cannot be less than -613579352.
2753
+ v = {week = - 613579353 }
2754
+ _ , err = box .execute (sql , {{[' #v' ] = v }})
2755
+ res = [[ Type mismatch: can not convert ]] ..
2756
+ [[ map({"week": -613579353}) to interval]]
2757
+ t .assert_equals (err .message , res )
2758
+
2759
+ -- "day" cannot be more than 4295055470.
2760
+ v = {day = 4295055471 }
2761
+ _ , err = box .execute (sql , {{[' #v' ] = v }})
2762
+ res = [[ Type mismatch: can not convert ]] ..
2763
+ [[ map({"day": 4295055471}) to interval]]
2764
+ t .assert_equals (err .message , res )
2765
+
2766
+ -- "day" cannot be less than -4295055470.
2767
+ v = {day = - 4295055471 }
2768
+ _ , err = box .execute (sql , {{[' #v' ] = v }})
2769
+ res = [[ Type mismatch: can not convert ]] ..
2770
+ [[ map({"day": -4295055471}) to interval]]
2771
+ t .assert_equals (err .message , res )
2772
+
2773
+ -- "hour" cannot be more than 103081331286.
2774
+ v = {hour = 103081331287 }
2775
+ _ , err = box .execute (sql , {{[' #v' ] = v }})
2776
+ res = [[ Type mismatch: can not convert ]] ..
2777
+ [[ map({"hour": 103081331287}) to interval]]
2778
+ t .assert_equals (err .message , res )
2779
+
2780
+ -- "hour" cannot be less than 103081331286.
2781
+ v = {hour = - 103081331287 }
2782
+ _ , err = box .execute (sql , {{[' #v' ] = v }})
2783
+ res = [[ Type mismatch: can not convert ]] ..
2784
+ [[ map({"hour": -103081331287}) to interval]]
2785
+ t .assert_equals (err .message , res )
2786
+
2787
+ -- "min" cannot be more than 6184879877160.
2788
+ v = {min = 6184879877161 }
2789
+ _ , err = box .execute (sql , {{[' #v' ] = v }})
2790
+ res = [[ Type mismatch: can not convert ]] ..
2791
+ [[ map({"min": 6184879877161}) to interval]]
2792
+ t .assert_equals (err .message , res )
2793
+
2794
+ -- "min" cannot be less than -6184879877160.
2795
+ v = {min = - 6184879877161 }
2796
+ _ , err = box .execute (sql , {{[' #v' ] = v }})
2797
+ res = [[ Type mismatch: can not convert ]] ..
2798
+ [[ map({"min": -6184879877161}) to interval]]
2799
+ t .assert_equals (err .message , res )
2800
+
2801
+ -- "sec" cannot be more than 371092792629600.
2802
+ v = {sec = 371092792629601 }
2803
+ _ , err = box .execute (sql , {{[' #v' ] = v }})
2804
+ res = [[ Type mismatch: can not convert ]] ..
2805
+ [[ map({"sec": 371092792629601}) to interval]]
2806
+ t .assert_equals (err .message , res )
2807
+
2808
+ -- "sec" cannot be less than -371092792629600.
2809
+ v = {sec = - 371092792629601 }
2810
+ _ , err = box .execute (sql , {{[' #v' ] = v }})
2811
+ res = [[ Type mismatch: can not convert ]] ..
2812
+ [[ map({"sec": -371092792629601}) to interval]]
2813
+ t .assert_equals (err .message , res )
2814
+
2815
+ -- "nsec" cannot be more than 2147483647.
2816
+ v = {nsec = 2147483648 }
2817
+ _ , err = box .execute (sql , {{[' #v' ] = v }})
2818
+ res = [[ Type mismatch: can not convert map({"nsec": 2147483648}) ]] ..
2819
+ [[ to interval]]
2820
+ t .assert_equals (err .message , res )
2821
+
2822
+ -- "nsec" cannot be less than -2147483647.
2823
+ v = {nsec = - 2147483648 }
2824
+ _ , err = box .execute (sql , {{[' #v' ] = v }})
2825
+ res = [[ Type mismatch: can not convert map({"nsec": -2147483648}) ]] ..
2826
+ [[ to interval]]
2827
+ t .assert_equals (err .message , res )
2828
+
2829
+ -- "adjust" cannot be anything other than "none", "excess", "last".
2830
+ v = {adjust = 1 }
2831
+ _ , err = box .execute (sql , {{[' #v' ] = v }})
2832
+ res = [[ Type mismatch: can not convert map({"adjust": 1}) ]] ..
2833
+ [[ to interval]]
2834
+ t .assert_equals (err .message , res )
2835
+
2836
+ v = {adjust = ' asd' }
2837
+ _ , err = box .execute (sql , {{[' #v' ] = v }})
2838
+ res = [[ Type mismatch: can not convert map({"adjust": "asd"}) ]] ..
2839
+ [[ to interval]]
2840
+ t .assert_equals (err .message , res )
2841
+ end )
2842
+ end
2843
+
2844
+ --
2845
+ -- Make sure that any of the DECIMAL, INTEGER, and DOUBLE values can be used as
2846
+ -- values in the MAP converted to a INTERVAL.
2847
+ --
2848
+ g .test_datetime_34_3 = function ()
2849
+ g .server :exec (function ()
2850
+ local t = require (' luatest' )
2851
+ local itv = require (' datetime' ).interval
2852
+ local dt1 = itv .new ({year = 1 })
2853
+ local sql = [[ SELECT CAST({'year': 1.0} AS INTERVAL);]]
2854
+ t .assert_equals (box .execute (sql ).rows , {{dt1 }})
2855
+
2856
+ sql = [[ SELECT CAST({'year': 1.e0} AS INTERVAL);]]
2857
+ t .assert_equals (box .execute (sql ).rows , {{dt1 }})
2858
+
2859
+ sql = [[ SELECT CAST({'year': 1} AS INTERVAL);]]
2860
+ t .assert_equals (box .execute (sql ).rows , {{dt1 }})
2861
+ end )
2862
+ end
0 commit comments