-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't discard the timezone information when reading timezone with tim… #196
Open
olivierdeckers
wants to merge
5
commits into
zio:main
Choose a base branch
from
olivierdeckers:fix-timestamp-with-offset
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fc6f8ce
Don't discard the timezone information when reading timezone with tim…
olivierdeckers b5e8458
Merge remote-tracking branch 'origin/main' into fix-timestamp-with-of…
olivierdeckers 427c024
fmt
olivierdeckers 2a505a4
Add test using duckdb
olivierdeckers de5735f
Add test using duckdb
olivierdeckers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package zio.jdbc | ||
|
||
import org.duckdb.DuckDBConnection | ||
import org.testcontainers.jdbc.ConnectionWrapper | ||
import zio._ | ||
import zio.schema.{ DeriveSchema, Schema } | ||
import zio.test.TestAspect.{ repeats, sequential, shrinks, withLiveClock } | ||
import zio.test.{ Gen, Spec, TestEnvironment, ZIOSpec, assertTrue, check } | ||
|
||
import java.sql.{ Array => _, _ } | ||
import java.time.{ OffsetDateTime, ZoneOffset } | ||
import java.time.temporal.ChronoUnit | ||
import java.util.Properties | ||
|
||
object DuckDbSpec extends ZIOSpec[ZConnection] { | ||
|
||
override def bootstrap: ZLayer[Any, Any, ZConnection] = | ||
ZLayer.scoped( | ||
for { | ||
connection <- ZIO.acquireRelease( | ||
ZIO.succeedBlocking { | ||
val props = new Properties() | ||
val duckDb = DriverManager | ||
.getConnection(s"jdbc:duckdb:", props) | ||
.asInstanceOf[DuckDBConnection] | ||
WorkaroundDuckDBConnection(duckDb) | ||
} | ||
)(c => ZIO.succeed(c.close())) | ||
zConnection <- ZConnection.make(connection) | ||
} yield zConnection | ||
) | ||
|
||
case class OffsetDateTimeRow(value: OffsetDateTime) | ||
object OffsetDateTimeRow { | ||
implicit val schema: Schema[OffsetDateTimeRow] = DeriveSchema.gen[OffsetDateTimeRow] | ||
implicit val jdbcDecoder: JdbcDecoder[OffsetDateTimeRow] = JdbcDecoder.fromSchema | ||
} | ||
|
||
override def spec: Spec[ZConnection with TestEnvironment with Scope, Any] = suite("DuckDB")( | ||
test("should be able to decode case classes with OffsetDateTime fields and handle timezones correctly") { | ||
check( | ||
Gen.offsetDateTime( | ||
OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC), | ||
OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC) | ||
) | ||
) { offsetDateTime => | ||
for { | ||
_ <- sql"""CREATE TABLE offset_datetime (value TIMESTAMP WITH TIME ZONE)""".execute | ||
_ <- sql"""INSERT INTO offset_datetime VALUES ($offsetDateTime)""".execute | ||
d <- sql"""SELECT value FROM offset_datetime""".query[OffsetDateTimeRow].selectOne | ||
_ <- sql"DROP TABLE offset_datetime".execute | ||
expected = | ||
offsetDateTime | ||
.truncatedTo(ChronoUnit.MICROS) | ||
.withOffsetSameInstant(ZoneOffset.UTC) | ||
} yield assertTrue( | ||
d.isDefined, | ||
d.get.value == expected | ||
) | ||
} | ||
} | ||
) @@ sequential @@ shrinks(0) @@ repeats(100) @@ withLiveClock | ||
|
||
val noop = new Runnable() { | ||
override def run(): Unit = () | ||
} | ||
|
||
/** | ||
* The DuckDBConnection hasn't implemented some operations that are used by zio-jdbc. This class works around those. | ||
*/ | ||
case class WorkaroundDuckDBConnection(duckDb: DuckDBConnection) extends ConnectionWrapper(duckDb, noop) { | ||
// Work around this feature not being implemented by duckdb jdbc | ||
override def prepareStatement(sql: String, autoGeneratedKeys: Int): PreparedStatement = prepareStatement(sql) | ||
override def getClientInfo(name: String): String = null | ||
override def getClientInfo: Properties = new Properties() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When using query[OffsetDateTime] directly things were working already, the code path I changed in JdbcDecoder is only hit when using the JdbcDecoder.fromSchema
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without the change, the case class would have had to have an Instant field instead of an OffsetDateTime field.
The data would be returned in the default system timezone, but interpreted as a timestamp in UTC. Where I live this results in a 2 hour offset currently between writing and reading an OffsetDateTimeRow