1 | /* | |
2 | * Copyright OpenSearch Contributors | |
3 | * SPDX-License-Identifier: Apache-2.0 | |
4 | */ | |
5 | ||
6 | ||
7 | package org.opensearch.sql.ast.tree; | |
8 | ||
9 | import static org.opensearch.sql.utils.MLCommonsConstants.ACTION; | |
10 | import static org.opensearch.sql.utils.MLCommonsConstants.ALGO; | |
11 | import static org.opensearch.sql.utils.MLCommonsConstants.ASYNC; | |
12 | import static org.opensearch.sql.utils.MLCommonsConstants.CLUSTERID; | |
13 | import static org.opensearch.sql.utils.MLCommonsConstants.KMEANS; | |
14 | import static org.opensearch.sql.utils.MLCommonsConstants.MODELID; | |
15 | import static org.opensearch.sql.utils.MLCommonsConstants.PREDICT; | |
16 | import static org.opensearch.sql.utils.MLCommonsConstants.RCF; | |
17 | import static org.opensearch.sql.utils.MLCommonsConstants.RCF_ANOMALOUS; | |
18 | import static org.opensearch.sql.utils.MLCommonsConstants.RCF_ANOMALY_GRADE; | |
19 | import static org.opensearch.sql.utils.MLCommonsConstants.RCF_SCORE; | |
20 | import static org.opensearch.sql.utils.MLCommonsConstants.RCF_TIME_FIELD; | |
21 | import static org.opensearch.sql.utils.MLCommonsConstants.STATUS; | |
22 | import static org.opensearch.sql.utils.MLCommonsConstants.TASKID; | |
23 | import static org.opensearch.sql.utils.MLCommonsConstants.TRAIN; | |
24 | import static org.opensearch.sql.utils.MLCommonsConstants.TRAINANDPREDICT; | |
25 | ||
26 | import com.google.common.collect.ImmutableList; | |
27 | import java.util.HashMap; | |
28 | import java.util.List; | |
29 | import java.util.Map; | |
30 | import lombok.AllArgsConstructor; | |
31 | import lombok.EqualsAndHashCode; | |
32 | import lombok.Getter; | |
33 | import lombok.RequiredArgsConstructor; | |
34 | import lombok.Setter; | |
35 | import lombok.ToString; | |
36 | import org.opensearch.sql.analysis.TypeEnvironment; | |
37 | import org.opensearch.sql.ast.AbstractNodeVisitor; | |
38 | import org.opensearch.sql.ast.expression.Literal; | |
39 | import org.opensearch.sql.data.type.ExprCoreType; | |
40 | ||
41 | @Getter | |
42 | @Setter | |
43 | @ToString | |
44 | @EqualsAndHashCode(callSuper = true) | |
45 | @RequiredArgsConstructor | |
46 | @AllArgsConstructor | |
47 | public class ML extends UnresolvedPlan { | |
48 | private UnresolvedPlan child; | |
49 | ||
50 | private final Map<String, Literal> arguments; | |
51 | ||
52 | @Override | |
53 | public UnresolvedPlan attach(UnresolvedPlan child) { | |
54 | this.child = child; | |
55 |
1
1. attach : replaced return value with null for org/opensearch/sql/ast/tree/ML::attach → NO_COVERAGE |
return this; |
56 | } | |
57 | ||
58 | @Override | |
59 | public <T, C> T accept(AbstractNodeVisitor<T, C> nodeVisitor, C context) { | |
60 |
1
1. accept : replaced return value with null for org/opensearch/sql/ast/tree/ML::accept → KILLED |
return nodeVisitor.visitML(this, context); |
61 | } | |
62 | ||
63 | @Override | |
64 | public List<UnresolvedPlan> getChild() { | |
65 |
1
1. getChild : replaced return value with Collections.emptyList for org/opensearch/sql/ast/tree/ML::getChild → KILLED |
return ImmutableList.of(this.child); |
66 | } | |
67 | ||
68 | private String getAction() { | |
69 |
1
1. getAction : replaced return value with "" for org/opensearch/sql/ast/tree/ML::getAction → KILLED |
return (String) arguments.get(ACTION).getValue(); |
70 | } | |
71 | ||
72 | /** | |
73 | * Generate the ml output schema. | |
74 | * | |
75 | * @param env the current environment | |
76 | * @return the schema | |
77 | */ | |
78 | public Map<String, ExprCoreType> getOutputSchema(TypeEnvironment env) { | |
79 | switch (getAction()) { | |
80 | case TRAIN: | |
81 |
1
1. getOutputSchema : removed call to org/opensearch/sql/analysis/TypeEnvironment::clearAllFields → SURVIVED |
env.clearAllFields(); |
82 |
1
1. getOutputSchema : replaced return value with Collections.emptyMap for org/opensearch/sql/ast/tree/ML::getOutputSchema → KILLED |
return getTrainOutputSchema(); |
83 | case PREDICT: | |
84 | case TRAINANDPREDICT: | |
85 |
1
1. getOutputSchema : replaced return value with Collections.emptyMap for org/opensearch/sql/ast/tree/ML::getOutputSchema → KILLED |
return getPredictOutputSchema(); |
86 | default: | |
87 | throw new IllegalArgumentException( | |
88 | "Action error. Please indicate train, predict or trainandpredict."); | |
89 | } | |
90 | } | |
91 | ||
92 | /** | |
93 | * Generate the ml predict output schema. | |
94 | * | |
95 | * @return the schema | |
96 | */ | |
97 | public Map<String, ExprCoreType> getPredictOutputSchema() { | |
98 | HashMap<String, ExprCoreType> res = new HashMap<>(); | |
99 |
1
1. getPredictOutputSchema : negated conditional → KILLED |
String algo = arguments.containsKey(ALGO) ? (String) arguments.get(ALGO).getValue() : null; |
100 | switch (algo) { | |
101 | case KMEANS: | |
102 | res.put(CLUSTERID, ExprCoreType.INTEGER); | |
103 | break; | |
104 | case RCF: | |
105 | res.put(RCF_SCORE, ExprCoreType.DOUBLE); | |
106 |
1
1. getPredictOutputSchema : negated conditional → KILLED |
if (arguments.containsKey(RCF_TIME_FIELD)) { |
107 | res.put(RCF_ANOMALY_GRADE, ExprCoreType.DOUBLE); | |
108 | res.put((String) arguments.get(RCF_TIME_FIELD).getValue(), ExprCoreType.TIMESTAMP); | |
109 | } else { | |
110 | res.put(RCF_ANOMALOUS, ExprCoreType.BOOLEAN); | |
111 | } | |
112 | break; | |
113 | default: | |
114 | throw new IllegalArgumentException("Unsupported algorithm: " + algo); | |
115 | } | |
116 |
1
1. getPredictOutputSchema : replaced return value with Collections.emptyMap for org/opensearch/sql/ast/tree/ML::getPredictOutputSchema → KILLED |
return res; |
117 | } | |
118 | ||
119 | /** | |
120 | * Generate the ml train output schema. | |
121 | * | |
122 | * @return the schema | |
123 | */ | |
124 | public Map<String, ExprCoreType> getTrainOutputSchema() { | |
125 |
1
1. getTrainOutputSchema : negated conditional → KILLED |
boolean isAsync = arguments.containsKey(ASYNC) |
126 | ? (boolean) arguments.get(ASYNC).getValue() : false; | |
127 | Map<String, ExprCoreType> res = new HashMap<>(Map.of(STATUS, ExprCoreType.STRING)); | |
128 |
1
1. getTrainOutputSchema : negated conditional → KILLED |
if (isAsync) { |
129 | res.put(TASKID, ExprCoreType.STRING); | |
130 | } else { | |
131 | res.put(MODELID, ExprCoreType.STRING); | |
132 | } | |
133 |
1
1. getTrainOutputSchema : replaced return value with Collections.emptyMap for org/opensearch/sql/ast/tree/ML::getTrainOutputSchema → KILLED |
return res; |
134 | } | |
135 | } | |
Mutations | ||
55 |
1.1 |
|
60 |
1.1 |
|
65 |
1.1 |
|
69 |
1.1 |
|
81 |
1.1 |
|
82 |
1.1 |
|
85 |
1.1 |
|
99 |
1.1 |
|
106 |
1.1 |
|
116 |
1.1 |
|
125 |
1.1 |
|
128 |
1.1 |
|
133 |
1.1 |