Skip to content
This repository has been archived by the owner on Mar 30, 2021. It is now read-only.

Commit

Permalink
Support Date_Format in JS
Browse files Browse the repository at this point in the history
  • Loading branch information
jpullokkaran committed Sep 14, 2016
1 parent 58f2216 commit 4f12e1d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,13 @@ case class JSCodeGenerator(dqb: DruidQueryBuilder, e: Expression, mulInParamsAll
case TruncDate(d, f@Literal(v, _)) if v.isInstanceOf[UTF8String] =>
for (de <- genExprCode(d); tr <- trunc(de.getRef, f.value.toString)) yield
JSExpr(None, de.linesSoFar, tr, DateType)
case DateFormatClass(de, fmt) => {
for (jsDe <- genExprCode(de);
jsFmt <- genExprCode(fmt);
fmtDStr <- dateFormatCode(jsDe, jsFmt.curLine)) yield {
JSExpr(None, jsDe.linesSoFar + jsFmt.linesSoFar, fmtDStr, StringType, false)
}
}
case Add(l, r) => Some(genBArithmeticExprCode(l, r, e, "+")).flatten
case Subtract(l, r) => Some(genBArithmeticExprCode(l, r, e, "-")).flatten
case Multiply(l, r) => Some(genBArithmeticExprCode(l, r, e, "*")).flatten
Expand Down
12 changes: 12 additions & 0 deletions src/main/scala/org/sparklinedata/druid/jscodegen/JSDateTime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.sparklinedata.druid.jscodegen

import org.apache.spark.sql.types.{DateType, StringType, TimestampType}


private[jscodegen] case class JSDateTimeCtx(val tz_id: String, val ctx: JSCodeGenerator) {
private[jscodegen] val tzVar: String = ctx.makeUniqueVarName
Expand Down Expand Up @@ -51,6 +53,16 @@ private[jscodegen] object JSDateTimeCtx {
private val timeStampFormat = "yyyy-MM-dd HH:mm:ss"
private val mSecsInDay = 86400000

private[jscodegen] def dateFormatCode(dtVal: JSExpr, fmt: String): Option[String] =
for (javaDate <- dtVal.fnDT match {
case TimestampType => Some(s"""${dtVal.curLine}.toDate()""".stripMargin)
case DateType => Some(s"""${dtVal.curLine}.toDate()""".stripMargin)
case StringType => Some(s"""${dtVal.curLine}""".stripMargin)
case _ => None
}) yield {
s"(new java.text.SimpleDateFormat(${fmt})).format(${javaDate})"
}

private[jscodegen] def dtInFormatCode(f: String, ctx: JSDateTimeCtx) = {
ctx.createJodaTZ = true
s"""org.joda.time.format.forPattern($f).withZone(${ctx.tzVar})""".stripMargin
Expand Down
18 changes: 18 additions & 0 deletions src/test/scala/org/sparklinedata/druid/client/CodeGenTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -799,5 +799,23 @@ class CodeGenTest extends BaseTest with BeforeAndAfterAll with Logging {
|order by x, y
""".stripMargin
,0,true,true)

test("date_format1",
"""
|select o_orderstatus as x, date_format(cast(o_orderdate as date), 'YY') as y
|from orderLineItemPartSupplier
|group by o_orderstatus, date_format(cast(o_orderdate as date), 'YY')
|order by x, y
""".stripMargin
,1,true,true)

test("date_format2",
"""
|select o_orderstatus as x, date_format(cast(o_orderdate as date), 'u') as y
|from orderLineItemPartSupplier
|group by o_orderstatus, date_format(cast(o_orderdate as date), 'u')
|order by x, y
""".stripMargin
,1,true,true)
}

0 comments on commit 4f12e1d

Please sign in to comment.