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..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 @@ -19,15 +19,16 @@ import com.pingcap.tikv.codec.CodecDataInput; import com.pingcap.tikv.codec.CodecDataOutput; import com.pingcap.tikv.exception.CodecException; -import com.pingcap.tikv.types.Converter; 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 +70,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} */