|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.jdbc |
| 19 | + |
| 20 | +import java.math.BigDecimal |
| 21 | +import java.sql.{Connection, Date, Timestamp} |
| 22 | +import java.util.Properties |
| 23 | + |
| 24 | +import org.scalatest._ |
| 25 | + |
| 26 | +import org.apache.spark.tags.DockerTest |
| 27 | + |
| 28 | +@DockerTest |
| 29 | +@Ignore // AMPLab Jenkins needs to be updated before shared memory works on docker |
| 30 | +class DB2IntegrationSuite extends DockerJDBCIntegrationSuite { |
| 31 | + override val db = new DatabaseOnDocker { |
| 32 | + override val imageName = "lresende/db2express-c:10.5.0.5-3.10.0" |
| 33 | + override val env = Map( |
| 34 | + "DB2INST1_PASSWORD" -> "rootpass", |
| 35 | + "LICENSE" -> "accept" |
| 36 | + ) |
| 37 | + override val usesIpc = true |
| 38 | + override val jdbcPort: Int = 50000 |
| 39 | + override def getJdbcUrl(ip: String, port: Int): String = |
| 40 | + s"jdbc:db2://$ip:$port/foo:user=db2inst1;password=rootpass;" |
| 41 | + override def getStartupProcessName: Option[String] = Some("db2start") |
| 42 | + } |
| 43 | + |
| 44 | + override def dataPreparation(conn: Connection): Unit = { |
| 45 | + conn.prepareStatement("CREATE TABLE tbl (x INTEGER, y VARCHAR(8))").executeUpdate() |
| 46 | + conn.prepareStatement("INSERT INTO tbl VALUES (42,'fred')").executeUpdate() |
| 47 | + conn.prepareStatement("INSERT INTO tbl VALUES (17,'dave')").executeUpdate() |
| 48 | + |
| 49 | + conn.prepareStatement("CREATE TABLE numbers (onebit BIT(1), tenbits BIT(10), " |
| 50 | + + "small SMALLINT, med MEDIUMINT, nor INT, big BIGINT, deci DECIMAL(40,20), flt FLOAT, " |
| 51 | + + "dbl DOUBLE)").executeUpdate() |
| 52 | + conn.prepareStatement("INSERT INTO numbers VALUES (b'0', b'1000100101', " |
| 53 | + + "17, 77777, 123456789, 123456789012345, 123456789012345.123456789012345, " |
| 54 | + + "42.75, 1.0000000000000002)").executeUpdate() |
| 55 | + |
| 56 | + conn.prepareStatement("CREATE TABLE dates (d DATE, t TIME, dt DATETIME, ts TIMESTAMP, " |
| 57 | + + "yr YEAR)").executeUpdate() |
| 58 | + conn.prepareStatement("INSERT INTO dates VALUES ('1991-11-09', '13:31:24', " |
| 59 | + + "'1996-01-01 01:23:45', '2009-02-13 23:31:30', '2001')").executeUpdate() |
| 60 | + |
| 61 | + // TODO: Test locale conversion for strings. |
| 62 | + conn.prepareStatement("CREATE TABLE strings (a CHAR(10), b VARCHAR(10), c CLOB, d BLOB, " |
| 63 | + + "e CHAR FOR BIT DATA)").executeUpdate() |
| 64 | + conn.prepareStatement("INSERT INTO strings VALUES ('the', 'quick', 'brown', 'fox', 'jumps'") |
| 65 | + .executeUpdate() |
| 66 | + } |
| 67 | + |
| 68 | + test("Basic test") { |
| 69 | + val df = sqlContext.read.jdbc(jdbcUrl, "tbl", new Properties) |
| 70 | + val rows = df.collect() |
| 71 | + assert(rows.length == 2) |
| 72 | + val types = rows(0).toSeq.map(x => x.getClass.toString) |
| 73 | + assert(types.length == 2) |
| 74 | + assert(types(0).equals("class java.lang.Integer")) |
| 75 | + assert(types(1).equals("class java.lang.String")) |
| 76 | + } |
| 77 | + |
| 78 | + test("Numeric types") { |
| 79 | + val df = sqlContext.read.jdbc(jdbcUrl, "numbers", new Properties) |
| 80 | + val rows = df.collect() |
| 81 | + assert(rows.length == 1) |
| 82 | + val types = rows(0).toSeq.map(x => x.getClass.toString) |
| 83 | + assert(types.length == 9) |
| 84 | + assert(types(0).equals("class java.lang.Boolean")) |
| 85 | + assert(types(1).equals("class java.lang.Long")) |
| 86 | + assert(types(2).equals("class java.lang.Integer")) |
| 87 | + assert(types(3).equals("class java.lang.Integer")) |
| 88 | + assert(types(4).equals("class java.lang.Integer")) |
| 89 | + assert(types(5).equals("class java.lang.Long")) |
| 90 | + assert(types(6).equals("class java.math.BigDecimal")) |
| 91 | + assert(types(7).equals("class java.lang.Double")) |
| 92 | + assert(types(8).equals("class java.lang.Double")) |
| 93 | + assert(rows(0).getBoolean(0) == false) |
| 94 | + assert(rows(0).getLong(1) == 0x225) |
| 95 | + assert(rows(0).getInt(2) == 17) |
| 96 | + assert(rows(0).getInt(3) == 77777) |
| 97 | + assert(rows(0).getInt(4) == 123456789) |
| 98 | + assert(rows(0).getLong(5) == 123456789012345L) |
| 99 | + val bd = new BigDecimal("123456789012345.12345678901234500000") |
| 100 | + assert(rows(0).getAs[BigDecimal](6).equals(bd)) |
| 101 | + assert(rows(0).getDouble(7) == 42.75) |
| 102 | + assert(rows(0).getDouble(8) == 1.0000000000000002) |
| 103 | + } |
| 104 | + |
| 105 | + test("Date types") { |
| 106 | + val df = sqlContext.read.jdbc(jdbcUrl, "dates", new Properties) |
| 107 | + val rows = df.collect() |
| 108 | + assert(rows.length == 1) |
| 109 | + val types = rows(0).toSeq.map(x => x.getClass.toString) |
| 110 | + assert(types.length == 5) |
| 111 | + assert(types(0).equals("class java.sql.Date")) |
| 112 | + assert(types(1).equals("class java.sql.Timestamp")) |
| 113 | + assert(types(2).equals("class java.sql.Timestamp")) |
| 114 | + assert(types(3).equals("class java.sql.Timestamp")) |
| 115 | + assert(types(4).equals("class java.sql.Date")) |
| 116 | + assert(rows(0).getAs[Date](0).equals(Date.valueOf("1991-11-09"))) |
| 117 | + assert(rows(0).getAs[Timestamp](1).equals(Timestamp.valueOf("1970-01-01 13:31:24"))) |
| 118 | + assert(rows(0).getAs[Timestamp](2).equals(Timestamp.valueOf("1996-01-01 01:23:45"))) |
| 119 | + assert(rows(0).getAs[Timestamp](3).equals(Timestamp.valueOf("2009-02-13 23:31:30"))) |
| 120 | + assert(rows(0).getAs[Date](4).equals(Date.valueOf("2001-01-01"))) |
| 121 | + } |
| 122 | + |
| 123 | + test("String types") { |
| 124 | + val df = sqlContext.read.jdbc(jdbcUrl, "strings", new Properties) |
| 125 | + val rows = df.collect() |
| 126 | + assert(rows.length == 1) |
| 127 | + val types = rows(0).toSeq.map(x => x.getClass.toString) |
| 128 | + assert(types.length == 9) |
| 129 | + assert(types(0).equals("class java.lang.String")) |
| 130 | + assert(types(1).equals("class java.lang.String")) |
| 131 | + assert(types(2).equals("class java.lang.String")) |
| 132 | + assert(types(3).equals("class java.lang.String")) |
| 133 | + assert(types(4).equals("class java.lang.String")) |
| 134 | + assert(types(5).equals("class java.lang.String")) |
| 135 | + assert(types(6).equals("class [B")) |
| 136 | + assert(types(7).equals("class [B")) |
| 137 | + assert(types(8).equals("class [B")) |
| 138 | + assert(rows(0).getString(0).equals("the")) |
| 139 | + assert(rows(0).getString(1).equals("quick")) |
| 140 | + assert(rows(0).getString(2).equals("brown")) |
| 141 | + assert(rows(0).getString(3).equals("fox")) |
| 142 | + assert(rows(0).getString(4).equals("jumps")) |
| 143 | + assert(rows(0).getString(5).equals("over")) |
| 144 | + assert(java.util.Arrays.equals(rows(0).getAs[Array[Byte]](6), Array[Byte](116, 104, 101, 0))) |
| 145 | + assert(java.util.Arrays.equals(rows(0).getAs[Array[Byte]](7), Array[Byte](108, 97, 122, 121))) |
| 146 | + assert(java.util.Arrays.equals(rows(0).getAs[Array[Byte]](8), Array[Byte](100, 111, 103))) |
| 147 | + } |
| 148 | + |
| 149 | + test("Basic write test") { |
| 150 | + val df1 = sqlContext.read.jdbc(jdbcUrl, "numbers", new Properties) |
| 151 | + val df2 = sqlContext.read.jdbc(jdbcUrl, "dates", new Properties) |
| 152 | + val df3 = sqlContext.read.jdbc(jdbcUrl, "strings", new Properties) |
| 153 | + df1.write.jdbc(jdbcUrl, "numberscopy", new Properties) |
| 154 | + df2.write.jdbc(jdbcUrl, "datescopy", new Properties) |
| 155 | + df3.write.jdbc(jdbcUrl, "stringscopy", new Properties) |
| 156 | + } |
| 157 | +} |
0 commit comments