-
Notifications
You must be signed in to change notification settings - Fork 81
Added custom sqlite types support #1741
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b226564
Added Sqlite custom types
AndreiKingsley 1cfe5af
Merge branch 'fix_jdbc_types' into custom_sqlite_types
AndreiKingsley 4ac5740
add sqlite custom types nullability check and tests
AndreiKingsley 08d1529
add sqlite tests
AndreiKingsley 4cda0ab
ktlintFormat
AndreiKingsley 9b24090
remove Long? column from moz_places
AndreiKingsley 7ee5de6
Merge branch 'master' into custom_sqlite_types
AndreiKingsley 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
54 changes: 54 additions & 0 deletions
54
dataframe-jdbc/src/test/kotlin/org/jetbrains/kotlinx/dataframe/io/sqliteCustomTypesTest.kt
This file contains hidden or 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,54 @@ | ||
| package org.jetbrains.kotlinx.dataframe.io | ||
|
|
||
| import io.kotest.matchers.shouldBe | ||
| import org.jetbrains.kotlinx.dataframe.DataFrame | ||
| import org.jetbrains.kotlinx.dataframe.io.db.Sqlite | ||
| import org.jetbrains.kotlinx.dataframe.type | ||
| import org.junit.AfterClass | ||
| import org.junit.BeforeClass | ||
| import org.junit.Test | ||
| import java.sql.Connection | ||
| import java.sql.DriverManager | ||
| import kotlin.reflect.typeOf | ||
|
|
||
| class SqliteTestCustomTypes { | ||
|
|
||
| companion object { | ||
| private lateinit var connection: Connection | ||
|
|
||
| private val dbUrl = | ||
| "jdbc:sqlite:${(this::class as Any).javaClass.classLoader | ||
| .getResource("safe_moz_places_sample.sqlite").path}" | ||
|
|
||
| @BeforeClass | ||
| @JvmStatic | ||
| fun setUpClass() { | ||
| connection = DriverManager.getConnection(dbUrl) | ||
| } | ||
|
|
||
| @AfterClass | ||
| @JvmStatic | ||
| fun tearDownClass() { | ||
| try { | ||
| connection.close() | ||
| } catch (e: Exception) { | ||
| // Log, but not fail | ||
| println("Warning: Could not clean up test database file: ${e.message}") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private val sqliteCustomTypes = Sqlite.withCustomTypes(mapOf("LONGVARCHAR" to typeOf<String>())) | ||
|
|
||
| private val df = DataFrame.readSqlTable(connection, "moz_places", dbType = sqliteCustomTypes) | ||
|
|
||
| @Test | ||
| fun `LONGVARCHAR columns should be read as String`() { | ||
| df["url"].type shouldBe typeOf<String>() | ||
| df["title"].type shouldBe typeOf<String?>() | ||
|
|
||
| df["url"][0] shouldBe "https://support.example.org/products/browser" | ||
| df["title"][0] shouldBe null | ||
| df["title"][4] shouldBe "Column selectors | Sample Docs" | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
dataframe-jdbc/src/test/kotlin/org/jetbrains/kotlinx/dataframe/io/sqliteDynamicTypes.kt
This file contains hidden or 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,55 @@ | ||
| package org.jetbrains.kotlinx.dataframe.io | ||
|
|
||
| import io.kotest.matchers.shouldBe | ||
| import org.jetbrains.kotlinx.dataframe.DataFrame | ||
| import org.jetbrains.kotlinx.dataframe.type | ||
| import org.junit.AfterClass | ||
| import org.junit.BeforeClass | ||
| import org.junit.Ignore | ||
| import org.junit.Test | ||
| import java.sql.Connection | ||
| import java.sql.DriverManager | ||
| import kotlin.reflect.typeOf | ||
|
|
||
| /** | ||
| * TODO: | ||
| * Xerial SQLite JDBC driver seems to give identical metadata for `Int?` and `Long?` columns, | ||
| * so we have to solve it #1747. | ||
| */ | ||
| class SqliteTestDynamicTypes { | ||
|
|
||
| companion object { | ||
| private lateinit var connection: Connection | ||
|
|
||
| private val dbUrl = | ||
| "jdbc:sqlite:${(this::class as Any).javaClass.classLoader | ||
| .getResource("simple_int_long_nullable.sqlite").path}" | ||
|
|
||
| @BeforeClass | ||
| @JvmStatic | ||
| fun setUpClass() { | ||
| connection = DriverManager.getConnection(dbUrl) | ||
| } | ||
|
|
||
| @AfterClass | ||
| @JvmStatic | ||
| fun tearDownClass() { | ||
| try { | ||
| connection.close() | ||
| } catch (e: Exception) { | ||
| // Log, but not fail | ||
| println("Warning: Could not clean up test database file: ${e.message}") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private val df = DataFrame.readSqlTable(connection, "numbers") | ||
|
|
||
| @Ignore | ||
| @Test | ||
| fun `INTEGER column with big values should be read as Long`() { | ||
| df["int_col"].type shouldBe typeOf<Int?>() | ||
| // Fails! #1747 | ||
| df["long_col"].type shouldBe typeOf<Long?>() | ||
| } | ||
| } |
Binary file not shown.
Binary file not shown.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
A fun trick I just learned. If you make
Sqliteopen, you can make the companion object be the default itself, likecompanion object : Sqlite() {}This means users can specify both
SqliteandSqlite.withCustomTypes(customTypes). Not sure if you like that :)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.
Oh, seems great!
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.
But I am not sure if this is actually good here, need a research of use-cases.
#1797