1 | /* | |
2 | * Copyright OpenSearch Contributors | |
3 | * SPDX-License-Identifier: Apache-2.0 | |
4 | */ | |
5 | ||
6 | ||
7 | package org.opensearch.sql.ast.expression; | |
8 | ||
9 | import static org.opensearch.sql.expression.function.BuiltinFunctionName.CAST_TO_BOOLEAN; | |
10 | import static org.opensearch.sql.expression.function.BuiltinFunctionName.CAST_TO_BYTE; | |
11 | import static org.opensearch.sql.expression.function.BuiltinFunctionName.CAST_TO_DATE; | |
12 | import static org.opensearch.sql.expression.function.BuiltinFunctionName.CAST_TO_DATETIME; | |
13 | import static org.opensearch.sql.expression.function.BuiltinFunctionName.CAST_TO_DOUBLE; | |
14 | import static org.opensearch.sql.expression.function.BuiltinFunctionName.CAST_TO_FLOAT; | |
15 | import static org.opensearch.sql.expression.function.BuiltinFunctionName.CAST_TO_INT; | |
16 | import static org.opensearch.sql.expression.function.BuiltinFunctionName.CAST_TO_LONG; | |
17 | import static org.opensearch.sql.expression.function.BuiltinFunctionName.CAST_TO_SHORT; | |
18 | import static org.opensearch.sql.expression.function.BuiltinFunctionName.CAST_TO_STRING; | |
19 | import static org.opensearch.sql.expression.function.BuiltinFunctionName.CAST_TO_TIME; | |
20 | import static org.opensearch.sql.expression.function.BuiltinFunctionName.CAST_TO_TIMESTAMP; | |
21 | ||
22 | import com.google.common.collect.ImmutableMap; | |
23 | import java.util.Collections; | |
24 | import java.util.List; | |
25 | import java.util.Locale; | |
26 | import java.util.Map; | |
27 | import lombok.AllArgsConstructor; | |
28 | import lombok.EqualsAndHashCode; | |
29 | import lombok.Getter; | |
30 | import lombok.ToString; | |
31 | import org.opensearch.sql.ast.AbstractNodeVisitor; | |
32 | import org.opensearch.sql.ast.Node; | |
33 | import org.opensearch.sql.data.type.ExprType; | |
34 | import org.opensearch.sql.expression.function.FunctionName; | |
35 | ||
36 | /** | |
37 | * AST node that represents Cast clause. | |
38 | */ | |
39 | @AllArgsConstructor | |
40 | @EqualsAndHashCode(callSuper = false) | |
41 | @Getter | |
42 | @ToString | |
43 | public class Cast extends UnresolvedExpression { | |
44 | ||
45 | private static final Map<String, FunctionName> CONVERTED_TYPE_FUNCTION_NAME_MAP = | |
46 | new ImmutableMap.Builder<String, FunctionName>() | |
47 | .put("string", CAST_TO_STRING.getName()) | |
48 | .put("byte", CAST_TO_BYTE.getName()) | |
49 | .put("short", CAST_TO_SHORT.getName()) | |
50 | .put("int", CAST_TO_INT.getName()) | |
51 | .put("integer", CAST_TO_INT.getName()) | |
52 | .put("long", CAST_TO_LONG.getName()) | |
53 | .put("float", CAST_TO_FLOAT.getName()) | |
54 | .put("double", CAST_TO_DOUBLE.getName()) | |
55 | .put("boolean", CAST_TO_BOOLEAN.getName()) | |
56 | .put("date", CAST_TO_DATE.getName()) | |
57 | .put("time", CAST_TO_TIME.getName()) | |
58 | .put("timestamp", CAST_TO_TIMESTAMP.getName()) | |
59 | .put("datetime", CAST_TO_DATETIME.getName()) | |
60 | .build(); | |
61 | ||
62 | /** | |
63 | * The source expression cast from. | |
64 | */ | |
65 | private final UnresolvedExpression expression; | |
66 | ||
67 | /** | |
68 | * Expression that represents ELSE statement result. | |
69 | */ | |
70 | private final UnresolvedExpression convertedType; | |
71 | ||
72 | /** | |
73 | * Check if the given function name is a cast function or not. | |
74 | * @param name function name | |
75 | * @return true if cast function, otherwise false. | |
76 | */ | |
77 | public static boolean isCastFunction(FunctionName name) { | |
78 |
2
1. isCastFunction : replaced boolean return with false for org/opensearch/sql/ast/expression/Cast::isCastFunction → KILLED 2. isCastFunction : replaced boolean return with true for org/opensearch/sql/ast/expression/Cast::isCastFunction → KILLED |
return CONVERTED_TYPE_FUNCTION_NAME_MAP.containsValue(name); |
79 | } | |
80 | ||
81 | /** | |
82 | * Get the cast function name for a given target data type. | |
83 | * @param targetType target data type | |
84 | * @return cast function name corresponding | |
85 | */ | |
86 | public static FunctionName getCastFunctionName(ExprType targetType) { | |
87 | String type = targetType.typeName().toLowerCase(Locale.ROOT); | |
88 |
1
1. getCastFunctionName : replaced return value with null for org/opensearch/sql/ast/expression/Cast::getCastFunctionName → KILLED |
return CONVERTED_TYPE_FUNCTION_NAME_MAP.get(type); |
89 | } | |
90 | ||
91 | /** | |
92 | * Get the converted type. | |
93 | * | |
94 | * @return converted type | |
95 | */ | |
96 | public FunctionName convertFunctionName() { | |
97 | String type = convertedType.toString().toLowerCase(Locale.ROOT); | |
98 |
1
1. convertFunctionName : negated conditional → KILLED |
if (CONVERTED_TYPE_FUNCTION_NAME_MAP.containsKey(type)) { |
99 |
1
1. convertFunctionName : replaced return value with null for org/opensearch/sql/ast/expression/Cast::convertFunctionName → KILLED |
return CONVERTED_TYPE_FUNCTION_NAME_MAP.get(type); |
100 | } else { | |
101 | throw new IllegalStateException("unsupported cast type: " + type); | |
102 | } | |
103 | } | |
104 | ||
105 | @Override | |
106 | public List<? extends Node> getChild() { | |
107 |
1
1. getChild : replaced return value with Collections.emptyList for org/opensearch/sql/ast/expression/Cast::getChild → NO_COVERAGE |
return Collections.singletonList(expression); |
108 | } | |
109 | ||
110 | @Override | |
111 | public <T, C> T accept(AbstractNodeVisitor<T, C> nodeVisitor, C context) { | |
112 |
1
1. accept : replaced return value with null for org/opensearch/sql/ast/expression/Cast::accept → KILLED |
return nodeVisitor.visitCast(this, context); |
113 | } | |
114 | } | |
Mutations | ||
78 |
1.1 2.2 |
|
88 |
1.1 |
|
98 |
1.1 |
|
99 |
1.1 |
|
107 |
1.1 |
|
112 |
1.1 |