Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TiFlash overflow (#2740) #2741

Open
wants to merge 4 commits into
base: release-2.5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions core/src/test/scala/org/apache/spark/sql/IssueTestSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,33 @@ import org.apache.spark.sql.functions.{col, sum}

class IssueTestSuite extends BaseTiSparkTest {

test("test tiflash overflow in unsigned bigint") {
if (!enableTiFlashTest) {
cancel("tiflash test not enabled")
}
val dbTable = "tispark_test.tiflash_overflow"
tidbStmt.execute(s"drop table if exists $dbTable")
tidbStmt.execute(
s"CREATE TABLE $dbTable (`a` bigint(20) UNSIGNED NOT NULL,`b` bigint(20) UNSIGNED NOT NULL)")
tidbStmt.execute(s"insert into $dbTable values(16717361816800086255, 16717361816800086255)")
tidbStmt.execute(s"ALTER TABLE $dbTable SET TIFLASH REPLICA 1")

Thread.sleep(5 * 1000)

val prev = spark.conf.getOption(TiConfigConst.ISOLATION_READ_ENGINES)
try {
spark.conf
.set(TiConfigConst.ISOLATION_READ_ENGINES, TiConfigConst.TIFLASH_STORAGE_ENGINE)
val df = spark.sql(s"select * from $dbTable")
val row = Row(BigDecimal("16717361816800086255"), BigDecimal("16717361816800086255"))
checkAnswer(df, Seq(row))
} finally {
spark.conf.set(
TiConfigConst.ISOLATION_READ_ENGINES,
prev.getOrElse(TiConfigConst.DEFAULT_STORAGE_ENGINES))
}
}

//https://github.com/pingcap/tispark/issues/2268
test("show rowid in commonhandle") {
spark.sqlContext.setConf(TiConfigConst.SHOW_ROWID, "true")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

import static com.pingcap.tikv.util.MemoryUtil.EMPTY_BYTE_BUFFER_DIRECT;

import com.google.common.primitives.UnsignedLong;
import com.pingcap.tikv.ExtendedDateTime;
import com.pingcap.tikv.codec.Codec.DateCodec;
import com.pingcap.tikv.codec.Codec.DateTimeCodec;
import com.pingcap.tikv.columnar.datatypes.CHType;
import com.pingcap.tikv.types.AbstractDateTimeType;
import com.pingcap.tikv.types.BytesType;
import com.pingcap.tikv.types.DateType;
import com.pingcap.tikv.types.DecimalType;
import com.pingcap.tikv.util.MemoryUtil;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -236,6 +238,10 @@ public double getDouble(int rowId) {
@Override
public BigDecimal getDecimal(int rowId, int precision, int scale) {
long rowIdAddr = (long) rowId * fixedLength + dataAddr;
// avoid unsigned long overflow here
if (type == DecimalType.BIG_INT_DECIMAL) {
return new BigDecimal(UnsignedLong.fromLongBits(this.getLong(rowId)).bigIntegerValue());
}
if (fixedLength == 4) {
return MemoryUtil.getDecimal32(rowIdAddr, scale);
} else if (fixedLength == 8) {
Expand Down