Skip to content

Commit 3196f5c

Browse files
wt0530githubgxll
authored andcommitted
[fix][runtime] Construct intervals using table fields
1 parent 2024736 commit 3196f5c

File tree

18 files changed

+265
-132
lines changed

18 files changed

+265
-132
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Copyright 2021 DataCanvas
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.dingodb.expr.common.utils;
18+
19+
import org.checkerframework.checker.nullness.qual.NonNull;
20+
21+
import java.math.BigDecimal;
22+
23+
public class CastWithString {
24+
25+
public static double doubleCastWithStringCompat(String value) {
26+
double result = 0;
27+
try {
28+
//For efficiency.
29+
result = Double.parseDouble(value);
30+
} catch (NumberFormatException e) {
31+
int lastNumberPos = 0;
32+
if (value != null) {
33+
//find the last digit position.
34+
String v = trimDigitString(value);
35+
try {
36+
result = Double.parseDouble(v);
37+
} catch (NumberFormatException e1) {
38+
result = 0;
39+
}
40+
}
41+
}
42+
return result;
43+
}
44+
45+
public static float floatCastWithStringCompat(@NonNull String value) {
46+
float result = 0;
47+
String val = value.trim();
48+
try {
49+
result = Float.parseFloat(val);
50+
} catch (NumberFormatException e) {
51+
int lastNumberPos = 0;
52+
if (value != null) {
53+
//find the last digit position.
54+
String v = trimDigitString(value);
55+
try {
56+
result = Float.parseFloat(v);
57+
} catch (NumberFormatException e1) {
58+
result = 0;
59+
}
60+
}
61+
}
62+
63+
return result;
64+
}
65+
66+
public static @NonNull BigDecimal decimalCastWithStringCompat(@NonNull String value) {
67+
BigDecimal result = null;
68+
String val = value.trim();
69+
try {
70+
result = new BigDecimal(val);
71+
} catch (NumberFormatException e) {
72+
int lastNumberPos = 0;
73+
if (value != null) {
74+
//find the last digit position.
75+
String v = trimDigitString(value);
76+
try {
77+
result = new BigDecimal(v);
78+
} catch (NumberFormatException e1) {
79+
result = new BigDecimal(0);
80+
}
81+
}
82+
}
83+
84+
return result;
85+
}
86+
87+
public static int intCastWithStringCompat(@NonNull String value) {
88+
int result = 0;
89+
String val = value.trim();
90+
try {
91+
result = Integer.parseInt(val);
92+
} catch (NumberFormatException e) {
93+
int lastNumberPos = 0;
94+
if (value != null) {
95+
String v = trimDigitString(value);
96+
try {
97+
result = Integer.parseInt(value.trim());
98+
} catch (NumberFormatException e1) {
99+
result = 0;
100+
}
101+
}
102+
}
103+
104+
return result;
105+
}
106+
107+
public static long longCastWithStringCompat(@NonNull String value) {
108+
long result = 0;
109+
String val = value.trim();
110+
try {
111+
result = Long.parseLong(val);
112+
} catch (NumberFormatException e) {
113+
int lastNumberPos = 0;
114+
if (value != null) {
115+
//find the last digit position.
116+
String v = trimDigitString(value);
117+
try {
118+
result = Long.parseLong(v);
119+
} catch (NumberFormatException e1) {
120+
result = 0;
121+
}
122+
}
123+
}
124+
125+
return result;
126+
}
127+
128+
public static String trimDigitString(String value) {
129+
int lastNumberPos = 0;
130+
int ePos = -1;
131+
132+
value = value.trim();
133+
for (int i = 0; i < value.length(); i++) {
134+
if (Character.isDigit(value.charAt(i))) {
135+
lastNumberPos++;
136+
} else if (value.charAt(i) == '.'
137+
|| value.charAt(i) == 'e'
138+
|| value.charAt(i) == 'E') {
139+
if (ePos != -1) {
140+
break;
141+
} else {
142+
ePos = i;
143+
}
144+
lastNumberPos++;
145+
} else {
146+
break;
147+
}
148+
}
149+
150+
if (lastNumberPos > 0 && lastNumberPos == ePos + 1) {
151+
lastNumberPos--;
152+
}
153+
return value.substring(0, lastNumberPos);
154+
}
155+
}

runtime/src/main/java/io/dingodb/expr/runtime/op/BinaryIntervalOp.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,20 @@
1616

1717
package io.dingodb.expr.runtime.op;
1818

19+
import io.dingodb.expr.common.type.IntervalDayType;
20+
import io.dingodb.expr.common.type.IntervalHourType;
21+
import io.dingodb.expr.common.type.IntervalMinuteType;
22+
import io.dingodb.expr.common.type.IntervalMonthType;
23+
import io.dingodb.expr.common.type.IntervalSecondType;
24+
import io.dingodb.expr.common.type.IntervalType;
25+
import io.dingodb.expr.common.type.IntervalWeekType;
26+
import io.dingodb.expr.common.type.IntervalYearType;
1927
import io.dingodb.expr.common.type.Type;
2028
import org.checkerframework.checker.nullness.qual.NonNull;
2129
import org.checkerframework.checker.nullness.qual.Nullable;
2230

31+
import java.math.BigDecimal;
32+
2333
public abstract class BinaryIntervalOp extends BinaryOp {
2434

2535
private static final long serialVersionUID = -4216390313132926199L;
@@ -29,4 +39,33 @@ public abstract class BinaryIntervalOp extends BinaryOp {
2939
return OpKeys.INTERVAL.keyOf(type0, type1);
3040
}
3141

42+
public static IntervalType buildInterval(BigDecimal value0, IntervalType value1) {
43+
if (value1 instanceof IntervalYearType.IntervalYear) {
44+
return new IntervalYearType.IntervalYear(value0.multiply(new BigDecimal(12)),
45+
((IntervalYearType.IntervalYear) value1).elementType);
46+
} else if (value1 instanceof IntervalMonthType.IntervalMonth) {
47+
return new IntervalMonthType.IntervalMonth(value0, null);
48+
} else if (value1 instanceof IntervalDayType.IntervalDay) {
49+
return new IntervalDayType.IntervalDay(value0.multiply(new BigDecimal(24 * 60 * 60 * 1000)),
50+
((IntervalDayType.IntervalDay) value1).elementType
51+
);
52+
} else if (value1 instanceof IntervalWeekType.IntervalWeek) {
53+
return new IntervalWeekType.IntervalWeek(value0.multiply(new BigDecimal(60 * 60 * 1000)),
54+
((IntervalWeekType.IntervalWeek) value1).elementType
55+
);
56+
} else if (value1 instanceof IntervalHourType.IntervalHour) {
57+
return new IntervalHourType.IntervalHour(value0.multiply(new BigDecimal(60 * 60 * 1000)),
58+
((IntervalHourType.IntervalHour) value1).elementType
59+
);
60+
} else if (value1 instanceof IntervalMinuteType.IntervalMinute) {
61+
return new IntervalHourType.IntervalHour(value0.multiply(new BigDecimal(60 * 1000)),
62+
((IntervalMinuteType.IntervalMinute) value1).elementType
63+
);
64+
} else if (value1 instanceof IntervalSecondType.IntervalSecond) {
65+
return new IntervalSecondType.IntervalSecond(value0.multiply(new BigDecimal(1000)),
66+
((IntervalSecondType.IntervalSecond) value1).elementType
67+
);
68+
}
69+
return null;
70+
}
3271
}

runtime/src/main/java/io/dingodb/expr/runtime/op/OpKeys.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,14 @@ public static final class BinaryOtherOpKeys {
161161
private static final List<Type> types0 = Arrays.asList(
162162
Types.DATE,
163163
Types.TIME,
164-
Types.TIMESTAMP);
164+
Types.TIMESTAMP,
165+
Types.INT,
166+
Types.LONG,
167+
Types.DOUBLE,
168+
Types.FLOAT,
169+
Types.BOOL,
170+
Types.DECIMAL,
171+
Types.NULL);
165172

166173
private static final List<Type> types1 = Arrays.asList(
167174
Types.INTERVAL_YEAR,

runtime/src/main/java/io/dingodb/expr/runtime/op/cast/CastOp.java

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,4 @@ public abstract class CastOp extends UnaryOp {
3333
return "CAST" + getType().toString();
3434
}
3535

36-
public static String trimDigitString(String value) {
37-
int lastNumberPos = 0;
38-
int ePos = -1;
39-
40-
value = value.trim();
41-
for (int i = 0; i < value.length(); i++) {
42-
if (Character.isDigit(value.charAt(i))) {
43-
lastNumberPos++;
44-
} else if (value.charAt(i) == '.'
45-
|| value.charAt(i) == 'e'
46-
|| value.charAt(i) == 'E') {
47-
if (ePos != -1) {
48-
break;
49-
} else {
50-
ePos = i;
51-
}
52-
lastNumberPos++;
53-
} else {
54-
break;
55-
}
56-
}
57-
58-
if (lastNumberPos > 0 && lastNumberPos == ePos + 1) {
59-
lastNumberPos--;
60-
}
61-
return value.substring(0, lastNumberPos);
62-
}
6336
}

runtime/src/main/java/io/dingodb/expr/runtime/op/cast/DecimalCastOp.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package io.dingodb.expr.runtime.op.cast;
1818

19-
import io.dingodb.expr.annotations.Operators;
2019
import io.dingodb.expr.common.type.Type;
2120
import io.dingodb.expr.common.type.Types;
2221
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -78,24 +77,4 @@ public final Type getType() {
7877
return Types.DECIMAL;
7978
}
8079

81-
static @NonNull BigDecimal decimalCastWithStringCompat(@NonNull String value) {
82-
BigDecimal result = null;
83-
String val = value.trim();
84-
try {
85-
result = new BigDecimal(val);
86-
} catch (NumberFormatException e) {
87-
int lastNumberPos = 0;
88-
if (value != null) {
89-
//find the last digit position.
90-
String v = trimDigitString(value);
91-
try {
92-
result = new BigDecimal(v);
93-
} catch (NumberFormatException e1) {
94-
result = new BigDecimal(0);
95-
}
96-
}
97-
}
98-
99-
return result;
100-
}
10180
}

runtime/src/main/java/io/dingodb/expr/runtime/op/cast/DecimalCastOpFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.dingodb.expr.runtime.op.cast;
1818

1919
import io.dingodb.expr.common.type.Types;
20+
import io.dingodb.expr.common.utils.CastWithString;
2021
import io.dingodb.expr.runtime.ExprConfig;
2122
import io.dingodb.expr.runtime.ExprContext;
2223
import io.dingodb.expr.runtime.op.OpKey;
@@ -28,6 +29,8 @@
2829
import java.util.HashMap;
2930
import java.util.Map;
3031

32+
import static io.dingodb.expr.common.utils.CastWithString.decimalCastWithStringCompat;
33+
3134
public class DecimalCastOpFactory extends DecimalCastOp {
3235

3336
private static final long serialVersionUID = -5114883244820122992L;

runtime/src/main/java/io/dingodb/expr/runtime/op/cast/DoubleCastOp.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package io.dingodb.expr.runtime.op.cast;
1818

19-
import io.dingodb.expr.annotations.Operators;
2019
import io.dingodb.expr.common.type.Type;
2120
import io.dingodb.expr.common.type.Types;
2221
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -73,26 +72,6 @@ static double doubleCast(byte[] value) {
7372
return null;
7473
}
7574

76-
static double doubleCastWithStringCompat(String value) {
77-
double result = 0;
78-
try {
79-
//For efficiency.
80-
result = Double.parseDouble(value);
81-
} catch (NumberFormatException e) {
82-
int lastNumberPos = 0;
83-
if (value != null) {
84-
//find the last digit position.
85-
String v = trimDigitString(value);
86-
try {
87-
result = Double.parseDouble(v);
88-
} catch (NumberFormatException e1) {
89-
result = 0;
90-
}
91-
}
92-
}
93-
return result;
94-
}
95-
9675
@Override
9776
public final Type getType() {
9877
return Types.DOUBLE;

runtime/src/main/java/io/dingodb/expr/runtime/op/cast/DoubleCastOpFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.util.HashMap;
2929
import java.util.Map;
3030

31+
import static io.dingodb.expr.common.utils.CastWithString.doubleCastWithStringCompat;
32+
3133
public final class DoubleCastOpFactory extends DoubleCastOp {
3234
private static final long serialVersionUID = -5477208717628123675L;
3335

runtime/src/main/java/io/dingodb/expr/runtime/op/cast/FloatCastOp.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package io.dingodb.expr.runtime.op.cast;
1818

19-
import io.dingodb.expr.annotations.Operators;
2019
import io.dingodb.expr.common.type.Type;
2120
import io.dingodb.expr.common.type.Types;
2221
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -78,24 +77,4 @@ public final Type getType() {
7877
return Types.FLOAT;
7978
}
8079

81-
static float floatCastWithStringCompat(@NonNull String value) {
82-
float result = 0;
83-
String val = value.trim();
84-
try {
85-
result = Float.parseFloat(val);
86-
} catch (NumberFormatException e) {
87-
int lastNumberPos = 0;
88-
if (value != null) {
89-
//find the last digit position.
90-
String v = trimDigitString(value);
91-
try {
92-
result = Float.parseFloat(v);
93-
} catch (NumberFormatException e1) {
94-
result = 0;
95-
}
96-
}
97-
}
98-
99-
return result;
100-
}
10180
}

runtime/src/main/java/io/dingodb/expr/runtime/op/cast/FloatCastOpFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.util.HashMap;
2929
import java.util.Map;
3030

31+
import static io.dingodb.expr.common.utils.CastWithString.floatCastWithStringCompat;
32+
3133
public final class FloatCastOpFactory extends FloatCastOp {
3234
private static final long serialVersionUID = 5390811599799032720L;
3335

0 commit comments

Comments
 (0)