Skip to content

Add support for varchar columns in PostgreSQL #518

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public object PostgreSql : DbType("postgresql") {
"timestamp" -> rs.getString(name)
"timestamptz", "timestamp with time zone" -> rs.getString(name)
"uuid" -> rs.getString(name)
"varchar" -> rs.getString(name)
"xml" -> rs.getString(name)
else -> throw IllegalArgumentException("Unsupported PostgreSQL type: ${tableColumnMetadata.sqlTypeName}")
}
Expand Down Expand Up @@ -87,6 +88,7 @@ public object PostgreSql : DbType("postgresql") {
"timestamp" -> ColumnSchema.Value(typeOf<String>())
"timestamptz", "timestamp with time zone" -> ColumnSchema.Value(typeOf<String>())
"uuid" -> ColumnSchema.Value(typeOf<String>())
"varchar" -> ColumnSchema.Value(typeOf<String>())
"xml" -> ColumnSchema.Value(typeOf<String>())
else -> throw IllegalArgumentException("Unsupported PostgreSQL type: ${tableColumnMetadata.sqlTypeName} for column ${tableColumnMetadata.name}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package org.jetbrains.kotlinx.dataframe.io
import io.kotest.matchers.shouldBe
import org.intellij.lang.annotations.Language
import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.DataRow
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
import org.jetbrains.kotlinx.dataframe.api.cast
import org.jetbrains.kotlinx.dataframe.api.filter
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
import org.junit.AfterClass
import org.junit.BeforeClass
import org.junit.Test
Expand All @@ -16,6 +18,7 @@ import java.sql.DriverManager
import java.sql.SQLException
import java.util.UUID
import org.junit.Ignore
import kotlin.reflect.typeOf

private const val URL = "jdbc:postgresql://localhost:5432/test"
private const val USER_NAME = "postgres"
Expand All @@ -39,6 +42,7 @@ interface Table1 {
val intervalcol: String
val jsoncol: String
val jsonbcol: String
val varcharcol: String
}

@DataSchema
Expand Down Expand Up @@ -104,7 +108,8 @@ class PostgresTest {
integerCol integer,
intervalCol interval,
jsonCol json,
jsonbCol jsonb
jsonbCol jsonb,
varcharCol varchar(10)
)
"""
connection.createStatement().execute(
Expand Down Expand Up @@ -146,8 +151,8 @@ class PostgresTest {
bigintCol, bigserialCol, booleanCol,
boxCol, byteaCol, characterCol, characterNCol, charCol,
circleCol, dateCol, doubleCol,
integerCol, intervalCol, jsonCol, jsonbCol
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
integerCol, intervalCol, jsonCol, jsonbCol, varcharCol
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"""

@Language("SQL")
Expand Down Expand Up @@ -184,6 +189,7 @@ class PostgresTest {

st.setObject(14, jsonbObject)
st.setObject(15, jsonbObject)
st.setObject(16, "a varchar")
st.executeUpdate()
}
}
Expand Down Expand Up @@ -274,6 +280,7 @@ class PostgresTest {
table1Df.rowsCount() shouldBe 3
table1Df.filter { it[Table1::integercol] > 12345 }.rowsCount() shouldBe 2
table1Df[0][1] shouldBe 1000L
table1Df[0][16] shouldBe "a varchar"

val table2Df = dataframes[1].cast<Table2>()

Expand All @@ -283,4 +290,27 @@ class PostgresTest {

//TODO: add test for JSON column
}

@Test
fun `read schema from sql table`() {
val schema = DataFrame.getSchemaForSqlTable(connection, "table1")

schema.columns["id"]?.type shouldBe typeOf<Int>()
schema.columns["bigintcol"]?.type shouldBe typeOf<Long>()
schema.columns["bigserialcol"]?.type shouldBe typeOf<Long>()
schema.columns["booleancol"]?.type shouldBe typeOf<Boolean>()
schema.columns["boxcol"]?.type shouldBe typeOf<String>()
schema.columns["byteacol"]?.type shouldBe typeOf<ByteArray>()
schema.columns["charactercol"]?.type shouldBe typeOf<String>()
schema.columns["characterncol"]?.type shouldBe typeOf<String>()
schema.columns["charcol"]?.type shouldBe typeOf<String>()
schema.columns["circlecol"]?.type shouldBe typeOf<String>()
schema.columns["datecol"]?.type shouldBe typeOf<String>()
schema.columns["doublecol"]?.type shouldBe typeOf<Double>()
schema.columns["integercol"]?.type shouldBe typeOf<Int>()
schema.columns["intervalcol"]?.type shouldBe typeOf<String>()
schema.columns["jsoncol"]?.type shouldBe typeOf<ColumnGroup<DataRow<String>>>()
schema.columns["jsonbcol"]?.type shouldBe typeOf<ColumnGroup<DataRow<String>>>()
schema.columns["varcharcol"]?.type shouldBe typeOf<String>()
}
}