Skip to content

Commit

Permalink
Addressed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bgaidioz committed Sep 5, 2024
1 parent 21ca680 commit c7bcc77
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ import com.rawlabs.compiler.utils.RecordFieldsNaming

import java.io.{IOException, OutputStream}
import java.sql.ResultSet
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoField
import scala.annotation.tailrec

object TypedResultSetCsvWriter {
Expand Down Expand Up @@ -144,8 +144,17 @@ class TypedResultSetCsvWriter(os: OutputStream, lineSeparator: String, maxRows:
val date = v.getDate(i).toLocalDate
gen.writeString(dateFormatter.format(date))
case _: RawTimeType =>
// Extract the SQL time (a JDBC object) from the result set.
val sqlTime = v.getTime(i)
val time = LocalTime.ofNanoOfDay(sqlTime.getTime * 1000000)
// Turn it into LocalTime. It does something proper with potential timezone conversion, but
// doesn't have the milliseconds (toLocalTime's doc says it sets the LocalTime nanoseconds field to zero).
val withoutMilliseconds = sqlTime.toLocalTime
// Get the value as milliseconds (possibly shifted by a certain timezone) but we have the milliseconds.
val asMillis = sqlTime.getTime
// Extract the actual milliseconds.
val millis = asMillis % 1000
// Fix the LocalTime milliseconds.
val time = withoutMilliseconds.`with`(ChronoField.MILLI_OF_SECOND, millis)
val formatter = if (time.getNano > 0) timeFormatter else timeFormatterNoMs
val formatted = formatter.format(time)
gen.writeString(formatted)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class TestSqlCompilerServiceAirports
jdbcUrl = Some(jdbcUrl)
)
}
private def asCsv(params: Map[String, RawValue], scopes: Set[String] = Set.empty): ProgramEnvironment = {
private def asCsv(params: Map[String, RawValue] = Map.empty, scopes: Set[String] = Set.empty): ProgramEnvironment = {
ProgramEnvironment(
user,
if (params.isEmpty) None else Some(params.toArray),
Expand Down Expand Up @@ -1026,37 +1026,58 @@ class TestSqlCompilerServiceAirports
assert(errors.isEmpty)
val GetProgramDescriptionSuccess(_) = compilerService.getProgramDescription(t.q, asJson())
val baos = new ByteArrayOutputStream()
for (fmt <- Seq("json", "csv")) {
for (env <- Seq(asJson(), asCsv())) {
baos.reset()
assert(compilerService.execute(t.q, asJson(), None, baos) == ExecutionSuccess(true))
assert(baos.toString().contains("14.567"))
assert(compilerService.execute(t.q, env, None, baos) == ExecutionSuccess(true))
assert(baos.toString().contains("12:13:14.567"))
}
}

test("""SELECT TIME '12:13:14.567' AS t""".stripMargin) { t =>
test("""SELECT TIMESTAMP '2001-07-01 12:13:14' AS t""".stripMargin) { t =>
val ValidateResponse(errors) = compilerService.validate(t.q, asJson())
assert(errors.isEmpty)
val GetProgramDescriptionSuccess(_) = compilerService.getProgramDescription(t.q, asJson())
val baos = new ByteArrayOutputStream()
baos.reset()
for (fmt <- Seq("json", "csv")) {
assert(compilerService.execute(t.q, asCsv(), None, baos) == ExecutionSuccess(true))
assert(baos.toString().contains("12:13:14"))
assert(!baos.toString().contains("12:13:14.000"))
}

test("""SELECT TIME '12:13:14.567' AS t""".stripMargin) { t =>
val ValidateResponse(errors) = compilerService.validate(t.q, asJson())
assert(errors.isEmpty)
val GetProgramDescriptionSuccess(_) = compilerService.getProgramDescription(t.q, asJson())
val baos = new ByteArrayOutputStream()
for (env <- Seq(asJson(), asCsv())) {
baos.reset()
assert(compilerService.execute(t.q, asJson(), None, baos) == ExecutionSuccess(true))
assert(baos.toString().contains("14.567"))
assert(compilerService.execute(t.q, env, None, baos) == ExecutionSuccess(true))
assert(baos.toString().contains("12:13:14.567"))
}
}

test("""SELECT TIME '12:13:14' AS t""".stripMargin) { t =>
val ValidateResponse(errors) = compilerService.validate(t.q, asJson())
assert(errors.isEmpty)
val GetProgramDescriptionSuccess(_) = compilerService.getProgramDescription(t.q, asJson())
val baos = new ByteArrayOutputStream()
baos.reset()
assert(compilerService.execute(t.q, asCsv(), None, baos) == ExecutionSuccess(true))
assert(baos.toString().contains("12:13:14"))
assert(!baos.toString().contains("12:13:14.000"))
}

test("""-- @default t TIME '12:13:14.567'
|SELECT :t AS t""".stripMargin) { t =>
val ValidateResponse(errors) = compilerService.validate(t.q, asJson())
assert(errors.isEmpty)
val GetProgramDescriptionSuccess(_) = compilerService.getProgramDescription(t.q, asJson())
val baos = new ByteArrayOutputStream()
baos.reset()
for (fmt <- Seq("json", "csv")) {
for (env <- Seq(asJson(), asCsv())) {
baos.reset()
assert(compilerService.execute(t.q, asJson(), None, baos) == ExecutionSuccess(true))
assert(baos.toString().contains("14.567"))
assert(compilerService.execute(t.q, env, None, baos) == ExecutionSuccess(true))
assert(baos.toString().contains("12:13:14.567"))
}
}

Expand Down

0 comments on commit c7bcc77

Please sign in to comment.