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

Support localdate convert to date in datetype #2780

Merged
merged 8 commits into from
Oct 8, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,30 @@ package com.pingcap.tispark.datatype
import java.sql.{Date, Timestamp}
import java.util.Calendar
import com.pingcap.tispark.datasource.BaseBatchWriteTest
import org.apache.spark.sql.Row
import org.apache.spark.sql.{AnalysisException, Row}
import org.apache.spark.sql.types._
import org.scalatest.Matchers.{be, noException, the}
import org.tikv.common.exception.TiBatchWriteException

class BatchWriteDataTypeSuite extends BaseBatchWriteTest("test_data_type", "test") {

test("Test date type pr 2780") {
val dbtable1 = "test.test_date1"
val dbtable2 = "test.test_date2"
jdbcUpdate(s"drop table if exists $dbtable1")
jdbcUpdate(s"drop table if exists $dbtable2")
jdbcUpdate(s"create table $dbtable1 (dt date)")
jdbcUpdate(s"create table $dbtable2 (dt date)")
jdbcUpdate(s"insert into $dbtable2 values ('2020-01-01')")

noException should be thrownBy spark
.sql(s"insert into $dbtable1 select * from $dbtable2")
.show()
the[AnalysisException] thrownBy spark
.sql(s"insert into $dbtable1 values ('2020-01-01')")
.show()
}

test("Test Read different types") {
jdbcUpdate(s"""
|create table $dbtable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.pingcap.tikv.codec.ExtendedDateTime;
import com.pingcap.tikv.meta.TiColumnInfo.InternalTypeHolder;
import java.sql.Timestamp;
import java.time.Instant;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.tikv.common.exception.ConvertNotSupportException;
Expand Down Expand Up @@ -130,6 +131,8 @@ java.sql.Timestamp convertToMysqlDateTime(Object value) throws ConvertNotSupport
result = new java.sql.Timestamp(((java.sql.Date) value).getTime());
} else if (value instanceof java.sql.Timestamp) {
result = (java.sql.Timestamp) value;
} else if (value instanceof Instant) {
result = java.sql.Timestamp.from((Instant) value);
} else {
throw new ConvertNotSupportException(value.getClass().getName(), this.getClass().getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ private java.sql.Date convertToMysqlDate(Object value) throws ConvertNotSupportE
result = (java.sql.Date) value;
} else if (value instanceof java.sql.Timestamp) {
result = new java.sql.Date(((java.sql.Timestamp) value).getTime());
} else if (value instanceof java.time.LocalDate) {
result = java.sql.Date.valueOf(((java.time.LocalDate) value).toString());
} else {
throw new ConvertNotSupportException(value.getClass().getName(), this.getClass().getName());
}
Expand Down
Loading