Skip to content

Commit

Permalink
Removes sealed class DateTimeType as it was not needed. (#489)
Browse files Browse the repository at this point in the history
* Removes sealed class DateTimeType as it was not needed.
  • Loading branch information
abhikuhikar authored Dec 22, 2021
1 parent 58f5edb commit aaa5936
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 79 deletions.
4 changes: 2 additions & 2 deletions lang/src/org/partiql/lang/ast/AstSerialization.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ private class AstSerializerImpl(val astVersion: AstVersion, val ion: IonSystem):
is DropTable -> case { writeDropTable(expr) }
is DropIndex -> case { writeDropIndex(expr) }
is Parameter -> case { writeParameter(expr)}
is DateTimeType.Date -> throw UnsupportedOperationException("DATE literals not supported by the V0 AST")
is DateTimeType.Time -> throw UnsupportedOperationException("TIME literals not supported by the V0 AST")
is DateLiteral -> throw UnsupportedOperationException("DATE literals not supported by the V0 AST")
is TimeLiteral -> throw UnsupportedOperationException("TIME literals not supported by the V0 AST")
is Exec -> throw UnsupportedOperationException("EXEC clause not supported by the V0 AST")
}.toUnit()
}
Expand Down
35 changes: 16 additions & 19 deletions lang/src/org/partiql/lang/ast/ExprNodeToStatement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fun ExprNode.toAstStatement(): PartiqlAst.Statement {
val node = this
return when(node) {
is Literal, is LiteralMissing, is VariableReference, is Parameter, is NAry, is CallAgg,
is Typed, is Path, is SimpleCase, is SearchedCase, is Select, is Struct, is DateTimeType,
is Typed, is Path, is SimpleCase, is SearchedCase, is Select, is Struct, is DateLiteral, is TimeLiteral,
is Seq -> PartiqlAst.build { query(toAstExpr()) }

is DataManipulation -> node.toAstDml()
Expand All @@ -36,7 +36,7 @@ private fun ExprNode.toAstDdl(): PartiqlAst.Statement {
return PartiqlAst.build {
when(thiz) {
is Literal, is LiteralMissing, is VariableReference, is Parameter, is NAry, is CallAgg, is Typed,
is Path, is SimpleCase, is SearchedCase, is Select, is Struct, is Seq, is DateTimeType,
is Path, is SimpleCase, is SearchedCase, is Select, is Struct, is Seq, is DateLiteral, is TimeLiteral,
is DataManipulation, is Exec -> error("Can't convert ${thiz.javaClass} to PartiqlAst.ddl")

is CreateTable -> ddl(createTable(thiz.tableName), metas)
Expand Down Expand Up @@ -184,23 +184,20 @@ fun ExprNode.toAstExpr(): PartiqlAst.Expr {
is DataManipulation, is CreateTable, is CreateIndex, is DropTable, is DropIndex, is Exec ->
error("Can't transform ${node.javaClass} to a PartiqlAst.expr }")
// DateTime types
is DateTimeType -> {
when (node) {
is DateTimeType.Date -> date(node.year.toLong(), node.month.toLong(), node.day.toLong(), metas)
is DateTimeType.Time -> litTime(
timeValue(
node.hour.toLong(),
node.minute.toLong(),
node.second.toLong(),
node.nano.toLong(),
node.precision.toLong(),
node.with_time_zone,
node.tz_minutes?.toLong()
),
metas
)
}
}
is DateLiteral -> date(node.year.toLong(), node.month.toLong(), node.day.toLong(), metas)
is TimeLiteral ->
litTime(
timeValue(
node.hour.toLong(),
node.minute.toLong(),
node.second.toLong(),
node.nano.toLong(),
node.precision.toLong(),
node.with_time_zone,
node.tz_minutes?.toLong()
),
metas
)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions lang/src/org/partiql/lang/ast/StatementToExprNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,9 @@ private class StatementTransformer(val ion: IonSystem) {
offset = offset?.toExprNode(),
metas = metas
)
is Expr.Date ->
DateTimeType.Date(year.value.toInt(), month.value.toInt(), day.value.toInt(), metas)
is Expr.Date -> DateLiteral(year.value.toInt(), month.value.toInt(), day.value.toInt(), metas)
is Expr.LitTime ->
DateTimeType.Time(
TimeLiteral(
value.hour.value.toInt(),
value.minute.value.toInt(),
value.second.value.toInt(),
Expand Down
83 changes: 39 additions & 44 deletions lang/src/org/partiql/lang/ast/ast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ sealed class ExprNode : AstNode(), HasMetas {
is Exec -> {
copy(metas = metas)
}
is DateTimeType.Date -> {
is DateLiteral -> {
copy(metas = metas)
}
is DateTimeType.Time -> {
is TimeLiteral -> {
copy(metas = metas)
}
}
Expand Down Expand Up @@ -996,52 +996,47 @@ enum class OrderingSpec {
}

/**
* The sealed class includes all the datetime types such as DATE, TIME, TIMESTAMP
* Note that the ast nodes corresponding to the DATE, TIME and TIMESTAMP here are different from the [Literal] nodes.
* You can create an Ion literal as [Timestamp] which will correspond to the [Literal] node and will have the type
* [SqlDataType.TIMESTAMP]. However that will be different from the
* `TIMESTAMP` here.
* Note: TIME and TIMESTAMP are yet to be added.
* AST Node corresponding to the DATE literal
*
* @param year represents the year of the DATE as Integer.
* @param month represents the month of the DATE as Integer.
* @param day represents the day of the DATE as Integer.
*/
sealed class DateTimeType : ExprNode() {
/**
* AST Node corresponding to the DATE literal
*/
data class Date(
val year: Int,
val month: Int,
val day: Int,
override val metas: MetaContainer
) : DateTimeType() {
override val children: List<AstNode> = listOf()
}
data class DateLiteral(
val year: Int,
val month: Int,
val day: Int,
override val metas: MetaContainer
) : ExprNode() {
override val children: List<AstNode> = listOf()
}

/**
* AST node representing the TIME literal.
*
* @param hour represents the hour value.
* @param minute represents the minute value.
* @param second represents the second value.
* @param nano represents the fractional part of the second up to the nanoseconds' precision.
* @param precision is an optional parameter which, if specified, represents the precision of the fractional second.
* @param with_time_zone is a boolean to decide whether the time has a time zone.
* True represents "TIME WITH TIME ZONE", while false represents "TIME WITHOUT TIME ZONE".
* @param tz_minutes is the optional time zone in minutes which can be explicitly specified with "WITH TIME ZONE".
*/
data class Time(
val hour: Int,
val minute: Int,
val second: Int,
val nano: Int,
val precision: Int,
val with_time_zone: Boolean,
val tz_minutes: Int? = null,
override val metas: MetaContainer
) : DateTimeType() {
override val children: List<AstNode> = listOf()
}
/**
* AST node representing the TIME literal.
*
* @param hour represents the hour value.
* @param minute represents the minute value.
* @param second represents the second value.
* @param nano represents the fractional part of the second up to the nanoseconds' precision.
* @param precision is an optional parameter which, if specified, represents the precision of the fractional second.
* @param with_time_zone is a boolean to decide whether the time has a time zone.
* True represents "TIME WITH TIME ZONE", while false represents "TIME WITHOUT TIME ZONE".
* @param tz_minutes is the optional time zone in minutes which can be explicitly specified with "WITH TIME ZONE".
*/
data class TimeLiteral(
val hour: Int,
val minute: Int,
val second: Int,
val nano: Int,
val precision: Int,
val with_time_zone: Boolean,
val tz_minutes: Int? = null,
override val metas: MetaContainer
) : ExprNode() {
override val children: List<AstNode> = listOf()
}


/**
* Indicates strategy for binding lookup within scopes.
*/
Expand Down
12 changes: 6 additions & 6 deletions lang/src/org/partiql/lang/ast/passes/AstRewriterBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ open class AstRewriterBase : AstRewriter {
is DropTable -> rewriteDropTable(node)
is DropIndex -> rewriteDropIndex(node)
is Exec -> rewriteExec(node)
is DateTimeType.Date -> rewriteDate(node)
is DateTimeType.Time -> rewriteTime(node)
is DateLiteral -> rewriteDate(node)
is TimeLiteral -> rewriteTime(node)
}
}

Expand Down Expand Up @@ -448,16 +448,16 @@ open class AstRewriterBase : AstRewriter {
node.args.map { rewriteExprNode(it) },
rewriteMetas(node))

open fun rewriteDate(node: DateTimeType.Date): DateTimeType.Date =
DateTimeType.Date(
open fun rewriteDate(node: DateLiteral): DateLiteral =
DateLiteral(
node.year,
node.month,
node.day,
rewriteMetas(node)
)

open fun rewriteTime(node: DateTimeType.Time): DateTimeType.Time =
DateTimeType.Time(
open fun rewriteTime(node: TimeLiteral): TimeLiteral =
TimeLiteral(
node.hour,
node.minute,
node.second,
Expand Down
2 changes: 1 addition & 1 deletion lang/src/org/partiql/lang/ast/passes/AstWalker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ open class AstWalker(private val visitor: AstVisitor) {
}
}
is CreateTable, is DropTable, is DropIndex,
is Exec, is DateTimeType -> case { }
is Exec, is TimeLiteral, is DateLiteral -> case { }
}.toUnit()
}
}
Expand Down
8 changes: 4 additions & 4 deletions lang/src/org/partiql/lang/eval/EvaluatingCompiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ internal class EvaluatingCompiler(
is DropIndex,
is DropTable -> compileDdl(expr)
is Exec -> compileExec(expr)
is DateTimeType.Date -> compileDate(expr)
is DateTimeType.Time -> compileTime(expr)
is DateLiteral -> compileDateLiteral(expr)
is TimeLiteral -> compileTimeLiteral(expr)
}
}

Expand Down Expand Up @@ -2035,13 +2035,13 @@ internal class EvaluatingCompiler(
}
}

private fun compileDate(node: DateTimeType.Date): ThunkEnv {
private fun compileDateLiteral(node: DateLiteral): ThunkEnv {
val (year, month, day, metas) = node
val value = valueFactory.newDate(year, month, day)
return thunkFactory.thunkEnv(metas) { value }
}

private fun compileTime(node: DateTimeType.Time) : ThunkEnv {
private fun compileTimeLiteral(node: TimeLiteral) : ThunkEnv {
val (hour, minute, second, nano, precision, with_time_zone, tz_minutes, metas) = node
return thunkFactory.thunkEnv(metas) {
// Add the default time zone if the type "TIME WITH TIME ZONE" does not have an explicitly specified time zone.
Expand Down

0 comments on commit aaa5936

Please sign in to comment.