forked from pingcap/tispark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding pk and unique index test for batch write (pingcap#1049)
- Loading branch information
Showing
12 changed files
with
180 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
core/src/test/scala/org/apache/spark/sql/insertion/BatchWritePKAndUniqueIndexSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.apache.spark.sql.insertion | ||
|
||
import com.pingcap.tikv.meta.TiColumnInfo | ||
import com.pingcap.tispark.datasource.BaseDataSourceTest | ||
import com.pingcap.tispark.utils.TiUtil | ||
import org.apache.spark.sql.Row | ||
import org.apache.spark.sql.test.generator.DataType.ReflectedDataType | ||
import org.apache.spark.sql.test.generator.Schema | ||
import org.apache.spark.sql.test.generator.TestDataGenerator._ | ||
|
||
class BatchWritePKAndUniqueIndexSuite | ||
extends BaseDataSourceTest( | ||
"batch_write_insertion_pk_and_one_unique_index", | ||
"batch_write_test_index" | ||
) | ||
with EnumerateUniqueIndexDataTypeTestAction { | ||
// TODO: support binary insertion. | ||
override val dataTypes: List[ReflectedDataType] = integers ::: decimals ::: doubles ::: charCharset | ||
override val unsignedDataTypes: List[ReflectedDataType] = integers ::: decimals ::: doubles | ||
override val database = "batch_write_test_pk_and_index" | ||
override val testDesc = "Test for pk and unique index type in batch-write insertion" | ||
|
||
override def beforeAll(): Unit = { | ||
super.beforeAll() | ||
tidbStmt.execute(s"drop database if exists $database") | ||
tidbStmt.execute(s"create database $database") | ||
} | ||
|
||
private def tiRowToSparkRow(row: TiRow, tiColsInfos: java.util.List[TiColumnInfo]) = { | ||
val sparkRow = new Array[Any](row.fieldCount()) | ||
for (i <- 0 until row.fieldCount()) { | ||
val colTp = tiColsInfos.get(i).getType | ||
val colVal = row.get(i, colTp) | ||
sparkRow(i) = colVal | ||
} | ||
Row.fromSeq(sparkRow) | ||
} | ||
|
||
private def dropAndCreateTbl(schema: Schema): Unit = { | ||
// drop table if exits | ||
dropTable(schema.tableName) | ||
|
||
// create table in tidb first | ||
jdbcUpdate(schema.toString) | ||
} | ||
|
||
private def insertAndSelect(schema: Schema): Unit = { | ||
val tblName = schema.tableName | ||
|
||
val tiTblInfo = getTableInfo(database, tblName) | ||
val tiColInfos = tiTblInfo.getColumns | ||
// gen data | ||
val rows = | ||
generateRandomRows(schema, rowCount, r).map(row => tiRowToSparkRow(row, tiColInfos)) | ||
// insert data to tikv | ||
tidbWriteWithTable(rows, TiUtil.getSchemaFromTable(tiTblInfo), tblName) | ||
// select data from tikv and compare with tidb | ||
compareTiDBSelectWithJDBCWithTable_V2(tblName = tblName, "col_bigint") | ||
} | ||
|
||
test("test pk and unique indices cases") { | ||
val schemas = genSchema(dataTypes, table) | ||
|
||
schemas.foreach { schema => | ||
dropAndCreateTbl(schema) | ||
} | ||
|
||
schemas.foreach { schema => | ||
insertAndSelect(schema) | ||
} | ||
} | ||
|
||
// this is only for mute the warning | ||
override def test(): Unit = {} | ||
|
||
override def afterAll(): Unit = | ||
try { | ||
dropTable() | ||
} finally { | ||
super.afterAll() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...st/scala/org/apache/spark/sql/insertion/EnumeratePKAndUniqueIndexDataTypeTestAction.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.apache.spark.sql.insertion | ||
|
||
import org.apache.commons.math3.util.Combinations | ||
import org.apache.spark.sql.test.generator.DataType.ReflectedDataType | ||
import org.apache.spark.sql.test.generator.{DefaultColumn, Index, IndexColumn, Key, PrefixColumn, PrimaryKey} | ||
import org.apache.spark.sql.test.generator.TestDataGenerator.isStringType | ||
|
||
import scala.util.Random | ||
|
||
trait EnumeratePKAndUniqueIndexDataTypeTestAction extends BaseEnumerateDataTypesTestSpec { | ||
private def genPk(dataTypes: List[ReflectedDataType], r: Random): List[Index] = { | ||
val size = dataTypes.length | ||
val keyList = scala.collection.mutable.ListBuffer.empty[PrimaryKey] | ||
for (i <- 0 until size) { | ||
// we add extra one to the column id since 1 is reserved to primary key | ||
val pkCol = if (isStringType(dataTypes(i))) { | ||
PrefixColumn(i + 1, r.nextInt(4) + 2) :: Nil | ||
} else { | ||
DefaultColumn(i + 1) :: Nil | ||
} | ||
keyList += PrimaryKey(pkCol) | ||
} | ||
keyList.toList | ||
} | ||
|
||
private def genUniqueIndex(dataTypes: List[ReflectedDataType], r: Random): List[Index] = { | ||
val size = dataTypes.length | ||
// the first step is generate all possible keys | ||
val keyList = scala.collection.mutable.ListBuffer.empty[Key] | ||
for (i <- 1 until 3) { | ||
val combination = new Combinations(size, i) | ||
//(i, size) | ||
val iterator = combination.iterator() | ||
while (iterator.hasNext) { | ||
val intArray = iterator.next() | ||
val indexColumnList = scala.collection.mutable.ListBuffer.empty[IndexColumn] | ||
// index may have multiple column | ||
for (j <- 0 until intArray.length) { | ||
// we add extra one to the column id since 1 is reserved to primary key | ||
if (isStringType(dataTypes(intArray(j)))) { | ||
indexColumnList += PrefixColumn(intArray(j) + 1, r.nextInt(4) + 2) | ||
} else { | ||
indexColumnList += DefaultColumn(intArray(j) + 1) | ||
} | ||
} | ||
|
||
keyList += Key(indexColumnList.toList) | ||
} | ||
} | ||
|
||
keyList.toList | ||
} | ||
|
||
override def genIndex(dataTypes: List[ReflectedDataType], r: Random): List[List[Index]] = { | ||
val pkIdxList = genPk(dataTypes, r) | ||
val uniqueIdxList = genUniqueIndex(dataTypes, r) | ||
val constraints = scala.collection.mutable.ListBuffer.empty[List[Index]] | ||
for (i <- pkIdxList.indices) { | ||
val tmpIdxList = scala.collection.mutable.ListBuffer.empty[Index] | ||
for (j <- uniqueIdxList.indices) { | ||
tmpIdxList += pkIdxList(i) | ||
tmpIdxList += uniqueIdxList(j) | ||
} | ||
constraints += tmpIdxList.toList | ||
} | ||
constraints.toList | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
core/src/test/scala/org/apache/spark/sql/types/DataTypeTestDir.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.apache.spark.sql.types | ||
|
||
trait DataTypeTestDir { | ||
val dataTypeTestDir: String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters