From 0fcc30c9ed20daaad4e7030055a79eaecccfb228 Mon Sep 17 00:00:00 2001 From: shi yuhang <52435083+shiyuhang0@users.noreply.github.com> Date: Sat, 10 Dec 2022 00:35:43 +0800 Subject: [PATCH 1/2] This is an automated cherry-pick of #2598 Signed-off-by: ti-chi-bot --- .../datasource/AutoIncrementSuite.scala | 2 +- .../tispark/test/generator/IndexColumn.scala | 4 +++- .../com/pingcap/tikv/key/CommonHandle.java | 18 ++++++++++++------ .../java/com/pingcap/tikv/types/DateType.java | 11 +++-------- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/core/src/test/scala/com/pingcap/tispark/datasource/AutoIncrementSuite.scala b/core/src/test/scala/com/pingcap/tispark/datasource/AutoIncrementSuite.scala index df05a7ac28..38967595c3 100644 --- a/core/src/test/scala/com/pingcap/tispark/datasource/AutoIncrementSuite.scala +++ b/core/src/test/scala/com/pingcap/tispark/datasource/AutoIncrementSuite.scala @@ -29,7 +29,7 @@ class AutoIncrementSuite extends BaseBatchWriteTest("test_datasource_auto_increm val schema = StructType(List(StructField("j", LongType))) jdbcUpdate( - s"create table $dbtable(i int NOT NULL AUTO_INCREMENT, j int NOT NULL, primary key (i)) SHARD_ROW_ID_BITS=4") + s"create table $dbtable(i int NOT NULL AUTO_INCREMENT, j int NOT NULL, primary key (i)/*T![clustered_index] NONCLUSTERED */) SHARD_ROW_ID_BITS=4") val tiTableInfo = ti.tiSession.getCatalog.getTable(dbPrefix + database, table) assert(!tiTableInfo.isPkHandle) diff --git a/db-random-test/src/main/scala/com/pingcap/tispark/test/generator/IndexColumn.scala b/db-random-test/src/main/scala/com/pingcap/tispark/test/generator/IndexColumn.scala index 4d64e5e1f7..b419044952 100644 --- a/db-random-test/src/main/scala/com/pingcap/tispark/test/generator/IndexColumn.scala +++ b/db-random-test/src/main/scala/com/pingcap/tispark/test/generator/IndexColumn.scala @@ -110,7 +110,9 @@ case class IndexInfo( isUnique: Boolean) { def toString(isClusteredIndex: Boolean): String = { - val clusteredIndexStr = if (isClusteredIndex) " /*T![clustered_index] CLUSTERED */" else "" + val clusteredIndexStr = + if (isClusteredIndex) " /*T![clustered_index] CLUSTERED */" + else "/*T![clustered_index] NONCLUSTERED */" val indexColumnString = indexColumns.mkString("(", ",", ")") if (isPrimary) { s"PRIMARY KEY $indexColumnString$clusteredIndexStr" diff --git a/tikv-client/src/main/java/com/pingcap/tikv/key/CommonHandle.java b/tikv-client/src/main/java/com/pingcap/tikv/key/CommonHandle.java index 7769d93085..d5f606d53c 100644 --- a/tikv-client/src/main/java/com/pingcap/tikv/key/CommonHandle.java +++ b/tikv-client/src/main/java/com/pingcap/tikv/key/CommonHandle.java @@ -18,16 +18,22 @@ import com.pingcap.tikv.codec.Codec; import com.pingcap.tikv.codec.CodecDataInput; import com.pingcap.tikv.codec.CodecDataOutput; +<<<<<<< HEAD:tikv-client/src/main/java/com/pingcap/tikv/key/CommonHandle.java import com.pingcap.tikv.exception.CodecException; import com.pingcap.tikv.types.Converter; +======= +import com.pingcap.tikv.key.Key; +>>>>>>> 47d082c7c (Compatible with TiDB mater (v6.5.0) (#2598)):tikv-client/src/main/java/com/pingcap/tikv/handle/CommonHandle.java import com.pingcap.tikv.types.DataType; import com.pingcap.tikv.types.MySQLType; import com.pingcap.tikv.util.FastByteComparisons; import java.sql.Date; import java.sql.Timestamp; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.TimeZone; import java.util.stream.Collectors; import org.joda.time.Days; import org.joda.time.LocalDate; @@ -69,16 +75,16 @@ public static CommonHandle newCommonHandle( // When indexScan or tableScan, it will pass `Long` object. // It's a compromise here since we don't have a good way to make them consistent. if (data[i] instanceof Date) { - days = Days.daysBetween(new LocalDate(0), new LocalDate(data[i])).getDays(); + days = Days.daysBetween(new LocalDate(1970, 1, 1), new LocalDate(data[i])).getDays(); } else { days = (long) data[i]; } - // Convert to UTC days for row key. - if (Converter.getLocalTimezone().getOffset(0) < 0) { - days += 1; - } - dataTypes[i].encode(cdo, DataType.EncodeType.KEY, new Date((days) * MS_OF_ONE_DAY)); + SimpleDateFormat utcFmt = new SimpleDateFormat("yyyy-MM-dd"); + utcFmt.setTimeZone(TimeZone.getTimeZone("UTC")); + + dataTypes[i].encode( + cdo, DataType.EncodeType.KEY, Date.valueOf(utcFmt.format(days * MS_OF_ONE_DAY))); } else { if (prefixLengthes[i] > 0 && data[i] instanceof String) { String source = (String) data[i]; diff --git a/tikv-client/src/main/java/com/pingcap/tikv/types/DateType.java b/tikv-client/src/main/java/com/pingcap/tikv/types/DateType.java index 1330badc7a..ebc7626576 100644 --- a/tikv-client/src/main/java/com/pingcap/tikv/types/DateType.java +++ b/tikv-client/src/main/java/com/pingcap/tikv/types/DateType.java @@ -29,7 +29,7 @@ import org.joda.time.LocalDate; public class DateType extends AbstractDateTimeType { - private static final LocalDate EPOCH = new LocalDate(0); + private static final LocalDate EPOCH = new LocalDate(1970, 1, 1); public static final DateType DATE = new DateType(MySQLType.TypeDate); public static final MySQLType[] subTypes = new MySQLType[] {MySQLType.TypeDate}; @@ -93,13 +93,8 @@ public String getName() { } public int getDays(LocalDate d) { - // count how many days from EPOCH - int days = Days.daysBetween(EPOCH, d).getDays(); - // if the timezone has negative offset, minus one day. - if (getTimezone().getOffset(0) < 0) { - days -= 1; - } - return days; + // count how many days from EPOCH (UTC) + return Days.daysBetween(EPOCH, d).getDays(); } /** {@inheritDoc} */ From 39ba24b3a8d7e97782a3c50a782688c86fe47d16 Mon Sep 17 00:00:00 2001 From: shi yuhang <52435083+shiyuhang0@users.noreply.github.com> Date: Mon, 12 Dec 2022 11:20:11 +0800 Subject: [PATCH 2/2] Update CommonHandle.java --- .../src/main/java/com/pingcap/tikv/key/CommonHandle.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tikv-client/src/main/java/com/pingcap/tikv/key/CommonHandle.java b/tikv-client/src/main/java/com/pingcap/tikv/key/CommonHandle.java index d5f606d53c..c77943a41e 100644 --- a/tikv-client/src/main/java/com/pingcap/tikv/key/CommonHandle.java +++ b/tikv-client/src/main/java/com/pingcap/tikv/key/CommonHandle.java @@ -18,12 +18,7 @@ import com.pingcap.tikv.codec.Codec; import com.pingcap.tikv.codec.CodecDataInput; import com.pingcap.tikv.codec.CodecDataOutput; -<<<<<<< HEAD:tikv-client/src/main/java/com/pingcap/tikv/key/CommonHandle.java import com.pingcap.tikv.exception.CodecException; -import com.pingcap.tikv.types.Converter; -======= -import com.pingcap.tikv.key.Key; ->>>>>>> 47d082c7c (Compatible with TiDB mater (v6.5.0) (#2598)):tikv-client/src/main/java/com/pingcap/tikv/handle/CommonHandle.java import com.pingcap.tikv.types.DataType; import com.pingcap.tikv.types.MySQLType; import com.pingcap.tikv.util.FastByteComparisons;