Skip to content

Commit

Permalink
sql (fix): Support TIMESTAMP AT TIME ZONE literal (#3576)
Browse files Browse the repository at this point in the history
  • Loading branch information
takezoe committed Jul 2, 2024
1 parent 9fa19d0 commit b123a75
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,14 @@ object Expression {
override def sqlExpr = s"TIMESTAMP '${value}'"
override def toString = s"Literal(TIMESTAMP '${value}')"
}
case class TimestampWithTimeZoneLiteral(value: String, timezone: String, nodeLocation: Option[NodeLocation])
extends Literal
with LeafExpression {
override def dataType: DataType = DataType.TimestampType(TimestampField.TIMESTAMP, false)
override def stringValue: String = value
override def sqlExpr = s"TIMESTAMP '${value}' AT TIME ZONE '${timezone}'"
override def toString = s"Literal(TIMESTAMP '${value}' AT '${timezone}')"
}
case class DecimalLiteral(value: String, nodeLocation: Option[NodeLocation]) extends Literal with LeafExpression {
override def dataType: DataType = DataType.DecimalType(TypeVariable("precision"), TypeVariable("scale"))
override def stringValue: String = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,20 @@ class SQLInterpreter(withNodeLocation: Boolean = true) extends SqlBaseBaseVisito
// TODO Parse decimal-type precision properly
case "decimal" => DecimalLiteral(v, getLocation(ctx))
case "char" => CharLiteral(v, getLocation(ctx))
case other =>
GenericLiteral(tpe, v, getLocation(ctx))
case other => GenericLiteral(tpe, v, getLocation(ctx))
}
}
}

override def visitAtTimeZone(ctx: AtTimeZoneContext): Expression = {
val v = expression(ctx.timeZoneSpecifier()).asInstanceOf[StringLiteral].value

expression(ctx.valueExpression()) match {
case t: TimestampLiteral => TimestampWithTimeZoneLiteral(t.value, v, t.nodeLocation)
case other => other
}
}

override def visitBasicStringLiteral(ctx: BasicStringLiteralContext): StringLiteral = {
StringLiteral(unquote(ctx.STRING().getText), getLocation(ctx))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,12 @@ class SQLGeneratorTest extends AirSpec {
val sql = SQLGenerator.print(resolvedPlan).toLowerCase
sql shouldBe "select * from (select * from a) join a using (id)"
}

test("print TIMESTAMP AT TIME ZONE") {
val resolvedPlan =
SQLAnalyzer.analyze("SELECT TIMESTAMP '1992-02-01 00:00 UTC' AT TIME ZONE 'Asia/Tokyo'", "default", demoCatalog)

val sql = SQLGenerator.print(resolvedPlan)
sql shouldBe "SELECT TIMESTAMP '1992-02-01 00:00 UTC' AT TIME ZONE 'Asia/Tokyo'"
}
}

0 comments on commit b123a75

Please sign in to comment.