Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Commit

Permalink
implement columnar from_unixtime (#577)
Browse files Browse the repository at this point in the history
Signed-off-by: Yuan Zhou <yuan.zhou@intel.com>
  • Loading branch information
zhouyuan authored Nov 23, 2021
1 parent 0ee1ca6 commit 9bc56ff
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import scala.collection.mutable.ListBuffer

import com.intel.oap.expression.ColumnarDateTimeExpressions.ColumnarDateDiff
import com.intel.oap.expression.ColumnarDateTimeExpressions.ColumnarUnixTimestamp
import com.intel.oap.expression.ColumnarDateTimeExpressions.ColumnarFromUnixTime

/**
* A version of add that supports columnar processing for longs.
Expand Down Expand Up @@ -83,6 +84,8 @@ object ColumnarBinaryExpression {
new ColumnarDateDiff(left, right)
case a: UnixTimestamp =>
new ColumnarUnixTimestamp(left, right)
case a: FromUnixTime =>
new ColumnarFromUnixTime(left, right)
case other =>
throw new UnsupportedOperationException(s"not currently supported: $other.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import org.apache.spark.sql.catalyst.expressions.UnixMicros
import org.apache.spark.sql.catalyst.expressions.UnixMillis
import org.apache.spark.sql.catalyst.expressions.UnixSeconds
import org.apache.spark.sql.catalyst.expressions.UnixTimestamp
import org.apache.spark.sql.catalyst.expressions.FromUnixTime
import org.apache.spark.sql.catalyst.expressions.Year
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.{DateType, IntegerType, LongType, StringType, TimestampType}
Expand Down Expand Up @@ -493,6 +494,43 @@ object ColumnarDateTimeExpressions {
}
}

class ColumnarFromUnixTime(left: Expression, right: Expression)
extends FromUnixTime(left, right) with
ColumnarExpression {

buildCheck()

def buildCheck(): Unit = {
val supportedTypes = List(TimestampType)
if (supportedTypes.indexOf(left.dataType) == -1) {
throw new UnsupportedOperationException(
s"${left.dataType} is not supported in ColumnarFromUnixTime.")
}
if (left.dataType == StringType) {
right match {
case literal: ColumnarLiteral =>
val format = literal.value.toString
if (format.length > 10) {
throw new UnsupportedOperationException(
s"$format is not supported in ColumnarFromUnixTime.")
}
case _ =>
}
}
}

override def doColumnarCodeGen(args: Object): (TreeNode, ArrowType) = {
val (leftNode, leftType) = left.asInstanceOf[ColumnarExpression].doColumnarCodeGen(args)
//val (rightNode, rightType) = right.asInstanceOf[ColumnarExpression].doColumnarCodeGen(args)
val outType = CodeGeneration.getResultType(dataType)

val dateNode = TreeBuilder.makeFunction(
"castVARCHAR", Lists.newArrayList(leftNode, TreeBuilder.makeLiteral(java.lang.Long.valueOf(10L))), outType)

(dateNode, outType)
}
}

class ColumnarDateDiff(left: Expression, right: Expression)
extends DateDiff(left, right) with ColumnarExpression {

Expand Down

0 comments on commit 9bc56ff

Please sign in to comment.