Skip to content

Commit

Permalink
fixed timestamp conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
zinal committed Dec 22, 2024
1 parent 62ed6d6 commit abeb1f9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<dependency>
<groupId>tech.ydb</groupId>
<artifactId>ydb-sdk-bom</artifactId>
<version>2.3.6</version>
<version>2.3.8-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/tech/ydb/spark/connector/YdbTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.time.format.DateTimeParseException;
import java.util.Map;

import org.apache.spark.sql.catalyst.util.DateTimeUtils;
import org.apache.spark.sql.types.DataType;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.Decimal;
Expand All @@ -32,6 +33,8 @@
*/
public final class YdbTypes implements Serializable {

private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(YdbTypes.class);

private static final long serialVersionUID = 1L;

public static final DataType SPARK_DECIMAL = DataTypes.createDecimalType(38, 10);
Expand Down Expand Up @@ -604,9 +607,8 @@ private Value<?> convertTimestampToYdb(Object v, YdbFieldType t) {
if (v == null) {
return PrimitiveType.Timestamp.makeOptional().emptyValue();
}
if (v instanceof Number) {
return PrimitiveValue.newTimestamp(
Instant.ofEpochMilli(((Number) v).longValue() / 1000L));
if (v instanceof Long) {
return PrimitiveValue.newTimestamp(DateTimeUtils.microsToInstant((Long) v));
}
try {
if (v instanceof String || v instanceof UTF8String) {
Expand All @@ -624,9 +626,8 @@ private Value<?> convertDatetimeToYdb(Object v, YdbFieldType t) {
if (v instanceof java.sql.Timestamp) {
return PrimitiveValue.newDatetime(((java.sql.Timestamp) v).toInstant());
}
if (v instanceof Number) {
return PrimitiveValue.newDatetime(
Instant.ofEpochMilli(((Number) v).longValue() / 1000L));
if (v instanceof Long) {
return PrimitiveValue.newDatetime(DateTimeUtils.microsToInstant((Long) v));
}
try {
if (v instanceof String || v instanceof UTF8String) {
Expand Down

0 comments on commit abeb1f9

Please sign in to comment.