Skip to content

Commit cc645f8

Browse files
committed
Fix tests
1 parent 631b0ec commit cc645f8

9 files changed

Lines changed: 91 additions & 181 deletions

File tree

phoenix5-spark/src/it/resources/globalSetup.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ UPSERT INTO TEST_DECIMAL VALUES (1, 123.456789)
5050
CREATE TABLE TEST_SMALL_TINY (ID BIGINT NOT NULL PRIMARY KEY, COL1 SMALLINT, COL2 TINYINT)
5151
UPSERT INTO TEST_SMALL_TINY VALUES (1, 32767, 127)
5252
CREATE TABLE DATE_TEST(ID BIGINT NOT NULL PRIMARY KEY, COL1 DATE)
53-
UPSERT INTO DATE_TEST VALUES(1, CURRENT_DATE())
53+
UPSERT INTO DATE_TEST VALUES(1, '2021-01-01T00:00:00Z')
5454
CREATE TABLE TIME_TEST(ID BIGINT NOT NULL PRIMARY KEY, COL1 TIME)
55-
UPSERT INTO TIME_TEST VALUES(1, CURRENT_TIME())
55+
UPSERT INTO TIME_TEST VALUES(1, '2021-01-01T00:00:00Z')
5656
CREATE TABLE "space" ("key" VARCHAR PRIMARY KEY, "first name" VARCHAR)
5757
UPSERT INTO "space" VALUES ('key1', 'xyz')
5858
CREATE TABLE "small" ("key" VARCHAR PRIMARY KEY, "first name" VARCHAR, "salary" INTEGER )

phoenix5-spark/src/it/scala/org/apache/phoenix/spark/AbstractPhoenixSparkIT.scala

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
package org.apache.phoenix.spark
1515

1616
import java.sql.{Connection, DriverManager}
17-
import java.util.Properties
18-
19-
import org.apache.hadoop.conf.Configuration;
20-
import org.apache.hadoop.hbase.HConstants;
17+
import java.util.{Properties, TimeZone}
18+
import org.apache.hadoop.conf.Configuration
19+
import org.apache.hadoop.hbase.HConstants
2120
import org.apache.phoenix.query.BaseTest
21+
import org.apache.phoenix.spark.datasource.v2.writer.PhoenixTestingDataSourceWriter
2222
import org.apache.phoenix.util.PhoenixRuntime
23-
import org.apache.phoenix.util.ReadOnlyProps;
23+
import org.apache.phoenix.util.ReadOnlyProps
2424
import org.apache.spark.sql.SparkSession
2525
import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll, FunSuite, Matchers}
2626

@@ -30,6 +30,8 @@ object PhoenixSparkITHelper extends BaseTest {
3030
def getTestClusterConfig = new Configuration(BaseTest.config);
3131

3232
def doSetup = {
33+
// Set-up fixed timezone for DATE and TIMESTAMP tests
34+
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
3335
// The @ClassRule doesn't seem to be getting picked up, force creation here before setup
3436
BaseTest.tmpFolder.create()
3537
BaseTest.setUpTestDriver(ReadOnlyProps.EMPTY_PROPS);
@@ -92,6 +94,11 @@ class AbstractPhoenixSparkIT extends FunSuite with Matchers with BeforeAndAfter
9294
conn.commit()
9395
}
9496

97+
before{
98+
// Reset batch counter after each test
99+
PhoenixTestingDataSourceWriter.TOTAL_BATCHES_COMMITTED_COUNT = 0
100+
}
101+
95102
override def beforeAll() {
96103
PhoenixSparkITHelper.doSetup
97104

phoenix5-spark/src/it/scala/org/apache/phoenix/spark/PhoenixSparkDatasourceV1IT.scala

Lines changed: 8 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.apache.spark.sql.types._
2424
import org.apache.spark.sql.{Row, SaveMode}
2525

2626
import java.sql.DriverManager
27+
import java.time.{LocalDate, ZoneId}
2728
import java.util.Date
2829
import scala.collection.mutable
2930
import scala.collection.mutable.ListBuffer
@@ -243,30 +244,6 @@ class PhoenixSparkDatasourceV1IT extends AbstractPhoenixSparkIT {
243244
count shouldEqual 1L
244245
}
245246

246-
test("Can use extraOptions to set configs for workers during reads") {
247-
// Pass in true, so we will get null when fetching the current row, leading to an NPE
248-
var extraOptions = PhoenixTestingInputPartitionReader.RETURN_NULL_CURR_ROW + "=true"
249-
var rdd = spark.sqlContext.read
250-
.format(PhoenixTestingDataSource.TEST_SOURCE)
251-
.options( Map("table" -> "TABLE1", PhoenixDataSource.ZOOKEEPER_URL -> quorumAddress,
252-
PhoenixDataSource.PHOENIX_CONFIGS -> extraOptions)).load
253-
254-
// Expect to get a NullPointerException in the executors
255-
var error = intercept[SparkException] {
256-
rdd.take(2)(0)(1)
257-
}
258-
assert(error.getCause.isInstanceOf[NullPointerException])
259-
260-
// Pass in false, so we will get the expected rows
261-
extraOptions = PhoenixTestingInputPartitionReader.RETURN_NULL_CURR_ROW + "=false"
262-
rdd = spark.sqlContext.read
263-
.format(PhoenixTestingDataSource.TEST_SOURCE)
264-
.options( Map("table" -> "TABLE1", PhoenixDataSource.ZOOKEEPER_URL -> quorumAddress,
265-
PhoenixDataSource.PHOENIX_CONFIGS -> extraOptions)).load
266-
val stringValue = rdd.take(2)(0)(1)
267-
stringValue shouldEqual "test_row_1"
268-
}
269-
270247
test("Can save to phoenix table from Spark without specifying all the columns") {
271248
val dataSet = List(Row(1L, "1", 1), Row(2L, "2", 2), Row(3L, "3", 3))
272249

@@ -300,47 +277,6 @@ class PhoenixSparkDatasourceV1IT extends AbstractPhoenixSparkIT {
300277
}
301278
}
302279

303-
test("Can use extraOptions to set configs for workers during writes") {
304-
val totalRecords = 100
305-
val upsertBatchSize = 5
306-
307-
val records = new mutable.MutableList[Row]
308-
for (x <- 1 to totalRecords) {
309-
records += Row(x.toLong, x.toString, x)
310-
}
311-
val dataSet = records.toList
312-
313-
val schema = StructType(
314-
Seq(StructField("ID", LongType, nullable = false),
315-
StructField("COL1", StringType),
316-
StructField("COL2", IntegerType)))
317-
318-
// Distribute the dataset into an RDD with just 1 partition so we use only 1 executor.
319-
// This makes it easy to deterministically count the batched commits from that executor
320-
// since it corresponds to exactly 1 input partition. In case of multiple executors with
321-
// an uneven distribution of input partitions, if
322-
// (number of records in that partition) % batchSize != 0, some updates would also be committed
323-
// via PhoenixDataWriter#commit rather than the batch commits via PhoenixDataWriter#write
324-
// and those would thus, not be counted by PhoenixTestingDataWriter.
325-
val rowRDD = spark.sparkContext.parallelize(dataSet, 1)
326-
327-
// Apply the schema to the RDD.
328-
val df = spark.sqlContext.createDataFrame(rowRDD, schema)
329-
val extraOptions = PhoenixConfigurationUtil.UPSERT_BATCH_SIZE + "=" + upsertBatchSize.toString
330-
331-
// Initially, this should be zero
332-
PhoenixTestingDataSourceWriter.TOTAL_BATCHES_COMMITTED_COUNT shouldEqual 0
333-
df.write
334-
.format(PhoenixTestingDataSource.TEST_SOURCE)
335-
.options(Map("table" -> "OUTPUT_TEST_TABLE", PhoenixDataSource.ZOOKEEPER_URL -> quorumAddress,
336-
PhoenixDataSource.PHOENIX_CONFIGS -> extraOptions))
337-
.mode(SaveMode.Overwrite)
338-
.save()
339-
340-
// Verify the number of times batched updates are committed via DataWriters
341-
PhoenixTestingDataSourceWriter.TOTAL_BATCHES_COMMITTED_COUNT shouldEqual totalRecords/upsertBatchSize
342-
}
343-
344280
test("Can save dates to Phoenix using java.sql.Date") {
345281
val date = java.sql.Date.valueOf("2016-09-30")
346282

@@ -703,14 +639,14 @@ class PhoenixSparkDatasourceV1IT extends AbstractPhoenixSparkIT {
703639
.options(Map("table" -> "DATE_TEST", PhoenixDataSource.ZOOKEEPER_URL -> quorumAddress))
704640
.load
705641
val dt = df.select("COL1").first().getDate(0).getTime
706-
val epoch = new Date().getTime
642+
val expected = LocalDate.of(2021,1,1).atStartOfDay(ZoneId.systemDefault()).toInstant.toEpochMilli
707643

708644
// NOTE: Spark DateType drops hour, minute, second, as per the java.sql.Date spec
709645
// Use 'dateAsTimestamp' option to coerce DATE to TIMESTAMP without losing resolution
710646

711647
// Note that Spark also applies the timezone offset to the returned date epoch. Rather than perform timezone
712648
// gymnastics, just make sure we're within 24H of the epoch generated just now
713-
assert(Math.abs(epoch - dt) < 86400000)
649+
assert(expected == dt)
714650

715651
df.createOrReplaceTempView("DATE_TEST")
716652
val df2 = spark.sql("SELECT * FROM DATE_TEST WHERE COL1 > TO_DATE('1990-01-01 00:00:01', 'yyyy-MM-dd HH:mm:ss')")
@@ -773,9 +709,9 @@ class PhoenixSparkDatasourceV1IT extends AbstractPhoenixSparkIT {
773709
.load
774710
val dtRes = df.select("COL1").first()
775711
val ts = dtRes.getTimestamp(0).getTime
776-
val epoch = new Date().getTime
712+
val expected = LocalDate.of(2021, 1, 1).atStartOfDay(ZoneId.systemDefault()).toInstant.toEpochMilli
777713

778-
assert(Math.abs(epoch - ts) < 300000)
714+
assert(expected == ts)
779715
}
780716

781717
test("Can load Phoenix Time columns through DataFrame API") {
@@ -784,8 +720,9 @@ class PhoenixSparkDatasourceV1IT extends AbstractPhoenixSparkIT {
784720
.options(Map("table" -> "TIME_TEST", PhoenixDataSource.ZOOKEEPER_URL -> quorumAddress))
785721
.load
786722
val time = df.select("COL1").first().getTimestamp(0).getTime
787-
val epoch = new Date().getTime
788-
assert(Math.abs(epoch - time) < 86400000)
723+
val expected = LocalDate.of(2021, 1, 1).atStartOfDay(ZoneId.systemDefault()).toInstant.toEpochMilli
724+
725+
assert(expected == time)
789726
}
790727

791728
test("can read all Phoenix data types") {

0 commit comments

Comments
 (0)