Skip to content

Commit 56973eb

Browse files
committed
format and minor refine
Committed-by: bingqing.lbq from Dev container Committed-by: bingqing.lbq from Dev container Committed-by: bingqing.lbq from Dev container Committed-by: bingqing.lbq from Dev container
1 parent 98d01a1 commit 56973eb

File tree

2 files changed

+74
-59
lines changed

2 files changed

+74
-59
lines changed

interactive_engine/common/src/main/java/com/alibaba/graphscope/groot/common/schema/wrapper/PropertyValue.java

Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -74,65 +74,11 @@ private static Object stringToObj(DataType dataType, Object val) {
7474
case STRING:
7575
return valString;
7676
case DATE:
77-
{
78-
try {
79-
return Integer.valueOf(valString);
80-
} catch (Exception e) {
81-
try {
82-
LocalDate date = LocalDate.parse(valString, DateTimeFormatter.ISO_LOCAL_DATE);
83-
long epochDays = date.toEpochDay();
84-
return Integer.valueOf((int) epochDays);
85-
} catch (Exception e1) {
86-
throw new InvalidArgumentException("unable to parse date string to date. DataType ["
87-
+ dataType
88-
+ "], Object ["
89-
+ valString
90-
+ "], class ["
91-
+ valString.getClass()
92-
+ "]", e1);
93-
}
94-
}
95-
}
77+
return parseDate(valString);
9678
case TIME32:
97-
{
98-
try {
99-
return Integer.valueOf(valString);
100-
} catch (Exception e) {
101-
try {
102-
LocalTime time = LocalTime.parse(valString, DateTimeFormatter.ISO_LOCAL_TIME);
103-
int seconds = time.toSecondOfDay();
104-
return Integer.valueOf(seconds);
105-
} catch (Exception e1) {
106-
throw new InvalidArgumentException("unable to parse time32 string to int. DataType ["
107-
+ dataType
108-
+ "], Object ["
109-
+ valString
110-
+ "], class ["
111-
+ valString.getClass()
112-
+ "]", e1);
113-
}
114-
}
115-
}
79+
return parseTime32(valString);
11680
case TIMESTAMP:
117-
{
118-
try {
119-
return Long.valueOf(valString);
120-
} catch (Exception e) {
121-
try {
122-
LocalDateTime dateTime = LocalDateTime.parse(valString, DateTimeFormatter.ISO_DATE_TIME);
123-
long millis = dateTime.toEpochSecond(ZoneOffset.UTC) * 1000;
124-
return Long.valueOf(millis);
125-
} catch (Exception e1) {
126-
throw new InvalidArgumentException("unable to parse timestamp string to long. DataType ["
127-
+ dataType
128-
+ "], Object ["
129-
+ valString
130-
+ "], class ["
131-
+ valString.getClass()
132-
+ "]", e1);
133-
}
134-
}
135-
}
81+
return parseTimestamp(valString);
13682
default:
13783
throw new IllegalStateException("Unexpected value: " + dataType);
13884
}
@@ -149,6 +95,65 @@ private static Object stringToObj(DataType dataType, Object val) {
14995
}
15096
}
15197

98+
private static Integer parseDate(String valString) {
99+
try {
100+
return Integer.valueOf(valString);
101+
} catch (Exception e) {
102+
try {
103+
LocalDate date = LocalDate.parse(valString, DateTimeFormatter.ISO_LOCAL_DATE);
104+
long epochDays = date.toEpochDay();
105+
return (int) epochDays;
106+
} catch (Exception e1) {
107+
throw new InvalidArgumentException(
108+
"Unable to parse date string to date. Object ["
109+
+ valString
110+
+ "], class ["
111+
+ valString.getClass()
112+
+ "].",
113+
e1);
114+
}
115+
}
116+
}
117+
118+
private static Integer parseTime32(String valString) {
119+
try {
120+
return Integer.valueOf(valString);
121+
} catch (Exception e) {
122+
try {
123+
LocalTime time = LocalTime.parse(valString, DateTimeFormatter.ISO_LOCAL_TIME);
124+
return time.toSecondOfDay();
125+
} catch (Exception e1) {
126+
throw new InvalidArgumentException(
127+
"Unable to parse time32 string to int. Object ["
128+
+ valString
129+
+ "], class ["
130+
+ valString.getClass()
131+
+ "].",
132+
e1);
133+
}
134+
}
135+
}
136+
137+
private static Long parseTimestamp(String valString) {
138+
try {
139+
return Long.valueOf(valString);
140+
} catch (Exception e) {
141+
try {
142+
LocalDateTime dateTime =
143+
LocalDateTime.parse(valString, DateTimeFormatter.ISO_DATE_TIME);
144+
return dateTime.toEpochSecond(ZoneOffset.UTC) * 1000;
145+
} catch (Exception e1) {
146+
throw new InvalidArgumentException(
147+
"Unable to parse timestamp string to long. Object ["
148+
+ valString
149+
+ "], class ["
150+
+ valString.getClass()
151+
+ "].",
152+
e1);
153+
}
154+
}
155+
}
156+
152157
public static PropertyValue parseProto(PropertyValuePb proto) {
153158
try {
154159
DataType dataType = DataType.parseProto(proto.getDataType());

interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/meta/schema/IrDataTypeConvertor.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.slf4j.Logger;
3737
import org.slf4j.LoggerFactory;
3838

39+
import java.math.BigDecimal;
3940
import java.util.Map;
4041
import java.util.Objects;
4142

@@ -45,6 +46,15 @@
4546
public interface IrDataTypeConvertor<T> {
4647
Logger logger = LoggerFactory.getLogger(IrDataTypeConvertor.class);
4748

49+
// support unsigned type as decimal type with fixed precision and scale
50+
int UINT32_PRECISION = 10;
51+
int UINT32_SCALE = 0;
52+
int UINT64_PRECISION = 20;
53+
int UINT64_SCALE = 0;
54+
55+
BigDecimal UINT32_MAX = new BigDecimal("4294967295");
56+
BigDecimal UINT64_MAX = new BigDecimal("18446744073709551615");
57+
4858
RelDataType convert(T dataFrom);
4959

5060
T convert(RelDataType dataFrom);
@@ -80,14 +90,14 @@ public RelDataType convert(DataType from) {
8090
case UINT:
8191
// 4-bytes unsigned integer
8292
return typeFactory.createSqlType(
83-
SqlTypeName.DECIMAL, UINT32_PRECISION, UINT32_SCALE);
93+
SqlTypeName.DECIMAL, UINT32_PRECISION, UINT32_SCALE);
8494
case LONG:
8595
// 8-bytes signed integer
8696
return typeFactory.createSqlType(SqlTypeName.BIGINT);
8797
case ULONG:
8898
// 8-bytes unsigned integer
8999
return typeFactory.createSqlType(
90-
SqlTypeName.DECIMAL, UINT64_PRECISION, UINT64_SCALE);
100+
SqlTypeName.DECIMAL, UINT64_PRECISION, UINT64_SCALE);
91101
case FLOAT:
92102
// single precision floating point, 4 bytes
93103
return typeFactory.createSqlType(SqlTypeName.FLOAT);

0 commit comments

Comments
 (0)