|
17 | 17 |
|
18 | 18 | package org.apache.spark.sql.jdbc |
19 | 19 |
|
20 | | -import java.sql.Connection |
| 20 | +import java.sql.{Connection, Date, Timestamp} |
21 | 21 | import java.util.Properties |
22 | 22 |
|
| 23 | +import org.apache.spark.sql.Row |
23 | 24 | import org.apache.spark.sql.test.SharedSQLContext |
| 25 | +import org.apache.spark.sql.types._ |
24 | 26 | import org.apache.spark.tags.DockerTest |
25 | 27 |
|
26 | 28 | /** |
@@ -77,4 +79,74 @@ class OracleIntegrationSuite extends DockerJDBCIntegrationSuite with SharedSQLCo |
77 | 79 | // verify the value is the inserted correct or not |
78 | 80 | assert(rows(0).getString(0).equals("foo")) |
79 | 81 | } |
| 82 | + |
| 83 | + test("SPARK-16625: General data types to be mapped to Oracle") { |
| 84 | + val props = new Properties() |
| 85 | + props.put("oracle.jdbc.mapDateToTimestamp", "false") |
| 86 | + |
| 87 | + val schema = StructType(Seq( |
| 88 | + StructField("boolean_type", BooleanType, true), |
| 89 | + StructField("integer_type", IntegerType, true), |
| 90 | + StructField("long_type", LongType, true), |
| 91 | + StructField("float_Type", FloatType, true), |
| 92 | + StructField("double_type", DoubleType, true), |
| 93 | + StructField("byte_type", ByteType, true), |
| 94 | + StructField("short_type", ShortType, true), |
| 95 | + StructField("string_type", StringType, true), |
| 96 | + StructField("binary_type", BinaryType, true), |
| 97 | + StructField("date_type", DateType, true), |
| 98 | + StructField("timestamp_type", TimestampType, true) |
| 99 | + )) |
| 100 | + |
| 101 | + val tableName = "test_oracle_general_types" |
| 102 | + val booleanVal = true |
| 103 | + val integerVal = 1 |
| 104 | + val longVal = 2L |
| 105 | + val floatVal = 3.0f |
| 106 | + val doubleVal = 4.0 |
| 107 | + val byteVal = 2.toByte |
| 108 | + val shortVal = 5.toShort |
| 109 | + val stringVal = "string" |
| 110 | + val binaryVal = Array[Byte](6, 7, 8) |
| 111 | + val dateVal = Date.valueOf("2016-07-26") |
| 112 | + val timestampVal = Timestamp.valueOf("2016-07-26 11:49:45") |
| 113 | + |
| 114 | + val data = spark.sparkContext.parallelize(Seq( |
| 115 | + Row( |
| 116 | + booleanVal, integerVal, longVal, floatVal, doubleVal, byteVal, shortVal, stringVal, |
| 117 | + binaryVal, dateVal, timestampVal |
| 118 | + ))) |
| 119 | + |
| 120 | + val dfWrite = spark.createDataFrame(data, schema) |
| 121 | + dfWrite.write.jdbc(jdbcUrl, tableName, props) |
| 122 | + |
| 123 | + val dfRead = spark.read.jdbc(jdbcUrl, tableName, props) |
| 124 | + val rows = dfRead.collect() |
| 125 | + // verify the data type is inserted |
| 126 | + val types = rows(0).toSeq.map(x => x.getClass.toString) |
| 127 | + assert(types(0).equals("class java.lang.Boolean")) |
| 128 | + assert(types(1).equals("class java.lang.Integer")) |
| 129 | + assert(types(2).equals("class java.lang.Long")) |
| 130 | + assert(types(3).equals("class java.lang.Float")) |
| 131 | + assert(types(4).equals("class java.lang.Float")) |
| 132 | + assert(types(5).equals("class java.lang.Integer")) |
| 133 | + assert(types(6).equals("class java.lang.Integer")) |
| 134 | + assert(types(7).equals("class java.lang.String")) |
| 135 | + assert(types(8).equals("class [B")) |
| 136 | + assert(types(9).equals("class java.sql.Date")) |
| 137 | + assert(types(10).equals("class java.sql.Timestamp")) |
| 138 | + // verify the value is the inserted correct or not |
| 139 | + val values = rows(0) |
| 140 | + assert(values.getBoolean(0).equals(booleanVal)) |
| 141 | + assert(values.getInt(1).equals(integerVal)) |
| 142 | + assert(values.getLong(2).equals(longVal)) |
| 143 | + assert(values.getFloat(3).equals(floatVal)) |
| 144 | + assert(values.getFloat(4).equals(doubleVal.toFloat)) |
| 145 | + assert(values.getInt(5).equals(byteVal.toInt)) |
| 146 | + assert(values.getInt(6).equals(shortVal.toInt)) |
| 147 | + assert(values.getString(7).equals(stringVal)) |
| 148 | + assert(values.getAs[Array[Byte]](8).mkString.equals("678")) |
| 149 | + assert(values.getDate(9).equals(dateVal)) |
| 150 | + assert(values.getTimestamp(10).equals(timestampVal)) |
| 151 | + } |
80 | 152 | } |
0 commit comments