You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2023/08/16 08:44:29 UTC
[skywalking] branch master updated: Support `Compare Operation` in MQE (#11226)
This is an automated email from the ASF dual-hosted git repository.
wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking.git
The following commit(s) were added to refs/heads/master by this push:
new 5491c04a8a Support `Compare Operation` in MQE (#11226)
5491c04a8a is described below
commit 5491c04a8a633e20cc70e3f57e8ecec0f47a2881
Author: Wan Kai <wa...@foxmail.com>
AuthorDate: Wed Aug 16 16:44:23 2023 +0800
Support `Compare Operation` in MQE (#11226)
---
docs/en/api/metrics-query-expression.md | 71 +++++-
docs/en/changes/changes.md | 17 +-
.../apache/skywalking/mqe/rt/grammar/MQEParser.g4 | 1 +
.../apache/skywalking/mqe/rt/MQEVisitorBase.java | 30 +++
.../skywalking/mqe/rt/operation/BinaryOp.java | 264 +--------------------
.../skywalking/mqe/rt/operation/CompareOp.java | 64 +++++
.../mqe/rt/operation/{BinaryOp.java => LROp.java} | 114 ++++-----
.../skywalking/mqe/rt/type/ExpressionResult.java | 1 +
.../apache/skywalking/mqe/rt/CompareOPTest.java | 248 +++++++++++++++++++
test/e2e-v2/cases/mqe/expected/compare-OP.yml | 31 +++
test/e2e-v2/cases/mqe/mqe-cases.yaml | 6 +-
11 files changed, 508 insertions(+), 339 deletions(-)
diff --git a/docs/en/api/metrics-query-expression.md b/docs/en/api/metrics-query-expression.md
index b171a671c4..6c5798c3fa 100644
--- a/docs/en/api/metrics-query-expression.md
+++ b/docs/en/api/metrics-query-expression.md
@@ -48,7 +48,7 @@ If we want to rename the label values to `P50,P75,P90,P95,P99`, see [Relabel Ope
The `ExpressionResultType` of the expression is `TIME_SERIES_VALUES` and with labels.
## Binary Operation
-The binary Operation is an operation that takes two expressions and performs a calculation on their results.
+The Binary Operation is an operation that takes two expressions and performs a calculation on their results.
The following table lists the binary operations supported by MQE.
Expression:
@@ -87,6 +87,75 @@ The expression could be on the left or right side of the operator.
| TIME_SERIES_VALUES | SORTED_LIST/RECORD_LIST | no | |
| SORTED_LIST/RECORD_LIST | SORTED_LIST/RECORD_LIST | no | |
+## Compare Operation
+Compare Operation takes two expressions and compares their results.
+The following table lists the compare operations supported by MQE.
+
+Expression:
+```text
+Expression1 <Compare-Operator> Expression2
+```
+
+| Operator | Definition |
+|----------|-----------------------|
+| \> | greater than |
+| \>= | greater than or equal |
+| < | less than |
+| <= | less than or equal |
+| == | equal |
+| != | not equal |
+
+The result of the compare operation is an **int value**:
+* 1: true
+* 0: false
+
+For example:
+Compare the `service_resp_time` metric value if greater than 3000, if the `service_resp_time` result is:
+```json
+{
+ "data": {
+ "execExpression": {
+ "type": "TIME_SERIES_VALUES",
+ "error": null,
+ "results": [
+ {
+ "metric": {
+ "labels": []
+ },
+ "values": [{"id": "1691658000000", "value": "2500", "traceID": null}, {"id": "1691661600000", "value": 3500, "traceID": null}]
+ }
+ ]
+ }
+ }
+}
+```
+we can use the following expression:
+```text
+service_resp_time > 3000
+```
+and get result:
+```json
+{
+ "data": {
+ "execExpression": {
+ "type": "TIME_SERIES_VALUES",
+ "error": null,
+ "results": [
+ {
+ "metric": {
+ "labels": []
+ },
+ "values": [{"id": "1691658000000", "value": "0", "traceID": null}, {"id": "1691661600000", "value": 1, "traceID": null}]
+ }
+ ]
+ }
+ }
+}
+```
+
+### Compare Operation Rules and Result Type
+Same as the [Binary Operation Rules](#binary-operation-rules).
+
## Aggregation Operation
Aggregation Operation takes an expression and performs aggregate calculations on its results.
diff --git a/docs/en/changes/changes.md b/docs/en/changes/changes.md
index 16b41fafd8..6b9267c7d7 100644
--- a/docs/en/changes/changes.md
+++ b/docs/en/changes/changes.md
@@ -48,9 +48,7 @@
* Cache all service entity in memory for query.
* Bump up jackson version to 2.15.2.
* Increase the default memory size to avoid OOM.
-* Apply MQE on `MYSQL`, `POSTGRESQL`, `REDIS`, `ELASTICSEARCH` and `DYNAMODB` layer UI-templates.
* Bump up graphql-java to 21.0.
-* Apply MQE on Virtual-Cache layer UI-templates
* Add Echo component ID(5015) language: Golang.
* Fix `index out of bounds exception` in `aggregate_labels` MQE function.
* Support MongoDB Server/Cluster monitoring powered by OTEL.
@@ -60,24 +58,19 @@
* Add component ID for Lock (ID=5016).
* [Breaking change] Adjust the structure of hooks in the `alarm-settings.yml`. Support multiple configs for each hook types and specifying the hooks in the alarm rule.
* Bump up Armeria to 1.24.3.
-* Apply MQE on APISIX, AWS_EKS, AWS_GATEWAY and AWS_S3 layer UI templates.
* Fix BooleanMatch and BooleanNotEqualMatch doing Boolean comparison.
* Support LogQL HTTP query APIs.
* Add Mux Server component ID(5017) language: Golang.
* Remove ElasticSearch 6.3.2 from our client lib tests.
* Bump up ElasticSearch server 8.8.1 to 8.9.0 for latest e2e testing. 8.1.0, 7.16.3 and 7.17.10 are still tested.
* Add OpenSearch 2.8.0 to our client lib tests.
-* Apply MQE on RabbitMQ Dashboards.
* Use listening mode for apollo implementation of dynamic configuration.
* Add `view_as_seq` function in MQE for listing metrics in the given prioritized sequence.
-* Apply MQE on Virtual-MQ layer UI-templates
-* Apply MQE on Infra-Linux layer UI-templates
* Fix the wrong default value of `k8sServiceNameRule` if it's not explicitly set.
-* Apply MQE on Infra-Windows layer UI-templates
* Improve PromQL to allow for multiple metric operations within a single query.
* Fix MQE Binary Operation between labeled metrics and other type of value result.
* Add component ID for Nacos (ID=150).
-* Apply MQE on Browser layer UI-templates.
+* Support `Compare Operation` in MQE.
#### UI
@@ -96,6 +89,14 @@
* Fix service associates instances.
* Remove node-sass.
* Fix commit error on Windows.
+* Apply MQE on `MYSQL`, `POSTGRESQL`, `REDIS`, `ELASTICSEARCH` and `DYNAMODB` layer UI-templates.
+* Apply MQE on Virtual-Cache layer UI-templates
+* Apply MQE on APISIX, AWS_EKS, AWS_GATEWAY and AWS_S3 layer UI templates.
+* Apply MQE on RabbitMQ Dashboards.
+* Apply MQE on Virtual-MQ layer UI-templates
+* Apply MQE on Infra-Linux layer UI-templates
+* Apply MQE on Infra-Windows layer UI-templates
+* Apply MQE on Browser layer UI-templates.
#### Documentation
diff --git a/oap-server/mqe-grammar/src/main/antlr4/org/apache/skywalking/mqe/rt/grammar/MQEParser.g4 b/oap-server/mqe-grammar/src/main/antlr4/org/apache/skywalking/mqe/rt/grammar/MQEParser.g4
index 6851155970..0c2d9a9317 100644
--- a/oap-server/mqe-grammar/src/main/antlr4/org/apache/skywalking/mqe/rt/grammar/MQEParser.g4
+++ b/oap-server/mqe-grammar/src/main/antlr4/org/apache/skywalking/mqe/rt/grammar/MQEParser.g4
@@ -24,6 +24,7 @@ root: expression;
expression
: expressionNode # exprNode
+ | L_PAREN expression R_PAREN # parensOp
| expression mulDivMod expression # mulDivModOp
| expression addSub expression # addSubOp
| expression compare expression # compareOp
diff --git a/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/MQEVisitorBase.java b/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/MQEVisitorBase.java
index aa5c5d0277..817d29d757 100644
--- a/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/MQEVisitorBase.java
+++ b/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/MQEVisitorBase.java
@@ -27,6 +27,7 @@ import org.apache.skywalking.mqe.rt.grammar.MQEParserBaseVisitor;
import org.apache.skywalking.mqe.rt.operation.AggregateLabelsOp;
import org.apache.skywalking.mqe.rt.operation.AggregationOp;
import org.apache.skywalking.mqe.rt.operation.BinaryOp;
+import org.apache.skywalking.mqe.rt.operation.CompareOp;
import org.apache.skywalking.mqe.rt.operation.LogicalFunctionOp;
import org.apache.skywalking.mqe.rt.operation.MathematicalFunctionOp;
import org.apache.skywalking.mqe.rt.type.ExpressionResult;
@@ -39,6 +40,10 @@ import org.apache.skywalking.oap.server.library.util.StringUtil;
@Slf4j
public abstract class MQEVisitorBase extends MQEParserBaseVisitor<ExpressionResult> {
+ @Override
+ public ExpressionResult visitParensOp(MQEParser.ParensOpContext ctx) {
+ return visit(ctx.expression());
+ }
@Override
public ExpressionResult visitAddSubOp(MQEParser.AddSubOpContext ctx) {
@@ -218,6 +223,31 @@ public abstract class MQEVisitorBase extends MQEParserBaseVisitor<ExpressionResu
}
}
+ @Override
+ public ExpressionResult visitCompareOp(MQEParser.CompareOpContext ctx) {
+ ExpressionResult left = visit(ctx.expression(0));
+ if (StringUtil.isNotBlank(left.getError())) {
+ return left;
+ }
+ ExpressionResult right = visit(ctx.expression(1));
+ if (StringUtil.isNotBlank(right.getError())) {
+ return right;
+ }
+ int opType = ctx.compare().getStart().getType();
+ try {
+ ExpressionResult result = CompareOp.doCompareOP(left, right, opType);
+ if (ctx.parent == null) {
+ result.setBoolResult(true);
+ }
+ return result;
+ } catch (IllegalExpressionException e) {
+ ExpressionResult result = new ExpressionResult();
+ result.setType(ExpressionResultType.UNKNOWN);
+ result.setError(e.getMessage());
+ return result;
+ }
+ }
+
@Override
public abstract ExpressionResult visitMetric(MQEParser.MetricContext ctx);
}
diff --git a/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/operation/BinaryOp.java b/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/operation/BinaryOp.java
index f3d4680562..00bfec134f 100644
--- a/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/operation/BinaryOp.java
+++ b/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/operation/BinaryOp.java
@@ -18,38 +18,19 @@
package org.apache.skywalking.mqe.rt.operation;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
import org.apache.skywalking.mqe.rt.exception.IllegalExpressionException;
import org.apache.skywalking.mqe.rt.grammar.MQEParser;
import org.apache.skywalking.mqe.rt.type.ExpressionResult;
-import org.apache.skywalking.mqe.rt.type.ExpressionResultType;
-import org.apache.skywalking.mqe.rt.type.MQEValue;
-import org.apache.skywalking.mqe.rt.type.MQEValues;
-import org.apache.skywalking.oap.server.core.query.type.KeyValue;
public class BinaryOp {
public static ExpressionResult doBinaryOp(ExpressionResult left,
ExpressionResult right,
int opType) throws IllegalExpressionException {
- if (left.getType() == ExpressionResultType.SINGLE_VALUE && right.getType() == ExpressionResultType.SINGLE_VALUE) {
- return single2SingleBinaryOp(left, right, opType);
- } else if ((left.getType() == ExpressionResultType.TIME_SERIES_VALUES ||
- left.getType() == ExpressionResultType.SORTED_LIST ||
- left.getType() == ExpressionResultType.RECORD_LIST)
- && right.getType() == ExpressionResultType.SINGLE_VALUE) {
- return many2OneBinaryOp(left, right, opType);
- } else if (left.getType() == ExpressionResultType.SINGLE_VALUE &&
- (right.getType() == ExpressionResultType.TIME_SERIES_VALUES ||
- right.getType() == ExpressionResultType.SORTED_LIST ||
- right.getType() == ExpressionResultType.RECORD_LIST)) {
- return one2ManyBinaryOp(left, right, opType);
- } else if (left.getType() == ExpressionResultType.TIME_SERIES_VALUES && right.getType() == ExpressionResultType.TIME_SERIES_VALUES) {
- return seriesBinaryOp(left, right, opType);
+ try {
+ return LROp.doLROp(left, right, opType, BinaryOp::scalarBinaryOp);
+ } catch (IllegalExpressionException e) {
+ throw new IllegalExpressionException("Unsupported binary operation.");
}
-
- throw new IllegalExpressionException("Unsupported binary operation.");
}
//scalar with scalar
@@ -74,241 +55,4 @@ public class BinaryOp {
}
return calculatedResult;
}
-
- //series with series
- private static ExpressionResult seriesBinaryOp(ExpressionResult seriesLeft,
- ExpressionResult seriesRight,
- int opType) throws IllegalExpressionException {
- ExpressionResult result = new ExpressionResult();
- if (!seriesLeft.isLabeledResult() && !seriesRight.isLabeledResult()) { // no labeled with no labeled
- result = seriesNoLabeled(seriesLeft, seriesRight, opType);
- } else if (seriesLeft.isLabeledResult() && !seriesRight.isLabeledResult()) { // labeled with no labeled
- result = seriesLabeledWithNoLabeled(seriesLeft, seriesRight, opType);
- } else if (!seriesLeft.isLabeledResult() && seriesRight.isLabeledResult()) { // no labeled with labeled
- result = seriesNoLabeledWithLabeled(seriesLeft, seriesRight, opType);
- } else if (seriesLeft.isLabeledResult() && seriesRight.isLabeledResult()) { // labeled with labeled
- result = seriesLabeledWithLabeled(seriesLeft, seriesRight, opType);
- } else {
- throw new IllegalExpressionException("Binary operation don't support these expression.");
- }
-
- return result;
- }
-
- private static ExpressionResult single2SingleBinaryOp(ExpressionResult singleLeft,
- ExpressionResult singleRight,
- int opType) throws IllegalExpressionException {
- if (!singleLeft.isLabeledResult() && !singleRight.isLabeledResult()) { // no labeled with no labeled
- return single2SingleNoLabeled(singleLeft, singleRight, opType);
- } else if (singleLeft.isLabeledResult() && !singleRight.isLabeledResult()) { // labeled with no labeled
- return many2OneBinaryOp(singleLeft, singleRight, opType);
- } else if (!singleLeft.isLabeledResult() && singleRight.isLabeledResult()) { // no labeled with labeled
- return one2ManyBinaryOp(singleLeft, singleRight, opType);
- } else if (singleLeft.isLabeledResult() && singleRight.isLabeledResult()) { // labeled with labeled
- return single2SingleLabeled(singleLeft, singleRight, opType);
- } else {
- throw new IllegalExpressionException("Binary operation don't support these expression.");
- }
- }
-
- private static ExpressionResult single2SingleNoLabeled(ExpressionResult singleLeft,
- ExpressionResult singleRight,
- int opType) {
- ExpressionResult result = new ExpressionResult();
- MQEValue mqeValue = new MQEValue();
- MQEValues mqeValues = new MQEValues();
- mqeValues.getValues().add(mqeValue);
- result.getResults().add(mqeValues);
- result.setType(ExpressionResultType.SINGLE_VALUE);
-
- MQEValue left = singleLeft.getResults().get(0).getValues().get(0);
- MQEValue right = singleRight.getResults().get(0).getValues().get(0);
- //return null if one of them is empty
- if (left.isEmptyValue() || right.isEmptyValue()) {
- mqeValue.setEmptyValue(true);
- } else {
- double value = scalarBinaryOp(left.getDoubleValue(), right.getDoubleValue(), opType);
- mqeValue.setDoubleValue(value);
- mqeValue.setEmptyValue(false);
- }
- return result;
- }
-
- private static ExpressionResult single2SingleLabeled(ExpressionResult singleLeft,
- ExpressionResult singleRight,
- int opType) throws IllegalExpressionException {
- Map<KeyValue, List<MQEValue>> labelMapR = new HashMap<>();
- if (singleLeft.getResults().size() != singleRight.getResults().size()) {
- throw new IllegalExpressionException(
- "Binary operation between labeled metrics should have the same label.");
- }
- singleRight.getResults().forEach(mqeValuesR -> {
- // For now, we only have a single anonymous label named `_`
- labelMapR.put(mqeValuesR.getMetric().getLabels().get(0), mqeValuesR.getValues());
- });
- for (MQEValues mqeValuesL : singleLeft.getResults()) {
- //reserve left metric info
- MQEValue valueL = mqeValuesL.getValues().get(0);
- List<MQEValue> mqeValuesR = labelMapR.get(mqeValuesL.getMetric().getLabels().get(0));
- if (mqeValuesR == null) {
- throw new IllegalExpressionException(
- "Binary operation between labeled metrics should have the same label.");
- }
- MQEValue valueR = mqeValuesR.get(0);
- if (valueL.isEmptyValue() || valueR.isEmptyValue()) {
- valueL.setEmptyValue(true);
- valueL.setDoubleValue(0);
- } else {
- double value = scalarBinaryOp(valueL.getDoubleValue(), valueR.getDoubleValue(), opType);
- valueL.setDoubleValue(value);
- valueL.setEmptyValue(false);
- }
- }
-
- return singleLeft;
- }
-
- //series or list or labeled single value with scalar
- private static ExpressionResult many2OneBinaryOp(ExpressionResult manyResult,
- ExpressionResult singleResult,
- int opType) {
- manyResult.getResults().forEach(mqeValues -> {
- mqeValues.getValues().forEach(mqeValue -> {
- if (!mqeValue.isEmptyValue()) {
- double newValue = scalarBinaryOp(
- mqeValue.getDoubleValue(), singleResult.getResults()
- .get(0)
- .getValues()
- .get(0)
- .getDoubleValue(), opType);
- mqeValue.setDoubleValue(newValue);
- }
- });
- });
- return manyResult;
- }
-
- //scalar with series or list or labeled single value
- private static ExpressionResult one2ManyBinaryOp(ExpressionResult singleResult,
- ExpressionResult manyResult,
- int opType) {
- manyResult.getResults().forEach(mqeValues -> {
- mqeValues.getValues().forEach(mqeValue -> {
- if (!mqeValue.isEmptyValue()) {
- double newValue = scalarBinaryOp(
- singleResult.getResults()
- .get(0)
- .getValues()
- .get(0)
- .getDoubleValue(),
- mqeValue.getDoubleValue(), opType
- );
- mqeValue.setDoubleValue(newValue);
- }
- });
- });
- return manyResult;
- }
-
- private static ExpressionResult seriesNoLabeled(ExpressionResult seriesLeft,
- ExpressionResult seriesRight,
- int opType) {
- MQEValues mqeValuesL = seriesLeft.getResults().get(0);
- MQEValues mqeValuesR = seriesRight.getResults().get(0);
- mqeValuesL.setMetric(null);
- for (int i = 0; i < mqeValuesL.getValues().size(); i++) {
- //clean metric info
- MQEValue valueL = mqeValuesL.getValues().get(i);
- MQEValue valueR = mqeValuesR.getValues().get(i);
- if (valueL.isEmptyValue() || valueR.isEmptyValue()) {
- valueL.setEmptyValue(true);
- valueL.setDoubleValue(0);
- continue;
- }
- //time should be mapped
- double newValue = scalarBinaryOp(valueL.getDoubleValue(), valueR.getDoubleValue(), opType);
- mqeValuesL.getValues().get(i).setDoubleValue(newValue);
- }
-
- return seriesLeft;
- }
-
- private static ExpressionResult seriesLabeledWithNoLabeled(ExpressionResult seriesLeft,
- ExpressionResult seriesRight,
- int opType) {
- MQEValues mqeValuesR = seriesRight.getResults().get(0);
- seriesLeft.getResults().forEach(mqeValuesL -> {
- for (int i = 0; i < mqeValuesL.getValues().size(); i++) {
- //reserve left metric info
- MQEValue valueL = mqeValuesL.getValues().get(i);
- MQEValue valueR = mqeValuesR.getValues().get(i);
- if (valueL.isEmptyValue() || valueR.isEmptyValue()) {
- valueL.setEmptyValue(true);
- valueL.setDoubleValue(0);
- continue;
- }
- double newValue = scalarBinaryOp(valueL.getDoubleValue(), valueR.getDoubleValue(), opType);
- mqeValuesL.getValues().get(i).setDoubleValue(newValue);
- }
- });
-
- return seriesLeft;
- }
-
- private static ExpressionResult seriesNoLabeledWithLabeled(ExpressionResult seriesLeft,
- ExpressionResult seriesRight,
- int opType) {
- MQEValues mqeValuesL = seriesLeft.getResults().get(0);
- seriesRight.getResults().forEach(mqeValuesR -> {
- for (int i = 0; i < mqeValuesL.getValues().size(); i++) {
- //reserve left metric info
- MQEValue valueL = mqeValuesL.getValues().get(i);
- MQEValue valueR = mqeValuesR.getValues().get(i);
- if (valueL.isEmptyValue() || valueR.isEmptyValue()) {
- valueL.setEmptyValue(true);
- valueL.setDoubleValue(0);
- continue;
- }
- double newValue = scalarBinaryOp(valueL.getDoubleValue(), valueR.getDoubleValue(), opType);
- mqeValuesR.getValues().get(i).setDoubleValue(newValue);
- }
- });
-
- return seriesRight;
- }
-
- private static ExpressionResult seriesLabeledWithLabeled(ExpressionResult seriesLeft,
- ExpressionResult seriesRight,
- int opType) throws IllegalExpressionException {
- Map<KeyValue, List<MQEValue>> labelMapR = new HashMap<>();
- if (seriesLeft.getResults().size() != seriesRight.getResults().size()) {
- throw new IllegalExpressionException(
- "Binary operation between labeled metrics should have the same label.");
- }
- seriesRight.getResults().forEach(mqeValuesR -> {
- // For now, we only have a single anonymous label named `_`
- labelMapR.put(mqeValuesR.getMetric().getLabels().get(0), mqeValuesR.getValues());
- });
- for (MQEValues mqeValuesL : seriesLeft.getResults()) {
- for (int i = 0; i < mqeValuesL.getValues().size(); i++) {
- //reserve left metric info
- MQEValue valueL = mqeValuesL.getValues().get(i);
- List<MQEValue> mqeValuesR = labelMapR.get(mqeValuesL.getMetric().getLabels().get(0));
- if (mqeValuesR == null) {
- throw new IllegalExpressionException(
- "Binary operation between labeled metrics should have the same label.");
- }
- MQEValue valueR = mqeValuesR.get(i);
- if (valueL.isEmptyValue() || valueR.isEmptyValue()) {
- valueL.setEmptyValue(true);
- valueL.setDoubleValue(0);
- continue;
- }
- double newValue = scalarBinaryOp(valueL.getDoubleValue(), valueR.getDoubleValue(), opType);
- mqeValuesL.getValues().get(i).setDoubleValue(newValue);
- }
- }
-
- return seriesLeft;
- }
}
diff --git a/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/operation/CompareOp.java b/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/operation/CompareOp.java
new file mode 100644
index 0000000000..499e7ca79e
--- /dev/null
+++ b/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/operation/CompareOp.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.mqe.rt.operation;
+
+import org.apache.skywalking.mqe.rt.exception.IllegalExpressionException;
+import org.apache.skywalking.mqe.rt.grammar.MQEParser;
+import org.apache.skywalking.mqe.rt.type.ExpressionResult;
+
+public class CompareOp {
+ public static ExpressionResult doCompareOP(ExpressionResult left,
+ ExpressionResult right,
+ int opType) throws IllegalExpressionException {
+ try {
+ return LROp.doLROp(left, right, opType, CompareOp::scalarCompareOp);
+ } catch (IllegalExpressionException e) {
+ throw new IllegalExpressionException("Unsupported compare operation.");
+ }
+ }
+
+ private static int scalarCompareOp(double leftValue, double rightValue, int opType) {
+ int comparedResult = 0;
+ switch (opType) {
+ case MQEParser.DEQ:
+ comparedResult = boolToInt(leftValue == rightValue);
+ break;
+ case MQEParser.NEQ:
+ comparedResult = boolToInt(leftValue != rightValue);
+ break;
+ case MQEParser.GT:
+ comparedResult = boolToInt(leftValue > rightValue);
+ break;
+ case MQEParser.LT:
+ comparedResult = boolToInt(leftValue < rightValue);
+ break;
+ case MQEParser.GTE:
+ comparedResult = boolToInt(leftValue >= rightValue);
+ break;
+ case MQEParser.LTE:
+ comparedResult = boolToInt(leftValue <= rightValue);
+ break;
+ }
+ return comparedResult;
+ }
+
+ private static int boolToInt(boolean v) {
+ return v ? 1 : 0;
+ }
+}
diff --git a/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/operation/BinaryOp.java b/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/operation/LROp.java
similarity index 77%
copy from oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/operation/BinaryOp.java
copy to oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/operation/LROp.java
index f3d4680562..b7a9e2c866 100644
--- a/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/operation/BinaryOp.java
+++ b/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/operation/LROp.java
@@ -22,74 +22,52 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.skywalking.mqe.rt.exception.IllegalExpressionException;
-import org.apache.skywalking.mqe.rt.grammar.MQEParser;
import org.apache.skywalking.mqe.rt.type.ExpressionResult;
import org.apache.skywalking.mqe.rt.type.ExpressionResultType;
import org.apache.skywalking.mqe.rt.type.MQEValue;
import org.apache.skywalking.mqe.rt.type.MQEValues;
import org.apache.skywalking.oap.server.core.query.type.KeyValue;
-public class BinaryOp {
- public static ExpressionResult doBinaryOp(ExpressionResult left,
- ExpressionResult right,
- int opType) throws IllegalExpressionException {
+@FunctionalInterface
+public interface LROp {
+
+ double apply(double left, double right, int opType);
+
+ static ExpressionResult doLROp(ExpressionResult left,
+ ExpressionResult right,
+ int opType, LROp calculate) throws IllegalExpressionException {
if (left.getType() == ExpressionResultType.SINGLE_VALUE && right.getType() == ExpressionResultType.SINGLE_VALUE) {
- return single2SingleBinaryOp(left, right, opType);
+ return single2SingleBinaryOp(left, right, opType, calculate);
} else if ((left.getType() == ExpressionResultType.TIME_SERIES_VALUES ||
left.getType() == ExpressionResultType.SORTED_LIST ||
left.getType() == ExpressionResultType.RECORD_LIST)
&& right.getType() == ExpressionResultType.SINGLE_VALUE) {
- return many2OneBinaryOp(left, right, opType);
+ return many2OneBinaryOp(left, right, opType, calculate);
} else if (left.getType() == ExpressionResultType.SINGLE_VALUE &&
(right.getType() == ExpressionResultType.TIME_SERIES_VALUES ||
right.getType() == ExpressionResultType.SORTED_LIST ||
right.getType() == ExpressionResultType.RECORD_LIST)) {
- return one2ManyBinaryOp(left, right, opType);
+ return one2ManyBinaryOp(left, right, opType, calculate);
} else if (left.getType() == ExpressionResultType.TIME_SERIES_VALUES && right.getType() == ExpressionResultType.TIME_SERIES_VALUES) {
- return seriesBinaryOp(left, right, opType);
+ return seriesBinaryOp(left, right, opType, calculate);
}
- throw new IllegalExpressionException("Unsupported binary operation.");
- }
-
- //scalar with scalar
- private static double scalarBinaryOp(double leftValue, double rightValue, int opType) {
- double calculatedResult = 0;
- switch (opType) {
- case MQEParser.ADD:
- calculatedResult = leftValue + rightValue;
- break;
- case MQEParser.SUB:
- calculatedResult = leftValue - rightValue;
- break;
- case MQEParser.MUL:
- calculatedResult = leftValue * rightValue;
- break;
- case MQEParser.DIV:
- calculatedResult = leftValue / rightValue;
- break;
- case MQEParser.MOD:
- calculatedResult = leftValue % rightValue;
- break;
- }
- return calculatedResult;
+ throw new IllegalExpressionException("Unsupported operation.");
}
//series with series
private static ExpressionResult seriesBinaryOp(ExpressionResult seriesLeft,
ExpressionResult seriesRight,
- int opType) throws IllegalExpressionException {
+ int opType, LROp calculate) throws IllegalExpressionException {
ExpressionResult result = new ExpressionResult();
if (!seriesLeft.isLabeledResult() && !seriesRight.isLabeledResult()) { // no labeled with no labeled
- result = seriesNoLabeled(seriesLeft, seriesRight, opType);
+ result = seriesNoLabeled(seriesLeft, seriesRight, opType, calculate);
} else if (seriesLeft.isLabeledResult() && !seriesRight.isLabeledResult()) { // labeled with no labeled
- result = seriesLabeledWithNoLabeled(seriesLeft, seriesRight, opType);
+ result = seriesLabeledWithNoLabeled(seriesLeft, seriesRight, opType, calculate);
} else if (!seriesLeft.isLabeledResult() && seriesRight.isLabeledResult()) { // no labeled with labeled
- result = seriesNoLabeledWithLabeled(seriesLeft, seriesRight, opType);
- } else if (seriesLeft.isLabeledResult() && seriesRight.isLabeledResult()) { // labeled with labeled
- result = seriesLabeledWithLabeled(seriesLeft, seriesRight, opType);
- } else {
- throw new IllegalExpressionException("Binary operation don't support these expression.");
+ result = seriesNoLabeledWithLabeled(seriesLeft, seriesRight, opType, calculate);
+ } else { // labeled with labeled
+ result = seriesLabeledWithLabeled(seriesLeft, seriesRight, opType, calculate);
}
return result;
@@ -97,23 +75,21 @@ public class BinaryOp {
private static ExpressionResult single2SingleBinaryOp(ExpressionResult singleLeft,
ExpressionResult singleRight,
- int opType) throws IllegalExpressionException {
+ int opType, LROp calculate) throws IllegalExpressionException {
if (!singleLeft.isLabeledResult() && !singleRight.isLabeledResult()) { // no labeled with no labeled
- return single2SingleNoLabeled(singleLeft, singleRight, opType);
+ return single2SingleNoLabeled(singleLeft, singleRight, opType, calculate);
} else if (singleLeft.isLabeledResult() && !singleRight.isLabeledResult()) { // labeled with no labeled
- return many2OneBinaryOp(singleLeft, singleRight, opType);
+ return many2OneBinaryOp(singleLeft, singleRight, opType, calculate);
} else if (!singleLeft.isLabeledResult() && singleRight.isLabeledResult()) { // no labeled with labeled
- return one2ManyBinaryOp(singleLeft, singleRight, opType);
- } else if (singleLeft.isLabeledResult() && singleRight.isLabeledResult()) { // labeled with labeled
- return single2SingleLabeled(singleLeft, singleRight, opType);
- } else {
- throw new IllegalExpressionException("Binary operation don't support these expression.");
+ return one2ManyBinaryOp(singleLeft, singleRight, opType, calculate);
+ } else { // labeled with labeled
+ return single2SingleLabeled(singleLeft, singleRight, opType, calculate);
}
}
private static ExpressionResult single2SingleNoLabeled(ExpressionResult singleLeft,
ExpressionResult singleRight,
- int opType) {
+ int opType, LROp calculate) {
ExpressionResult result = new ExpressionResult();
MQEValue mqeValue = new MQEValue();
MQEValues mqeValues = new MQEValues();
@@ -127,7 +103,7 @@ public class BinaryOp {
if (left.isEmptyValue() || right.isEmptyValue()) {
mqeValue.setEmptyValue(true);
} else {
- double value = scalarBinaryOp(left.getDoubleValue(), right.getDoubleValue(), opType);
+ double value = calculate.apply(left.getDoubleValue(), right.getDoubleValue(), opType);
mqeValue.setDoubleValue(value);
mqeValue.setEmptyValue(false);
}
@@ -136,11 +112,11 @@ public class BinaryOp {
private static ExpressionResult single2SingleLabeled(ExpressionResult singleLeft,
ExpressionResult singleRight,
- int opType) throws IllegalExpressionException {
+ int opType, LROp calculate) throws IllegalExpressionException {
Map<KeyValue, List<MQEValue>> labelMapR = new HashMap<>();
if (singleLeft.getResults().size() != singleRight.getResults().size()) {
throw new IllegalExpressionException(
- "Binary operation between labeled metrics should have the same label.");
+ "Operation between labeled metrics should have the same label.");
}
singleRight.getResults().forEach(mqeValuesR -> {
// For now, we only have a single anonymous label named `_`
@@ -152,14 +128,14 @@ public class BinaryOp {
List<MQEValue> mqeValuesR = labelMapR.get(mqeValuesL.getMetric().getLabels().get(0));
if (mqeValuesR == null) {
throw new IllegalExpressionException(
- "Binary operation between labeled metrics should have the same label.");
+ "Operation between labeled metrics should have the same label.");
}
MQEValue valueR = mqeValuesR.get(0);
if (valueL.isEmptyValue() || valueR.isEmptyValue()) {
valueL.setEmptyValue(true);
valueL.setDoubleValue(0);
} else {
- double value = scalarBinaryOp(valueL.getDoubleValue(), valueR.getDoubleValue(), opType);
+ double value = calculate.apply(valueL.getDoubleValue(), valueR.getDoubleValue(), opType);
valueL.setDoubleValue(value);
valueL.setEmptyValue(false);
}
@@ -171,11 +147,11 @@ public class BinaryOp {
//series or list or labeled single value with scalar
private static ExpressionResult many2OneBinaryOp(ExpressionResult manyResult,
ExpressionResult singleResult,
- int opType) {
+ int opType, LROp calculate) {
manyResult.getResults().forEach(mqeValues -> {
mqeValues.getValues().forEach(mqeValue -> {
if (!mqeValue.isEmptyValue()) {
- double newValue = scalarBinaryOp(
+ double newValue = calculate.apply(
mqeValue.getDoubleValue(), singleResult.getResults()
.get(0)
.getValues()
@@ -191,11 +167,11 @@ public class BinaryOp {
//scalar with series or list or labeled single value
private static ExpressionResult one2ManyBinaryOp(ExpressionResult singleResult,
ExpressionResult manyResult,
- int opType) {
+ int opType, LROp calculate) {
manyResult.getResults().forEach(mqeValues -> {
mqeValues.getValues().forEach(mqeValue -> {
if (!mqeValue.isEmptyValue()) {
- double newValue = scalarBinaryOp(
+ double newValue = calculate.apply(
singleResult.getResults()
.get(0)
.getValues()
@@ -212,7 +188,7 @@ public class BinaryOp {
private static ExpressionResult seriesNoLabeled(ExpressionResult seriesLeft,
ExpressionResult seriesRight,
- int opType) {
+ int opType, LROp calculate) {
MQEValues mqeValuesL = seriesLeft.getResults().get(0);
MQEValues mqeValuesR = seriesRight.getResults().get(0);
mqeValuesL.setMetric(null);
@@ -226,7 +202,7 @@ public class BinaryOp {
continue;
}
//time should be mapped
- double newValue = scalarBinaryOp(valueL.getDoubleValue(), valueR.getDoubleValue(), opType);
+ double newValue = calculate.apply(valueL.getDoubleValue(), valueR.getDoubleValue(), opType);
mqeValuesL.getValues().get(i).setDoubleValue(newValue);
}
@@ -235,7 +211,7 @@ public class BinaryOp {
private static ExpressionResult seriesLabeledWithNoLabeled(ExpressionResult seriesLeft,
ExpressionResult seriesRight,
- int opType) {
+ int opType, LROp calculate) {
MQEValues mqeValuesR = seriesRight.getResults().get(0);
seriesLeft.getResults().forEach(mqeValuesL -> {
for (int i = 0; i < mqeValuesL.getValues().size(); i++) {
@@ -247,7 +223,7 @@ public class BinaryOp {
valueL.setDoubleValue(0);
continue;
}
- double newValue = scalarBinaryOp(valueL.getDoubleValue(), valueR.getDoubleValue(), opType);
+ double newValue = calculate.apply(valueL.getDoubleValue(), valueR.getDoubleValue(), opType);
mqeValuesL.getValues().get(i).setDoubleValue(newValue);
}
});
@@ -257,7 +233,7 @@ public class BinaryOp {
private static ExpressionResult seriesNoLabeledWithLabeled(ExpressionResult seriesLeft,
ExpressionResult seriesRight,
- int opType) {
+ int opType, LROp calculate) {
MQEValues mqeValuesL = seriesLeft.getResults().get(0);
seriesRight.getResults().forEach(mqeValuesR -> {
for (int i = 0; i < mqeValuesL.getValues().size(); i++) {
@@ -269,7 +245,7 @@ public class BinaryOp {
valueL.setDoubleValue(0);
continue;
}
- double newValue = scalarBinaryOp(valueL.getDoubleValue(), valueR.getDoubleValue(), opType);
+ double newValue = calculate.apply(valueL.getDoubleValue(), valueR.getDoubleValue(), opType);
mqeValuesR.getValues().get(i).setDoubleValue(newValue);
}
});
@@ -279,11 +255,11 @@ public class BinaryOp {
private static ExpressionResult seriesLabeledWithLabeled(ExpressionResult seriesLeft,
ExpressionResult seriesRight,
- int opType) throws IllegalExpressionException {
+ int opType, LROp calculate) throws IllegalExpressionException {
Map<KeyValue, List<MQEValue>> labelMapR = new HashMap<>();
if (seriesLeft.getResults().size() != seriesRight.getResults().size()) {
throw new IllegalExpressionException(
- "Binary operation between labeled metrics should have the same label.");
+ "Operation between labeled metrics should have the same label.");
}
seriesRight.getResults().forEach(mqeValuesR -> {
// For now, we only have a single anonymous label named `_`
@@ -296,7 +272,7 @@ public class BinaryOp {
List<MQEValue> mqeValuesR = labelMapR.get(mqeValuesL.getMetric().getLabels().get(0));
if (mqeValuesR == null) {
throw new IllegalExpressionException(
- "Binary operation between labeled metrics should have the same label.");
+ "Operation between labeled metrics should have the same label.");
}
MQEValue valueR = mqeValuesR.get(i);
if (valueL.isEmptyValue() || valueR.isEmptyValue()) {
@@ -304,7 +280,7 @@ public class BinaryOp {
valueL.setDoubleValue(0);
continue;
}
- double newValue = scalarBinaryOp(valueL.getDoubleValue(), valueR.getDoubleValue(), opType);
+ double newValue = calculate.apply(valueL.getDoubleValue(), valueR.getDoubleValue(), opType);
mqeValuesL.getValues().get(i).setDoubleValue(newValue);
}
}
diff --git a/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/type/ExpressionResult.java b/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/type/ExpressionResult.java
index f5c6b94029..8de0383a9c 100644
--- a/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/type/ExpressionResult.java
+++ b/oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/type/ExpressionResult.java
@@ -29,4 +29,5 @@ public class ExpressionResult {
private List<MQEValues> results = new ArrayList<>();
private String error;
private boolean isLabeledResult = false;
+ private boolean isBoolResult = false;
}
diff --git a/oap-server/mqe-rt/src/test/java/org/apache/skywalking/mqe/rt/CompareOPTest.java b/oap-server/mqe-rt/src/test/java/org/apache/skywalking/mqe/rt/CompareOPTest.java
new file mode 100644
index 0000000000..b0fc02cd5f
--- /dev/null
+++ b/oap-server/mqe-rt/src/test/java/org/apache/skywalking/mqe/rt/CompareOPTest.java
@@ -0,0 +1,248 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.mqe.rt;
+
+import org.apache.skywalking.mqe.rt.exception.IllegalExpressionException;
+import org.apache.skywalking.mqe.rt.grammar.MQEParser;
+import org.apache.skywalking.mqe.rt.operation.CompareOp;
+import org.apache.skywalking.mqe.rt.type.ExpressionResult;
+import org.apache.skywalking.mqe.rt.type.ExpressionResultType;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class CompareOPTest {
+ private final MockData mockData = new MockData();
+
+ //GT/GTE/LT/LTE... are the same logic and tested in here, the others only test GT is enough.
+ @Test
+ public void seriesNoLabeledTest() throws Exception {
+ ExpressionResult gt = CompareOp.doCompareOP(
+ mockData.newSeriesNoLabeledResult(100, 200), mockData.newSeriesNoLabeledResult(200, 100), MQEParser.GT);
+ assertEquals(ExpressionResultType.TIME_SERIES_VALUES, gt.getType());
+ assertEquals(0, gt.getResults().get(0).getValues().get(0).getDoubleValue());
+ assertEquals(1, gt.getResults().get(0).getValues().get(1).getDoubleValue());
+
+ ExpressionResult gte = CompareOp.doCompareOP(
+ mockData.newSeriesNoLabeledResult(200, 200), mockData.newSeriesNoLabeledResult(200, 100), MQEParser.GTE);
+ assertEquals(ExpressionResultType.TIME_SERIES_VALUES, gte.getType());
+ assertEquals(1, gte.getResults().get(0).getValues().get(0).getDoubleValue());
+ assertEquals(1, gte.getResults().get(0).getValues().get(1).getDoubleValue());
+
+ ExpressionResult lt = CompareOp.doCompareOP(
+ mockData.newSeriesNoLabeledResult(200, 200), mockData.newSeriesNoLabeledResult(200, 300), MQEParser.LT);
+ assertEquals(ExpressionResultType.TIME_SERIES_VALUES, lt.getType());
+ assertEquals(0, lt.getResults().get(0).getValues().get(0).getDoubleValue());
+ assertEquals(1, lt.getResults().get(0).getValues().get(1).getDoubleValue());
+
+ ExpressionResult lte = CompareOp.doCompareOP(
+ mockData.newSeriesNoLabeledResult(200, 200), mockData.newSeriesNoLabeledResult(200, 100), MQEParser.LTE);
+ assertEquals(ExpressionResultType.TIME_SERIES_VALUES, lte.getType());
+ assertEquals(1, lte.getResults().get(0).getValues().get(0).getDoubleValue());
+ assertEquals(0, lte.getResults().get(0).getValues().get(1).getDoubleValue());
+
+ ExpressionResult neq = CompareOp.doCompareOP(
+ mockData.newSeriesNoLabeledResult(200, 200), mockData.newSeriesNoLabeledResult(200, 100), MQEParser.NEQ);
+ assertEquals(ExpressionResultType.TIME_SERIES_VALUES, neq.getType());
+ assertEquals(0, neq.getResults().get(0).getValues().get(0).getDoubleValue());
+ assertEquals(1, neq.getResults().get(0).getValues().get(1).getDoubleValue());
+
+ ExpressionResult deq = CompareOp.doCompareOP(
+ mockData.newSeriesNoLabeledResult(200, 200), mockData.newSeriesNoLabeledResult(200, 100), MQEParser.DEQ);
+ assertEquals(ExpressionResultType.TIME_SERIES_VALUES, neq.getType());
+ assertEquals(1, deq.getResults().get(0).getValues().get(0).getDoubleValue());
+ assertEquals(0, deq.getResults().get(0).getValues().get(1).getDoubleValue());
+ }
+
+ @Test
+ public void seriesLabeledTest() throws Exception {
+ //seriesLabeled > seriesNoLabeled
+ ExpressionResult gt = CompareOp.doCompareOP(mockData.newSeriesLabeledResult(100, 300, 101, 200),
+ mockData.newSeriesNoLabeledResult(100, 200), MQEParser.GT
+ );
+ assertEquals(ExpressionResultType.TIME_SERIES_VALUES, gt.getType());
+ //label=1
+ assertEquals("1", gt.getResults().get(0).getMetric().getLabels().get(0).getValue());
+ assertEquals("100", gt.getResults().get(0).getValues().get(0).getId());
+ assertEquals(0, gt.getResults().get(0).getValues().get(0).getDoubleValue());
+ assertEquals("300", gt.getResults().get(0).getValues().get(1).getId());
+ assertEquals(1, gt.getResults().get(0).getValues().get(1).getDoubleValue());
+ //label=2
+ assertEquals("2", gt.getResults().get(1).getMetric().getLabels().get(0).getValue());
+ assertEquals("100", gt.getResults().get(1).getValues().get(0).getId());
+ assertEquals(1, gt.getResults().get(1).getValues().get(0).getDoubleValue());
+ assertEquals("300", gt.getResults().get(1).getValues().get(1).getId());
+ assertEquals(0, gt.getResults().get(1).getValues().get(1).getDoubleValue());
+
+ //seriesLabeled > seriesLabeled
+ gt = CompareOp.doCompareOP(mockData.newSeriesLabeledResult(101, 300, 100, 201),
+ mockData.newSeriesLabeledResult(100, 300, 101, 200), MQEParser.GT
+ );
+ assertEquals(ExpressionResultType.TIME_SERIES_VALUES, gt.getType());
+ //label=1
+ assertEquals("1", gt.getResults().get(0).getMetric().getLabels().get(0).getValue());
+ assertEquals("100", gt.getResults().get(0).getValues().get(0).getId());
+ assertEquals(1, gt.getResults().get(0).getValues().get(0).getDoubleValue());
+ assertEquals("300", gt.getResults().get(0).getValues().get(1).getId());
+ assertEquals(0, gt.getResults().get(0).getValues().get(1).getDoubleValue());
+ //label=2
+ assertEquals("2", gt.getResults().get(1).getMetric().getLabels().get(0).getValue());
+ assertEquals("100", gt.getResults().get(1).getValues().get(0).getId());
+ assertEquals(0, gt.getResults().get(1).getValues().get(0).getDoubleValue());
+ assertEquals("300", gt.getResults().get(1).getValues().get(1).getId());
+ assertEquals(1, gt.getResults().get(1).getValues().get(1).getDoubleValue());
+
+ //seriesNoLabeled > seriesLabeled
+ gt = CompareOp.doCompareOP(mockData.newSeriesNoLabeledResult(101, 202),
+ mockData.newSeriesLabeledResult(100, 300, 101, 201), MQEParser.GT
+ );
+ assertEquals(ExpressionResultType.TIME_SERIES_VALUES, gt.getType());
+ //label=1
+ assertEquals("1", gt.getResults().get(0).getMetric().getLabels().get(0).getValue());
+ assertEquals("100", gt.getResults().get(0).getValues().get(0).getId());
+ assertEquals(1, gt.getResults().get(0).getValues().get(0).getDoubleValue());
+ assertEquals("300", gt.getResults().get(0).getValues().get(1).getId());
+ assertEquals(0, gt.getResults().get(0).getValues().get(1).getDoubleValue());
+ //label=2
+ assertEquals("2", gt.getResults().get(1).getMetric().getLabels().get(0).getValue());
+ assertEquals("100", gt.getResults().get(1).getValues().get(0).getId());
+ assertEquals(0, gt.getResults().get(1).getValues().get(0).getDoubleValue());
+ assertEquals("300", gt.getResults().get(1).getValues().get(1).getId());
+ assertEquals(1, gt.getResults().get(1).getValues().get(1).getDoubleValue());
+ }
+
+ @Test
+ public void many2OneTest() throws Exception {
+ //sort_list > single
+ ExpressionResult gt = CompareOp.doCompareOP(
+ mockData.newListResult(), mockData.newSingleResult(200), MQEParser.GT);
+ assertEquals(ExpressionResultType.SORTED_LIST, gt.getType());
+ assertEquals("service_A", gt.getResults().get(0).getValues().get(0).getId());
+ assertEquals(0, gt.getResults().get(0).getValues().get(0).getDoubleValue());
+ assertEquals("service_B", gt.getResults().get(0).getValues().get(1).getId());
+ assertEquals(1, gt.getResults().get(0).getValues().get(1).getDoubleValue());
+
+ //seriesNoLabeled > single
+ gt = CompareOp.doCompareOP(
+ mockData.newSeriesNoLabeledResult(100, 200), mockData.newSingleResult(101), MQEParser.GT);
+ assertEquals(ExpressionResultType.TIME_SERIES_VALUES, gt.getType());
+ assertEquals("100", gt.getResults().get(0).getValues().get(0).getId());
+ assertEquals(0, gt.getResults().get(0).getValues().get(0).getDoubleValue());
+ assertEquals("300", gt.getResults().get(0).getValues().get(1).getId());
+ assertEquals(1, gt.getResults().get(0).getValues().get(1).getDoubleValue());
+
+ //seriesLabeled > single
+ gt = CompareOp.doCompareOP(mockData.newSeriesLabeledResult(100, 300, 101, 200),
+ mockData.newSingleResult(101), MQEParser.GT
+ );
+ assertEquals(ExpressionResultType.TIME_SERIES_VALUES, gt.getType());
+ //label=1
+ assertEquals("1", gt.getResults().get(0).getMetric().getLabels().get(0).getValue());
+ assertEquals("100", gt.getResults().get(0).getValues().get(0).getId());
+ assertEquals(0, gt.getResults().get(0).getValues().get(0).getDoubleValue());
+ assertEquals("300", gt.getResults().get(0).getValues().get(1).getId());
+ assertEquals(1, gt.getResults().get(0).getValues().get(1).getDoubleValue());
+ //label=2
+ assertEquals("2", gt.getResults().get(1).getMetric().getLabels().get(0).getValue());
+ assertEquals("100", gt.getResults().get(1).getValues().get(0).getId());
+ assertEquals(0, gt.getResults().get(1).getValues().get(0).getDoubleValue());
+ assertEquals("300", gt.getResults().get(1).getValues().get(1).getId());
+ assertEquals(1, gt.getResults().get(1).getValues().get(1).getDoubleValue());
+ }
+
+ @Test
+ public void one2ManyTest() throws Exception {
+ // single > sort_list
+ ExpressionResult gt = CompareOp.doCompareOP(
+ mockData.newSingleResult(200), mockData.newListResult(), MQEParser.GT);
+ assertEquals(ExpressionResultType.SORTED_LIST, gt.getType());
+ assertEquals("service_A", gt.getResults().get(0).getValues().get(0).getId());
+ assertEquals(1, gt.getResults().get(0).getValues().get(0).getDoubleValue());
+ assertEquals("service_B", gt.getResults().get(0).getValues().get(1).getId());
+ assertEquals(0, gt.getResults().get(0).getValues().get(1).getDoubleValue());
+
+ //single > seriesNoLabeled
+ gt = CompareOp.doCompareOP(
+ mockData.newSingleResult(101), mockData.newSeriesNoLabeledResult(100, 200), MQEParser.GT);
+ assertEquals(ExpressionResultType.TIME_SERIES_VALUES, gt.getType());
+ assertEquals("100", gt.getResults().get(0).getValues().get(0).getId());
+ assertEquals(1, gt.getResults().get(0).getValues().get(0).getDoubleValue());
+ assertEquals("300", gt.getResults().get(0).getValues().get(1).getId());
+ assertEquals(0, gt.getResults().get(0).getValues().get(1).getDoubleValue());
+
+ //single > seriesLabeled
+ gt = CompareOp.doCompareOP(mockData.newSingleResult(101),
+ mockData.newSeriesLabeledResult(100, 200, 200, 100), MQEParser.GT);
+ assertEquals(ExpressionResultType.TIME_SERIES_VALUES, gt.getType());
+ //label=1
+ assertEquals("1", gt.getResults().get(0).getMetric().getLabels().get(0).getValue());
+ assertEquals("100", gt.getResults().get(0).getValues().get(0).getId());
+ assertEquals(1, gt.getResults().get(0).getValues().get(0).getDoubleValue());
+ assertEquals("300", gt.getResults().get(0).getValues().get(1).getId());
+ assertEquals(0, gt.getResults().get(0).getValues().get(1).getDoubleValue());
+ //label=2
+ assertEquals("2", gt.getResults().get(1).getMetric().getLabels().get(0).getValue());
+ assertEquals("100", gt.getResults().get(1).getValues().get(0).getId());
+ assertEquals(0, gt.getResults().get(1).getValues().get(0).getDoubleValue());
+ assertEquals("300", gt.getResults().get(1).getValues().get(1).getId());
+ assertEquals(1, gt.getResults().get(1).getValues().get(1).getDoubleValue());
+ }
+
+ @Test
+ public void single2SingleTest() throws IllegalExpressionException {
+ //noLabeled > noLabeled
+ ExpressionResult gt = CompareOp.doCompareOP(
+ mockData.newSingleResult(100), mockData.newSingleResult(200), MQEParser.GT);
+ assertEquals(ExpressionResultType.SINGLE_VALUE, gt.getType());
+ assertEquals(0, gt.getResults().get(0).getValues().get(0).getDoubleValue());
+
+ //labeled > noLabeled
+ gt = CompareOp.doCompareOP(
+ mockData.newSingleLabeledResult(100, 200), mockData.newSingleResult(100), MQEParser.GT);
+ assertEquals(ExpressionResultType.SINGLE_VALUE, gt.getType());
+ //label=1
+ assertEquals("1", gt.getResults().get(0).getMetric().getLabels().get(0).getValue());
+ assertEquals(0, gt.getResults().get(0).getValues().get(0).getDoubleValue());
+ //label=2
+ assertEquals("2", gt.getResults().get(1).getMetric().getLabels().get(0).getValue());
+ assertEquals(1, gt.getResults().get(1).getValues().get(0).getDoubleValue());
+
+ //nolabeled > labeled
+ gt = CompareOp.doCompareOP(
+ mockData.newSingleResult(101), mockData.newSingleLabeledResult(100, 200), MQEParser.GT);
+ assertEquals(ExpressionResultType.SINGLE_VALUE, gt.getType());
+ //label=1
+ assertEquals("1", gt.getResults().get(0).getMetric().getLabels().get(0).getValue());
+ assertEquals(1, gt.getResults().get(0).getValues().get(0).getDoubleValue());
+ //label=2
+ assertEquals("2", gt.getResults().get(1).getMetric().getLabels().get(0).getValue());
+ assertEquals(0, gt.getResults().get(1).getValues().get(0).getDoubleValue());
+
+ //labeled > labeled
+ gt = CompareOp.doCompareOP(
+ mockData.newSingleLabeledResult(100, 202), mockData.newSingleLabeledResult(100, 200), MQEParser.GT);
+ assertEquals(ExpressionResultType.SINGLE_VALUE, gt.getType());
+ //label=1
+ assertEquals("1", gt.getResults().get(0).getMetric().getLabels().get(0).getValue());
+ assertEquals(0, gt.getResults().get(0).getValues().get(0).getDoubleValue());
+ //label=2
+ assertEquals("2", gt.getResults().get(1).getMetric().getLabels().get(0).getValue());
+ assertEquals(1, gt.getResults().get(1).getValues().get(0).getDoubleValue());
+ }
+}
diff --git a/test/e2e-v2/cases/mqe/expected/compare-OP.yml b/test/e2e-v2/cases/mqe/expected/compare-OP.yml
new file mode 100644
index 0000000000..3045f2bd74
--- /dev/null
+++ b/test/e2e-v2/cases/mqe/expected/compare-OP.yml
@@ -0,0 +1,31 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+type: TIME_SERIES_VALUES
+results:
+{{- contains .results }}
+ - metric:
+ labels: []
+ values:
+ {{- contains .values }}
+ - id: {{ notEmpty .id }}
+ value: "1"
+ traceid: null
+ - id: {{ notEmpty .id }}
+ value: null
+ traceid: null
+ {{- end}}
+{{- end}}
+error: null
diff --git a/test/e2e-v2/cases/mqe/mqe-cases.yaml b/test/e2e-v2/cases/mqe/mqe-cases.yaml
index 77d2356173..d857cfbf37 100644
--- a/test/e2e-v2/cases/mqe/mqe-cases.yaml
+++ b/test/e2e-v2/cases/mqe/mqe-cases.yaml
@@ -24,6 +24,10 @@ cases:
- query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression="service_sla * 2 /100 + 500 - 50" --service-name=e2e-service-provider
expected: expected/binary-OP.yml
+ # compare-OP
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression="(service_cpm + 1) > 2" --service-name=e2e-service-provider
+ expected: expected/compare-OP.yml
+
# aggregation-OP
- query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression="avg(service_sla/100)" --service-name=e2e-service-provider
expected: expected/aggregation-OP.yml
@@ -62,4 +66,4 @@ cases:
# viewAsSeq-OP
- query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression="view_as_seq(mq_service_consume_sla,service_sla)" --service-name=e2e-service-provider
- expected: expected/viewAsSeq-OP.yml
\ No newline at end of file
+ expected: expected/viewAsSeq-OP.yml