@@ -10,7 +10,6 @@ import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
10
10
import org.jetbrains.kotlinx.dataframe.api.add
11
11
import org.jetbrains.kotlinx.dataframe.api.cast
12
12
import org.jetbrains.kotlinx.dataframe.api.filter
13
- import org.jetbrains.kotlinx.dataframe.api.schema
14
13
import org.jetbrains.kotlinx.dataframe.api.select
15
14
import org.jetbrains.kotlinx.dataframe.io.DbConnectionConfig
16
15
import org.jetbrains.kotlinx.dataframe.io.db.H2
@@ -20,6 +19,7 @@ import org.jetbrains.kotlinx.dataframe.io.getSchemaForAllSqlTables
20
19
import org.jetbrains.kotlinx.dataframe.io.getSchemaForResultSet
21
20
import org.jetbrains.kotlinx.dataframe.io.getSchemaForSqlQuery
22
21
import org.jetbrains.kotlinx.dataframe.io.getSchemaForSqlTable
22
+ import org.jetbrains.kotlinx.dataframe.io.inferNullability
23
23
import org.jetbrains.kotlinx.dataframe.io.readAllSqlTables
24
24
import org.jetbrains.kotlinx.dataframe.io.readDataFrame
25
25
import org.jetbrains.kotlinx.dataframe.io.readResultSet
@@ -841,128 +841,9 @@ class JdbcTest {
841
841
saleDataSchema1.columns[" amount" ]!! .type shouldBe typeOf<BigDecimal >()
842
842
}
843
843
844
- // TODO: add the same test for each particular database and refactor the scenario to the common test case
845
- // https://github.com/Kotlin/dataframe/issues/688
846
844
@Test
847
845
fun `infer nullability` () {
848
- // prepare tables and data
849
- @Language(" SQL" )
850
- val createTestTable1Query = """
851
- CREATE TABLE TestTable1 (
852
- id INT PRIMARY KEY,
853
- name VARCHAR(50),
854
- surname VARCHAR(50),
855
- age INT NOT NULL
856
- )
857
- """
858
-
859
- connection.createStatement().execute(createTestTable1Query)
860
-
861
- connection.createStatement()
862
- .execute(" INSERT INTO TestTable1 (id, name, surname, age) VALUES (1, 'John', 'Crawford', 40)" )
863
- connection.createStatement()
864
- .execute(" INSERT INTO TestTable1 (id, name, surname, age) VALUES (2, 'Alice', 'Smith', 25)" )
865
- connection.createStatement()
866
- .execute(" INSERT INTO TestTable1 (id, name, surname, age) VALUES (3, 'Bob', 'Johnson', 47)" )
867
- connection.createStatement()
868
- .execute(" INSERT INTO TestTable1 (id, name, surname, age) VALUES (4, 'Sam', NULL, 15)" )
869
-
870
- // start testing `readSqlTable` method
871
-
872
- // with default inferNullability: Boolean = true
873
- val tableName = " TestTable1"
874
- val df = DataFrame .readSqlTable(connection, tableName)
875
- df.schema().columns[" id" ]!! .type shouldBe typeOf<Int >()
876
- df.schema().columns[" name" ]!! .type shouldBe typeOf<String >()
877
- df.schema().columns[" surname" ]!! .type shouldBe typeOf<String ?>()
878
- df.schema().columns[" age" ]!! .type shouldBe typeOf<Int >()
879
-
880
- val dataSchema = DataFrame .getSchemaForSqlTable(connection, tableName)
881
- dataSchema.columns.size shouldBe 4
882
- dataSchema.columns[" id" ]!! .type shouldBe typeOf<Int >()
883
- dataSchema.columns[" name" ]!! .type shouldBe typeOf<String ?>()
884
- dataSchema.columns[" surname" ]!! .type shouldBe typeOf<String ?>()
885
- dataSchema.columns[" age" ]!! .type shouldBe typeOf<Int >()
886
-
887
- // with inferNullability: Boolean = false
888
- val df1 = DataFrame .readSqlTable(connection, tableName, inferNullability = false )
889
- df1.schema().columns[" id" ]!! .type shouldBe typeOf<Int >()
890
-
891
- // this column changed a type because it doesn't contain nulls
892
- df1.schema().columns[" name" ]!! .type shouldBe typeOf<String ?>()
893
- df1.schema().columns[" surname" ]!! .type shouldBe typeOf<String ?>()
894
- df1.schema().columns[" age" ]!! .type shouldBe typeOf<Int >()
895
-
896
- // end testing `readSqlTable` method
897
-
898
- // start testing `readSQLQuery` method
899
-
900
- // ith default inferNullability: Boolean = true
901
- @Language(" SQL" )
902
- val sqlQuery =
903
- """
904
- SELECT name, surname, age FROM TestTable1
905
- """ .trimIndent()
906
-
907
- val df2 = DataFrame .readSqlQuery(connection, sqlQuery)
908
- df2.schema().columns[" name" ]!! .type shouldBe typeOf<String >()
909
- df2.schema().columns[" surname" ]!! .type shouldBe typeOf<String ?>()
910
- df2.schema().columns[" age" ]!! .type shouldBe typeOf<Int >()
911
-
912
- val dataSchema2 = DataFrame .getSchemaForSqlQuery(connection, sqlQuery)
913
- dataSchema2.columns.size shouldBe 3
914
- dataSchema2.columns[" name" ]!! .type shouldBe typeOf<String ?>()
915
- dataSchema2.columns[" surname" ]!! .type shouldBe typeOf<String ?>()
916
- dataSchema2.columns[" age" ]!! .type shouldBe typeOf<Int >()
917
-
918
- // with inferNullability: Boolean = false
919
- val df3 = DataFrame .readSqlQuery(connection, sqlQuery, inferNullability = false )
920
-
921
- // this column changed a type because it doesn't contain nulls
922
- df3.schema().columns[" name" ]!! .type shouldBe typeOf<String ?>()
923
- df3.schema().columns[" surname" ]!! .type shouldBe typeOf<String ?>()
924
- df3.schema().columns[" age" ]!! .type shouldBe typeOf<Int >()
925
-
926
- // end testing `readSQLQuery` method
927
-
928
- // start testing `readResultSet` method
929
-
930
- connection.createStatement(ResultSet .TYPE_SCROLL_SENSITIVE , ResultSet .CONCUR_UPDATABLE ).use { st ->
931
- @Language(" SQL" )
932
- val selectStatement = " SELECT * FROM TestTable1"
933
-
934
- st.executeQuery(selectStatement).use { rs ->
935
- // ith default inferNullability: Boolean = true
936
- val df4 = DataFrame .readResultSet(rs, H2 (MySql ))
937
- df4.schema().columns[" id" ]!! .type shouldBe typeOf<Int >()
938
- df4.schema().columns[" name" ]!! .type shouldBe typeOf<String >()
939
- df4.schema().columns[" surname" ]!! .type shouldBe typeOf<String ?>()
940
- df4.schema().columns[" age" ]!! .type shouldBe typeOf<Int >()
941
-
942
- rs.beforeFirst()
943
-
944
- val dataSchema3 = DataFrame .getSchemaForResultSet(rs, H2 (MySql ))
945
- dataSchema3.columns.size shouldBe 4
946
- dataSchema3.columns[" id" ]!! .type shouldBe typeOf<Int >()
947
- dataSchema3.columns[" name" ]!! .type shouldBe typeOf<String ?>()
948
- dataSchema3.columns[" surname" ]!! .type shouldBe typeOf<String ?>()
949
- dataSchema3.columns[" age" ]!! .type shouldBe typeOf<Int >()
950
-
951
- // with inferNullability: Boolean = false
952
- rs.beforeFirst()
953
-
954
- val df5 = DataFrame .readResultSet(rs, H2 (MySql ), inferNullability = false )
955
- df5.schema().columns[" id" ]!! .type shouldBe typeOf<Int >()
956
-
957
- // this column changed a type because it doesn't contain nulls
958
- df5.schema().columns[" name" ]!! .type shouldBe typeOf<String ?>()
959
- df5.schema().columns[" surname" ]!! .type shouldBe typeOf<String ?>()
960
- df5.schema().columns[" age" ]!! .type shouldBe typeOf<Int >()
961
- }
962
- }
963
- // end testing `readResultSet` method
964
-
965
- connection.createStatement().execute(" DROP TABLE TestTable1" )
846
+ inferNullability(connection)
966
847
}
967
848
968
849
@Test
0 commit comments