Skip to content

Commit 9f6b66c

Browse files
Revert automatic cast feature and corresponding changes.
Signed-off-by: Yury Fridlyand <[email protected]>
1 parent e511e30 commit 9f6b66c

File tree

10 files changed

+30
-115
lines changed

10 files changed

+30
-115
lines changed

core/src/main/java/org/opensearch/sql/data/type/ExprCoreType.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ public enum ExprCoreType implements ExprType {
3131
*/
3232
UNDEFINED,
3333

34+
/**
35+
* Numbers.
36+
*/
37+
BYTE(UNDEFINED),
38+
SHORT(BYTE),
39+
INTEGER(SHORT),
40+
LONG(INTEGER),
41+
FLOAT(LONG),
42+
DOUBLE(FLOAT),
43+
3444
/**
3545
* String.
3646
*/
@@ -51,16 +61,6 @@ public enum ExprCoreType implements ExprType {
5161
DATETIME(STRING),
5262
INTERVAL(UNDEFINED),
5363

54-
/**
55-
* Numbers.
56-
*/
57-
BYTE(UNDEFINED),
58-
SHORT(BYTE),
59-
INTEGER(SHORT, DATE),
60-
LONG(INTEGER),
61-
FLOAT(LONG),
62-
DOUBLE(FLOAT, DATETIME, TIME),
63-
6464
/**
6565
* Struct.
6666
*/

core/src/main/java/org/opensearch/sql/expression/operator/convert/TypeCastOperator.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import static org.opensearch.sql.expression.function.FunctionDSL.impl;
2222
import static org.opensearch.sql.expression.function.FunctionDSL.nullMissingHandling;
2323

24-
import java.time.format.DateTimeFormatter;
2524
import java.util.Arrays;
2625
import java.util.stream.Collectors;
2726
import java.util.stream.Stream;
@@ -106,9 +105,7 @@ private static FunctionResolver castToInt() {
106105
impl(nullMissingHandling(
107106
(v) -> new ExprIntegerValue(v.integerValue())), INTEGER, DOUBLE),
108107
impl(nullMissingHandling(
109-
(v) -> new ExprIntegerValue(v.booleanValue() ? 1 : 0)), INTEGER, BOOLEAN),
110-
impl(nullMissingHandling(
111-
(v) -> new ExprIntegerValue(Double.parseDouble(v.dateValue().format(DateTimeFormatter.ofPattern("uuuuMMdd"))))), INTEGER, DATE)
108+
(v) -> new ExprIntegerValue(v.booleanValue() ? 1 : 0)), INTEGER, BOOLEAN)
112109
);
113110
}
114111

@@ -141,11 +138,7 @@ private static FunctionResolver castToDouble() {
141138
impl(nullMissingHandling(
142139
(v) -> new ExprDoubleValue(v.doubleValue())), DOUBLE, DOUBLE),
143140
impl(nullMissingHandling(
144-
(v) -> new ExprDoubleValue(v.booleanValue() ? 1D : 0D)), DOUBLE, BOOLEAN),
145-
impl(nullMissingHandling(
146-
(v) -> new ExprDoubleValue(Double.parseDouble(v.datetimeValue().format(DateTimeFormatter.ofPattern("uuuuMMddHHmmss.nnnnnn"))))), DOUBLE, DATETIME),
147-
impl(nullMissingHandling(
148-
(v) -> new ExprDoubleValue(Double.parseDouble(v.timeValue().format(DateTimeFormatter.ofPattern("HHmmss.nnnnnn"))))), DOUBLE, TIME)
141+
(v) -> new ExprDoubleValue(v.booleanValue() ? 1D : 0D)), DOUBLE, BOOLEAN)
149142
);
150143
}
151144

core/src/test/java/org/opensearch/sql/analysis/ExpressionAnalyzerTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,6 @@ public void now_like_functions(Function<Expression[], FunctionExpression> functi
582582
function.apply(new Expression[]{DSL.literal(3)}),
583583
AstDSL.function(name, intLiteral(3)));
584584
}
585-
586-
assertAnalyzeEqual(
587-
dsl.add(function.apply(new Expression[]{}), DSL.literal(0)),
588-
AstDSL.function("+", AstDSL.function(name), intLiteral(0))
589-
);
590585
}
591586

592587
protected Expression analyze(UnresolvedExpression unresolvedExpression) {

core/src/test/java/org/opensearch/sql/expression/datetime/NowLikeFunctionTest.java

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,23 @@ private static Stream<Arguments> functionNames() {
3939
var dsl = new DSL(new ExpressionConfig().functionRepository());
4040
return Stream.of(
4141
Arguments.of((Function<Expression[], FunctionExpression>)dsl::now,
42-
"now", DATETIME, true, (Supplier<Temporal>)LocalDateTime::now, "uuuuMMddHHmmss.nnnnnn"),
42+
"now", DATETIME, true, (Supplier<Temporal>)LocalDateTime::now),
4343
Arguments.of((Function<Expression[], FunctionExpression>)dsl::current_timestamp,
44-
"current_timestamp", DATETIME, true, (Supplier<Temporal>)LocalDateTime::now, "uuuuMMddHHmmss.nnnnnn"),
44+
"current_timestamp", DATETIME, true, (Supplier<Temporal>)LocalDateTime::now),
4545
Arguments.of((Function<Expression[], FunctionExpression>)dsl::localtimestamp,
46-
"localtimestamp", DATETIME, true, (Supplier<Temporal>)LocalDateTime::now, "uuuuMMddHHmmss.nnnnnn"),
46+
"localtimestamp", DATETIME, true, (Supplier<Temporal>)LocalDateTime::now),
4747
Arguments.of((Function<Expression[], FunctionExpression>)dsl::localtime,
48-
"localtime", DATETIME, true, (Supplier<Temporal>)LocalDateTime::now, "uuuuMMddHHmmss.nnnnnn"),
48+
"localtime", DATETIME, true, (Supplier<Temporal>)LocalDateTime::now),
4949
Arguments.of((Function<Expression[], FunctionExpression>)dsl::sysdate,
50-
"sysdate", DATETIME, true, (Supplier<Temporal>)LocalDateTime::now, "uuuuMMddHHmmss.nnnnnn"),
50+
"sysdate", DATETIME, true, (Supplier<Temporal>)LocalDateTime::now),
5151
Arguments.of((Function<Expression[], FunctionExpression>)dsl::curtime,
52-
"curtime", TIME, true, (Supplier<Temporal>)LocalTime::now, "HHmmss.nnnnnn"),
52+
"curtime", TIME, true, (Supplier<Temporal>)LocalTime::now),
5353
Arguments.of((Function<Expression[], FunctionExpression>)dsl::current_time,
54-
"current_time", TIME, true, (Supplier<Temporal>)LocalTime::now, "HHmmss.nnnnnn"),
54+
"current_time", TIME, true, (Supplier<Temporal>)LocalTime::now),
5555
Arguments.of((Function<Expression[], FunctionExpression>)dsl::curdate,
56-
"curdate", DATE, false, (Supplier<Temporal>)LocalDate::now, "uuuuMMdd"),
56+
"curdate", DATE, false, (Supplier<Temporal>)LocalDate::now),
5757
Arguments.of((Function<Expression[], FunctionExpression>)dsl::current_date,
58-
"current_date", DATE, false, (Supplier<Temporal>)LocalDate::now, "uuuuMMdd"));
58+
"current_date", DATE, false, (Supplier<Temporal>)LocalDate::now));
5959
}
6060

6161
private ExprCoreType getCastRule(ExprCoreType from) {
@@ -92,8 +92,7 @@ public void the_test(Function<Expression[], FunctionExpression> function,
9292
String name,
9393
ExprCoreType resType,
9494
Boolean hasFsp,
95-
Supplier<Temporal> referenceGetter,
96-
String formatterPattern) {
95+
Supplier<Temporal> referenceGetter) {
9796
// Check return types:
9897
// `func()`
9998
FunctionExpression expr = function.apply(new Expression[]{});
@@ -106,15 +105,9 @@ public void the_test(Function<Expression[], FunctionExpression> function,
106105
expr = function.apply(new Expression[]{DSL.literal(6)});
107106
assertEquals(resType, expr.type());
108107
}
109-
// Check return type of automatic cast:
110-
// `func() + x`
111-
expr = dsl.add(function.apply(new Expression[]{}), DSL.literal(0));
112-
assertEquals(getCastRule(resType), expr.type());
113108

114109
// Check how calculations are precise:
115110
// `func()`
116-
var a = extractValue(function.apply(new Expression[]{}));
117-
var b = referenceGetter.get();
118111
assertTrue(Math.abs(getDiff(
119112
extractValue(function.apply(new Expression[]{})),
120113
referenceGetter.get()
@@ -126,13 +119,5 @@ public void the_test(Function<Expression[], FunctionExpression> function,
126119
referenceGetter.get()
127120
)) <= 1);
128121
}
129-
// `func() + x`
130-
assertTrue(Math.abs(
131-
dsl.add(
132-
function.apply(new Expression[]{}),
133-
DSL.literal(0)
134-
).valueOf(null).doubleValue() -
135-
Double.parseDouble(DateTimeFormatter.ofPattern(formatterPattern).format(referenceGetter.get()))
136-
) <= 1);
137122
}
138123
}

core/src/test/java/org/opensearch/sql/expression/function/WideningTypeRuleTest.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,6 @@ class WideningTypeRuleTest {
6161
.put(STRING, DATE, 1)
6262
.put(STRING, TIME, 1)
6363
.put(STRING, DATETIME, 1)
64-
.put(STRING, INTEGER, 2)
65-
.put(STRING, LONG, 3)
66-
.put(STRING, FLOAT, 4)
67-
.put(STRING, DOUBLE, 2)
68-
69-
.put(DATE, INTEGER, 1)
70-
.put(DATE, LONG, 2)
71-
.put(DATE, FLOAT, 3)
72-
.put(DATE, DOUBLE, 4)
73-
74-
.put(DATETIME, DOUBLE, 1)
75-
.put(TIME, DOUBLE, 1)
76-
7764
.put(UNDEFINED, BYTE, 1)
7865
.put(UNDEFINED, SHORT, 2)
7966
.put(UNDEFINED, INTEGER, 3)

integ-test/src/test/java/org/opensearch/sql/ppl/DateTimeFunctionIT.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,6 @@ private List<ImmutableMap<Object, Object>> nowLikeFunctionsData() {
493493
.put("referenceGetter", (Supplier<Temporal>) LocalDateTime::now)
494494
.put("parser", (BiFunction<CharSequence, DateTimeFormatter, Temporal>) LocalDateTime::parse)
495495
.put("serializationPattern", "uuuu-MM-dd HH:mm:ss")
496-
.put("castPattern", "uuuuMMddHHmmss")
497496
.build(),
498497
ImmutableMap.builder()
499498
.put("name", "current_timestamp")
@@ -503,7 +502,6 @@ private List<ImmutableMap<Object, Object>> nowLikeFunctionsData() {
503502
.put("referenceGetter", (Supplier<Temporal>) LocalDateTime::now)
504503
.put("parser", (BiFunction<CharSequence, DateTimeFormatter, Temporal>) LocalDateTime::parse)
505504
.put("serializationPattern", "uuuu-MM-dd HH:mm:ss")
506-
.put("castPattern", "uuuuMMddHHmmss")
507505
.build(),
508506
ImmutableMap.builder()
509507
.put("name", "localtimestamp")
@@ -513,7 +511,6 @@ private List<ImmutableMap<Object, Object>> nowLikeFunctionsData() {
513511
.put("referenceGetter", (Supplier<Temporal>) LocalDateTime::now)
514512
.put("parser", (BiFunction<CharSequence, DateTimeFormatter, Temporal>) LocalDateTime::parse)
515513
.put("serializationPattern", "uuuu-MM-dd HH:mm:ss")
516-
.put("castPattern", "uuuuMMddHHmmss")
517514
.build(),
518515
ImmutableMap.builder()
519516
.put("name", "localtime")
@@ -523,7 +520,6 @@ private List<ImmutableMap<Object, Object>> nowLikeFunctionsData() {
523520
.put("referenceGetter", (Supplier<Temporal>) LocalDateTime::now)
524521
.put("parser", (BiFunction<CharSequence, DateTimeFormatter, Temporal>) LocalDateTime::parse)
525522
.put("serializationPattern", "uuuu-MM-dd HH:mm:ss")
526-
.put("castPattern", "uuuuMMddHHmmss")
527523
.build(),
528524
ImmutableMap.builder()
529525
.put("name", "sysdate")
@@ -533,7 +529,6 @@ private List<ImmutableMap<Object, Object>> nowLikeFunctionsData() {
533529
.put("referenceGetter", (Supplier<Temporal>) LocalDateTime::now)
534530
.put("parser", (BiFunction<CharSequence, DateTimeFormatter, Temporal>) LocalDateTime::parse)
535531
.put("serializationPattern", "uuuu-MM-dd HH:mm:ss")
536-
.put("castPattern", "uuuuMMddHHmmss")
537532
.build(),
538533
ImmutableMap.builder()
539534
.put("name", "curtime")
@@ -543,7 +538,6 @@ private List<ImmutableMap<Object, Object>> nowLikeFunctionsData() {
543538
.put("referenceGetter", (Supplier<Temporal>) LocalTime::now)
544539
.put("parser", (BiFunction<CharSequence, DateTimeFormatter, Temporal>) LocalTime::parse)
545540
.put("serializationPattern", "HH:mm:ss")
546-
.put("castPattern", "HHmmss")
547541
.build(),
548542
ImmutableMap.builder()
549543
.put("name", "current_time")
@@ -553,7 +547,6 @@ private List<ImmutableMap<Object, Object>> nowLikeFunctionsData() {
553547
.put("referenceGetter", (Supplier<Temporal>) LocalTime::now)
554548
.put("parser", (BiFunction<CharSequence, DateTimeFormatter, Temporal>) LocalTime::parse)
555549
.put("serializationPattern", "HH:mm:ss")
556-
.put("castPattern", "HHmmss")
557550
.build(),
558551
ImmutableMap.builder()
559552
.put("name", "curdate")
@@ -563,7 +556,6 @@ private List<ImmutableMap<Object, Object>> nowLikeFunctionsData() {
563556
.put("referenceGetter", (Supplier<Temporal>) LocalDate::now)
564557
.put("parser", (BiFunction<CharSequence, DateTimeFormatter, Temporal>) LocalDate::parse)
565558
.put("serializationPattern", "uuuu-MM-dd")
566-
.put("castPattern", "uuuuMMdd")
567559
.build(),
568560
ImmutableMap.builder()
569561
.put("name", "current_date")
@@ -573,7 +565,6 @@ private List<ImmutableMap<Object, Object>> nowLikeFunctionsData() {
573565
.put("referenceGetter", (Supplier<Temporal>) LocalDate::now)
574566
.put("parser", (BiFunction<CharSequence, DateTimeFormatter, Temporal>) LocalDate::parse)
575567
.put("serializationPattern", "uuuu-MM-dd")
576-
.put("castPattern", "uuuuMMdd")
577568
.build()
578569
);
579570
}
@@ -602,18 +593,13 @@ public void testNowLikeFunctions() throws IOException {
602593
BiFunction<CharSequence, DateTimeFormatter, Temporal> parser =
603594
(BiFunction<CharSequence, DateTimeFormatter, Temporal>) funcData.get("parser");
604595
String serializationPatternStr = (String) funcData.get("serializationPattern");
605-
String castPatternStr = (String) funcData.get("castPattern");
606596

607597
var serializationPattern = new DateTimeFormatterBuilder()
608598
.appendPattern(serializationPatternStr)
609599
.optionalStart()
610600
.appendFraction(ChronoField.NANO_OF_SECOND, 0, 6, true)
611601
.toFormatter();
612-
var castPattern = new DateTimeFormatterBuilder()
613-
.appendPattern(castPatternStr)
614-
.optionalStart()
615-
.appendFraction(ChronoField.NANO_OF_SECOND, 0, 6, true)
616-
.toFormatter();
602+
617603
Temporal reference = referenceGetter.get();
618604
double delta = 2d; // acceptable time diff, secs
619605
if (reference instanceof LocalDate)
@@ -622,17 +608,16 @@ public void testNowLikeFunctions() throws IOException {
622608

623609
var calls = new ArrayList<String>() {{
624610
add(name + "()");
625-
add(name + "() + 0");
626611
}};
627612
if (hasShortcut)
628613
calls.add(name);
629614
if (hasFsp)
630615
calls.add(name + "(0)");
631616

632-
// Column order is: func(), func() + 0, func, func(0)
633-
// shortcut ^ fsp ^
617+
// Column order is: func(), func, func(0)
618+
// shortcut ^ fsp ^
634619
// Query looks like:
635-
// source=people2 | eval `now()`=now(), `now() + 0`=now() + 0 | fields `now()`, `now() + 0`;
620+
// source=people2 | eval `now()`=now() | fields `now()`;
636621
JSONObject result = executeQuery("source=" + TEST_INDEX_PEOPLE2
637622
+ " | eval " + calls.stream().map(c -> String.format("`%s`=%s", c, c)).collect(Collectors.joining(","))
638623
+ " | fields " + calls.stream().map(c -> String.format("`%s`", c)).collect(Collectors.joining(",")));
@@ -648,9 +633,6 @@ public void testNowLikeFunctions() throws IOException {
648633
assertEquals(0,
649634
getDiff(reference, parser.apply(row.getString(column++), serializationPattern)), delta);
650635

651-
assertEquals(0,
652-
Double.parseDouble(castPattern.format(reference)) - row.getDouble(column++), delta);
653-
654636
if (hasShortcut) {
655637
assertEquals(0,
656638
getDiff(reference, parser.apply(row.getString(column++), serializationPattern)), delta);

integ-test/src/test/java/org/opensearch/sql/sql/ConditionalIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void ifnullWithNullInputTest() {
6262
verifySchema(response,
6363
schema("IFNULL(null, firstname)", "IFNULL1", "keyword"),
6464
schema("IFNULL(firstname, null)", "IFNULL2", "keyword"),
65-
schema("IFNULL(null, null)", "IFNULL3", "keyword"));
65+
schema("IFNULL(null, null)", "IFNULL3", "byte"));
6666
verifyDataRows(response,
6767
rows("Hattie", "Hattie", LITERAL_NULL.value()),
6868
rows( "Elinor", "Elinor", LITERAL_NULL.value())

0 commit comments

Comments
 (0)