title: Hive macro not returning expected results
body: <p>I am using Hive temporary macros to help with date algebra (finding the first day of the prior month in this case) and I am getting unexpected results.</p>
  
  <blockquote>
    <p>create temporary macro month1st_sub(dt date)<br>
      cast(concat(<br>
       case<br>
        when month(dt) = 1 then cast(year(dt)-1 as string)<br>
        else cast(year(dt) as string)<br>
       end,<br>
       "-",<br>
       case<br>
        when month(dt) = 1 then "12"<br>
        else cast(month(dt)-1 as string)<br>
       end,<br>
       "-01"<br>
      ) as date)<br>
     ;</p>
  </blockquote>
  
  <p>When I test this macro using a vars table that contains a single value for max_dt (8-15-2014) using the following:</p>
  
  <blockquote>
    <p>select<br>
     max_dt,<br>
     month1st_sub(cast("2013-1-1" as date)),<br>
     month1st_sub(max_dt),<br>
     month1st_sub(cast("2013-1-1" as date)),<br>
     month1st_sub(cast("2013-4-1" as date)),<br>
     month1st_sub(cast("2013-5-1" as date)),<br>
     month1st_sub(cast("2013-6-1" as date))<br>
     from vars;</p>
  </blockquote>
  
  <p>I receive the following output:</p>
  
  <blockquote>
    <p>max_dt _c1 _c2 _c3 _c4 _c5 _c6<br>
    2013-08-01 2012-12-01  2013-07-01  2012-12-01  2013-03-01  2013-04-01  2013-07-01</p>
  </blockquote>
  
  <p>The last returned value, 2013-07-01, should be 2013-05-01. This error is reproducible if I remove the 6-1 line then the 5-1 line will return 2013-07-01. The issue appears to always be with the last returned value of a set of macro invocations.</p>
  
  <p>The setting I use use are as follows:</p>
  
  <blockquote>
    <p>set hive.cli.print.header=true;<br>
     set mapreduce.input.fileinputformat.split.maxsize=10000000;<br>
     set hive.auto.convert.join = true;<br>
     set hive.exec.dynamic.partition.mode=nonstrict;  </p>
  </blockquote>
  
  <p>Question 1: Am I doing something wrong? If not is this an issue with Hive or likely to be some environment issue?<br>
  Question 2: is the temporary macro functionality in hive trustworthy enough to use or should I be writing java udfs to do this?</p>