title: Aggregation with Group By date in Spark SQL
body: <p>I have an RDD containing a timestamp named <strong>time</strong> of type long:</p>
  
  <pre><code>root
   |-- id: string (nullable = true)
   |-- value1: string (nullable = true)
   |-- value2: string (nullable = true)
   |-- time: long (nullable = true)
   |-- type: string (nullable = true)
  </code></pre>
  
  <p>I am trying to group by value1, value2 and time as YYYY-MM-DD. I tried to group by cast(time as Date) but then I got the following error:</p>
  
  <pre><code>Exception in thread "main" java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.apache.spark.deploy.worker.DriverWrapper$.main(DriverWrapper.scala:40)
      at org.apache.spark.deploy.worker.DriverWrapper.main(DriverWrapper.scala)
  Caused by: java.lang.RuntimeException: [1.21] failure: ``DECIMAL'' expected but identifier Date found
  </code></pre>
  
  <p>Does that mean there is not way to group by a date? I even tried to add another level of casting to have it as a String:</p>
  
  <pre><code>cast(cast(time as Date) as String)
  </code></pre>
  
  <p>Which returns the same error.</p>
  
  <p>I've read that I could use probably aggregateByKey on the RDD but I don't understand how to use it for a few columns and convert that long to a YYYY-MM-DD String. How should I proceed? </p>