diff --git a/build.gradle b/build.gradle index a256fd1d22..290a1e1786 100644 --- a/build.gradle +++ b/build.gradle @@ -86,6 +86,7 @@ spotless { target fileTree('.') { include 'datasources/**/*.java', 'core/**/*.java', + 'prometheus/**/*.java', 'sql/**/*.java', 'common/**/*.java', 'ppl/**/*.java' diff --git a/prometheus/build.gradle b/prometheus/build.gradle index e98dfd83e4..0d915a6d4a 100644 --- a/prometheus/build.gradle +++ b/prometheus/build.gradle @@ -13,6 +13,9 @@ repositories { mavenCentral() } +checkstyleTest.ignoreFailures = true +checkstyleMain.ignoreFailures = true + dependencies { api project(':core') implementation project(':datasources') diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/client/PrometheusClientImpl.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/client/PrometheusClientImpl.java index 9472be7487..2bfaaccd47 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/client/PrometheusClientImpl.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/client/PrometheusClientImpl.java @@ -38,16 +38,18 @@ public PrometheusClientImpl(OkHttpClient okHttpClient, URI uri) { this.uri = uri; } - @Override public JSONObject queryRange(String query, Long start, Long end, String step) throws IOException { - String queryUrl = String.format("%s/api/v1/query_range?query=%s&start=%s&end=%s&step=%s", - uri.toString().replaceAll("/$", ""), URLEncoder.encode(query, StandardCharsets.UTF_8), - start, end, step); + String queryUrl = + String.format( + "%s/api/v1/query_range?query=%s&start=%s&end=%s&step=%s", + uri.toString().replaceAll("/$", ""), + URLEncoder.encode(query, StandardCharsets.UTF_8), + start, + end, + step); logger.debug("queryUrl: " + queryUrl); - Request request = new Request.Builder() - .url(queryUrl) - .build(); + Request request = new Request.Builder().url(queryUrl).build(); Response response = this.okHttpClient.newCall(request).execute(); JSONObject jsonObject = readResponse(response); return jsonObject.getJSONObject("data"); @@ -55,14 +57,14 @@ public JSONObject queryRange(String query, Long start, Long end, String step) th @Override public List getLabels(String metricName) throws IOException { - String queryUrl = String.format("%s/api/v1/labels?%s=%s", - uri.toString().replaceAll("/$", ""), - URLEncoder.encode("match[]", StandardCharsets.UTF_8), - URLEncoder.encode(metricName, StandardCharsets.UTF_8)); + String queryUrl = + String.format( + "%s/api/v1/labels?%s=%s", + uri.toString().replaceAll("/$", ""), + URLEncoder.encode("match[]", StandardCharsets.UTF_8), + URLEncoder.encode(metricName, StandardCharsets.UTF_8)); logger.debug("queryUrl: " + queryUrl); - Request request = new Request.Builder() - .url(queryUrl) - .build(); + Request request = new Request.Builder().url(queryUrl).build(); Response response = this.okHttpClient.newCall(request).execute(); JSONObject jsonObject = readResponse(response); return toListOfLabels(jsonObject.getJSONArray("data")); @@ -70,28 +72,26 @@ public List getLabels(String metricName) throws IOException { @Override public Map> getAllMetrics() throws IOException { - String queryUrl = String.format("%s/api/v1/metadata", - uri.toString().replaceAll("/$", "")); + String queryUrl = String.format("%s/api/v1/metadata", uri.toString().replaceAll("/$", "")); logger.debug("queryUrl: " + queryUrl); - Request request = new Request.Builder() - .url(queryUrl) - .build(); + Request request = new Request.Builder().url(queryUrl).build(); Response response = this.okHttpClient.newCall(request).execute(); JSONObject jsonObject = readResponse(response); - TypeReference>> typeRef - = new TypeReference<>() {}; + TypeReference>> typeRef = new TypeReference<>() {}; return new ObjectMapper().readValue(jsonObject.getJSONObject("data").toString(), typeRef); } @Override public JSONArray queryExemplars(String query, Long start, Long end) throws IOException { - String queryUrl = String.format("%s/api/v1/query_exemplars?query=%s&start=%s&end=%s", - uri.toString().replaceAll("/$", ""), URLEncoder.encode(query, StandardCharsets.UTF_8), - start, end); + String queryUrl = + String.format( + "%s/api/v1/query_exemplars?query=%s&start=%s&end=%s", + uri.toString().replaceAll("/$", ""), + URLEncoder.encode(query, StandardCharsets.UTF_8), + start, + end); logger.debug("queryUrl: " + queryUrl); - Request request = new Request.Builder() - .url(queryUrl) - .build(); + Request request = new Request.Builder().url(queryUrl).build(); Response response = this.okHttpClient.newCall(request).execute(); JSONObject jsonObject = readResponse(response); return jsonObject.getJSONArray("data"); @@ -100,8 +100,8 @@ public JSONArray queryExemplars(String query, Long start, Long end) throws IOExc private List toListOfLabels(JSONArray array) { List result = new ArrayList<>(); for (int i = 0; i < array.length(); i++) { - //__name__ is internal label in prometheus representing the metric name. - //Exempting this from labels list as it is not required in any of the operations. + // __name__ is internal label in prometheus representing the metric name. + // Exempting this from labels list as it is not required in any of the operations. if (!"__name__".equals(array.optString(i))) { result.add(array.optString(i)); } @@ -109,7 +109,6 @@ private List toListOfLabels(JSONArray array) { return result; } - private JSONObject readResponse(Response response) throws IOException { if (response.isSuccessful()) { JSONObject jsonObject = new JSONObject(Objects.requireNonNull(response.body()).string()); @@ -120,10 +119,9 @@ private JSONObject readResponse(Response response) throws IOException { } } else { throw new RuntimeException( - String.format("Request to Prometheus is Unsuccessful with : %s", Objects.requireNonNull( - response.body(), "Response body can't be null").string())); + String.format( + "Request to Prometheus is Unsuccessful with : %s", + Objects.requireNonNull(response.body(), "Response body can't be null").string())); } } - - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/data/constants/PrometheusFieldConstants.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/data/constants/PrometheusFieldConstants.java index 88e9df6a88..0f687b3cd1 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/data/constants/PrometheusFieldConstants.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/data/constants/PrometheusFieldConstants.java @@ -18,6 +18,6 @@ public class PrometheusFieldConstants { public static final String EXEMPLARS_KEY = "exemplars"; public static final String TRACE_ID_KEY = "traceID"; public static final String LABELS_KEY = "labels"; - public static final String TIMESTAMP_KEY = "timestamp"; - public static final String VALUE_KEY = "value"; + public static final String TIMESTAMP_KEY = "timestamp"; + public static final String VALUE_KEY = "value"; } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/implementation/QueryExemplarFunctionImplementation.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/implementation/QueryExemplarFunctionImplementation.java index 9d455b3cfc..bbd3a36f5f 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/implementation/QueryExemplarFunctionImplementation.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/implementation/QueryExemplarFunctionImplementation.java @@ -28,8 +28,8 @@ import org.opensearch.sql.prometheus.storage.QueryExemplarsTable; import org.opensearch.sql.storage.Table; -public class QueryExemplarFunctionImplementation extends FunctionExpression implements - TableFunctionImplementation { +public class QueryExemplarFunctionImplementation extends FunctionExpression + implements TableFunctionImplementation { private final FunctionName functionName; private final List arguments; @@ -39,10 +39,10 @@ public class QueryExemplarFunctionImplementation extends FunctionExpression impl * Required argument constructor. * * @param functionName name of the function - * @param arguments a list of arguments provided + * @param arguments a list of arguments provided */ - public QueryExemplarFunctionImplementation(FunctionName functionName, List arguments, - PrometheusClient prometheusClient) { + public QueryExemplarFunctionImplementation( + FunctionName functionName, List arguments, PrometheusClient prometheusClient) { super(functionName, arguments); this.functionName = functionName; this.arguments = arguments; @@ -51,10 +51,11 @@ public QueryExemplarFunctionImplementation(FunctionName functionName, List valueEnv) { - throw new UnsupportedOperationException(String.format( - "Prometheus defined function [%s] is only " - + "supported in SOURCE clause with prometheus connector catalog", - functionName)); + throw new UnsupportedOperationException( + String.format( + "Prometheus defined function [%s] is only " + + "supported in SOURCE clause with prometheus connector catalog", + functionName)); } @Override @@ -64,10 +65,15 @@ public ExprType type() { @Override public String toString() { - List args = arguments.stream() - .map(arg -> String.format("%s=%s", ((NamedArgumentExpression) arg) - .getArgName(), ((NamedArgumentExpression) arg).getValue().toString())) - .collect(Collectors.toList()); + List args = + arguments.stream() + .map( + arg -> + String.format( + "%s=%s", + ((NamedArgumentExpression) arg).getArgName(), + ((NamedArgumentExpression) arg).getValue().toString())) + .collect(Collectors.toList()); return String.format("%s(%s)", functionName, String.join(", ", args)); } @@ -79,27 +85,26 @@ public Table applyArguments() { private PrometheusQueryExemplarsRequest buildExemplarsQueryRequest(List arguments) { PrometheusQueryExemplarsRequest request = new PrometheusQueryExemplarsRequest(); - arguments.forEach(arg -> { - String argName = ((NamedArgumentExpression) arg).getArgName(); - Expression argValue = ((NamedArgumentExpression) arg).getValue(); - ExprValue literalValue = argValue.valueOf(); - switch (argName) { - case QUERY: - request - .setQuery((String) literalValue.value()); - break; - case STARTTIME: - request.setStartTime(((Number) literalValue.value()).longValue()); - break; - case ENDTIME: - request.setEndTime(((Number) literalValue.value()).longValue()); - break; - default: - throw new ExpressionEvaluationException( - String.format("Invalid Function Argument:%s", argName)); - } - }); + arguments.forEach( + arg -> { + String argName = ((NamedArgumentExpression) arg).getArgName(); + Expression argValue = ((NamedArgumentExpression) arg).getValue(); + ExprValue literalValue = argValue.valueOf(); + switch (argName) { + case QUERY: + request.setQuery((String) literalValue.value()); + break; + case STARTTIME: + request.setStartTime(((Number) literalValue.value()).longValue()); + break; + case ENDTIME: + request.setEndTime(((Number) literalValue.value()).longValue()); + break; + default: + throw new ExpressionEvaluationException( + String.format("Invalid Function Argument:%s", argName)); + } + }); return request; } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/implementation/QueryRangeFunctionImplementation.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/implementation/QueryRangeFunctionImplementation.java index 2d3710037a..0719bd1525 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/implementation/QueryRangeFunctionImplementation.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/implementation/QueryRangeFunctionImplementation.java @@ -29,8 +29,8 @@ import org.opensearch.sql.prometheus.storage.PrometheusMetricTable; import org.opensearch.sql.storage.Table; -public class QueryRangeFunctionImplementation extends FunctionExpression implements - TableFunctionImplementation { +public class QueryRangeFunctionImplementation extends FunctionExpression + implements TableFunctionImplementation { private final FunctionName functionName; private final List arguments; @@ -40,10 +40,10 @@ public class QueryRangeFunctionImplementation extends FunctionExpression impleme * Required argument constructor. * * @param functionName name of the function - * @param arguments a list of expressions + * @param arguments a list of expressions */ - public QueryRangeFunctionImplementation(FunctionName functionName, List arguments, - PrometheusClient prometheusClient) { + public QueryRangeFunctionImplementation( + FunctionName functionName, List arguments, PrometheusClient prometheusClient) { super(functionName, arguments); this.functionName = functionName; this.arguments = arguments; @@ -52,10 +52,11 @@ public QueryRangeFunctionImplementation(FunctionName functionName, List valueEnv) { - throw new UnsupportedOperationException(String.format( - "Prometheus defined function [%s] is only " - + "supported in SOURCE clause with prometheus connector catalog", - functionName)); + throw new UnsupportedOperationException( + String.format( + "Prometheus defined function [%s] is only " + + "supported in SOURCE clause with prometheus connector catalog", + functionName)); } @Override @@ -65,10 +66,15 @@ public ExprType type() { @Override public String toString() { - List args = arguments.stream() - .map(arg -> String.format("%s=%s", ((NamedArgumentExpression) arg) - .getArgName(), ((NamedArgumentExpression) arg).getValue().toString())) - .collect(Collectors.toList()); + List args = + arguments.stream() + .map( + arg -> + String.format( + "%s=%s", + ((NamedArgumentExpression) arg).getArgName(), + ((NamedArgumentExpression) arg).getValue().toString())) + .collect(Collectors.toList()); return String.format("%s(%s)", functionName, String.join(", ", args)); } @@ -80,30 +86,29 @@ public Table applyArguments() { private PrometheusQueryRequest buildQueryFromQueryRangeFunction(List arguments) { PrometheusQueryRequest prometheusQueryRequest = new PrometheusQueryRequest(); - arguments.forEach(arg -> { - String argName = ((NamedArgumentExpression) arg).getArgName(); - Expression argValue = ((NamedArgumentExpression) arg).getValue(); - ExprValue literalValue = argValue.valueOf(); - switch (argName) { - case QUERY: - prometheusQueryRequest - .setPromQl((String) literalValue.value()); - break; - case STARTTIME: - prometheusQueryRequest.setStartTime(((Number) literalValue.value()).longValue()); - break; - case ENDTIME: - prometheusQueryRequest.setEndTime(((Number) literalValue.value()).longValue()); - break; - case STEP: - prometheusQueryRequest.setStep(literalValue.value().toString()); - break; - default: - throw new ExpressionEvaluationException( - String.format("Invalid Function Argument:%s", argName)); - } - }); + arguments.forEach( + arg -> { + String argName = ((NamedArgumentExpression) arg).getArgName(); + Expression argValue = ((NamedArgumentExpression) arg).getValue(); + ExprValue literalValue = argValue.valueOf(); + switch (argName) { + case QUERY: + prometheusQueryRequest.setPromQl((String) literalValue.value()); + break; + case STARTTIME: + prometheusQueryRequest.setStartTime(((Number) literalValue.value()).longValue()); + break; + case ENDTIME: + prometheusQueryRequest.setEndTime(((Number) literalValue.value()).longValue()); + break; + case STEP: + prometheusQueryRequest.setStep(literalValue.value().toString()); + break; + default: + throw new ExpressionEvaluationException( + String.format("Invalid Function Argument:%s", argName)); + } + }); return prometheusQueryRequest; } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolver.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolver.java index a82e5a397a..78d87b0a0b 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolver.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolver.java @@ -22,9 +22,9 @@ import org.opensearch.sql.prometheus.functions.implementation.QueryExemplarFunctionImplementation; /** - * This class is for query_exemplars table function resolver {@link FunctionResolver}. - * It takes care of validating function arguments and also creating - * required {@link org.opensearch.sql.expression.function.TableFunctionImplementation} Class. + * This class is for query_exemplars table function resolver {@link FunctionResolver}. It takes care + * of validating function arguments and also creating required {@link + * org.opensearch.sql.expression.function.TableFunctionImplementation} Class. */ @RequiredArgsConstructor public class QueryExemplarsTableFunctionResolver implements FunctionResolver { @@ -41,13 +41,15 @@ public Pair resolve(FunctionSignature unreso final FunctionName functionName = FunctionName.of(QUERY_EXEMPLARS); FunctionSignature functionSignature = new FunctionSignature(FunctionName.of(QUERY_EXEMPLARS), List.of(STRING, LONG, LONG)); - FunctionBuilder functionBuilder = (functionProperties, arguments) -> { - final List argumentNames = List.of(QUERY, STARTTIME, ENDTIME); - validatePrometheusTableFunctionArguments(arguments, argumentNames); - List namedArguments = getNamedArgumentsOfTableFunction(arguments, argumentNames); - return new QueryExemplarFunctionImplementation(functionName, - namedArguments, prometheusClient); - }; + FunctionBuilder functionBuilder = + (functionProperties, arguments) -> { + final List argumentNames = List.of(QUERY, STARTTIME, ENDTIME); + validatePrometheusTableFunctionArguments(arguments, argumentNames); + List namedArguments = + getNamedArgumentsOfTableFunction(arguments, argumentNames); + return new QueryExemplarFunctionImplementation( + functionName, namedArguments, prometheusClient); + }; return Pair.of(functionSignature, functionBuilder); } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/resolver/QueryRangeTableFunctionResolver.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/resolver/QueryRangeTableFunctionResolver.java index 8bb2a2d758..8dfa12134e 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/resolver/QueryRangeTableFunctionResolver.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/resolver/QueryRangeTableFunctionResolver.java @@ -39,11 +39,14 @@ public Pair resolve(FunctionSignature unreso FunctionSignature functionSignature = new FunctionSignature(functionName, List.of(STRING, LONG, LONG, STRING)); final List argumentNames = List.of(QUERY, STARTTIME, ENDTIME, STEP); - FunctionBuilder functionBuilder = (functionProperties, arguments) -> { - validatePrometheusTableFunctionArguments(arguments, argumentNames); - List namedArguments = getNamedArgumentsOfTableFunction(arguments, argumentNames); - return new QueryRangeFunctionImplementation(functionName, namedArguments, prometheusClient); - }; + FunctionBuilder functionBuilder = + (functionProperties, arguments) -> { + validatePrometheusTableFunctionArguments(arguments, argumentNames); + List namedArguments = + getNamedArgumentsOfTableFunction(arguments, argumentNames); + return new QueryRangeFunctionImplementation( + functionName, namedArguments, prometheusClient); + }; return Pair.of(functionSignature, functionBuilder); } @@ -51,5 +54,4 @@ public Pair resolve(FunctionSignature unreso public FunctionName getFunctionName() { return FunctionName.of(QUERY_RANGE); } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/PrometheusFunctionResponseHandle.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/PrometheusFunctionResponseHandle.java index f2cefa85ec..bbc0516df6 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/PrometheusFunctionResponseHandle.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/PrometheusFunctionResponseHandle.java @@ -8,14 +8,10 @@ import org.opensearch.sql.data.model.ExprValue; import org.opensearch.sql.executor.ExecutionEngine; -/** - * Handle Prometheus response. - */ +/** Handle Prometheus response. */ public interface PrometheusFunctionResponseHandle { - /** - * Return true if Prometheus response has more result. - */ + /** Return true if Prometheus response has more result. */ boolean hasNext(); /** @@ -24,8 +20,6 @@ public interface PrometheusFunctionResponseHandle { */ ExprValue next(); - /** - * Return ExecutionEngine.Schema of the Prometheus response. - */ + /** Return ExecutionEngine.Schema of the Prometheus response. */ ExecutionEngine.Schema schema(); } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/QueryExemplarsFunctionResponseHandle.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/QueryExemplarsFunctionResponseHandle.java index f030ce8f7a..8d1c267a90 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/QueryExemplarsFunctionResponseHandle.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/QueryExemplarsFunctionResponseHandle.java @@ -44,8 +44,8 @@ public QueryExemplarsFunctionResponseHandle(JSONArray responseArray) { private void constructIteratorAndSchema(JSONArray responseArray) { List columnList = new ArrayList<>(); columnList.add(new ExecutionEngine.Schema.Column(SERIES_LABELS_KEY, SERIES_LABELS_KEY, STRUCT)); - columnList.add(new ExecutionEngine.Schema.Column(EXEMPLARS_KEY, EXEMPLARS_KEY, - ExprCoreType.ARRAY)); + columnList.add( + new ExecutionEngine.Schema.Column(EXEMPLARS_KEY, EXEMPLARS_KEY, ExprCoreType.ARRAY)); this.schema = new ExecutionEngine.Schema(columnList); List result = new ArrayList<>(); for (int i = 0; i < responseArray.length(); i++) { @@ -62,7 +62,8 @@ private void constructIteratorAndSchema(JSONArray responseArray) { private ExprValue constructSeriesLabels(JSONObject seriesLabels) { LinkedHashMap seriesLabelsMap = new LinkedHashMap<>(); - seriesLabels.keySet() + seriesLabels + .keySet() .forEach(key -> seriesLabelsMap.put(key, new ExprStringValue(seriesLabels.getString(key)))); return new ExprTupleValue(seriesLabelsMap); } @@ -78,13 +79,13 @@ private ExprValue constructExemplarList(JSONArray exemplars) { private ExprValue constructExemplar(JSONObject exemplarsJSONObject) { LinkedHashMap exemplarHashMap = new LinkedHashMap<>(); - exemplarHashMap.put(LABELS_KEY, - constructLabelsInExemplar(exemplarsJSONObject.getJSONObject(LABELS_KEY))); - exemplarHashMap.put(TIMESTAMP_KEY, - new ExprTimestampValue(Instant.ofEpochMilli((long)( - exemplarsJSONObject.getDouble(TIMESTAMP_KEY) * 1000)))); - exemplarHashMap.put(VALUE_KEY, - new ExprDoubleValue(exemplarsJSONObject.getDouble(VALUE_KEY))); + exemplarHashMap.put( + LABELS_KEY, constructLabelsInExemplar(exemplarsJSONObject.getJSONObject(LABELS_KEY))); + exemplarHashMap.put( + TIMESTAMP_KEY, + new ExprTimestampValue( + Instant.ofEpochMilli((long) (exemplarsJSONObject.getDouble(TIMESTAMP_KEY) * 1000)))); + exemplarHashMap.put(VALUE_KEY, new ExprDoubleValue(exemplarsJSONObject.getDouble(VALUE_KEY))); return new ExprTupleValue(exemplarHashMap); } @@ -106,7 +107,6 @@ public ExprValue next() { return responseIterator.next(); } - @Override public ExecutionEngine.Schema schema() { return schema; diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/QueryRangeFunctionResponseHandle.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/QueryRangeFunctionResponseHandle.java index a3c68617e8..e10c9d7aff 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/QueryRangeFunctionResponseHandle.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/QueryRangeFunctionResponseHandle.java @@ -30,9 +30,7 @@ import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.executor.ExecutionEngine; -/** - * Default implementation of QueryRangeFunctionResponseHandle. - */ +/** Default implementation of QueryRangeFunctionResponseHandle. */ public class QueryRangeFunctionResponseHandle implements PrometheusFunctionResponseHandle { private final JSONObject responseObject; @@ -62,25 +60,26 @@ private void constructIterator() { result.add(new ExprTupleValue(linkedHashMap)); } } else { - throw new RuntimeException(String.format("Unexpected Result Type: %s during Prometheus " - + "Response Parsing. 'matrix' resultType is expected", - responseObject.getString("resultType"))); + throw new RuntimeException( + String.format( + "Unexpected Result Type: %s during Prometheus " + + "Response Parsing. 'matrix' resultType is expected", + responseObject.getString("resultType"))); } this.responseIterator = result.iterator(); } - private static void extractTimestampAndValues(JSONArray values, - LinkedHashMap linkedHashMap) { + private static void extractTimestampAndValues( + JSONArray values, LinkedHashMap linkedHashMap) { List timestampList = new ArrayList<>(); List valueList = new ArrayList<>(); for (int j = 0; j < values.length(); j++) { JSONArray value = values.getJSONArray(j); - timestampList.add(new ExprTimestampValue( - Instant.ofEpochMilli((long) (value.getDouble(0) * 1000)))); + timestampList.add( + new ExprTimestampValue(Instant.ofEpochMilli((long) (value.getDouble(0) * 1000)))); valueList.add(new ExprDoubleValue(value.getDouble(1))); } - linkedHashMap.put(TIMESTAMP, - new ExprCollectionValue(timestampList)); + linkedHashMap.put(TIMESTAMP, new ExprCollectionValue(timestampList)); linkedHashMap.put(VALUE, new ExprCollectionValue(valueList)); } @@ -90,12 +89,10 @@ private void constructSchema() { private ExprValue extractLabels(JSONObject metric) { LinkedHashMap labelsMap = new LinkedHashMap<>(); - metric.keySet().forEach(key - -> labelsMap.put(key, new ExprStringValue(metric.getString(key)))); + metric.keySet().forEach(key -> labelsMap.put(key, new ExprStringValue(metric.getString(key)))); return new ExprTupleValue(labelsMap); } - private List getColumnList() { List columnList = new ArrayList<>(); columnList.add(new ExecutionEngine.Schema.Column(LABELS, LABELS, ExprCoreType.STRUCT)); diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanBuilder.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanBuilder.java index 8364173889..7e779eb77c 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanBuilder.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanBuilder.java @@ -12,9 +12,7 @@ import org.opensearch.sql.storage.TableScanOperator; import org.opensearch.sql.storage.read.TableScanBuilder; -/** - * TableScanBuilder for query_exemplars table function of prometheus connector. - */ +/** TableScanBuilder for query_exemplars table function of prometheus connector. */ @AllArgsConstructor public class QueryExemplarsFunctionTableScanBuilder extends TableScanBuilder { @@ -24,8 +22,8 @@ public class QueryExemplarsFunctionTableScanBuilder extends TableScanBuilder { @Override public TableScanOperator build() { - return new QueryExemplarsFunctionTableScanOperator(prometheusClient, - prometheusQueryExemplarsRequest); + return new QueryExemplarsFunctionTableScanOperator( + prometheusClient, prometheusQueryExemplarsRequest); } // Since we are determining the schema after table scan, @@ -34,5 +32,4 @@ public TableScanOperator build() { public boolean pushDownProject(LogicalProject project) { return true; } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanOperator.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanOperator.java index 85ba6c854a..1a58429328 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanOperator.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanOperator.java @@ -22,37 +22,37 @@ import org.opensearch.sql.storage.TableScanOperator; /** - * This class is for QueryExemplars function {@link TableScanOperator}. - * This takes care of getting exemplar data from prometheus by making - * {@link PrometheusQueryExemplarsRequest}. + * This class is for QueryExemplars function {@link TableScanOperator}. This takes care of getting + * exemplar data from prometheus by making {@link PrometheusQueryExemplarsRequest}. */ @RequiredArgsConstructor public class QueryExemplarsFunctionTableScanOperator extends TableScanOperator { private final PrometheusClient prometheusClient; - @Getter - private final PrometheusQueryExemplarsRequest request; + @Getter private final PrometheusQueryExemplarsRequest request; private QueryExemplarsFunctionResponseHandle queryExemplarsFunctionResponseHandle; private static final Logger LOG = LogManager.getLogger(); @Override public void open() { super.open(); - this.queryExemplarsFunctionResponseHandle - = AccessController - .doPrivileged((PrivilegedAction) () -> { - try { - JSONArray responseArray = prometheusClient.queryExemplars( - request.getQuery(), - request.getStartTime(), request.getEndTime()); - return new QueryExemplarsFunctionResponseHandle(responseArray); - } catch (IOException e) { - LOG.error(e.getMessage()); - throw new RuntimeException( - String.format("Error fetching data from prometheus server: %s", e.getMessage())); - } - }); + this.queryExemplarsFunctionResponseHandle = + AccessController.doPrivileged( + (PrivilegedAction) + () -> { + try { + JSONArray responseArray = + prometheusClient.queryExemplars( + request.getQuery(), request.getStartTime(), request.getEndTime()); + return new QueryExemplarsFunctionResponseHandle(responseArray); + } catch (IOException e) { + LOG.error(e.getMessage()); + throw new RuntimeException( + String.format( + "Error fetching data from prometheus server: %s", e.getMessage())); + } + }); } @Override @@ -72,7 +72,9 @@ public ExprValue next() { @Override public String explain() { - return String.format(Locale.ROOT, "query_exemplars(%s, %s, %s)", + return String.format( + Locale.ROOT, + "query_exemplars(%s, %s, %s)", request.getQuery(), request.getStartTime(), request.getEndTime()); diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanBuilder.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanBuilder.java index 00e2191d09..2d22c0af69 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanBuilder.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanBuilder.java @@ -15,9 +15,8 @@ import org.opensearch.sql.storage.read.TableScanBuilder; /** - * TableScanBuilder for query_range table function of prometheus connector. - * we can merge this when we refactor for existing - * ppl queries based on prometheus connector. + * TableScanBuilder for query_range table function of prometheus connector. we can merge this when + * we refactor for existing ppl queries based on prometheus connector. */ @AllArgsConstructor public class QueryRangeFunctionTableScanBuilder extends TableScanBuilder { diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanOperator.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanOperator.java index 68b9b60643..fc3f9f9a9b 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanOperator.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanOperator.java @@ -23,9 +23,7 @@ import org.opensearch.sql.prometheus.request.PrometheusQueryRequest; import org.opensearch.sql.storage.TableScanOperator; -/** - * This a table scan operator to handle Query Range table function. - */ +/** This a table scan operator to handle Query Range table function. */ @RequiredArgsConstructor public class QueryRangeFunctionTableScanOperator extends TableScanOperator { @@ -39,19 +37,25 @@ public class QueryRangeFunctionTableScanOperator extends TableScanOperator { @Override public void open() { super.open(); - this.prometheusResponseHandle - = AccessController.doPrivileged((PrivilegedAction) () -> { - try { - JSONObject responseObject = prometheusClient.queryRange( - request.getPromQl(), - request.getStartTime(), request.getEndTime(), request.getStep()); - return new QueryRangeFunctionResponseHandle(responseObject); - } catch (IOException e) { - LOG.error(e.getMessage()); - throw new RuntimeException( - String.format("Error fetching data from prometheus server: %s", e.getMessage())); - } - }); + this.prometheusResponseHandle = + AccessController.doPrivileged( + (PrivilegedAction) + () -> { + try { + JSONObject responseObject = + prometheusClient.queryRange( + request.getPromQl(), + request.getStartTime(), + request.getEndTime(), + request.getStep()); + return new QueryRangeFunctionResponseHandle(responseObject); + } catch (IOException e) { + LOG.error(e.getMessage()); + throw new RuntimeException( + String.format( + "Error fetching data from prometheus server: %s", e.getMessage())); + } + }); } @Override @@ -71,7 +75,9 @@ public ExprValue next() { @Override public String explain() { - return String.format(Locale.ROOT, "query_range(%s, %s, %s, %s)", + return String.format( + Locale.ROOT, + "query_range(%s, %s, %s, %s)", request.getPromQl(), request.getStartTime(), request.getEndTime(), diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalMetricAgg.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalMetricAgg.java index f348c699a1..f7c45f6ad2 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalMetricAgg.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalMetricAgg.java @@ -20,10 +20,7 @@ import org.opensearch.sql.planner.logical.LogicalPlan; import org.opensearch.sql.planner.logical.LogicalPlanNodeVisitor; - -/** - * Logical Metric Scan along with aggregation Operation. - */ +/** Logical Metric Scan along with aggregation Operation. */ @Getter @ToString @EqualsAndHashCode(callSuper = false) @@ -31,37 +28,29 @@ public class PrometheusLogicalMetricAgg extends LogicalPlan { private final String metricName; - /** - * Filter Condition. - */ - @Setter - private Expression filter; + /** Filter Condition. */ + @Setter private Expression filter; - /** - * Aggregation List. - */ - @Setter - private List aggregatorList; + /** Aggregation List. */ + @Setter private List aggregatorList; - /** - * Group List. - */ - @Setter - private List groupByList; + /** Group List. */ + @Setter private List groupByList; /** * Constructor for LogicalMetricAgg Logical Plan. * - * @param metricName metricName - * @param filter filter + * @param metricName metricName + * @param filter filter * @param aggregatorList aggregatorList - * @param groupByList groupByList. + * @param groupByList groupByList. */ @Builder - public PrometheusLogicalMetricAgg(String metricName, - Expression filter, - List aggregatorList, - List groupByList) { + public PrometheusLogicalMetricAgg( + String metricName, + Expression filter, + List aggregatorList, + List groupByList) { super(ImmutableList.of()); this.metricName = metricName; this.filter = filter; diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalMetricScan.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalMetricScan.java index 5e07d6899f..7b28a8a6c9 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalMetricScan.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalMetricScan.java @@ -17,8 +17,8 @@ import org.opensearch.sql.planner.logical.LogicalPlanNodeVisitor; /** - * Prometheus Logical Metric Scan Operation. - * In an optimized plan this node represents both Relation and Filter Operation. + * Prometheus Logical Metric Scan Operation. In an optimized plan this node represents both Relation + * and Filter Operation. */ @Getter @ToString @@ -27,9 +27,7 @@ public class PrometheusLogicalMetricScan extends LogicalPlan { private final String metricName; - /** - * Filter Condition. - */ + /** Filter Condition. */ private final Expression filter; /** @@ -39,8 +37,7 @@ public class PrometheusLogicalMetricScan extends LogicalPlan { * @param filter filter. */ @Builder - public PrometheusLogicalMetricScan(String metricName, - Expression filter) { + public PrometheusLogicalMetricScan(String metricName, Expression filter) { super(ImmutableList.of()); this.metricName = metricName; this.filter = filter; @@ -50,5 +47,4 @@ public PrometheusLogicalMetricScan(String metricName, public R accept(LogicalPlanNodeVisitor visitor, C context) { return visitor.visitNode(this, context); } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalPlanOptimizerFactory.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalPlanOptimizerFactory.java index 8a365b2786..ea14be0e0a 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalPlanOptimizerFactory.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalPlanOptimizerFactory.java @@ -5,7 +5,6 @@ package org.opensearch.sql.prometheus.planner.logical; - import java.util.Arrays; import lombok.experimental.UtilityClass; import org.opensearch.sql.planner.optimizer.LogicalPlanOptimizer; @@ -13,20 +12,14 @@ import org.opensearch.sql.prometheus.planner.logical.rules.MergeAggAndRelation; import org.opensearch.sql.prometheus.planner.logical.rules.MergeFilterAndRelation; -/** - * Prometheus storage engine specified logical plan optimizer. - */ +/** Prometheus storage engine specified logical plan optimizer. */ @UtilityClass public class PrometheusLogicalPlanOptimizerFactory { - /** - * Create Prometheus storage specified logical plan optimizer. - */ + /** Create Prometheus storage specified logical plan optimizer. */ public static LogicalPlanOptimizer create() { - return new LogicalPlanOptimizer(Arrays.asList( - new MergeFilterAndRelation(), - new MergeAggAndIndexScan(), - new MergeAggAndRelation() - )); + return new LogicalPlanOptimizer( + Arrays.asList( + new MergeFilterAndRelation(), new MergeAggAndIndexScan(), new MergeAggAndRelation())); } } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeAggAndIndexScan.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeAggAndIndexScan.java index 76bc6cc840..2594b74eb5 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeAggAndIndexScan.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeAggAndIndexScan.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.prometheus.planner.logical.rules; import static com.facebook.presto.matching.Pattern.typeOf; @@ -20,9 +19,7 @@ import org.opensearch.sql.prometheus.planner.logical.PrometheusLogicalMetricAgg; import org.opensearch.sql.prometheus.planner.logical.PrometheusLogicalMetricScan; -/** - * Merge Aggregation -- Relation to MetricScanAggregation. - */ +/** Merge Aggregation -- Relation to MetricScanAggregation. */ public class MergeAggAndIndexScan implements Rule { private final Capture capture; @@ -31,22 +28,18 @@ public class MergeAggAndIndexScan implements Rule { @Getter private final Pattern pattern; - /** - * Constructor of MergeAggAndIndexScan. - */ + /** Constructor of MergeAggAndIndexScan. */ public MergeAggAndIndexScan() { this.capture = Capture.newCapture(); - this.pattern = typeOf(LogicalAggregation.class) - .with(source().matching(typeOf(PrometheusLogicalMetricScan.class) - .capturedAs(capture))); + this.pattern = + typeOf(LogicalAggregation.class) + .with(source().matching(typeOf(PrometheusLogicalMetricScan.class).capturedAs(capture))); } @Override - public LogicalPlan apply(LogicalAggregation aggregation, - Captures captures) { + public LogicalPlan apply(LogicalAggregation aggregation, Captures captures) { PrometheusLogicalMetricScan indexScan = captures.get(capture); - return PrometheusLogicalMetricAgg - .builder() + return PrometheusLogicalMetricAgg.builder() .metricName(indexScan.getMetricName()) .filter(indexScan.getFilter()) .aggregatorList(aggregation.getAggregatorList()) diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeAggAndRelation.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeAggAndRelation.java index fa9b0c7206..e6170e41f9 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeAggAndRelation.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeAggAndRelation.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.prometheus.planner.logical.rules; import static com.facebook.presto.matching.Pattern.typeOf; @@ -20,9 +19,7 @@ import org.opensearch.sql.planner.optimizer.Rule; import org.opensearch.sql.prometheus.planner.logical.PrometheusLogicalMetricAgg; -/** - * Merge Aggregation -- Relation to IndexScanAggregation. - */ +/** Merge Aggregation -- Relation to IndexScanAggregation. */ public class MergeAggAndRelation implements Rule { private final Capture relationCapture; @@ -31,21 +28,18 @@ public class MergeAggAndRelation implements Rule { @Getter private final Pattern pattern; - /** - * Constructor of MergeAggAndRelation. - */ + /** Constructor of MergeAggAndRelation. */ public MergeAggAndRelation() { this.relationCapture = Capture.newCapture(); - this.pattern = typeOf(LogicalAggregation.class) - .with(source().matching(typeOf(LogicalRelation.class).capturedAs(relationCapture))); + this.pattern = + typeOf(LogicalAggregation.class) + .with(source().matching(typeOf(LogicalRelation.class).capturedAs(relationCapture))); } @Override - public LogicalPlan apply(LogicalAggregation aggregation, - Captures captures) { + public LogicalPlan apply(LogicalAggregation aggregation, Captures captures) { LogicalRelation relation = captures.get(relationCapture); - return PrometheusLogicalMetricAgg - .builder() + return PrometheusLogicalMetricAgg.builder() .metricName(relation.getRelationName()) .aggregatorList(aggregation.getAggregatorList()) .groupByList(aggregation.getGroupByList()) diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeFilterAndRelation.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeFilterAndRelation.java index a99eb695be..2013938d73 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeFilterAndRelation.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeFilterAndRelation.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.prometheus.planner.logical.rules; import static com.facebook.presto.matching.Pattern.typeOf; @@ -18,21 +17,18 @@ import org.opensearch.sql.planner.optimizer.Rule; import org.opensearch.sql.prometheus.planner.logical.PrometheusLogicalMetricScan; -/** - * Merge Filter -- Relation to LogicalMetricScan. - */ +/** Merge Filter -- Relation to LogicalMetricScan. */ public class MergeFilterAndRelation implements Rule { private final Capture relationCapture; private final Pattern pattern; - /** - * Constructor of MergeFilterAndRelation. - */ + /** Constructor of MergeFilterAndRelation. */ public MergeFilterAndRelation() { this.relationCapture = Capture.newCapture(); - this.pattern = typeOf(LogicalFilter.class) - .with(source().matching(typeOf(LogicalRelation.class).capturedAs(relationCapture))); + this.pattern = + typeOf(LogicalFilter.class) + .with(source().matching(typeOf(LogicalRelation.class).capturedAs(relationCapture))); } @Override @@ -41,11 +37,9 @@ public Pattern pattern() { } @Override - public LogicalPlan apply(LogicalFilter filter, - Captures captures) { + public LogicalPlan apply(LogicalFilter filter, Captures captures) { LogicalRelation relation = captures.get(relationCapture); - return PrometheusLogicalMetricScan - .builder() + return PrometheusLogicalMetricScan.builder() .metricName(relation.getRelationName()) .filter(filter.getCondition()) .build(); diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/PrometheusQueryExemplarsRequest.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/PrometheusQueryExemplarsRequest.java index 9cf3d41522..d4eea97c48 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/PrometheusQueryExemplarsRequest.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/PrometheusQueryExemplarsRequest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.prometheus.request; import lombok.AllArgsConstructor; @@ -12,9 +11,7 @@ import lombok.NoArgsConstructor; import lombok.ToString; -/** - * Prometheus metric query request. - */ +/** Prometheus metric query request. */ @EqualsAndHashCode @Data @ToString @@ -22,19 +19,12 @@ @NoArgsConstructor public class PrometheusQueryExemplarsRequest { - /** - * PromQL. - */ + /** PromQL. */ private String query; - /** - * startTime of the query. - */ + /** startTime of the query. */ private Long startTime; - /** - * endTime of the query. - */ + /** endTime of the query. */ private Long endTime; - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/PrometheusQueryRequest.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/PrometheusQueryRequest.java index d287ea4d65..e24c27c52a 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/PrometheusQueryRequest.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/PrometheusQueryRequest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.prometheus.request; import lombok.AllArgsConstructor; @@ -12,9 +11,7 @@ import lombok.NoArgsConstructor; import lombok.ToString; -/** - * Prometheus metric query request. - */ +/** Prometheus metric query request. */ @EqualsAndHashCode @Data @ToString @@ -22,24 +19,15 @@ @NoArgsConstructor public class PrometheusQueryRequest { - /** - * PromQL. - */ + /** PromQL. */ private String promQl; - /** - * startTime of the query. - */ + /** startTime of the query. */ private Long startTime; - /** - * endTime of the query. - */ + /** endTime of the query. */ private Long endTime; - /** - * step is the resolution required between startTime and endTime. - */ + /** step is the resolution required between startTime and endTime. */ private String step; - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusDescribeMetricRequest.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusDescribeMetricRequest.java index 2e0d46b3e8..b6a4e3c49c 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusDescribeMetricRequest.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusDescribeMetricRequest.java @@ -5,7 +5,6 @@ * */ - package org.opensearch.sql.prometheus.request.system; import static org.opensearch.sql.data.model.ExprValueUtils.stringValue; @@ -31,60 +30,63 @@ import org.opensearch.sql.prometheus.storage.PrometheusMetricDefaultSchema; /** - * Describe Metric metadata request. - * This is triggered in case of both query range table function and relation. - * In case of table function metric name is null. + * Describe Metric metadata request. This is triggered in case of both query range table function + * and relation. In case of table function metric name is null. */ @ToString(onlyExplicitlyIncluded = true) public class PrometheusDescribeMetricRequest implements PrometheusSystemRequest { private final PrometheusClient prometheusClient; - @ToString.Include - private final String metricName; + @ToString.Include private final String metricName; private final DataSourceSchemaName dataSourceSchemaName; private static final Logger LOG = LogManager.getLogger(); /** - * Constructor for Prometheus Describe Metric Request. - * In case of pass through queries like query_range function, - * metric names are optional. + * Constructor for Prometheus Describe Metric Request. In case of pass through queries like + * query_range function, metric names are optional. * - * @param prometheusClient prometheusClient. + * @param prometheusClient prometheusClient. * @param dataSourceSchemaName dataSourceSchemaName. - * @param metricName metricName. + * @param metricName metricName. */ - public PrometheusDescribeMetricRequest(PrometheusClient prometheusClient, - DataSourceSchemaName dataSourceSchemaName, - @NonNull String metricName) { + public PrometheusDescribeMetricRequest( + PrometheusClient prometheusClient, + DataSourceSchemaName dataSourceSchemaName, + @NonNull String metricName) { this.prometheusClient = prometheusClient; this.metricName = metricName; this.dataSourceSchemaName = dataSourceSchemaName; } - /** - * Get the mapping of field and type. - * Returns labels and default schema fields. + * Get the mapping of field and type. Returns labels and default schema fields. * * @return mapping of field and type. */ public Map getFieldTypes() { Map fieldTypes = new HashMap<>(); - AccessController.doPrivileged((PrivilegedAction>) () -> { - try { - prometheusClient.getLabels(metricName) - .forEach(label -> fieldTypes.put(label, ExprCoreType.STRING)); - } catch (IOException e) { - LOG.error("Error while fetching labels for {} from prometheus: {}", - metricName, e.getMessage()); - throw new RuntimeException(String.format("Error while fetching labels " - + "for %s from prometheus: %s", metricName, e.getMessage())); - } - return null; - }); + AccessController.doPrivileged( + (PrivilegedAction>) + () -> { + try { + prometheusClient + .getLabels(metricName) + .forEach(label -> fieldTypes.put(label, ExprCoreType.STRING)); + } catch (IOException e) { + LOG.error( + "Error while fetching labels for {} from prometheus: {}", + metricName, + e.getMessage()); + throw new RuntimeException( + String.format( + "Error while fetching labels " + "for %s from prometheus: %s", + metricName, e.getMessage())); + } + return null; + }); fieldTypes.putAll(PrometheusMetricDefaultSchema.DEFAULT_MAPPING.getMapping()); return fieldTypes; } @@ -93,14 +95,17 @@ public Map getFieldTypes() { public List search() { List results = new ArrayList<>(); for (Map.Entry entry : getFieldTypes().entrySet()) { - results.add(row(entry.getKey(), entry.getValue().legacyTypeName().toLowerCase(), - dataSourceSchemaName)); + results.add( + row( + entry.getKey(), + entry.getValue().legacyTypeName().toLowerCase(), + dataSourceSchemaName)); } return results; } - private ExprTupleValue row(String fieldName, String fieldType, - DataSourceSchemaName dataSourceSchemaName) { + private ExprTupleValue row( + String fieldName, String fieldType, DataSourceSchemaName dataSourceSchemaName) { LinkedHashMap valueMap = new LinkedHashMap<>(); valueMap.put("TABLE_CATALOG", stringValue(dataSourceSchemaName.getDataSourceName())); valueMap.put("TABLE_SCHEMA", stringValue(dataSourceSchemaName.getSchemaName())); diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusListMetricsRequest.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusListMetricsRequest.java index f5d2a44340..0e6c2bb2c6 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusListMetricsRequest.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusListMetricsRequest.java @@ -34,28 +34,33 @@ public class PrometheusListMetricsRequest implements PrometheusSystemRequest { private static final Logger LOG = LogManager.getLogger(); - @Override public List search() { - return AccessController.doPrivileged((PrivilegedAction>) () -> { - try { - Map> result = prometheusClient.getAllMetrics(); - return result.keySet() - .stream() - .map(x -> { - MetricMetadata metricMetadata = result.get(x).get(0); - return row(x, metricMetadata.getType(), - metricMetadata.getUnit(), metricMetadata.getHelp()); - }) - .collect(Collectors.toList()); - } catch (IOException e) { - LOG.error("Error while fetching metric list for from prometheus: {}", - e.getMessage()); - throw new RuntimeException(String.format("Error while fetching metric list " - + "for from prometheus: %s", e.getMessage())); - } - }); - + return AccessController.doPrivileged( + (PrivilegedAction>) + () -> { + try { + Map> result = prometheusClient.getAllMetrics(); + return result.keySet().stream() + .map( + x -> { + MetricMetadata metricMetadata = result.get(x).get(0); + return row( + x, + metricMetadata.getType(), + metricMetadata.getUnit(), + metricMetadata.getHelp()); + }) + .collect(Collectors.toList()); + } catch (IOException e) { + LOG.error( + "Error while fetching metric list for from prometheus: {}", e.getMessage()); + throw new RuntimeException( + String.format( + "Error while fetching metric list " + "for from prometheus: %s", + e.getMessage())); + } + }); } private ExprTupleValue row(String metricName, String tableType, String unit, String help) { diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusSystemRequest.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusSystemRequest.java index e68ad22c30..6972a9390c 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusSystemRequest.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusSystemRequest.java @@ -10,9 +10,7 @@ import java.util.List; import org.opensearch.sql.data.model.ExprValue; -/** - * Prometheus system request query to get metadata Info. - */ +/** Prometheus system request query to get metadata Info. */ public interface PrometheusSystemRequest { /** @@ -21,5 +19,4 @@ public interface PrometheusSystemRequest { * @return list of ExprValue. */ List search(); - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/response/PrometheusResponse.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/response/PrometheusResponse.java index ca250125e6..339d882f5a 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/response/PrometheusResponse.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/response/PrometheusResponse.java @@ -42,13 +42,12 @@ public class PrometheusResponse implements Iterable { /** * Constructor. * - * @param responseObject Prometheus responseObject. - * @param prometheusResponseFieldNames data model which - * contains field names for the metric measurement - * and timestamp fieldName. + * @param responseObject Prometheus responseObject. + * @param prometheusResponseFieldNames data model which contains field names for the metric + * measurement and timestamp fieldName. */ - public PrometheusResponse(JSONObject responseObject, - PrometheusResponseFieldNames prometheusResponseFieldNames) { + public PrometheusResponse( + JSONObject responseObject, PrometheusResponseFieldNames prometheusResponseFieldNames) { this.responseObject = responseObject; this.prometheusResponseFieldNames = prometheusResponseFieldNames; } @@ -66,18 +65,22 @@ public Iterator iterator() { for (int j = 0; j < values.length(); j++) { LinkedHashMap linkedHashMap = new LinkedHashMap<>(); JSONArray val = values.getJSONArray(j); - linkedHashMap.put(prometheusResponseFieldNames.getTimestampFieldName(), + linkedHashMap.put( + prometheusResponseFieldNames.getTimestampFieldName(), new ExprTimestampValue(Instant.ofEpochMilli((long) (val.getDouble(0) * 1000)))); - linkedHashMap.put(prometheusResponseFieldNames.getValueFieldName(), getValue(val, 1, - prometheusResponseFieldNames.getValueType())); + linkedHashMap.put( + prometheusResponseFieldNames.getValueFieldName(), + getValue(val, 1, prometheusResponseFieldNames.getValueType())); insertLabels(linkedHashMap, metric); result.add(new ExprTupleValue(linkedHashMap)); } } } else { - throw new RuntimeException(String.format("Unexpected Result Type: %s during Prometheus " - + "Response Parsing. 'matrix' resultType is expected", - responseObject.getString(RESULT_TYPE_KEY))); + throw new RuntimeException( + String.format( + "Unexpected Result Type: %s during Prometheus " + + "Response Parsing. 'matrix' resultType is expected", + responseObject.getString(RESULT_TYPE_KEY))); } return result.iterator(); } @@ -103,12 +106,11 @@ private String getKey(String key) { } else { return this.prometheusResponseFieldNames.getGroupByList().stream() .filter(expression -> expression.getDelegated() instanceof ReferenceExpression) - .filter(expression - -> ((ReferenceExpression) expression.getDelegated()).getAttr().equals(key)) + .filter( + expression -> ((ReferenceExpression) expression.getDelegated()).getAttr().equals(key)) .findFirst() .map(NamedExpression::getName) .orElse(key); } } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricDefaultSchema.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricDefaultSchema.java index 790189d903..f0933eee9d 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricDefaultSchema.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricDefaultSchema.java @@ -20,12 +20,11 @@ @Getter @RequiredArgsConstructor public enum PrometheusMetricDefaultSchema { - - DEFAULT_MAPPING(new ImmutableMap.Builder() - .put(TIMESTAMP, ExprCoreType.TIMESTAMP) - .put(VALUE, ExprCoreType.DOUBLE) - .build()); + DEFAULT_MAPPING( + new ImmutableMap.Builder() + .put(TIMESTAMP, ExprCoreType.TIMESTAMP) + .put(VALUE, ExprCoreType.DOUBLE) + .build()); private final Map mapping; - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricScan.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricScan.java index 7f75cb3c07..598e388914 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricScan.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricScan.java @@ -23,26 +23,19 @@ import org.opensearch.sql.prometheus.storage.model.PrometheusResponseFieldNames; import org.opensearch.sql.storage.TableScanOperator; -/** - * Prometheus metric scan operator. - */ +/** Prometheus metric scan operator. */ @EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false) @ToString(onlyExplicitlyIncluded = true) public class PrometheusMetricScan extends TableScanOperator { private final PrometheusClient prometheusClient; - @EqualsAndHashCode.Include - @Getter - @Setter - @ToString.Include + @EqualsAndHashCode.Include @Getter @Setter @ToString.Include private PrometheusQueryRequest request; private Iterator iterator; - @Setter - private PrometheusResponseFieldNames prometheusResponseFieldNames; - + @Setter private PrometheusResponseFieldNames prometheusResponseFieldNames; private static final Logger LOG = LogManager.getLogger(); @@ -60,17 +53,25 @@ public PrometheusMetricScan(PrometheusClient prometheusClient) { @Override public void open() { super.open(); - this.iterator = AccessController.doPrivileged((PrivilegedAction>) () -> { - try { - JSONObject responseObject = prometheusClient.queryRange( - request.getPromQl(), - request.getStartTime(), request.getEndTime(), request.getStep()); - return new PrometheusResponse(responseObject, prometheusResponseFieldNames).iterator(); - } catch (IOException e) { - LOG.error(e.getMessage()); - throw new RuntimeException("Error fetching data from prometheus server. " + e.getMessage()); - } - }); + this.iterator = + AccessController.doPrivileged( + (PrivilegedAction>) + () -> { + try { + JSONObject responseObject = + prometheusClient.queryRange( + request.getPromQl(), + request.getStartTime(), + request.getEndTime(), + request.getStep()); + return new PrometheusResponse(responseObject, prometheusResponseFieldNames) + .iterator(); + } catch (IOException e) { + LOG.error(e.getMessage()); + throw new RuntimeException( + "Error fetching data from prometheus server. " + e.getMessage()); + } + }); } @Override diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTable.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTable.java index 4844e1f6db..1124e93608 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTable.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTable.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.prometheus.storage; import static org.opensearch.sql.prometheus.data.constants.PrometheusFieldConstants.LABELS; @@ -26,40 +25,30 @@ import org.opensearch.sql.storage.read.TableScanBuilder; /** - * Prometheus table (metric) implementation. - * This can be constructed from a metric Name - * or from PrometheusQueryRequest In case of query_range table function. + * Prometheus table (metric) implementation. This can be constructed from a metric Name or from + * PrometheusQueryRequest In case of query_range table function. */ public class PrometheusMetricTable implements Table { private final PrometheusClient prometheusClient; - @Getter - private final String metricName; - - @Getter - private final PrometheusQueryRequest prometheusQueryRequest; + @Getter private final String metricName; + @Getter private final PrometheusQueryRequest prometheusQueryRequest; - /** - * The cached mapping of field and type in index. - */ + /** The cached mapping of field and type in index. */ private Map cachedFieldTypes = null; - /** - * Constructor only with metric name. - */ + /** Constructor only with metric name. */ public PrometheusMetricTable(PrometheusClient prometheusService, @Nonnull String metricName) { this.prometheusClient = prometheusService; this.metricName = metricName; this.prometheusQueryRequest = null; } - /** - * Constructor for entire promQl Request. - */ - public PrometheusMetricTable(PrometheusClient prometheusService, - @Nonnull PrometheusQueryRequest prometheusQueryRequest) { + /** Constructor for entire promQl Request. */ + public PrometheusMetricTable( + PrometheusClient prometheusService, @Nonnull PrometheusQueryRequest prometheusQueryRequest) { this.prometheusClient = prometheusService; this.metricName = null; this.prometheusQueryRequest = prometheusQueryRequest; @@ -67,14 +56,12 @@ public PrometheusMetricTable(PrometheusClient prometheusService, @Override public boolean exists() { - throw new UnsupportedOperationException( - "Prometheus metric exists operation is not supported"); + throw new UnsupportedOperationException("Prometheus metric exists operation is not supported"); } @Override public void create(Map schema) { - throw new UnsupportedOperationException( - "Prometheus metric create operation is not supported"); + throw new UnsupportedOperationException("Prometheus metric create operation is not supported"); } @Override @@ -82,11 +69,10 @@ public Map getFieldTypes() { if (cachedFieldTypes == null) { if (metricName != null) { cachedFieldTypes = - new PrometheusDescribeMetricRequest(prometheusClient, null, - metricName).getFieldTypes(); + new PrometheusDescribeMetricRequest(prometheusClient, null, metricName).getFieldTypes(); } else { - cachedFieldTypes = new HashMap<>(PrometheusMetricDefaultSchema.DEFAULT_MAPPING - .getMapping()); + cachedFieldTypes = + new HashMap<>(PrometheusMetricDefaultSchema.DEFAULT_MAPPING.getMapping()); cachedFieldTypes.put(LABELS, ExprCoreType.STRING); } } @@ -95,8 +81,7 @@ public Map getFieldTypes() { @Override public PhysicalPlan implement(LogicalPlan plan) { - PrometheusMetricScan metricScan = - new PrometheusMetricScan(prometheusClient); + PrometheusMetricScan metricScan = new PrometheusMetricScan(prometheusClient); return plan.accept(new PrometheusDefaultImplementor(), metricScan); } @@ -105,8 +90,8 @@ public LogicalPlan optimize(LogicalPlan plan) { return PrometheusLogicalPlanOptimizerFactory.create().optimize(plan); } - //Only handling query_range function for now. - //we need to move PPL implementations to ScanBuilder in future. + // Only handling query_range function for now. + // we need to move PPL implementations to ScanBuilder in future. @Override public TableScanBuilder createScanBuilder() { if (metricName == null) { diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageEngine.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageEngine.java index 738eb023b6..29fc15e2d0 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageEngine.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageEngine.java @@ -24,10 +24,7 @@ import org.opensearch.sql.storage.Table; import org.opensearch.sql.utils.SystemIndexUtils; - -/** - * Prometheus storage engine implementation. - */ +/** Prometheus storage engine implementation. */ @RequiredArgsConstructor public class PrometheusStorageEngine implements StorageEngine { @@ -52,16 +49,14 @@ public Table getTable(DataSourceSchemaName dataSourceSchemaName, String tableNam } } - private Table resolveInformationSchemaTable(DataSourceSchemaName dataSourceSchemaName, - String tableName) { + private Table resolveInformationSchemaTable( + DataSourceSchemaName dataSourceSchemaName, String tableName) { if (SystemIndexUtils.TABLE_NAME_FOR_TABLES_INFO.equals(tableName)) { - return new PrometheusSystemTable(prometheusClient, - dataSourceSchemaName, SystemIndexUtils.TABLE_INFO); + return new PrometheusSystemTable( + prometheusClient, dataSourceSchemaName, SystemIndexUtils.TABLE_INFO); } else { throw new SemanticCheckException( String.format("Information Schema doesn't contain %s table", tableName)); } } - - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactory.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactory.java index b3ecd25af3..edae263ce3 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactory.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactory.java @@ -56,23 +56,20 @@ public DataSourceType getDataSourceType() { @Override public DataSource createDataSource(DataSourceMetadata metadata) { return new DataSource( - metadata.getName(), - DataSourceType.PROMETHEUS, - getStorageEngine(metadata.getProperties())); + metadata.getName(), DataSourceType.PROMETHEUS, getStorageEngine(metadata.getProperties())); } - - //Need to refactor to a separate Validator class. + // Need to refactor to a separate Validator class. private void validateDataSourceConfigProperties(Map dataSourceMetadataConfig) throws URISyntaxException { if (dataSourceMetadataConfig.get(AUTH_TYPE) != null) { - AuthenticationType authenticationType - = AuthenticationType.get(dataSourceMetadataConfig.get(AUTH_TYPE)); + AuthenticationType authenticationType = + AuthenticationType.get(dataSourceMetadataConfig.get(AUTH_TYPE)); if (AuthenticationType.BASICAUTH.equals(authenticationType)) { validateMissingFields(dataSourceMetadataConfig, Set.of(URI, USERNAME, PASSWORD)); } else if (AuthenticationType.AWSSIGV4AUTH.equals(authenticationType)) { - validateMissingFields(dataSourceMetadataConfig, Set.of(URI, ACCESS_KEY, SECRET_KEY, - REGION)); + validateMissingFields( + dataSourceMetadataConfig, Set.of(URI, ACCESS_KEY, SECRET_KEY, REGION)); } } else { validateMissingFields(dataSourceMetadataConfig, Set.of(URI)); @@ -83,20 +80,21 @@ private void validateDataSourceConfigProperties(Map dataSourceMe StorageEngine getStorageEngine(Map requiredConfig) { PrometheusClient prometheusClient; prometheusClient = - AccessController.doPrivileged((PrivilegedAction) () -> { - try { - validateDataSourceConfigProperties(requiredConfig); - return new PrometheusClientImpl(getHttpClient(requiredConfig), - new URI(requiredConfig.get(URI))); - } catch (URISyntaxException e) { - throw new IllegalArgumentException( - String.format("Invalid URI in prometheus properties: %s", e.getMessage())); - } - }); + AccessController.doPrivileged( + (PrivilegedAction) + () -> { + try { + validateDataSourceConfigProperties(requiredConfig); + return new PrometheusClientImpl( + getHttpClient(requiredConfig), new URI(requiredConfig.get(URI))); + } catch (URISyntaxException e) { + throw new IllegalArgumentException( + String.format("Invalid URI in prometheus properties: %s", e.getMessage())); + } + }); return new PrometheusStorageEngine(prometheusClient); } - private OkHttpClient getHttpClient(Map config) { OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder(); okHttpClient.callTimeout(1, TimeUnit.MINUTES); @@ -104,16 +102,19 @@ private OkHttpClient getHttpClient(Map config) { if (config.get(AUTH_TYPE) != null) { AuthenticationType authenticationType = AuthenticationType.get(config.get(AUTH_TYPE)); if (AuthenticationType.BASICAUTH.equals(authenticationType)) { - okHttpClient.addInterceptor(new BasicAuthenticationInterceptor(config.get(USERNAME), - config.get(PASSWORD))); + okHttpClient.addInterceptor( + new BasicAuthenticationInterceptor(config.get(USERNAME), config.get(PASSWORD))); } else if (AuthenticationType.AWSSIGV4AUTH.equals(authenticationType)) { - okHttpClient.addInterceptor(new AwsSigningInterceptor( - new AWSStaticCredentialsProvider( - new BasicAWSCredentials(config.get(ACCESS_KEY), config.get(SECRET_KEY))), - config.get(REGION), "aps")); + okHttpClient.addInterceptor( + new AwsSigningInterceptor( + new AWSStaticCredentialsProvider( + new BasicAWSCredentials(config.get(ACCESS_KEY), config.get(SECRET_KEY))), + config.get(REGION), + "aps")); } else { throw new IllegalArgumentException( - String.format("AUTH Type : %s is not supported with Prometheus Connector", + String.format( + "AUTH Type : %s is not supported with Prometheus Connector", config.get(AUTH_TYPE))); } } @@ -132,13 +133,14 @@ private void validateMissingFields(Map config, Set field } StringBuilder errorStringBuilder = new StringBuilder(); if (missingFields.size() > 0) { - errorStringBuilder.append(String.format( - "Missing %s fields in the Prometheus connector properties.", missingFields)); + errorStringBuilder.append( + String.format( + "Missing %s fields in the Prometheus connector properties.", missingFields)); } if (invalidLengthFields.size() > 0) { - errorStringBuilder.append(String.format( - "Fields %s exceeds more than 1000 characters.", invalidLengthFields)); + errorStringBuilder.append( + String.format("Fields %s exceeds more than 1000 characters.", invalidLengthFields)); } if (errorStringBuilder.length() > 0) { throw new IllegalArgumentException(errorStringBuilder.toString()); @@ -148,8 +150,9 @@ private void validateMissingFields(Map config, Set field private void validateURI(Map config) throws URISyntaxException { URI uri = new URI(config.get(URI)); String host = uri.getHost(); - if (host == null || (!(DomainValidator.getInstance().isValid(host) - || DomainValidator.getInstance().isValidLocalTld(host)))) { + if (host == null + || (!(DomainValidator.getInstance().isValid(host) + || DomainValidator.getInstance().isValidLocalTld(host)))) { throw new IllegalArgumentException( String.format("Invalid hostname in the uri: %s", config.get(URI))); } else { @@ -158,10 +161,10 @@ private void validateURI(Map config) throws URISyntaxException { Matcher matcher = allowHostsPattern.matcher(host); if (!matcher.matches()) { throw new IllegalArgumentException( - String.format("Disallowed hostname in the uri: %s. Validate with %s config", + String.format( + "Disallowed hostname in the uri: %s. Validate with %s config", config.get(URI), Settings.Key.DATASOURCES_URI_ALLOWHOSTS.getKeyValue())); } } } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/QueryExemplarsTable.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/QueryExemplarsTable.java index dcb87c2cce..9ce8ae85fb 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/QueryExemplarsTable.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/QueryExemplarsTable.java @@ -20,20 +20,16 @@ import org.opensearch.sql.storage.read.TableScanBuilder; /** - * This is {@link Table} for querying exemplars in prometheus Table. - * Since {@link PrometheusMetricTable} is overloaded with query_range and normal - * PPL metric queries. Created a separate table for handling - * {@link PrometheusQueryExemplarsRequest} + * This is {@link Table} for querying exemplars in prometheus Table. Since {@link + * PrometheusMetricTable} is overloaded with query_range and normal PPL metric queries. Created a + * separate table for handling {@link PrometheusQueryExemplarsRequest} */ @RequiredArgsConstructor public class QueryExemplarsTable implements Table { - @Getter - private final PrometheusClient prometheusClient; - - @Getter - private final PrometheusQueryExemplarsRequest exemplarsRequest; + @Getter private final PrometheusClient prometheusClient; + @Getter private final PrometheusQueryExemplarsRequest exemplarsRequest; @Override public Map getFieldTypes() { @@ -49,5 +45,4 @@ public PhysicalPlan implement(LogicalPlan plan) { public TableScanBuilder createScanBuilder() { return new QueryExemplarsFunctionTableScanBuilder(prometheusClient, exemplarsRequest); } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/implementor/PrometheusDefaultImplementor.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/implementor/PrometheusDefaultImplementor.java index 6d426d13c8..f83a97dc06 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/implementor/PrometheusDefaultImplementor.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/implementor/PrometheusDefaultImplementor.java @@ -29,13 +29,9 @@ import org.opensearch.sql.prometheus.storage.querybuilder.StepParameterResolver; import org.opensearch.sql.prometheus.storage.querybuilder.TimeRangeParametersResolver; -/** - * Default Implementor of Logical plan for prometheus. - */ +/** Default Implementor of Logical plan for prometheus. */ @RequiredArgsConstructor -public class PrometheusDefaultImplementor - extends DefaultImplementor { - +public class PrometheusDefaultImplementor extends DefaultImplementor { @Override public PhysicalPlan visitNode(LogicalPlan plan, PrometheusMetricScan context) { @@ -44,62 +40,64 @@ public PhysicalPlan visitNode(LogicalPlan plan, PrometheusMetricScan context) { } else if (plan instanceof PrometheusLogicalMetricAgg) { return visitIndexAggregation((PrometheusLogicalMetricAgg) plan, context); } else { - throw new IllegalStateException(StringUtils.format("unexpected plan node type %s", - plan.getClass())); + throw new IllegalStateException( + StringUtils.format("unexpected plan node type %s", plan.getClass())); } } - /** - * Implement PrometheusLogicalMetricScan. - */ - public PhysicalPlan visitIndexScan(PrometheusLogicalMetricScan node, - PrometheusMetricScan context) { + /** Implement PrometheusLogicalMetricScan. */ + public PhysicalPlan visitIndexScan( + PrometheusLogicalMetricScan node, PrometheusMetricScan context) { String query = SeriesSelectionQueryBuilder.build(node.getMetricName(), node.getFilter()); context.getRequest().setPromQl(query); setTimeRangeParameters(node.getFilter(), context); - context.getRequest() - .setStep(StepParameterResolver.resolve(context.getRequest().getStartTime(), - context.getRequest().getEndTime(), null)); + context + .getRequest() + .setStep( + StepParameterResolver.resolve( + context.getRequest().getStartTime(), context.getRequest().getEndTime(), null)); return context; } - /** - * Implement PrometheusLogicalMetricAgg. - */ - public PhysicalPlan visitIndexAggregation(PrometheusLogicalMetricAgg node, - PrometheusMetricScan context) { + /** Implement PrometheusLogicalMetricAgg. */ + public PhysicalPlan visitIndexAggregation( + PrometheusLogicalMetricAgg node, PrometheusMetricScan context) { setTimeRangeParameters(node.getFilter(), context); - context.getRequest() - .setStep(StepParameterResolver.resolve(context.getRequest().getStartTime(), - context.getRequest().getEndTime(), node.getGroupByList())); + context + .getRequest() + .setStep( + StepParameterResolver.resolve( + context.getRequest().getStartTime(), + context.getRequest().getEndTime(), + node.getGroupByList())); String step = context.getRequest().getStep(); - String seriesSelectionQuery - = SeriesSelectionQueryBuilder.build(node.getMetricName(), node.getFilter()); + String seriesSelectionQuery = + SeriesSelectionQueryBuilder.build(node.getMetricName(), node.getFilter()); - String aggregateQuery - = AggregationQueryBuilder.build(node.getAggregatorList(), - node.getGroupByList()); + String aggregateQuery = + AggregationQueryBuilder.build(node.getAggregatorList(), node.getGroupByList()); String finalQuery = String.format(aggregateQuery, seriesSelectionQuery + "[" + step + "]"); context.getRequest().setPromQl(finalQuery); - //Since prometheus response doesn't have any fieldNames in its output. - //the field names are sent to PrometheusResponse constructor via context. + // Since prometheus response doesn't have any fieldNames in its output. + // the field names are sent to PrometheusResponse constructor via context. setPrometheusResponseFieldNames(node, context); return context; } @Override - public PhysicalPlan visitRelation(LogicalRelation node, - PrometheusMetricScan context) { + public PhysicalPlan visitRelation(LogicalRelation node, PrometheusMetricScan context) { PrometheusMetricTable prometheusMetricTable = (PrometheusMetricTable) node.getTable(); String query = SeriesSelectionQueryBuilder.build(node.getRelationName(), null); context.getRequest().setPromQl(query); setTimeRangeParameters(null, context); - context.getRequest() - .setStep(StepParameterResolver.resolve(context.getRequest().getStartTime(), - context.getRequest().getEndTime(), null)); + context + .getRequest() + .setStep( + StepParameterResolver.resolve( + context.getRequest().getStartTime(), context.getRequest().getEndTime(), null)); return context; } @@ -110,8 +108,8 @@ private void setTimeRangeParameters(Expression filter, PrometheusMetricScan cont context.getRequest().setEndTime(timeRange.getSecond()); } - private void setPrometheusResponseFieldNames(PrometheusLogicalMetricAgg node, - PrometheusMetricScan context) { + private void setPrometheusResponseFieldNames( + PrometheusLogicalMetricAgg node, PrometheusMetricScan context) { Optional spanExpression = getSpanExpression(node.getGroupByList()); if (spanExpression.isEmpty()) { throw new RuntimeException( @@ -133,6 +131,4 @@ private Optional getSpanExpression(List namedE .filter(expression -> expression.getDelegated() instanceof SpanExpression) .findFirst(); } - - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/model/PrometheusResponseFieldNames.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/model/PrometheusResponseFieldNames.java index d3a6ef184f..303ace7906 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/model/PrometheusResponseFieldNames.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/model/PrometheusResponseFieldNames.java @@ -17,7 +17,6 @@ import org.opensearch.sql.data.type.ExprType; import org.opensearch.sql.expression.NamedExpression; - @Getter @Setter public class PrometheusResponseFieldNames { @@ -26,5 +25,4 @@ public class PrometheusResponseFieldNames { private ExprType valueType = DOUBLE; private String timestampFieldName = TIMESTAMP; private List groupByList; - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/model/QueryRangeParameters.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/model/QueryRangeParameters.java index 86ca99cea8..02187c5662 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/model/QueryRangeParameters.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/model/QueryRangeParameters.java @@ -21,5 +21,4 @@ public class QueryRangeParameters { private Long start; private Long end; private String step; - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/AggregationQueryBuilder.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/AggregationQueryBuilder.java index a141707077..540e2d8cf4 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/AggregationQueryBuilder.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/AggregationQueryBuilder.java @@ -18,49 +18,51 @@ import org.opensearch.sql.expression.span.SpanExpression; /** - * This class builds aggregation query for the given stats commands. - * In the generated query a placeholder(%s) is added in place of metric selection query - * and later replaced by metric selection query. + * This class builds aggregation query for the given stats commands. In the generated query a + * placeholder(%s) is added in place of metric selection query and later replaced by metric + * selection query. */ @NoArgsConstructor public class AggregationQueryBuilder { - private static final Set allowedStatsFunctions = Set.of( - BuiltinFunctionName.MAX.getName().getFunctionName(), - BuiltinFunctionName.MIN.getName().getFunctionName(), - BuiltinFunctionName.COUNT.getName().getFunctionName(), - BuiltinFunctionName.SUM.getName().getFunctionName(), - BuiltinFunctionName.AVG.getName().getFunctionName() - ); - + private static final Set allowedStatsFunctions = + Set.of( + BuiltinFunctionName.MAX.getName().getFunctionName(), + BuiltinFunctionName.MIN.getName().getFunctionName(), + BuiltinFunctionName.COUNT.getName().getFunctionName(), + BuiltinFunctionName.SUM.getName().getFunctionName(), + BuiltinFunctionName.AVG.getName().getFunctionName()); /** * Build Aggregation query from series selector query from expression. * * @return query string. */ - public static String build(List namedAggregatorList, - List groupByList) { + public static String build( + List namedAggregatorList, List groupByList) { if (namedAggregatorList.size() > 1) { throw new RuntimeException( "Prometheus Catalog doesn't multiple aggregations in stats command"); } - if (!allowedStatsFunctions - .contains(namedAggregatorList.get(0).getFunctionName().getFunctionName())) { - throw new RuntimeException(String.format( - "Prometheus Catalog only supports %s aggregations.", allowedStatsFunctions)); + if (!allowedStatsFunctions.contains( + namedAggregatorList.get(0).getFunctionName().getFunctionName())) { + throw new RuntimeException( + String.format( + "Prometheus Catalog only supports %s aggregations.", allowedStatsFunctions)); } StringBuilder aggregateQuery = new StringBuilder(); - aggregateQuery.append(namedAggregatorList.get(0).getFunctionName().getFunctionName()) + aggregateQuery + .append(namedAggregatorList.get(0).getFunctionName().getFunctionName()) .append(" "); if (groupByList != null && !groupByList.isEmpty()) { - groupByList = groupByList.stream() - .filter(expression -> !(expression.getDelegated() instanceof SpanExpression)) - .collect(Collectors.toList()); + groupByList = + groupByList.stream() + .filter(expression -> !(expression.getDelegated() instanceof SpanExpression)) + .collect(Collectors.toList()); if (groupByList.size() > 0) { aggregateQuery.append("by("); aggregateQuery.append( @@ -78,5 +80,4 @@ public static String build(List namedAggregatorList, .append("(%s))"); return aggregateQuery.toString(); } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/SeriesSelectionQueryBuilder.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/SeriesSelectionQueryBuilder.java index 461b5341f8..d824fcb5b3 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/SeriesSelectionQueryBuilder.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/SeriesSelectionQueryBuilder.java @@ -7,7 +7,6 @@ package org.opensearch.sql.prometheus.storage.querybuilder; - import static org.opensearch.sql.prometheus.data.constants.PrometheusFieldConstants.TIMESTAMP; import java.util.stream.Collectors; @@ -19,14 +18,10 @@ import org.opensearch.sql.expression.ReferenceExpression; import org.opensearch.sql.expression.function.BuiltinFunctionName; -/** - * This class builds metric selection query from the filter condition - * and metric name. - */ +/** This class builds metric selection query from the filter condition and metric name. */ @NoArgsConstructor public class SeriesSelectionQueryBuilder { - /** * Build Prometheus series selector query from expression. * @@ -35,8 +30,8 @@ public class SeriesSelectionQueryBuilder { */ public static String build(String metricName, Expression filterCondition) { if (filterCondition != null) { - SeriesSelectionExpressionNodeVisitor seriesSelectionExpressionNodeVisitor - = new SeriesSelectionExpressionNodeVisitor(); + SeriesSelectionExpressionNodeVisitor seriesSelectionExpressionNodeVisitor = + new SeriesSelectionExpressionNodeVisitor(); String selectorQuery = filterCondition.accept(seriesSelectionExpressionNodeVisitor, null); if (selectorQuery != null) { return metricName + "{" + selectorQuery + "}"; @@ -54,9 +49,9 @@ public String visitFunction(FunctionExpression func, Object context) { .filter(StringUtils::isNotEmpty) .collect(Collectors.joining(" , ")); } else if ((BuiltinFunctionName.LTE.getName().equals(func.getFunctionName()) - || BuiltinFunctionName.GTE.getName().equals(func.getFunctionName()) - || BuiltinFunctionName.LESS.getName().equals(func.getFunctionName()) - || BuiltinFunctionName.GREATER.getName().equals(func.getFunctionName())) + || BuiltinFunctionName.GTE.getName().equals(func.getFunctionName()) + || BuiltinFunctionName.LESS.getName().equals(func.getFunctionName()) + || BuiltinFunctionName.GREATER.getName().equals(func.getFunctionName())) && ((ReferenceExpression) func.getArguments().get(0)).getAttr().equals(TIMESTAMP)) { return null; } else if (BuiltinFunctionName.EQUAL.getName().equals(func.getFunctionName())) { @@ -65,11 +60,10 @@ public String visitFunction(FunctionExpression func, Object context) { + func.getArguments().get(1); } else { throw new RuntimeException( - String.format("Prometheus Datasource doesn't support %s " - + "in where command.", + String.format( + "Prometheus Datasource doesn't support %s " + "in where command.", func.getFunctionName().getFunctionName())); } } } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/StepParameterResolver.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/StepParameterResolver.java index 2078950a5d..4c23ea9086 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/StepParameterResolver.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/StepParameterResolver.java @@ -15,25 +15,20 @@ import org.opensearch.sql.expression.NamedExpression; import org.opensearch.sql.expression.span.SpanExpression; -/** - * This class resolves step parameter required for - * query_range api of prometheus. - */ +/** This class resolves step parameter required for query_range api of prometheus. */ @NoArgsConstructor public class StepParameterResolver { /** - * Extract step from groupByList or apply heuristic arithmetic - * on endTime and startTime. - * + * Extract step from groupByList or apply heuristic arithmetic on endTime and startTime. * * @param startTime startTime. * @param endTime endTime. * @param groupByList groupByList. * @return Step String. */ - public static String resolve(@NonNull Long startTime, @NonNull Long endTime, - List groupByList) { + public static String resolve( + @NonNull Long startTime, @NonNull Long endTime, List groupByList) { Optional spanExpression = getSpanExpression(groupByList); if (spanExpression.isPresent()) { if (StringUtils.isEmpty(spanExpression.get().getUnit().getName())) { @@ -48,7 +43,7 @@ public static String resolve(@NonNull Long startTime, @NonNull Long endTime, } private static Optional getSpanExpression( - List namedExpressionList) { + List namedExpressionList) { if (namedExpressionList == null) { return Optional.empty(); } @@ -57,7 +52,4 @@ private static Optional getSpanExpression( .map(expression -> (SpanExpression) expression.getDelegated()) .findFirst(); } - - - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/TimeRangeParametersResolver.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/TimeRangeParametersResolver.java index b462f6bafe..c7766f22d6 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/TimeRangeParametersResolver.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/TimeRangeParametersResolver.java @@ -22,16 +22,14 @@ @NoArgsConstructor public class TimeRangeParametersResolver extends ExpressionNodeVisitor { - private Long startTime; private Long endTime; /** - * Build Range Query Parameters from filter expression. - * If the filter condition consists of @timestamp, startTime and - * endTime are derived. or else it will be defaulted to now() and now()-1hr. - * If one of starttime and endtime are provided, the other will be derived from them - * by fixing the time range duration to 1hr. + * Build Range Query Parameters from filter expression. If the filter condition consists + * of @timestamp, startTime and endTime are derived. or else it will be defaulted to now() and + * now()-1hr. If one of starttime and endtime are provided, the other will be derived from them by + * fixing the time range duration to 1hr. * * @param filterCondition expression. * @return query string @@ -72,13 +70,10 @@ public Void visitFunction(FunctionExpression func, Object context) { } } } else { - func.getArguments() - .stream() + func.getArguments().stream() .filter(arg -> arg instanceof FunctionExpression) .forEach(arg -> visitFunction((FunctionExpression) arg, context)); } return null; } - - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTable.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTable.java index dca946da57..b5557e7298 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTable.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTable.java @@ -5,7 +5,6 @@ package org.opensearch.sql.prometheus.storage.system; - import static org.opensearch.sql.utils.SystemIndexUtils.systemTable; import com.google.common.annotations.VisibleForTesting; @@ -25,13 +24,9 @@ import org.opensearch.sql.storage.Table; import org.opensearch.sql.utils.SystemIndexUtils; -/** - * Prometheus System Table Implementation. - */ +/** Prometheus System Table Implementation. */ public class PrometheusSystemTable implements Table { - /** - * System Index Name. - */ + /** System Index Name. */ private final Pair systemIndexBundle; private final DataSourceSchemaName dataSourceSchemaName; @@ -54,8 +49,7 @@ public PhysicalPlan implement(LogicalPlan plan) { @VisibleForTesting @RequiredArgsConstructor - public class PrometheusSystemTableDefaultImplementor - extends DefaultImplementor { + public class PrometheusSystemTableDefaultImplementor extends DefaultImplementor { @Override public PhysicalPlan visitRelation(LogicalRelation node, Object context) { @@ -67,12 +61,14 @@ private Pair buildIndexBun PrometheusClient client, String indexName) { SystemIndexUtils.SystemTable systemTable = systemTable(indexName); if (systemTable.isSystemInfoTable()) { - return Pair.of(PrometheusSystemTableSchema.SYS_TABLE_TABLES, + return Pair.of( + PrometheusSystemTableSchema.SYS_TABLE_TABLES, new PrometheusListMetricsRequest(client, dataSourceSchemaName)); } else { - return Pair.of(PrometheusSystemTableSchema.SYS_TABLE_MAPPINGS, - new PrometheusDescribeMetricRequest(client, - dataSourceSchemaName, systemTable.getTableName())); + return Pair.of( + PrometheusSystemTableSchema.SYS_TABLE_MAPPINGS, + new PrometheusDescribeMetricRequest( + client, dataSourceSchemaName, systemTable.getTableName())); } } } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableScan.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableScan.java index 5c0bc656fe..907e8a0c15 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableScan.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableScan.java @@ -13,16 +13,13 @@ import org.opensearch.sql.prometheus.request.system.PrometheusSystemRequest; import org.opensearch.sql.storage.TableScanOperator; -/** - * Prometheus table scan operator. - */ +/** Prometheus table scan operator. */ @RequiredArgsConstructor @EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false) @ToString(onlyExplicitlyIncluded = true) public class PrometheusSystemTableScan extends TableScanOperator { - @EqualsAndHashCode.Include - private final PrometheusSystemRequest request; + @EqualsAndHashCode.Include private final PrometheusSystemRequest request; private Iterator iterator; diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableSchema.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableSchema.java index 668a208c79..9272731dce 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableSchema.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableSchema.java @@ -18,22 +18,23 @@ @Getter @RequiredArgsConstructor public enum PrometheusSystemTableSchema { - - SYS_TABLE_TABLES(new ImmutableMap.Builder() - .put("TABLE_CATALOG", STRING) - .put("TABLE_SCHEMA", STRING) - .put("TABLE_NAME", STRING) - .put("TABLE_TYPE", STRING) - .put("UNIT", STRING) - .put("REMARKS", STRING) - .build()), - SYS_TABLE_MAPPINGS(new ImmutableMap.Builder() - .put("TABLE_CATALOG", STRING) - .put("TABLE_SCHEMA", STRING) - .put("TABLE_NAME", STRING) - .put("COLUMN_NAME", STRING) - .put("DATA_TYPE", STRING) - .build()); + SYS_TABLE_TABLES( + new ImmutableMap.Builder() + .put("TABLE_CATALOG", STRING) + .put("TABLE_SCHEMA", STRING) + .put("TABLE_NAME", STRING) + .put("TABLE_TYPE", STRING) + .put("UNIT", STRING) + .put("REMARKS", STRING) + .build()), + SYS_TABLE_MAPPINGS( + new ImmutableMap.Builder() + .put("TABLE_CATALOG", STRING) + .put("TABLE_SCHEMA", STRING) + .put("TABLE_NAME", STRING) + .put("COLUMN_NAME", STRING) + .put("DATA_TYPE", STRING) + .build()); private final Map mapping; } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/utils/TableFunctionUtils.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/utils/TableFunctionUtils.java index 35edc83614..24bec1ede3 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/utils/TableFunctionUtils.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/utils/TableFunctionUtils.java @@ -16,52 +16,54 @@ import org.opensearch.sql.expression.Expression; import org.opensearch.sql.expression.NamedArgumentExpression; -/** - * Utility class for common table function methods. - */ +/** Utility class for common table function methods. */ @UtilityClass public class TableFunctionUtils { /** - * Validates if function arguments are valid - * in both the cases when the arguments are passed by position or name. + * Validates if function arguments are valid in both the cases when the arguments are passed by + * position or name. * * @param arguments arguments of function provided in the input order. * @param argumentNames ordered argument names of the function. */ - public static void validatePrometheusTableFunctionArguments(List arguments, - List argumentNames) { - Boolean argumentsPassedByName = arguments.stream() - .noneMatch(arg -> StringUtils.isEmpty(((NamedArgumentExpression) arg).getArgName())); - Boolean argumentsPassedByPosition = arguments.stream() - .allMatch(arg -> StringUtils.isEmpty(((NamedArgumentExpression) arg).getArgName())); + public static void validatePrometheusTableFunctionArguments( + List arguments, List argumentNames) { + Boolean argumentsPassedByName = + arguments.stream() + .noneMatch(arg -> StringUtils.isEmpty(((NamedArgumentExpression) arg).getArgName())); + Boolean argumentsPassedByPosition = + arguments.stream() + .allMatch(arg -> StringUtils.isEmpty(((NamedArgumentExpression) arg).getArgName())); if (!(argumentsPassedByName || argumentsPassedByPosition)) { throw new SemanticCheckException("Arguments should be either passed by name or position"); } if (arguments.size() != argumentNames.size()) { throw new SemanticCheckException( - generateErrorMessageForMissingArguments(argumentsPassedByPosition, arguments, - argumentNames)); + generateErrorMessageForMissingArguments( + argumentsPassedByPosition, arguments, argumentNames)); } } /** - * Get Named Arguments of Table Function Arguments. - * If they are passed by position create new ones or else return the same arguments passed. + * Get Named Arguments of Table Function Arguments. If they are passed by position create new ones + * or else return the same arguments passed. * * @param arguments arguments of function provided in the input order. * @param argumentNames ordered argument names of the function. */ - public static List getNamedArgumentsOfTableFunction(List arguments, - List argumentNames) { - boolean argumentsPassedByPosition = arguments.stream() - .allMatch(arg -> StringUtils.isEmpty(((NamedArgumentExpression) arg).getArgName())); + public static List getNamedArgumentsOfTableFunction( + List arguments, List argumentNames) { + boolean argumentsPassedByPosition = + arguments.stream() + .allMatch(arg -> StringUtils.isEmpty(((NamedArgumentExpression) arg).getArgName())); if (argumentsPassedByPosition) { List namedArguments = new ArrayList<>(); for (int i = 0; i < arguments.size(); i++) { - namedArguments.add(new NamedArgumentExpression(argumentNames.get(i), - ((NamedArgumentExpression) arguments.get(i)).getValue())); + namedArguments.add( + new NamedArgumentExpression( + argumentNames.get(i), ((NamedArgumentExpression) arguments.get(i)).getValue())); } return namedArguments; } @@ -73,17 +75,17 @@ private static String generateErrorMessageForMissingArguments( List arguments, List argumentNames) { if (areArgumentsPassedByPosition) { - return String.format("Missing arguments:[%s]", + return String.format( + "Missing arguments:[%s]", String.join(",", argumentNames.subList(arguments.size(), argumentNames.size()))); } else { Set requiredArguments = new HashSet<>(argumentNames); Set providedArguments = - arguments.stream().map(expression -> ((NamedArgumentExpression) expression).getArgName()) + arguments.stream() + .map(expression -> ((NamedArgumentExpression) expression).getArgName()) .collect(Collectors.toSet()); requiredArguments.removeAll(providedArguments); return String.format("Missing arguments:[%s]", String.join(",", requiredArguments)); } } - - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/client/PrometheusClientImplTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/client/PrometheusClientImplTest.java index b26a45e301..735a1a1052 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/client/PrometheusClientImplTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/client/PrometheusClientImplTest.java @@ -43,7 +43,6 @@ public class PrometheusClientImplTest { private MockWebServer mockWebServer; private PrometheusClient prometheusClient; - @BeforeEach void setUp() throws IOException { this.mockWebServer = new MockWebServer(); @@ -52,13 +51,13 @@ void setUp() throws IOException { new PrometheusClientImpl(new OkHttpClient(), mockWebServer.url("").uri().normalize()); } - @Test @SneakyThrows void testQueryRange() { - MockResponse mockResponse = new MockResponse() - .addHeader("Content-Type", "application/json; charset=utf-8") - .setBody(getJson("query_range_response.json")); + MockResponse mockResponse = + new MockResponse() + .addHeader("Content-Type", "application/json; charset=utf-8") + .setBody(getJson("query_range_response.json")); mockWebServer.enqueue(mockResponse); JSONObject jsonObject = prometheusClient.queryRange(QUERY, STARTTIME, ENDTIME, STEP); assertTrue(new JSONObject(getJson("query_range_result.json")).similar(jsonObject)); @@ -69,13 +68,15 @@ void testQueryRange() { @Test @SneakyThrows void testQueryRangeWith2xxStatusAndError() { - MockResponse mockResponse = new MockResponse() - .addHeader("Content-Type", "application/json; charset=utf-8") - .setBody(getJson("error_response.json")); + MockResponse mockResponse = + new MockResponse() + .addHeader("Content-Type", "application/json; charset=utf-8") + .setBody(getJson("error_response.json")); mockWebServer.enqueue(mockResponse); - RuntimeException runtimeException - = assertThrows(RuntimeException.class, - () -> prometheusClient.queryRange(QUERY, STARTTIME, ENDTIME, STEP)); + RuntimeException runtimeException = + assertThrows( + RuntimeException.class, + () -> prometheusClient.queryRange(QUERY, STARTTIME, ENDTIME, STEP)); assertEquals("Error", runtimeException.getMessage()); RecordedRequest recordedRequest = mockWebServer.takeRequest(); verifyQueryRangeCall(recordedRequest); @@ -84,13 +85,15 @@ void testQueryRangeWith2xxStatusAndError() { @Test @SneakyThrows void testQueryRangeWithNon2xxError() { - MockResponse mockResponse = new MockResponse() - .addHeader("Content-Type", "application/json; charset=utf-8") - .setResponseCode(400); + MockResponse mockResponse = + new MockResponse() + .addHeader("Content-Type", "application/json; charset=utf-8") + .setResponseCode(400); mockWebServer.enqueue(mockResponse); - RuntimeException runtimeException - = assertThrows(RuntimeException.class, - () -> prometheusClient.queryRange(QUERY, STARTTIME, ENDTIME, STEP)); + RuntimeException runtimeException = + assertThrows( + RuntimeException.class, + () -> prometheusClient.queryRange(QUERY, STARTTIME, ENDTIME, STEP)); assertTrue( runtimeException.getMessage().contains("Request to Prometheus is Unsuccessful with :")); RecordedRequest recordedRequest = mockWebServer.takeRequest(); @@ -100,16 +103,20 @@ void testQueryRangeWithNon2xxError() { @Test @SneakyThrows void testGetLabel() { - MockResponse mockResponse = new MockResponse() - .addHeader("Content-Type", "application/json; charset=utf-8") - .setBody(getJson("get_labels_response.json")); + MockResponse mockResponse = + new MockResponse() + .addHeader("Content-Type", "application/json; charset=utf-8") + .setBody(getJson("get_labels_response.json")); mockWebServer.enqueue(mockResponse); List response = prometheusClient.getLabels(METRIC_NAME); - assertEquals(new ArrayList() {{ - add("call"); - add("code"); - } - }, response); + assertEquals( + new ArrayList() { + { + add("call"); + add("code"); + } + }, + response); RecordedRequest recordedRequest = mockWebServer.takeRequest(); verifyGetLabelsCall(recordedRequest); } @@ -117,30 +124,34 @@ void testGetLabel() { @Test @SneakyThrows void testGetAllMetrics() { - MockResponse mockResponse = new MockResponse() - .addHeader("Content-Type", "application/json; charset=utf-8") - .setBody(getJson("all_metrics_response.json")); + MockResponse mockResponse = + new MockResponse() + .addHeader("Content-Type", "application/json; charset=utf-8") + .setBody(getJson("all_metrics_response.json")); mockWebServer.enqueue(mockResponse); Map> response = prometheusClient.getAllMetrics(); Map> expected = new HashMap<>(); - expected.put("go_gc_duration_seconds", - Collections.singletonList(new MetricMetadata("summary", - "A summary of the pause duration of garbage collection cycles.", ""))); - expected.put("go_goroutines", - Collections.singletonList(new MetricMetadata("gauge", - "Number of goroutines that currently exist.", ""))); + expected.put( + "go_gc_duration_seconds", + Collections.singletonList( + new MetricMetadata( + "summary", "A summary of the pause duration of garbage collection cycles.", ""))); + expected.put( + "go_goroutines", + Collections.singletonList( + new MetricMetadata("gauge", "Number of goroutines that currently exist.", ""))); assertEquals(expected, response); RecordedRequest recordedRequest = mockWebServer.takeRequest(); verifyGetAllMetricsCall(recordedRequest); } - @Test @SneakyThrows void testQueryExemplars() { - MockResponse mockResponse = new MockResponse() - .addHeader("Content-Type", "application/json; charset=utf-8") - .setBody(getJson("query_exemplars_response.json")); + MockResponse mockResponse = + new MockResponse() + .addHeader("Content-Type", "application/json; charset=utf-8") + .setBody(getJson("query_exemplars_response.json")); mockWebServer.enqueue(mockResponse); JSONArray jsonArray = prometheusClient.queryExemplars(QUERY, STARTTIME, ENDTIME); assertTrue(new JSONArray(getJson("query_exemplars_result.json")).similar(jsonArray)); diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/implementation/QueryExemplarsFunctionImplementationTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/implementation/QueryExemplarsFunctionImplementationTest.java index d6e4a5cef4..6009d3229c 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/implementation/QueryExemplarsFunctionImplementationTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/implementation/QueryExemplarsFunctionImplementationTest.java @@ -25,29 +25,31 @@ import org.opensearch.sql.prometheus.request.PrometheusQueryExemplarsRequest; import org.opensearch.sql.prometheus.storage.QueryExemplarsTable; - @ExtendWith(MockitoExtension.class) class QueryExemplarsFunctionImplementationTest { - @Mock - private PrometheusClient client; - + @Mock private PrometheusClient client; @Test void testValueOfAndTypeAndToString() { FunctionName functionName = new FunctionName("query_exemplars"); - List namedArgumentExpressionList - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("starttime", DSL.literal(12345)), - DSL.namedArgument("endtime", DSL.literal(12345))); - QueryExemplarFunctionImplementation queryExemplarFunctionImplementation - = + List namedArgumentExpressionList = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("starttime", DSL.literal(12345)), + DSL.namedArgument("endtime", DSL.literal(12345))); + QueryExemplarFunctionImplementation queryExemplarFunctionImplementation = new QueryExemplarFunctionImplementation(functionName, namedArgumentExpressionList, client); - UnsupportedOperationException exception = assertThrows(UnsupportedOperationException.class, - () -> queryExemplarFunctionImplementation.valueOf()); - assertEquals("Prometheus defined function [query_exemplars] is only " - + "supported in SOURCE clause with prometheus connector catalog", exception.getMessage()); - assertEquals("query_exemplars(query=\"http_latency\", starttime=12345, endtime=12345)", + UnsupportedOperationException exception = + assertThrows( + UnsupportedOperationException.class, + () -> queryExemplarFunctionImplementation.valueOf()); + assertEquals( + "Prometheus defined function [query_exemplars] is only " + + "supported in SOURCE clause with prometheus connector catalog", + exception.getMessage()); + assertEquals( + "query_exemplars(query=\"http_latency\", starttime=12345, endtime=12345)", queryExemplarFunctionImplementation.toString()); assertEquals(ExprCoreType.STRUCT, queryExemplarFunctionImplementation.type()); } @@ -55,15 +57,15 @@ void testValueOfAndTypeAndToString() { @Test void testApplyArguments() { FunctionName functionName = new FunctionName("query_exemplars"); - List namedArgumentExpressionList - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("starttime", DSL.literal(12345)), - DSL.namedArgument("endtime", DSL.literal(1234))); - QueryExemplarFunctionImplementation queryExemplarFunctionImplementation - = + List namedArgumentExpressionList = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("starttime", DSL.literal(12345)), + DSL.namedArgument("endtime", DSL.literal(1234))); + QueryExemplarFunctionImplementation queryExemplarFunctionImplementation = new QueryExemplarFunctionImplementation(functionName, namedArgumentExpressionList, client); - QueryExemplarsTable queryExemplarsTable - = (QueryExemplarsTable) queryExemplarFunctionImplementation.applyArguments(); + QueryExemplarsTable queryExemplarsTable = + (QueryExemplarsTable) queryExemplarFunctionImplementation.applyArguments(); assertNotNull(queryExemplarsTable.getExemplarsRequest()); PrometheusQueryExemplarsRequest request = queryExemplarsTable.getExemplarsRequest(); assertEquals("http_latency", request.getQuery()); @@ -74,17 +76,17 @@ void testApplyArguments() { @Test void testApplyArgumentsException() { FunctionName functionName = new FunctionName("query_exemplars"); - List namedArgumentExpressionList - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("starttime", DSL.literal(12345)), - DSL.namedArgument("end_time", DSL.literal(1234))); - QueryExemplarFunctionImplementation queryExemplarFunctionImplementation - = + List namedArgumentExpressionList = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("starttime", DSL.literal(12345)), + DSL.namedArgument("end_time", DSL.literal(1234))); + QueryExemplarFunctionImplementation queryExemplarFunctionImplementation = new QueryExemplarFunctionImplementation(functionName, namedArgumentExpressionList, client); - ExpressionEvaluationException exception = assertThrows(ExpressionEvaluationException.class, - () -> queryExemplarFunctionImplementation.applyArguments()); + ExpressionEvaluationException exception = + assertThrows( + ExpressionEvaluationException.class, + () -> queryExemplarFunctionImplementation.applyArguments()); assertEquals("Invalid Function Argument:end_time", exception.getMessage()); } - - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/implementation/QueryRangeFunctionImplementationTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/implementation/QueryRangeFunctionImplementationTest.java index 48337e3f02..288bc35b0f 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/implementation/QueryRangeFunctionImplementationTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/implementation/QueryRangeFunctionImplementationTest.java @@ -26,29 +26,31 @@ import org.opensearch.sql.prometheus.request.PrometheusQueryRequest; import org.opensearch.sql.prometheus.storage.PrometheusMetricTable; - @ExtendWith(MockitoExtension.class) class QueryRangeFunctionImplementationTest { - @Mock - private PrometheusClient client; - + @Mock private PrometheusClient client; @Test void testValueOfAndTypeAndToString() { FunctionName functionName = new FunctionName("query_range"); - List namedArgumentExpressionList - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("starttime", DSL.literal(12345)), - DSL.namedArgument("endtime", DSL.literal(12345)), - DSL.namedArgument("step", DSL.literal(14))); - QueryRangeFunctionImplementation queryRangeFunctionImplementation - = new QueryRangeFunctionImplementation(functionName, namedArgumentExpressionList, client); - UnsupportedOperationException exception = assertThrows(UnsupportedOperationException.class, - () -> queryRangeFunctionImplementation.valueOf()); - assertEquals("Prometheus defined function [query_range] is only " - + "supported in SOURCE clause with prometheus connector catalog", exception.getMessage()); - assertEquals("query_range(query=\"http_latency\", starttime=12345, endtime=12345, step=14)", + List namedArgumentExpressionList = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("starttime", DSL.literal(12345)), + DSL.namedArgument("endtime", DSL.literal(12345)), + DSL.namedArgument("step", DSL.literal(14))); + QueryRangeFunctionImplementation queryRangeFunctionImplementation = + new QueryRangeFunctionImplementation(functionName, namedArgumentExpressionList, client); + UnsupportedOperationException exception = + assertThrows( + UnsupportedOperationException.class, () -> queryRangeFunctionImplementation.valueOf()); + assertEquals( + "Prometheus defined function [query_range] is only " + + "supported in SOURCE clause with prometheus connector catalog", + exception.getMessage()); + assertEquals( + "query_range(query=\"http_latency\", starttime=12345, endtime=12345, step=14)", queryRangeFunctionImplementation.toString()); assertEquals(ExprCoreType.STRUCT, queryRangeFunctionImplementation.type()); } @@ -56,19 +58,20 @@ void testValueOfAndTypeAndToString() { @Test void testApplyArguments() { FunctionName functionName = new FunctionName("query_range"); - List namedArgumentExpressionList - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("starttime", DSL.literal(12345)), - DSL.namedArgument("endtime", DSL.literal(1234)), - DSL.namedArgument("step", DSL.literal(14))); - QueryRangeFunctionImplementation queryRangeFunctionImplementation - = new QueryRangeFunctionImplementation(functionName, namedArgumentExpressionList, client); - PrometheusMetricTable prometheusMetricTable - = (PrometheusMetricTable) queryRangeFunctionImplementation.applyArguments(); + List namedArgumentExpressionList = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("starttime", DSL.literal(12345)), + DSL.namedArgument("endtime", DSL.literal(1234)), + DSL.namedArgument("step", DSL.literal(14))); + QueryRangeFunctionImplementation queryRangeFunctionImplementation = + new QueryRangeFunctionImplementation(functionName, namedArgumentExpressionList, client); + PrometheusMetricTable prometheusMetricTable = + (PrometheusMetricTable) queryRangeFunctionImplementation.applyArguments(); assertNull(prometheusMetricTable.getMetricName()); assertNotNull(prometheusMetricTable.getPrometheusQueryRequest()); - PrometheusQueryRequest prometheusQueryRequest - = prometheusMetricTable.getPrometheusQueryRequest(); + PrometheusQueryRequest prometheusQueryRequest = + prometheusMetricTable.getPrometheusQueryRequest(); assertEquals("http_latency", prometheusQueryRequest.getPromQl().toString()); assertEquals(12345, prometheusQueryRequest.getStartTime()); assertEquals(1234, prometheusQueryRequest.getEndTime()); @@ -78,17 +81,18 @@ void testApplyArguments() { @Test void testApplyArgumentsException() { FunctionName functionName = new FunctionName("query_range"); - List namedArgumentExpressionList - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("starttime", DSL.literal(12345)), - DSL.namedArgument("end_time", DSL.literal(1234)), - DSL.namedArgument("step", DSL.literal(14))); - QueryRangeFunctionImplementation queryRangeFunctionImplementation - = new QueryRangeFunctionImplementation(functionName, namedArgumentExpressionList, client); - ExpressionEvaluationException exception = assertThrows(ExpressionEvaluationException.class, - () -> queryRangeFunctionImplementation.applyArguments()); + List namedArgumentExpressionList = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("starttime", DSL.literal(12345)), + DSL.namedArgument("end_time", DSL.literal(1234)), + DSL.namedArgument("step", DSL.literal(14))); + QueryRangeFunctionImplementation queryRangeFunctionImplementation = + new QueryRangeFunctionImplementation(functionName, namedArgumentExpressionList, client); + ExpressionEvaluationException exception = + assertThrows( + ExpressionEvaluationException.class, + () -> queryRangeFunctionImplementation.applyArguments()); assertEquals("Invalid Function Argument:end_time", exception.getMessage()); } - - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolverTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolverTest.java index 3e26b46c8f..af8ebf48e2 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolverTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolverTest.java @@ -35,34 +35,34 @@ @ExtendWith(MockitoExtension.class) class QueryExemplarsTableFunctionResolverTest { - @Mock - private PrometheusClient client; + @Mock private PrometheusClient client; - @Mock - private FunctionProperties functionProperties; + @Mock private FunctionProperties functionProperties; @Test void testResolve() { - QueryExemplarsTableFunctionResolver queryExemplarsTableFunctionResolver - = new QueryExemplarsTableFunctionResolver(client); + QueryExemplarsTableFunctionResolver queryExemplarsTableFunctionResolver = + new QueryExemplarsTableFunctionResolver(client); FunctionName functionName = FunctionName.of("query_exemplars"); - List expressions - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("starttime", DSL.literal(12345)), - DSL.namedArgument("endtime", DSL.literal(12345))); - FunctionSignature functionSignature = new FunctionSignature(functionName, expressions - .stream().map(Expression::type).collect(Collectors.toList())); - Pair resolution - = queryExemplarsTableFunctionResolver.resolve(functionSignature); + List expressions = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("starttime", DSL.literal(12345)), + DSL.namedArgument("endtime", DSL.literal(12345))); + FunctionSignature functionSignature = + new FunctionSignature( + functionName, expressions.stream().map(Expression::type).collect(Collectors.toList())); + Pair resolution = + queryExemplarsTableFunctionResolver.resolve(functionSignature); assertEquals(functionName, resolution.getKey().getFunctionName()); assertEquals(functionName, queryExemplarsTableFunctionResolver.getFunctionName()); assertEquals(List.of(STRING, LONG, LONG), resolution.getKey().getParamTypeList()); FunctionBuilder functionBuilder = resolution.getValue(); - TableFunctionImplementation functionImplementation - = (TableFunctionImplementation) functionBuilder.apply(functionProperties, expressions); + TableFunctionImplementation functionImplementation = + (TableFunctionImplementation) functionBuilder.apply(functionProperties, expressions); assertTrue(functionImplementation instanceof QueryExemplarFunctionImplementation); - QueryExemplarsTable queryExemplarsTable - = (QueryExemplarsTable) functionImplementation.applyArguments(); + QueryExemplarsTable queryExemplarsTable = + (QueryExemplarsTable) functionImplementation.applyArguments(); assertNotNull(queryExemplarsTable.getExemplarsRequest()); PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest = queryExemplarsTable.getExemplarsRequest(); @@ -70,5 +70,4 @@ void testResolve() { assertEquals(12345L, prometheusQueryExemplarsRequest.getStartTime()); assertEquals(12345L, prometheusQueryExemplarsRequest.getEndTime()); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/resolver/QueryRangeTableFunctionResolverTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/resolver/QueryRangeTableFunctionResolverTest.java index 2a36600379..48050bcb15 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/resolver/QueryRangeTableFunctionResolverTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/resolver/QueryRangeTableFunctionResolverTest.java @@ -37,35 +37,35 @@ @ExtendWith(MockitoExtension.class) class QueryRangeTableFunctionResolverTest { - @Mock - private PrometheusClient client; + @Mock private PrometheusClient client; - @Mock - private FunctionProperties functionProperties; + @Mock private FunctionProperties functionProperties; @Test void testResolve() { - QueryRangeTableFunctionResolver queryRangeTableFunctionResolver - = new QueryRangeTableFunctionResolver(client); + QueryRangeTableFunctionResolver queryRangeTableFunctionResolver = + new QueryRangeTableFunctionResolver(client); FunctionName functionName = FunctionName.of("query_range"); - List expressions - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("starttime", DSL.literal(12345)), - DSL.namedArgument("endtime", DSL.literal(12345)), - DSL.namedArgument("step", DSL.literal(14))); - FunctionSignature functionSignature = new FunctionSignature(functionName, expressions - .stream().map(Expression::type).collect(Collectors.toList())); - Pair resolution - = queryRangeTableFunctionResolver.resolve(functionSignature); + List expressions = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("starttime", DSL.literal(12345)), + DSL.namedArgument("endtime", DSL.literal(12345)), + DSL.namedArgument("step", DSL.literal(14))); + FunctionSignature functionSignature = + new FunctionSignature( + functionName, expressions.stream().map(Expression::type).collect(Collectors.toList())); + Pair resolution = + queryRangeTableFunctionResolver.resolve(functionSignature); assertEquals(functionName, resolution.getKey().getFunctionName()); assertEquals(functionName, queryRangeTableFunctionResolver.getFunctionName()); assertEquals(List.of(STRING, LONG, LONG, STRING), resolution.getKey().getParamTypeList()); FunctionBuilder functionBuilder = resolution.getValue(); - TableFunctionImplementation functionImplementation - = (TableFunctionImplementation) functionBuilder.apply(functionProperties, expressions); + TableFunctionImplementation functionImplementation = + (TableFunctionImplementation) functionBuilder.apply(functionProperties, expressions); assertTrue(functionImplementation instanceof QueryRangeFunctionImplementation); - PrometheusMetricTable prometheusMetricTable - = (PrometheusMetricTable) functionImplementation.applyArguments(); + PrometheusMetricTable prometheusMetricTable = + (PrometheusMetricTable) functionImplementation.applyArguments(); assertNotNull(prometheusMetricTable.getPrometheusQueryRequest()); PrometheusQueryRequest prometheusQueryRequest = prometheusMetricTable.getPrometheusQueryRequest(); @@ -77,29 +77,31 @@ void testResolve() { @Test void testArgumentsPassedByPosition() { - QueryRangeTableFunctionResolver queryRangeTableFunctionResolver - = new QueryRangeTableFunctionResolver(client); + QueryRangeTableFunctionResolver queryRangeTableFunctionResolver = + new QueryRangeTableFunctionResolver(client); FunctionName functionName = FunctionName.of("query_range"); - List expressions - = List.of(DSL.namedArgument(null, DSL.literal("http_latency")), - DSL.namedArgument(null, DSL.literal(12345)), - DSL.namedArgument(null, DSL.literal(12345)), - DSL.namedArgument(null, DSL.literal(14))); - FunctionSignature functionSignature = new FunctionSignature(functionName, expressions - .stream().map(Expression::type).collect(Collectors.toList())); - - Pair resolution - = queryRangeTableFunctionResolver.resolve(functionSignature); + List expressions = + List.of( + DSL.namedArgument(null, DSL.literal("http_latency")), + DSL.namedArgument(null, DSL.literal(12345)), + DSL.namedArgument(null, DSL.literal(12345)), + DSL.namedArgument(null, DSL.literal(14))); + FunctionSignature functionSignature = + new FunctionSignature( + functionName, expressions.stream().map(Expression::type).collect(Collectors.toList())); + + Pair resolution = + queryRangeTableFunctionResolver.resolve(functionSignature); assertEquals(functionName, resolution.getKey().getFunctionName()); assertEquals(functionName, queryRangeTableFunctionResolver.getFunctionName()); assertEquals(List.of(STRING, LONG, LONG, STRING), resolution.getKey().getParamTypeList()); FunctionBuilder functionBuilder = resolution.getValue(); - TableFunctionImplementation functionImplementation - = (TableFunctionImplementation) functionBuilder.apply(functionProperties, expressions); + TableFunctionImplementation functionImplementation = + (TableFunctionImplementation) functionBuilder.apply(functionProperties, expressions); assertTrue(functionImplementation instanceof QueryRangeFunctionImplementation); - PrometheusMetricTable prometheusMetricTable - = (PrometheusMetricTable) functionImplementation.applyArguments(); + PrometheusMetricTable prometheusMetricTable = + (PrometheusMetricTable) functionImplementation.applyArguments(); assertNotNull(prometheusMetricTable.getPrometheusQueryRequest()); PrometheusQueryRequest prometheusQueryRequest = prometheusMetricTable.getPrometheusQueryRequest(); @@ -109,32 +111,33 @@ void testArgumentsPassedByPosition() { assertEquals("14", prometheusQueryRequest.getStep()); } - @Test void testArgumentsPassedByNameWithDifferentOrder() { - QueryRangeTableFunctionResolver queryRangeTableFunctionResolver - = new QueryRangeTableFunctionResolver(client); + QueryRangeTableFunctionResolver queryRangeTableFunctionResolver = + new QueryRangeTableFunctionResolver(client); FunctionName functionName = FunctionName.of("query_range"); - List expressions - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("endtime", DSL.literal(12345)), - DSL.namedArgument("step", DSL.literal(14)), - DSL.namedArgument("starttime", DSL.literal(12345))); - FunctionSignature functionSignature = new FunctionSignature(functionName, expressions - .stream().map(Expression::type).collect(Collectors.toList())); - - Pair resolution - = queryRangeTableFunctionResolver.resolve(functionSignature); + List expressions = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("endtime", DSL.literal(12345)), + DSL.namedArgument("step", DSL.literal(14)), + DSL.namedArgument("starttime", DSL.literal(12345))); + FunctionSignature functionSignature = + new FunctionSignature( + functionName, expressions.stream().map(Expression::type).collect(Collectors.toList())); + + Pair resolution = + queryRangeTableFunctionResolver.resolve(functionSignature); assertEquals(functionName, resolution.getKey().getFunctionName()); assertEquals(functionName, queryRangeTableFunctionResolver.getFunctionName()); assertEquals(List.of(STRING, LONG, LONG, STRING), resolution.getKey().getParamTypeList()); FunctionBuilder functionBuilder = resolution.getValue(); - TableFunctionImplementation functionImplementation - = (TableFunctionImplementation) functionBuilder.apply(functionProperties, expressions); + TableFunctionImplementation functionImplementation = + (TableFunctionImplementation) functionBuilder.apply(functionProperties, expressions); assertTrue(functionImplementation instanceof QueryRangeFunctionImplementation); - PrometheusMetricTable prometheusMetricTable - = (PrometheusMetricTable) functionImplementation.applyArguments(); + PrometheusMetricTable prometheusMetricTable = + (PrometheusMetricTable) functionImplementation.applyArguments(); assertNotNull(prometheusMetricTable.getPrometheusQueryRequest()); PrometheusQueryRequest prometheusQueryRequest = prometheusMetricTable.getPrometheusQueryRequest(); @@ -146,70 +149,81 @@ void testArgumentsPassedByNameWithDifferentOrder() { @Test void testMixedArgumentTypes() { - QueryRangeTableFunctionResolver queryRangeTableFunctionResolver - = new QueryRangeTableFunctionResolver(client); + QueryRangeTableFunctionResolver queryRangeTableFunctionResolver = + new QueryRangeTableFunctionResolver(client); FunctionName functionName = FunctionName.of("query_range"); - List expressions - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument(null, DSL.literal(12345)), - DSL.namedArgument(null, DSL.literal(12345)), - DSL.namedArgument(null, DSL.literal(14))); - FunctionSignature functionSignature = new FunctionSignature(functionName, expressions - .stream().map(Expression::type).collect(Collectors.toList())); - Pair resolution - = queryRangeTableFunctionResolver.resolve(functionSignature); + List expressions = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument(null, DSL.literal(12345)), + DSL.namedArgument(null, DSL.literal(12345)), + DSL.namedArgument(null, DSL.literal(14))); + FunctionSignature functionSignature = + new FunctionSignature( + functionName, expressions.stream().map(Expression::type).collect(Collectors.toList())); + Pair resolution = + queryRangeTableFunctionResolver.resolve(functionSignature); assertEquals(functionName, resolution.getKey().getFunctionName()); assertEquals(functionName, queryRangeTableFunctionResolver.getFunctionName()); assertEquals(List.of(STRING, LONG, LONG, STRING), resolution.getKey().getParamTypeList()); - SemanticCheckException exception = assertThrows(SemanticCheckException.class, - () -> resolution.getValue().apply(functionProperties, expressions)); + SemanticCheckException exception = + assertThrows( + SemanticCheckException.class, + () -> resolution.getValue().apply(functionProperties, expressions)); assertEquals("Arguments should be either passed by name or position", exception.getMessage()); } @Test void testWrongArgumentsSizeWhenPassedByName() { - QueryRangeTableFunctionResolver queryRangeTableFunctionResolver - = new QueryRangeTableFunctionResolver(client); + QueryRangeTableFunctionResolver queryRangeTableFunctionResolver = + new QueryRangeTableFunctionResolver(client); FunctionName functionName = FunctionName.of("query_range"); - List expressions - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("step", DSL.literal(12345))); - FunctionSignature functionSignature = new FunctionSignature(functionName, expressions - .stream().map(Expression::type).collect(Collectors.toList())); - Pair resolution - = queryRangeTableFunctionResolver.resolve(functionSignature); + List expressions = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("step", DSL.literal(12345))); + FunctionSignature functionSignature = + new FunctionSignature( + functionName, expressions.stream().map(Expression::type).collect(Collectors.toList())); + Pair resolution = + queryRangeTableFunctionResolver.resolve(functionSignature); assertEquals(functionName, resolution.getKey().getFunctionName()); assertEquals(functionName, queryRangeTableFunctionResolver.getFunctionName()); assertEquals(List.of(STRING, LONG, LONG, STRING), resolution.getKey().getParamTypeList()); - SemanticCheckException exception = assertThrows(SemanticCheckException.class, - () -> resolution.getValue().apply(functionProperties, expressions)); + SemanticCheckException exception = + assertThrows( + SemanticCheckException.class, + () -> resolution.getValue().apply(functionProperties, expressions)); assertEquals("Missing arguments:[endtime,starttime]", exception.getMessage()); } @Test void testWrongArgumentsSizeWhenPassedByPosition() { - QueryRangeTableFunctionResolver queryRangeTableFunctionResolver - = new QueryRangeTableFunctionResolver(client); + QueryRangeTableFunctionResolver queryRangeTableFunctionResolver = + new QueryRangeTableFunctionResolver(client); FunctionName functionName = FunctionName.of("query_range"); - List expressions - = List.of(DSL.namedArgument(null, DSL.literal("http_latency")), - DSL.namedArgument(null, DSL.literal(12345))); - FunctionSignature functionSignature = new FunctionSignature(functionName, expressions - .stream().map(Expression::type).collect(Collectors.toList())); - Pair resolution - = queryRangeTableFunctionResolver.resolve(functionSignature); + List expressions = + List.of( + DSL.namedArgument(null, DSL.literal("http_latency")), + DSL.namedArgument(null, DSL.literal(12345))); + FunctionSignature functionSignature = + new FunctionSignature( + functionName, expressions.stream().map(Expression::type).collect(Collectors.toList())); + Pair resolution = + queryRangeTableFunctionResolver.resolve(functionSignature); assertEquals(functionName, resolution.getKey().getFunctionName()); assertEquals(functionName, queryRangeTableFunctionResolver.getFunctionName()); assertEquals(List.of(STRING, LONG, LONG, STRING), resolution.getKey().getParamTypeList()); - SemanticCheckException exception = assertThrows(SemanticCheckException.class, - () -> resolution.getValue().apply(functionProperties, expressions)); + SemanticCheckException exception = + assertThrows( + SemanticCheckException.class, + () -> resolution.getValue().apply(functionProperties, expressions)); assertEquals("Missing arguments:[endtime,step]", exception.getMessage()); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanBuilderTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanBuilderTest.java index 6fd782b417..bb7806f824 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanBuilderTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanBuilderTest.java @@ -7,7 +7,6 @@ package org.opensearch.sql.prometheus.functions.scan; - import static org.opensearch.sql.prometheus.constants.TestConstants.ENDTIME; import static org.opensearch.sql.prometheus.constants.TestConstants.QUERY; import static org.opensearch.sql.prometheus.constants.TestConstants.STARTTIME; @@ -22,40 +21,35 @@ public class QueryExemplarsFunctionTableScanBuilderTest { - @Mock - private PrometheusClient prometheusClient; + @Mock private PrometheusClient prometheusClient; - @Mock - private LogicalProject logicalProject; + @Mock private LogicalProject logicalProject; @Test void testBuild() { - PrometheusQueryExemplarsRequest exemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest exemplarsRequest = new PrometheusQueryExemplarsRequest(); exemplarsRequest.setQuery(QUERY); exemplarsRequest.setStartTime(STARTTIME); exemplarsRequest.setEndTime(ENDTIME); - QueryExemplarsFunctionTableScanBuilder queryExemplarsFunctionTableScanBuilder - = new QueryExemplarsFunctionTableScanBuilder(prometheusClient, exemplarsRequest); - TableScanOperator queryExemplarsFunctionTableScanOperator - = queryExemplarsFunctionTableScanBuilder.build(); + QueryExemplarsFunctionTableScanBuilder queryExemplarsFunctionTableScanBuilder = + new QueryExemplarsFunctionTableScanBuilder(prometheusClient, exemplarsRequest); + TableScanOperator queryExemplarsFunctionTableScanOperator = + queryExemplarsFunctionTableScanBuilder.build(); Assertions.assertNotNull(queryExemplarsFunctionTableScanOperator); - Assertions.assertTrue(queryExemplarsFunctionTableScanOperator - instanceof QueryExemplarsFunctionTableScanOperator); + Assertions.assertTrue( + queryExemplarsFunctionTableScanOperator instanceof QueryExemplarsFunctionTableScanOperator); } @Test void testPushProject() { - PrometheusQueryExemplarsRequest exemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest exemplarsRequest = new PrometheusQueryExemplarsRequest(); exemplarsRequest.setQuery(QUERY); exemplarsRequest.setStartTime(STARTTIME); exemplarsRequest.setEndTime(ENDTIME); - QueryExemplarsFunctionTableScanBuilder queryExemplarsFunctionTableScanBuilder - = new QueryExemplarsFunctionTableScanBuilder(prometheusClient, exemplarsRequest); - Assertions.assertTrue(queryExemplarsFunctionTableScanBuilder - .pushDownProject(logicalProject)); + QueryExemplarsFunctionTableScanBuilder queryExemplarsFunctionTableScanBuilder = + new QueryExemplarsFunctionTableScanBuilder(prometheusClient, exemplarsRequest); + Assertions.assertTrue(queryExemplarsFunctionTableScanBuilder.pushDownProject(logicalProject)); } } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanOperatorTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanOperatorTest.java index d4e31d4d1e..5b8cf34fc2 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanOperatorTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanOperatorTest.java @@ -41,22 +41,21 @@ @ExtendWith(MockitoExtension.class) public class QueryExemplarsFunctionTableScanOperatorTest { - @Mock - private PrometheusClient prometheusClient; + @Mock private PrometheusClient prometheusClient; @Test @SneakyThrows void testQueryResponseIterator() { - PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest = + new PrometheusQueryExemplarsRequest(); prometheusQueryExemplarsRequest.setQuery(QUERY); prometheusQueryExemplarsRequest.setStartTime(STARTTIME); prometheusQueryExemplarsRequest.setEndTime(ENDTIME); - QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator - = new QueryExemplarsFunctionTableScanOperator(prometheusClient, - prometheusQueryExemplarsRequest); + QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator = + new QueryExemplarsFunctionTableScanOperator( + prometheusClient, prometheusQueryExemplarsRequest); when(prometheusClient.queryExemplars(any(), any(), any())) .thenReturn(new JSONArray(getJson("query_exemplars_result.json"))); @@ -68,24 +67,28 @@ void testQueryResponseIterator() { seriesLabelsHashMap.put("service", new ExprStringValue("bar")); seriesLabelsHashMap.put("job", new ExprStringValue("prometheus")); LinkedHashMap exemplarMap = new LinkedHashMap<>(); - exemplarMap.put("labels", new ExprTupleValue(new LinkedHashMap<>() { - { - put("traceID", new ExprStringValue("EpTxMJ40fUus7aGY")); - } - }) - ); + exemplarMap.put( + "labels", + new ExprTupleValue( + new LinkedHashMap<>() { + { + put("traceID", new ExprStringValue("EpTxMJ40fUus7aGY")); + } + })); exemplarMap.put("timestamp", new ExprTimestampValue(Instant.ofEpochMilli(1600096945479L))); exemplarMap.put("value", new ExprDoubleValue(6)); List exprValueList = new ArrayList<>(); exprValueList.add(new ExprTupleValue(exemplarMap)); ExprCollectionValue exemplars = new ExprCollectionValue(exprValueList); ExprTupleValue seriesLabels = new ExprTupleValue(seriesLabelsHashMap); - ExprTupleValue firstRow = new ExprTupleValue(new LinkedHashMap<>() { - { - put("seriesLabels", seriesLabels); - put("exemplars", exemplars); - } - }); + ExprTupleValue firstRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put("seriesLabels", seriesLabels); + put("exemplars", exemplars); + } + }); assertEquals(firstRow, queryExemplarsFunctionTableScanOperator.next()); } @@ -93,15 +96,15 @@ void testQueryResponseIterator() { @Test @SneakyThrows void testEmptyQueryWithNoMatrixKeyInResultJson() { - PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest = + new PrometheusQueryExemplarsRequest(); prometheusQueryExemplarsRequest.setQuery(QUERY); prometheusQueryExemplarsRequest.setStartTime(STARTTIME); prometheusQueryExemplarsRequest.setEndTime(ENDTIME); - QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator - = new QueryExemplarsFunctionTableScanOperator(prometheusClient, - prometheusQueryExemplarsRequest); + QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator = + new QueryExemplarsFunctionTableScanOperator( + prometheusClient, prometheusQueryExemplarsRequest); when(prometheusClient.queryExemplars(any(), any(), any())) .thenReturn(new JSONArray(getJson("query_exemplars_empty_result.json"))); @@ -113,15 +116,15 @@ void testEmptyQueryWithNoMatrixKeyInResultJson() { @SneakyThrows void testQuerySchema() { - PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest = + new PrometheusQueryExemplarsRequest(); prometheusQueryExemplarsRequest.setQuery(QUERY); prometheusQueryExemplarsRequest.setStartTime(STARTTIME); prometheusQueryExemplarsRequest.setEndTime(ENDTIME); - QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator - = new QueryExemplarsFunctionTableScanOperator(prometheusClient, - prometheusQueryExemplarsRequest); + QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator = + new QueryExemplarsFunctionTableScanOperator( + prometheusClient, prometheusQueryExemplarsRequest); when(prometheusClient.queryExemplars(any(), any(), any())) .thenReturn(new JSONArray(getJson("query_exemplars_result.json"))); @@ -140,53 +143,53 @@ void testQuerySchema() { @SneakyThrows void testEmptyQueryWithException() { - PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest = + new PrometheusQueryExemplarsRequest(); prometheusQueryExemplarsRequest.setQuery(QUERY); prometheusQueryExemplarsRequest.setStartTime(STARTTIME); prometheusQueryExemplarsRequest.setEndTime(ENDTIME); - QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator - = new QueryExemplarsFunctionTableScanOperator(prometheusClient, - prometheusQueryExemplarsRequest); + QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator = + new QueryExemplarsFunctionTableScanOperator( + prometheusClient, prometheusQueryExemplarsRequest); when(prometheusClient.queryExemplars(any(), any(), any())) .thenThrow(new IOException("Error Message")); - RuntimeException runtimeException - = assertThrows(RuntimeException.class, queryExemplarsFunctionTableScanOperator::open); - assertEquals("Error fetching data from prometheus server: Error Message", - runtimeException.getMessage()); + RuntimeException runtimeException = + assertThrows(RuntimeException.class, queryExemplarsFunctionTableScanOperator::open); + assertEquals( + "Error fetching data from prometheus server: Error Message", runtimeException.getMessage()); } - @Test @SneakyThrows void testExplain() { - PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest = + new PrometheusQueryExemplarsRequest(); prometheusQueryExemplarsRequest.setQuery(QUERY); prometheusQueryExemplarsRequest.setStartTime(STARTTIME); prometheusQueryExemplarsRequest.setEndTime(ENDTIME); - QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator - = new QueryExemplarsFunctionTableScanOperator(prometheusClient, - prometheusQueryExemplarsRequest); - Assertions.assertEquals("query_exemplars(test_query, 1664767694133, 1664771294133)", + QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator = + new QueryExemplarsFunctionTableScanOperator( + prometheusClient, prometheusQueryExemplarsRequest); + Assertions.assertEquals( + "query_exemplars(test_query, 1664767694133, 1664771294133)", queryExemplarsFunctionTableScanOperator.explain()); } @Test @SneakyThrows void testClose() { - PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest = + new PrometheusQueryExemplarsRequest(); prometheusQueryExemplarsRequest.setQuery(QUERY); prometheusQueryExemplarsRequest.setStartTime(STARTTIME); prometheusQueryExemplarsRequest.setEndTime(ENDTIME); - QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator - = new QueryExemplarsFunctionTableScanOperator(prometheusClient, - prometheusQueryExemplarsRequest); + QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator = + new QueryExemplarsFunctionTableScanOperator( + prometheusClient, prometheusQueryExemplarsRequest); queryExemplarsFunctionTableScanOperator.close(); } } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanBuilderTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanBuilderTest.java index 8532a35395..dca79d6905 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanBuilderTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanBuilderTest.java @@ -7,7 +7,6 @@ package org.opensearch.sql.prometheus.functions.scan; - import static org.opensearch.sql.prometheus.constants.TestConstants.ENDTIME; import static org.opensearch.sql.prometheus.constants.TestConstants.QUERY; import static org.opensearch.sql.prometheus.constants.TestConstants.STARTTIME; @@ -23,11 +22,9 @@ public class QueryRangeFunctionTableScanBuilderTest { - @Mock - private PrometheusClient prometheusClient; + @Mock private PrometheusClient prometheusClient; - @Mock - private LogicalProject logicalProject; + @Mock private LogicalProject logicalProject; @Test void testBuild() { @@ -37,13 +34,13 @@ void testBuild() { prometheusQueryRequest.setEndTime(ENDTIME); prometheusQueryRequest.setStep(STEP); - QueryRangeFunctionTableScanBuilder queryRangeFunctionTableScanBuilder - = new QueryRangeFunctionTableScanBuilder(prometheusClient, prometheusQueryRequest); - TableScanOperator queryRangeFunctionTableScanOperator - = queryRangeFunctionTableScanBuilder.build(); + QueryRangeFunctionTableScanBuilder queryRangeFunctionTableScanBuilder = + new QueryRangeFunctionTableScanBuilder(prometheusClient, prometheusQueryRequest); + TableScanOperator queryRangeFunctionTableScanOperator = + queryRangeFunctionTableScanBuilder.build(); Assertions.assertNotNull(queryRangeFunctionTableScanOperator); - Assertions.assertTrue(queryRangeFunctionTableScanOperator - instanceof QueryRangeFunctionTableScanOperator); + Assertions.assertTrue( + queryRangeFunctionTableScanOperator instanceof QueryRangeFunctionTableScanOperator); } @Test @@ -54,8 +51,8 @@ void testPushProject() { prometheusQueryRequest.setEndTime(ENDTIME); prometheusQueryRequest.setStep(STEP); - QueryRangeFunctionTableScanBuilder queryRangeFunctionTableScanBuilder - = new QueryRangeFunctionTableScanBuilder(prometheusClient, prometheusQueryRequest); + QueryRangeFunctionTableScanBuilder queryRangeFunctionTableScanBuilder = + new QueryRangeFunctionTableScanBuilder(prometheusClient, prometheusQueryRequest); Assertions.assertTrue(queryRangeFunctionTableScanBuilder.pushDownProject(logicalProject)); } } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanOperatorTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanOperatorTest.java index b476471153..e59a2bf7c4 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanOperatorTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanOperatorTest.java @@ -45,8 +45,7 @@ @ExtendWith(MockitoExtension.class) class QueryRangeFunctionTableScanOperatorTest { - @Mock - private PrometheusClient prometheusClient; + @Mock private PrometheusClient prometheusClient; @Test @SneakyThrows @@ -58,41 +57,63 @@ void testQueryResponseIterator() { prometheusQueryRequest.setEndTime(ENDTIME); prometheusQueryRequest.setStep(STEP); - QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator - = new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); + QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator = + new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); when(prometheusClient.queryRange(any(), any(), any(), any())) .thenReturn(new JSONObject(getJson("query_range_result.json"))); queryRangeFunctionTableScanOperator.open(); Assertions.assertTrue(queryRangeFunctionTableScanOperator.hasNext()); - LinkedHashMap labelsMap = new LinkedHashMap<>() {{ - put("instance", new ExprStringValue("localhost:9090")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("prometheus")); - }}; - ExprTupleValue firstRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(LABELS, new ExprTupleValue(labelsMap)); - put(TIMESTAMP, new ExprCollectionValue(Collections - .singletonList(new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))))); - put(VALUE, new ExprCollectionValue(Collections.singletonList(new ExprDoubleValue(1)))); - } - }); + LinkedHashMap labelsMap = + new LinkedHashMap<>() { + { + put("instance", new ExprStringValue("localhost:9090")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("prometheus")); + } + }; + ExprTupleValue firstRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(LABELS, new ExprTupleValue(labelsMap)); + put( + TIMESTAMP, + new ExprCollectionValue( + Collections.singletonList( + new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))))); + put( + VALUE, + new ExprCollectionValue(Collections.singletonList(new ExprDoubleValue(1)))); + } + }); assertEquals(firstRow, queryRangeFunctionTableScanOperator.next()); Assertions.assertTrue(queryRangeFunctionTableScanOperator.hasNext()); - LinkedHashMap labelsMap2 = new LinkedHashMap<>() {{ - put("instance", new ExprStringValue("localhost:9091")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("node")); - }}; - ExprTupleValue secondRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(LABELS, new ExprTupleValue(labelsMap2)); - put(TIMESTAMP, new ExprCollectionValue(Collections - .singletonList(new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))))); - put(VALUE, new ExprCollectionValue(Collections.singletonList(new ExprDoubleValue(0)))); - } - }); + LinkedHashMap labelsMap2 = + new LinkedHashMap<>() { + { + put("instance", new ExprStringValue("localhost:9091")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("node")); + } + }; + ExprTupleValue secondRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(LABELS, new ExprTupleValue(labelsMap2)); + put( + TIMESTAMP, + new ExprCollectionValue( + Collections.singletonList( + new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))))); + put( + VALUE, + new ExprCollectionValue(Collections.singletonList(new ExprDoubleValue(0)))); + } + }); assertEquals(secondRow, queryRangeFunctionTableScanOperator.next()); Assertions.assertFalse(queryRangeFunctionTableScanOperator.hasNext()); } @@ -106,16 +127,17 @@ void testEmptyQueryWithNoMatrixKeyInResultJson() { prometheusQueryRequest.setEndTime(ENDTIME); prometheusQueryRequest.setStep(STEP); - QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator - = new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); + QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator = + new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); when(prometheusClient.queryRange(any(), any(), any(), any())) .thenReturn(new JSONObject(getJson("no_matrix_query_range_result.json"))); - RuntimeException runtimeException - = assertThrows(RuntimeException.class, queryRangeFunctionTableScanOperator::open); + RuntimeException runtimeException = + assertThrows(RuntimeException.class, queryRangeFunctionTableScanOperator::open); assertEquals( "Unexpected Result Type: vector during Prometheus Response Parsing. " - + "'matrix' resultType is expected", runtimeException.getMessage()); + + "'matrix' resultType is expected", + runtimeException.getMessage()); } @Test @@ -127,8 +149,8 @@ void testQuerySchema() { prometheusQueryRequest.setEndTime(ENDTIME); prometheusQueryRequest.setStep(STEP); - QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator - = new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); + QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator = + new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); when(prometheusClient.queryRange(any(), any(), any(), any())) .thenReturn(new JSONObject(getJson("query_range_result.json"))); @@ -150,18 +172,17 @@ void testEmptyQueryWithException() { prometheusQueryRequest.setEndTime(ENDTIME); prometheusQueryRequest.setStep(STEP); - QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator - = new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); + QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator = + new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); when(prometheusClient.queryRange(any(), any(), any(), any())) .thenThrow(new IOException("Error Message")); - RuntimeException runtimeException - = assertThrows(RuntimeException.class, queryRangeFunctionTableScanOperator::open); - assertEquals("Error fetching data from prometheus server: Error Message", - runtimeException.getMessage()); + RuntimeException runtimeException = + assertThrows(RuntimeException.class, queryRangeFunctionTableScanOperator::open); + assertEquals( + "Error fetching data from prometheus server: Error Message", runtimeException.getMessage()); } - @Test @SneakyThrows void testExplain() { @@ -171,10 +192,11 @@ void testExplain() { prometheusQueryRequest.setEndTime(ENDTIME); prometheusQueryRequest.setStep(STEP); - QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator - = new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); + QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator = + new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); - Assertions.assertEquals("query_range(test_query, 1664767694133, 1664771294133, 14)", + Assertions.assertEquals( + "query_range(test_query, 1664767694133, 1664771294133, 14)", queryRangeFunctionTableScanOperator.explain()); } @@ -187,8 +209,8 @@ void testClose() { prometheusQueryRequest.setEndTime(ENDTIME); prometheusQueryRequest.setStep(STEP); - QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator - = new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); + QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator = + new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); queryRangeFunctionTableScanOperator.close(); } } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicOptimizerTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicOptimizerTest.java index a1d1cef91d..33c48e2f2d 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicOptimizerTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicOptimizerTest.java @@ -32,60 +32,50 @@ @ExtendWith(MockitoExtension.class) public class PrometheusLogicOptimizerTest { - @Mock - private Table table; + @Mock private Table table; @Test void project_filter_merge_with_relation() { assertEquals( project( - indexScan("prometheus_http_total_requests", - DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200")))) - ), + indexScan( + "prometheus_http_total_requests", + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))))), optimize( project( filter( relation("prometheus_http_total_requests", table), - DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))) - )) - ) - ); + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))))))); } @Test void aggregation_merge_relation() { assertEquals( project( - indexScanAgg("prometheus_http_total_requests", ImmutableList - .of(DSL.named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), + indexScanAgg( + "prometheus_http_total_requests", + ImmutableList.of(DSL.named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), ImmutableList.of(DSL.named("code", DSL.ref("code", STRING)))), DSL.named("AVG(intV)", DSL.ref("AVG(intV)", DOUBLE))), optimize( project( aggregation( relation("prometheus_http_total_requests", table), - ImmutableList - .of(DSL.named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(DSL.named("code", - DSL.ref("code", STRING)))), - DSL.named("AVG(intV)", DSL.ref("AVG(intV)", DOUBLE))) - ) - ); + ImmutableList.of(DSL.named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of(DSL.named("code", DSL.ref("code", STRING)))), + DSL.named("AVG(intV)", DSL.ref("AVG(intV)", DOUBLE))))); } - @Test void aggregation_merge_filter_relation() { assertEquals( project( - indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + indexScanAgg( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), - ImmutableList - .of(DSL.named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of(DSL.named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), ImmutableList.of(DSL.named("job", DSL.ref("job", STRING)))), DSL.named("AVG(@value)", DSL.ref("AVG(@value)", DOUBLE))), optimize( @@ -94,25 +84,16 @@ void aggregation_merge_filter_relation() { filter( relation("prometheus_http_total_requests", table), DSL.and( - DSL.equal(DSL.ref("code", STRING), - DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), - DSL.literal(stringValue("/ready/")))) - ), - ImmutableList - .of(DSL.named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(DSL.named("job", - DSL.ref("job", STRING)))), - DSL.named("AVG(@value)", DSL.ref("AVG(@value)", DOUBLE))) - ) - ); + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal( + DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))))), + ImmutableList.of(DSL.named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of(DSL.named("job", DSL.ref("job", STRING)))), + DSL.named("AVG(@value)", DSL.ref("AVG(@value)", DOUBLE))))); } - private LogicalPlan optimize(LogicalPlan plan) { final LogicalPlanOptimizer optimizer = PrometheusLogicalPlanOptimizerFactory.create(); return optimizer.optimize(plan); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/request/PrometheusDescribeMetricRequestTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/request/PrometheusDescribeMetricRequestTest.java index dfc9aee7dc..9add7896cf 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/request/PrometheusDescribeMetricRequestTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/request/PrometheusDescribeMetricRequestTest.java @@ -37,54 +37,61 @@ @ExtendWith(MockitoExtension.class) public class PrometheusDescribeMetricRequestTest { - @Mock - private PrometheusClient prometheusClient; + @Mock private PrometheusClient prometheusClient; @Test @SneakyThrows void testGetFieldTypes() { - when(prometheusClient.getLabels(METRIC_NAME)).thenReturn(new ArrayList() {{ - add("call"); - add("code"); - } - }); - Map expected = new HashMap<>() {{ - put("call", ExprCoreType.STRING); - put("code", ExprCoreType.STRING); - put(VALUE, ExprCoreType.DOUBLE); - put(TIMESTAMP, ExprCoreType.TIMESTAMP); - }}; - PrometheusDescribeMetricRequest prometheusDescribeMetricRequest - = new PrometheusDescribeMetricRequest(prometheusClient, - new DataSourceSchemaName("prometheus", "default"), METRIC_NAME); + when(prometheusClient.getLabels(METRIC_NAME)) + .thenReturn( + new ArrayList() { + { + add("call"); + add("code"); + } + }); + Map expected = + new HashMap<>() { + { + put("call", ExprCoreType.STRING); + put("code", ExprCoreType.STRING); + put(VALUE, ExprCoreType.DOUBLE); + put(TIMESTAMP, ExprCoreType.TIMESTAMP); + } + }; + PrometheusDescribeMetricRequest prometheusDescribeMetricRequest = + new PrometheusDescribeMetricRequest( + prometheusClient, new DataSourceSchemaName("prometheus", "default"), METRIC_NAME); assertEquals(expected, prometheusDescribeMetricRequest.getFieldTypes()); verify(prometheusClient, times(1)).getLabels(METRIC_NAME); } - @Test @SneakyThrows void testGetFieldTypesWithEmptyMetricName() { - Map expected = new HashMap<>() {{ - put(VALUE, ExprCoreType.DOUBLE); - put(TIMESTAMP, ExprCoreType.TIMESTAMP); - }}; - assertThrows(NullPointerException.class, - () -> new PrometheusDescribeMetricRequest(prometheusClient, - new DataSourceSchemaName("prometheus", "default"), - null)); + Map expected = + new HashMap<>() { + { + put(VALUE, ExprCoreType.DOUBLE); + put(TIMESTAMP, ExprCoreType.TIMESTAMP); + } + }; + assertThrows( + NullPointerException.class, + () -> + new PrometheusDescribeMetricRequest( + prometheusClient, new DataSourceSchemaName("prometheus", "default"), null)); } - @Test @SneakyThrows void testGetFieldTypesWhenException() { when(prometheusClient.getLabels(METRIC_NAME)).thenThrow(new RuntimeException("ERROR Message")); - PrometheusDescribeMetricRequest prometheusDescribeMetricRequest - = new PrometheusDescribeMetricRequest(prometheusClient, - new DataSourceSchemaName("prometheus", "default"), METRIC_NAME); - RuntimeException exception = assertThrows(RuntimeException.class, - prometheusDescribeMetricRequest::getFieldTypes); + PrometheusDescribeMetricRequest prometheusDescribeMetricRequest = + new PrometheusDescribeMetricRequest( + prometheusClient, new DataSourceSchemaName("prometheus", "default"), METRIC_NAME); + RuntimeException exception = + assertThrows(RuntimeException.class, prometheusDescribeMetricRequest::getFieldTypes); verify(prometheusClient, times(1)).getLabels(METRIC_NAME); assertEquals("ERROR Message", exception.getMessage()); } @@ -93,27 +100,30 @@ void testGetFieldTypesWhenException() { @SneakyThrows void testGetFieldTypesWhenIOException() { when(prometheusClient.getLabels(METRIC_NAME)).thenThrow(new IOException("ERROR Message")); - PrometheusDescribeMetricRequest prometheusDescribeMetricRequest - = new PrometheusDescribeMetricRequest(prometheusClient, - new DataSourceSchemaName("prometheus", "default"), METRIC_NAME); - RuntimeException exception = assertThrows(RuntimeException.class, - prometheusDescribeMetricRequest::getFieldTypes); - assertEquals("Error while fetching labels for http_requests_total" - + " from prometheus: ERROR Message", exception.getMessage()); + PrometheusDescribeMetricRequest prometheusDescribeMetricRequest = + new PrometheusDescribeMetricRequest( + prometheusClient, new DataSourceSchemaName("prometheus", "default"), METRIC_NAME); + RuntimeException exception = + assertThrows(RuntimeException.class, prometheusDescribeMetricRequest::getFieldTypes); + assertEquals( + "Error while fetching labels for http_requests_total" + " from prometheus: ERROR Message", + exception.getMessage()); verify(prometheusClient, times(1)).getLabels(METRIC_NAME); } @Test @SneakyThrows void testSearch() { - when(prometheusClient.getLabels(METRIC_NAME)).thenReturn(new ArrayList<>() { - { - add("call"); - } - }); - PrometheusDescribeMetricRequest prometheusDescribeMetricRequest - = new PrometheusDescribeMetricRequest(prometheusClient, - new DataSourceSchemaName("test", "default"), METRIC_NAME); + when(prometheusClient.getLabels(METRIC_NAME)) + .thenReturn( + new ArrayList<>() { + { + add("call"); + } + }); + PrometheusDescribeMetricRequest prometheusDescribeMetricRequest = + new PrometheusDescribeMetricRequest( + prometheusClient, new DataSourceSchemaName("test", "default"), METRIC_NAME); List result = prometheusDescribeMetricRequest.search(); assertEquals(3, result.size()); assertEquals(expectedRow(), result.get(0)); @@ -129,5 +139,4 @@ private ExprValue expectedRow() { valueMap.put("DATA_TYPE", stringValue(ExprCoreType.STRING.legacyTypeName().toLowerCase())); return new ExprTupleValue(valueMap); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/request/PrometheusListMetricsRequestTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/request/PrometheusListMetricsRequestTest.java index bf5bb22e96..09f63463b5 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/request/PrometheusListMetricsRequestTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/request/PrometheusListMetricsRequestTest.java @@ -35,45 +35,46 @@ @ExtendWith(MockitoExtension.class) public class PrometheusListMetricsRequestTest { - @Mock - private PrometheusClient prometheusClient; + @Mock private PrometheusClient prometheusClient; @Test @SneakyThrows void testSearch() { Map> metricsResult = new HashMap<>(); - metricsResult.put("go_gc_duration_seconds", - Collections.singletonList(new MetricMetadata("summary", - "A summary of the pause duration of garbage collection cycles.", ""))); - metricsResult.put("go_goroutines", - Collections.singletonList(new MetricMetadata("gauge", - "Number of goroutines that currently exist.", ""))); + metricsResult.put( + "go_gc_duration_seconds", + Collections.singletonList( + new MetricMetadata( + "summary", "A summary of the pause duration of garbage collection cycles.", ""))); + metricsResult.put( + "go_goroutines", + Collections.singletonList( + new MetricMetadata("gauge", "Number of goroutines that currently exist.", ""))); when(prometheusClient.getAllMetrics()).thenReturn(metricsResult); - PrometheusListMetricsRequest prometheusListMetricsRequest - = new PrometheusListMetricsRequest(prometheusClient, - new DataSourceSchemaName("prometheus", "information_schema")); + PrometheusListMetricsRequest prometheusListMetricsRequest = + new PrometheusListMetricsRequest( + prometheusClient, new DataSourceSchemaName("prometheus", "information_schema")); List result = prometheusListMetricsRequest.search(); assertEquals(expectedRow(), result.get(0)); assertEquals(2, result.size()); verify(prometheusClient, times(1)).getAllMetrics(); } - @Test @SneakyThrows void testSearchWhenIOException() { when(prometheusClient.getAllMetrics()).thenThrow(new IOException("ERROR Message")); - PrometheusListMetricsRequest prometheusListMetricsRequest - = new PrometheusListMetricsRequest(prometheusClient, - new DataSourceSchemaName("prometheus", "information_schema")); - RuntimeException exception = assertThrows(RuntimeException.class, - prometheusListMetricsRequest::search); - assertEquals("Error while fetching metric list for from prometheus: ERROR Message", + PrometheusListMetricsRequest prometheusListMetricsRequest = + new PrometheusListMetricsRequest( + prometheusClient, new DataSourceSchemaName("prometheus", "information_schema")); + RuntimeException exception = + assertThrows(RuntimeException.class, prometheusListMetricsRequest::search); + assertEquals( + "Error while fetching metric list for from prometheus: ERROR Message", exception.getMessage()); verify(prometheusClient, times(1)).getAllMetrics(); } - private ExprTupleValue expectedRow() { LinkedHashMap valueMap = new LinkedHashMap<>(); valueMap.put("TABLE_CATALOG", stringValue("prometheus")); @@ -81,9 +82,8 @@ private ExprTupleValue expectedRow() { valueMap.put("TABLE_NAME", stringValue("go_gc_duration_seconds")); valueMap.put("TABLE_TYPE", stringValue("summary")); valueMap.put("UNIT", stringValue("")); - valueMap.put("REMARKS", - stringValue("A summary of the pause duration of garbage collection cycles.")); + valueMap.put( + "REMARKS", stringValue("A summary of the pause duration of garbage collection cycles.")); return new ExprTupleValue(valueMap); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricScanTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricScanTest.java index 9c0207853c..00ddc973bc 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricScanTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricScanTest.java @@ -44,8 +44,7 @@ @ExtendWith(MockitoExtension.class) public class PrometheusMetricScanTest { - @Mock - private PrometheusClient prometheusClient; + @Mock private PrometheusClient prometheusClient; @Test @SneakyThrows @@ -60,24 +59,30 @@ void testQueryResponseIterator() { .thenReturn(new JSONObject(getJson("query_range_result.json"))); prometheusMetricScan.open(); Assertions.assertTrue(prometheusMetricScan.hasNext()); - ExprTupleValue firstRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); - put(VALUE, new ExprDoubleValue(1)); - put("instance", new ExprStringValue("localhost:9090")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("prometheus")); - } - }); + ExprTupleValue firstRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); + put(VALUE, new ExprDoubleValue(1)); + put("instance", new ExprStringValue("localhost:9090")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("prometheus")); + } + }); assertEquals(firstRow, prometheusMetricScan.next()); Assertions.assertTrue(prometheusMetricScan.hasNext()); - ExprTupleValue secondRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put("@timestamp", new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); - put("@value", new ExprDoubleValue(0)); - put("instance", new ExprStringValue("localhost:9091")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("node")); - } - }); + ExprTupleValue secondRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put("@timestamp", new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); + put("@value", new ExprDoubleValue(0)); + put("instance", new ExprStringValue("localhost:9091")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("node")); + } + }); assertEquals(secondRow, prometheusMetricScan.next()); Assertions.assertFalse(prometheusMetricScan.hasNext()); } @@ -85,8 +90,7 @@ void testQueryResponseIterator() { @Test @SneakyThrows void testQueryResponseIteratorWithGivenPrometheusResponseFieldNames() { - PrometheusResponseFieldNames prometheusResponseFieldNames - = new PrometheusResponseFieldNames(); + PrometheusResponseFieldNames prometheusResponseFieldNames = new PrometheusResponseFieldNames(); prometheusResponseFieldNames.setValueFieldName("count()"); prometheusResponseFieldNames.setValueType(INTEGER); prometheusResponseFieldNames.setTimestampFieldName(TIMESTAMP); @@ -101,34 +105,38 @@ void testQueryResponseIteratorWithGivenPrometheusResponseFieldNames() { .thenReturn(new JSONObject(getJson("query_range_result.json"))); prometheusMetricScan.open(); Assertions.assertTrue(prometheusMetricScan.hasNext()); - ExprTupleValue firstRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); - put("count()", new ExprIntegerValue(1)); - put("instance", new ExprStringValue("localhost:9090")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("prometheus")); - } - }); + ExprTupleValue firstRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); + put("count()", new ExprIntegerValue(1)); + put("instance", new ExprStringValue("localhost:9090")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("prometheus")); + } + }); assertEquals(firstRow, prometheusMetricScan.next()); Assertions.assertTrue(prometheusMetricScan.hasNext()); - ExprTupleValue secondRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); - put("count()", new ExprIntegerValue(0)); - put("instance", new ExprStringValue("localhost:9091")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("node")); - } - }); + ExprTupleValue secondRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); + put("count()", new ExprIntegerValue(0)); + put("instance", new ExprStringValue("localhost:9091")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("node")); + } + }); assertEquals(secondRow, prometheusMetricScan.next()); Assertions.assertFalse(prometheusMetricScan.hasNext()); } - @Test @SneakyThrows void testQueryResponseIteratorWithGivenPrometheusResponseWithLongInAggType() { - PrometheusResponseFieldNames prometheusResponseFieldNames - = new PrometheusResponseFieldNames(); + PrometheusResponseFieldNames prometheusResponseFieldNames = new PrometheusResponseFieldNames(); prometheusResponseFieldNames.setValueFieldName("testAgg"); prometheusResponseFieldNames.setValueType(LONG); prometheusResponseFieldNames.setTimestampFieldName(TIMESTAMP); @@ -143,24 +151,30 @@ void testQueryResponseIteratorWithGivenPrometheusResponseWithLongInAggType() { .thenReturn(new JSONObject(getJson("query_range_result.json"))); prometheusMetricScan.open(); Assertions.assertTrue(prometheusMetricScan.hasNext()); - ExprTupleValue firstRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); - put("testAgg", new ExprLongValue(1)); - put("instance", new ExprStringValue("localhost:9090")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("prometheus")); - } - }); + ExprTupleValue firstRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); + put("testAgg", new ExprLongValue(1)); + put("instance", new ExprStringValue("localhost:9090")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("prometheus")); + } + }); assertEquals(firstRow, prometheusMetricScan.next()); Assertions.assertTrue(prometheusMetricScan.hasNext()); - ExprTupleValue secondRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); - put("testAgg", new ExprLongValue(0)); - put("instance", new ExprStringValue("localhost:9091")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("node")); - } - }); + ExprTupleValue secondRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); + put("testAgg", new ExprLongValue(0)); + put("instance", new ExprStringValue("localhost:9091")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("node")); + } + }); assertEquals(secondRow, prometheusMetricScan.next()); Assertions.assertFalse(prometheusMetricScan.hasNext()); } @@ -168,8 +182,7 @@ void testQueryResponseIteratorWithGivenPrometheusResponseWithLongInAggType() { @Test @SneakyThrows void testQueryResponseIteratorWithGivenPrometheusResponseWithBackQuotedFieldNames() { - PrometheusResponseFieldNames prometheusResponseFieldNames - = new PrometheusResponseFieldNames(); + PrometheusResponseFieldNames prometheusResponseFieldNames = new PrometheusResponseFieldNames(); prometheusResponseFieldNames.setValueFieldName("testAgg"); prometheusResponseFieldNames.setValueType(LONG); prometheusResponseFieldNames.setTimestampFieldName(TIMESTAMP); @@ -186,29 +199,34 @@ void testQueryResponseIteratorWithGivenPrometheusResponseWithBackQuotedFieldName .thenReturn(new JSONObject(getJson("query_range_result.json"))); prometheusMetricScan.open(); Assertions.assertTrue(prometheusMetricScan.hasNext()); - ExprTupleValue firstRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); - put("testAgg", new ExprLongValue(1)); - put("`instance`", new ExprStringValue("localhost:9090")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("prometheus")); - } - }); + ExprTupleValue firstRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); + put("testAgg", new ExprLongValue(1)); + put("`instance`", new ExprStringValue("localhost:9090")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("prometheus")); + } + }); assertEquals(firstRow, prometheusMetricScan.next()); Assertions.assertTrue(prometheusMetricScan.hasNext()); - ExprTupleValue secondRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); - put("testAgg", new ExprLongValue(0)); - put("`instance`", new ExprStringValue("localhost:9091")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("node")); - } - }); + ExprTupleValue secondRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); + put("testAgg", new ExprLongValue(0)); + put("`instance`", new ExprStringValue("localhost:9091")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("node")); + } + }); assertEquals(secondRow, prometheusMetricScan.next()); Assertions.assertFalse(prometheusMetricScan.hasNext()); } - @Test @SneakyThrows void testEmptyQueryResponseIterator() { @@ -235,11 +253,12 @@ void testEmptyQueryWithNoMatrixKeyInResultJson() { when(prometheusClient.queryRange(any(), any(), any(), any())) .thenReturn(new JSONObject(getJson("no_matrix_query_range_result.json"))); - RuntimeException runtimeException - = Assertions.assertThrows(RuntimeException.class, prometheusMetricScan::open); + RuntimeException runtimeException = + Assertions.assertThrows(RuntimeException.class, prometheusMetricScan::open); assertEquals( "Unexpected Result Type: vector during Prometheus Response Parsing. " - + "'matrix' resultType is expected", runtimeException.getMessage()); + + "'matrix' resultType is expected", + runtimeException.getMessage()); } @Test @@ -253,13 +272,12 @@ void testEmptyQueryWithException() { when(prometheusClient.queryRange(any(), any(), any(), any())) .thenThrow(new IOException("Error Message")); - RuntimeException runtimeException - = assertThrows(RuntimeException.class, prometheusMetricScan::open); - assertEquals("Error fetching data from prometheus server. Error Message", - runtimeException.getMessage()); + RuntimeException runtimeException = + assertThrows(RuntimeException.class, prometheusMetricScan::open); + assertEquals( + "Error fetching data from prometheus server. Error Message", runtimeException.getMessage()); } - @Test @SneakyThrows void testExplain() { @@ -273,5 +291,4 @@ void testExplain() { + "endTime=1664771294133, step=14)", prometheusMetricScan.explain()); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTableTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTableTest.java index d43c38fc68..8bdab9244b 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTableTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTableTest.java @@ -62,15 +62,14 @@ @ExtendWith(MockitoExtension.class) class PrometheusMetricTableTest { - @Mock - private PrometheusClient client; + @Mock private PrometheusClient client; @Test @SneakyThrows void testGetFieldTypesFromMetric() { when(client.getLabels(TestConstants.METRIC_NAME)).thenReturn(List.of("label1", "label2")); - PrometheusMetricTable prometheusMetricTable - = new PrometheusMetricTable(client, TestConstants.METRIC_NAME); + PrometheusMetricTable prometheusMetricTable = + new PrometheusMetricTable(client, TestConstants.METRIC_NAME); Map expectedFieldTypes = new HashMap<>(); expectedFieldTypes.put("label1", ExprCoreType.STRING); expectedFieldTypes.put("label2", ExprCoreType.STRING); @@ -84,7 +83,7 @@ void testGetFieldTypesFromMetric() { assertNull(prometheusMetricTable.getPrometheusQueryRequest()); assertNotNull(prometheusMetricTable.getMetricName()); - //testing Caching + // testing Caching fieldTypes = prometheusMetricTable.getFieldTypes(); assertEquals(expectedFieldTypes, fieldTypes); @@ -96,8 +95,8 @@ void testGetFieldTypesFromMetric() { @Test @SneakyThrows void testGetFieldTypesFromPrometheusQueryRequest() { - PrometheusMetricTable prometheusMetricTable - = new PrometheusMetricTable(client, new PrometheusQueryRequest()); + PrometheusMetricTable prometheusMetricTable = + new PrometheusMetricTable(client, new PrometheusQueryRequest()); Map expectedFieldTypes = new HashMap<>(); expectedFieldTypes.put(VALUE, ExprCoreType.DOUBLE); expectedFieldTypes.put(TIMESTAMP, ExprCoreType.TIMESTAMP); @@ -117,14 +116,17 @@ void testImplementWithBasicMetricQuery() { new PrometheusMetricTable(client, "prometheus_http_requests_total"); List finalProjectList = new ArrayList<>(); finalProjectList.add(named("@value", ref("@value", ExprCoreType.DOUBLE))); - PhysicalPlan plan = prometheusMetricTable.implement( - project(relation("prometheus_http_requests_total", prometheusMetricTable), - finalProjectList, null)); + PhysicalPlan plan = + prometheusMetricTable.implement( + project( + relation("prometheus_http_requests_total", prometheusMetricTable), + finalProjectList, + null)); assertTrue(plan instanceof ProjectOperator); List projectList = ((ProjectOperator) plan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE), outputFields); assertTrue(((ProjectOperator) plan).getInput() instanceof PrometheusMetricScan); PrometheusMetricScan prometheusMetricScan = @@ -133,7 +135,6 @@ void testImplementWithBasicMetricQuery() { assertEquals(3600 / 250 + "s", prometheusMetricScan.getRequest().getStep()); } - @Test void testImplementPrometheusQueryWithStatsQueryAndNoFilter() { @@ -141,16 +142,23 @@ void testImplementPrometheusQueryWithStatsQueryAndNoFilter() { new PrometheusMetricTable(client, "prometheus_http_total_requests"); // IndexScanAgg without Filter - PhysicalPlan plan = prometheusMetricTable.implement( - filter( - indexScanAgg("prometheus_http_total_requests", ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("code", DSL.ref("code", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s")))), - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))))); + PhysicalPlan plan = + prometheusMetricTable.implement( + filter( + indexScanAgg( + "prometheus_http_total_requests", + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named("code", DSL.ref("code", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s")))), + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))))); assertTrue(plan.getChild().get(0) instanceof PrometheusMetricScan); PrometheusQueryRequest prometheusQueryRequest = @@ -166,28 +174,31 @@ void testImplementPrometheusQueryWithStatsQueryAndFilter() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - // IndexScanAgg with Filter - PhysicalPlan plan = prometheusMetricTable.implement( - indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("job", DSL.ref("job", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s"))))); + PhysicalPlan plan = + prometheusMetricTable.implement( + indexScanAgg( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named("job", DSL.ref("job", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s"))))); assertTrue(plan instanceof PrometheusMetricScan); PrometheusQueryRequest prometheusQueryRequest = ((PrometheusMetricScan) plan).getRequest(); assertEquals( "avg by(job) (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", prometheusQueryRequest.getPromQl()); - } - @Test void testImplementPrometheusQueryWithStatsQueryAndFilterAndProject() { @@ -198,77 +209,99 @@ void testImplementPrometheusQueryWithStatsQueryAndFilterAndProject() { List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); - PhysicalPlan plan = prometheusMetricTable.implement( - project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), - ImmutableList - .of(DSL.named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(DSL.named("job", DSL.ref("job", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s")))), - finalProjectList, null)); + PhysicalPlan plan = + prometheusMetricTable.implement( + project( + indexScanAgg( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), + ImmutableList.of(DSL.named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + DSL.named("job", DSL.ref("job", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s")))), + finalProjectList, + null)); assertTrue(plan instanceof ProjectOperator); assertTrue(((ProjectOperator) plan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); assertEquals(request.getStep(), "40s"); - assertEquals("avg by(job) (avg_over_time" + assertEquals( + "avg by(job) (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", request.getPromQl()); List projectList = ((ProjectOperator) plan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } - @Test void testTimeRangeResolver() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - - //Both endTime and startTime are set. + // Both endTime and startTime are set. List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); Long endTime = new Date(System.currentTimeMillis()).getTime(); Long startTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - PhysicalPlan plan = prometheusMetricTable.implement( - project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + PhysicalPlan plan = + prometheusMetricTable.implement( + project( + indexScanAgg( + "prometheus_http_total_requests", DSL.and( - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.and(DSL.gte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(startTime)), - ExprCoreType.TIMESTAMP))), - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP)))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("job", DSL.ref("job", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s")))), - finalProjectList, null)); + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.and( + DSL.equal( + DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), + DSL.and( + DSL.gte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(startTime)), + ExprCoreType.TIMESTAMP))), + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), + ExprCoreType.TIMESTAMP)))))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named("job", DSL.ref("job", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s")))), + finalProjectList, + null)); assertTrue(plan instanceof ProjectOperator); assertTrue(((ProjectOperator) plan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); assertEquals("40s", request.getStep()); - assertEquals("avg by(job) (avg_over_time" + assertEquals( + "avg by(job) (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", request.getPromQl()); List projectList = ((ProjectOperator) plan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } @@ -278,40 +311,51 @@ void testTimeRangeResolverWithOutEndTimeInFilter() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - - //Only endTime is set. + // Only endTime is set. List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); Long startTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - PhysicalPlan plan = prometheusMetricTable.implement( - project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + PhysicalPlan plan = + prometheusMetricTable.implement( + project( + indexScanAgg( + "prometheus_http_total_requests", DSL.and( - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.gte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(startTime)), - ExprCoreType.TIMESTAMP))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("job", DSL.ref("job", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s")))), - finalProjectList, null)); + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.and( + DSL.equal( + DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), + DSL.gte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(startTime)), + ExprCoreType.TIMESTAMP))))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named("job", DSL.ref("job", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s")))), + finalProjectList, + null)); assertTrue(plan instanceof ProjectOperator); assertTrue(((ProjectOperator) plan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); assertEquals("40s", request.getStep()); - assertEquals("avg by(job) (avg_over_time" + assertEquals( + "avg by(job) (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", request.getPromQl()); List projectList = ((ProjectOperator) plan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } @@ -321,78 +365,95 @@ void testTimeRangeResolverWithOutStartTimeInFilter() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - - //Both endTime and startTime are set. + // Both endTime and startTime are set. List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); Long endTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - PhysicalPlan plan = prometheusMetricTable.implement( - project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + PhysicalPlan plan = + prometheusMetricTable.implement( + project( + indexScanAgg( + "prometheus_http_total_requests", DSL.and( - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("job", DSL.ref("job", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s")))), - finalProjectList, null)); + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.and( + DSL.equal( + DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), + ExprCoreType.TIMESTAMP))))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named("job", DSL.ref("job", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s")))), + finalProjectList, + null)); assertTrue(plan instanceof ProjectOperator); assertTrue(((ProjectOperator) plan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); assertEquals("40s", request.getStep()); - assertEquals("avg by(job) (avg_over_time" + assertEquals( + "avg by(job) (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", request.getPromQl()); List projectList = ((ProjectOperator) plan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } - @Test void testSpanResolverWithoutSpanExpression() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); Long endTime = new Date(System.currentTimeMillis()).getTime(); Long startTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - LogicalPlan plan = project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + LogicalPlan plan = + project( + indexScanAgg( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), DSL.and( DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.and(DSL.gte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.and( + DSL.gte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), DSL.literal( - fromObjectValue(dateFormat.format(new Date(startTime)), + fromObjectValue( + dateFormat.format(new Date(startTime)), ExprCoreType.TIMESTAMP))), - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), + fromObjectValue( + dateFormat.format(new Date(endTime)), ExprCoreType.TIMESTAMP)))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), null), - finalProjectList, null); - RuntimeException runtimeException - = Assertions.assertThrows(RuntimeException.class, - () -> prometheusMetricTable.implement(plan)); - Assertions.assertEquals("Prometheus Catalog doesn't support " - + "aggregations without span expression", + finalProjectList, + null); + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusMetricTable.implement(plan)); + Assertions.assertEquals( + "Prometheus Catalog doesn't support " + "aggregations without span expression", runtimeException.getMessage()); } @@ -402,34 +463,41 @@ void testSpanResolverWithEmptyGroupByList() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); Long endTime = new Date(System.currentTimeMillis()).getTime(); Long startTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - LogicalPlan plan = project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + LogicalPlan plan = + project( + indexScanAgg( + "prometheus_http_total_requests", DSL.and( - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.and(DSL.gte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(startTime)), - ExprCoreType.TIMESTAMP))), - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP)))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of()), - finalProjectList, null); - RuntimeException runtimeException - = Assertions.assertThrows(RuntimeException.class, - () -> prometheusMetricTable.implement(plan)); - Assertions.assertEquals("Prometheus Catalog doesn't support " - + "aggregations without span expression", + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.and( + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), + DSL.and( + DSL.gte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(startTime)), + ExprCoreType.TIMESTAMP))), + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), + ExprCoreType.TIMESTAMP)))))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of()), + finalProjectList, + null); + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusMetricTable.implement(plan)); + Assertions.assertEquals( + "Prometheus Catalog doesn't support " + "aggregations without span expression", runtimeException.getMessage()); } @@ -439,44 +507,58 @@ void testSpanResolverWithSpanExpression() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); Long endTime = new Date(System.currentTimeMillis()).getTime(); Long startTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - PhysicalPlan plan = prometheusMetricTable.implement( - project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + PhysicalPlan plan = + prometheusMetricTable.implement( + project( + indexScanAgg( + "prometheus_http_total_requests", DSL.and( - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.and(DSL.gte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(startTime)), - ExprCoreType.TIMESTAMP))), - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP)))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("job", DSL.ref("job", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s")))), - finalProjectList, null)); + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.and( + DSL.equal( + DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), + DSL.and( + DSL.gte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(startTime)), + ExprCoreType.TIMESTAMP))), + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), + ExprCoreType.TIMESTAMP)))))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named("job", DSL.ref("job", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s")))), + finalProjectList, + null)); assertTrue(plan instanceof ProjectOperator); assertTrue(((ProjectOperator) plan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); assertEquals("40s", request.getStep()); - assertEquals("avg by(job) (avg_over_time" + assertEquals( + "avg by(job) (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", request.getPromQl()); List projectList = ((ProjectOperator) plan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } @@ -486,35 +568,45 @@ void testExpressionWithMissingTimeUnitInSpanExpression() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); Long endTime = new Date(System.currentTimeMillis()).getTime(); Long startTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - LogicalPlan logicalPlan = project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + LogicalPlan logicalPlan = + project( + indexScanAgg( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), DSL.and( DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.and(DSL.gte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.and( + DSL.gte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), DSL.literal( - fromObjectValue(dateFormat.format(new Date(startTime)), + fromObjectValue( + dateFormat.format(new Date(startTime)), ExprCoreType.TIMESTAMP))), - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), + fromObjectValue( + dateFormat.format(new Date(endTime)), ExprCoreType.TIMESTAMP)))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("job", DSL.ref("job", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "")))), - finalProjectList, null); + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named("job", DSL.ref("job", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), DSL.literal(40), "")))), + finalProjectList, + null); RuntimeException exception = - Assertions.assertThrows(RuntimeException.class, - () -> prometheusMetricTable.implement(logicalPlan)); + Assertions.assertThrows( + RuntimeException.class, () -> prometheusMetricTable.implement(logicalPlan)); assertEquals("Missing TimeUnit in the span expression", exception.getMessage()); } @@ -524,44 +616,57 @@ void testPrometheusQueryWithOnlySpanExpressionInGroupByList() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); Long endTime = new Date(System.currentTimeMillis()).getTime(); Long startTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - PhysicalPlan plan = prometheusMetricTable.implement( - project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + PhysicalPlan plan = + prometheusMetricTable.implement( + project( + indexScanAgg( + "prometheus_http_total_requests", DSL.and( - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.and(DSL.gte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(startTime)), - ExprCoreType.TIMESTAMP))), - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP)))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of( - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s")))), - finalProjectList, null)); + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.and( + DSL.equal( + DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), + DSL.and( + DSL.gte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(startTime)), + ExprCoreType.TIMESTAMP))), + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), + ExprCoreType.TIMESTAMP)))))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s")))), + finalProjectList, + null)); assertTrue(plan instanceof ProjectOperator); assertTrue(((ProjectOperator) plan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); assertEquals("40s", request.getStep()); - assertEquals("avg (avg_over_time" + assertEquals( + "avg (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", request.getPromQl()); List projectList = ((ProjectOperator) plan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } @@ -571,44 +676,57 @@ void testStatsWithNoGroupByList() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); Long endTime = new Date(System.currentTimeMillis()).getTime(); Long startTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - PhysicalPlan plan = prometheusMetricTable.implement( - project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + PhysicalPlan plan = + prometheusMetricTable.implement( + project( + indexScanAgg( + "prometheus_http_total_requests", DSL.and( - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.and(DSL.gte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(startTime)), - ExprCoreType.TIMESTAMP))), - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP)))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("span", - DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s")))), - finalProjectList, null)); + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.and( + DSL.equal( + DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), + DSL.and( + DSL.gte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(startTime)), + ExprCoreType.TIMESTAMP))), + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), + ExprCoreType.TIMESTAMP)))))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s")))), + finalProjectList, + null)); assertTrue(plan instanceof ProjectOperator); assertTrue(((ProjectOperator) plan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); assertEquals("40s", request.getStep()); - assertEquals("avg (avg_over_time" + assertEquals( + "avg (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", request.getPromQl()); List projectList = ((ProjectOperator) plan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } @@ -617,9 +735,11 @@ void testImplementWithUnexpectedLogicalNode() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); LogicalPlan plan = project(testLogicalPlanNode()); - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> prometheusMetricTable.implement(plan)); - assertEquals("unexpected plan node type class" + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusMetricTable.implement(plan)); + assertEquals( + "unexpected plan node type class" + " org.opensearch.sql.prometheus.utils.LogicalPlanUtils$TestLogicalPlan", runtimeException.getMessage()); } @@ -629,37 +749,44 @@ void testMultipleAggregationsThrowsRuntimeException() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - LogicalPlan plan = project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER))), - named("SUM(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("job", DSL.ref("job", STRING))))); - - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> prometheusMetricTable.implement(plan)); - assertEquals("Prometheus Catalog doesn't multiple aggregations in stats command", + LogicalPlan plan = + project( + indexScanAgg( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), + ImmutableList.of( + named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER))), + named("SUM(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of(named("job", DSL.ref("job", STRING))))); + + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusMetricTable.implement(plan)); + assertEquals( + "Prometheus Catalog doesn't multiple aggregations in stats command", runtimeException.getMessage()); } - @Test void testUnSupportedAggregation() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - LogicalPlan plan = project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), - ImmutableList - .of(named("VAR_SAMP(@value)", - DSL.varSamp(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("job", DSL.ref("job", STRING))))); - - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> prometheusMetricTable.implement(plan)); + LogicalPlan plan = + project( + indexScanAgg( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), + ImmutableList.of( + named("VAR_SAMP(@value)", DSL.varSamp(DSL.ref("@value", INTEGER)))), + ImmutableList.of(named("job", DSL.ref("job", STRING))))); + + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusMetricTable.implement(plan)); assertTrue(runtimeException.getMessage().contains("Prometheus Catalog only supports")); } @@ -667,13 +794,16 @@ void testUnSupportedAggregation() { void testImplementWithORConditionInWhereClause() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - LogicalPlan plan = indexScan("prometheus_http_total_requests", - DSL.or(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))))); - RuntimeException exception - = assertThrows(RuntimeException.class, () -> prometheusMetricTable.implement(plan)); - assertEquals("Prometheus Datasource doesn't support or in where command.", - exception.getMessage()); + LogicalPlan plan = + indexScan( + "prometheus_http_total_requests", + DSL.or( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))))); + RuntimeException exception = + assertThrows(RuntimeException.class, () -> prometheusMetricTable.implement(plan)); + assertEquals( + "Prometheus Datasource doesn't support or in where command.", exception.getMessage()); } @Test @@ -683,21 +813,26 @@ void testImplementWithRelationAndFilter() { finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - LogicalPlan logicalPlan = project(indexScan("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))))), - finalProjectList, null); + LogicalPlan logicalPlan = + project( + indexScan( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))))), + finalProjectList, + null); PhysicalPlan physicalPlan = prometheusMetricTable.implement(logicalPlan); assertTrue(physicalPlan instanceof ProjectOperator); assertTrue(((ProjectOperator) physicalPlan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest(); assertEquals((3600 / 250) + "s", request.getStep()); - assertEquals("prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}", - request.getPromQl()); + assertEquals( + "prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}", request.getPromQl()); List projectList = ((ProjectOperator) physicalPlan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } @@ -710,27 +845,30 @@ void testImplementWithRelationAndTimestampFilter() { Long endTime = new Date(System.currentTimeMillis()).getTime(); PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - LogicalPlan logicalPlan = project(indexScan("prometheus_http_total_requests", - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP))) - ), finalProjectList, null); + LogicalPlan logicalPlan = + project( + indexScan( + "prometheus_http_total_requests", + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), ExprCoreType.TIMESTAMP)))), + finalProjectList, + null); PhysicalPlan physicalPlan = prometheusMetricTable.implement(logicalPlan); assertTrue(physicalPlan instanceof ProjectOperator); assertTrue(((ProjectOperator) physicalPlan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest(); assertEquals((3600 / 250) + "s", request.getStep()); - assertEquals("prometheus_http_total_requests", - request.getPromQl()); + assertEquals("prometheus_http_total_requests", request.getPromQl()); List projectList = ((ProjectOperator) physicalPlan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } - @Test void testImplementWithRelationAndTimestampLTFilter() { List finalProjectList = new ArrayList<>(); @@ -740,27 +878,30 @@ void testImplementWithRelationAndTimestampLTFilter() { Long endTime = new Date(System.currentTimeMillis()).getTime(); PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - LogicalPlan logicalPlan = project(indexScan("prometheus_http_total_requests", - DSL.less(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP))) - ), finalProjectList, null); + LogicalPlan logicalPlan = + project( + indexScan( + "prometheus_http_total_requests", + DSL.less( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), ExprCoreType.TIMESTAMP)))), + finalProjectList, + null); PhysicalPlan physicalPlan = prometheusMetricTable.implement(logicalPlan); assertTrue(physicalPlan instanceof ProjectOperator); assertTrue(((ProjectOperator) physicalPlan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest(); assertEquals((3600 / 250) + "s", request.getStep()); - assertEquals("prometheus_http_total_requests", - request.getPromQl()); + assertEquals("prometheus_http_total_requests", request.getPromQl()); List projectList = ((ProjectOperator) physicalPlan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } - @Test void testImplementWithRelationAndTimestampGTFilter() { List finalProjectList = new ArrayList<>(); @@ -770,23 +911,27 @@ void testImplementWithRelationAndTimestampGTFilter() { Long endTime = new Date(System.currentTimeMillis()).getTime(); PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - LogicalPlan logicalPlan = project(indexScan("prometheus_http_total_requests", - DSL.greater(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP))) - ), finalProjectList, null); + LogicalPlan logicalPlan = + project( + indexScan( + "prometheus_http_total_requests", + DSL.greater( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), ExprCoreType.TIMESTAMP)))), + finalProjectList, + null); PhysicalPlan physicalPlan = prometheusMetricTable.implement(logicalPlan); assertTrue(physicalPlan instanceof ProjectOperator); assertTrue(((ProjectOperator) physicalPlan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest(); assertEquals((3600 / 250) + "s", request.getStep()); - assertEquals("prometheus_http_total_requests", - request.getPromQl()); + assertEquals("prometheus_http_total_requests", request.getPromQl()); List projectList = ((ProjectOperator) physicalPlan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } @@ -796,10 +941,9 @@ void testOptimize() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, prometheusQueryRequest); List finalProjectList = new ArrayList<>(); - LogicalPlan inputPlan = project(relation("query_range", prometheusMetricTable), - finalProjectList, null); - LogicalPlan optimizedPlan = prometheusMetricTable.optimize( - inputPlan); + LogicalPlan inputPlan = + project(relation("query_range", prometheusMetricTable), finalProjectList, null); + LogicalPlan optimizedPlan = prometheusMetricTable.optimize(inputPlan); assertEquals(inputPlan, optimizedPlan); } @@ -810,7 +954,8 @@ void testUnsupportedOperation() { new PrometheusMetricTable(client, prometheusQueryRequest); assertThrows(UnsupportedOperationException.class, prometheusMetricTable::exists); - assertThrows(UnsupportedOperationException.class, + assertThrows( + UnsupportedOperationException.class, () -> prometheusMetricTable.create(Collections.emptyMap())); } @@ -820,25 +965,29 @@ void testImplementPrometheusQueryWithBackQuotedFieldNamesInStatsQuery() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - // IndexScanAgg with Filter - PhysicalPlan plan = prometheusMetricTable.implement( - indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("`job`", DSL.ref("job", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s"))))); + PhysicalPlan plan = + prometheusMetricTable.implement( + indexScanAgg( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named("`job`", DSL.ref("job", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s"))))); assertTrue(plan instanceof PrometheusMetricScan); PrometheusQueryRequest prometheusQueryRequest = ((PrometheusMetricScan) plan).getRequest(); assertEquals( "avg by(job) (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", prometheusQueryRequest.getPromQl()); - } @Test @@ -848,14 +997,16 @@ void testImplementPrometheusQueryWithFilterQuery() { new PrometheusMetricTable(client, "prometheus_http_total_requests"); // IndexScanAgg without Filter - PhysicalPlan plan = prometheusMetricTable.implement( - indexScan("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))))); + PhysicalPlan plan = + prometheusMetricTable.implement( + indexScan( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))))); assertTrue(plan instanceof PrometheusMetricScan); - PrometheusQueryRequest prometheusQueryRequest = - ((PrometheusMetricScan) plan).getRequest(); + PrometheusQueryRequest prometheusQueryRequest = ((PrometheusMetricScan) plan).getRequest(); assertEquals( "prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}", prometheusQueryRequest.getPromQl()); @@ -867,15 +1018,22 @@ void testImplementPrometheusQueryWithUnsupportedFilterQuery() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - RuntimeException exception = assertThrows(RuntimeException.class, - () -> prometheusMetricTable.implement(indexScan("prometheus_http_total_requests", - DSL.and(DSL.lte(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))))))); - assertEquals("Prometheus Datasource doesn't support <= in where command.", - exception.getMessage()); + RuntimeException exception = + assertThrows( + RuntimeException.class, + () -> + prometheusMetricTable.implement( + indexScan( + "prometheus_http_total_requests", + DSL.and( + DSL.lte(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal( + DSL.ref("handler", STRING), + DSL.literal(stringValue("/ready/"))))))); + assertEquals( + "Prometheus Datasource doesn't support <= in where command.", exception.getMessage()); } - @Test void testCreateScanBuilderWithQueryRangeTableFunction() { PrometheusQueryRequest prometheusQueryRequest = new PrometheusQueryRequest(); @@ -895,5 +1053,4 @@ void testCreateScanBuilderWithPPLQuery() { TableScanBuilder tableScanBuilder = prometheusMetricTable.createScanBuilder(); Assertions.assertNull(tableScanBuilder); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageEngineTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageEngineTest.java index 4e8d470373..b925fe6538 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageEngineTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageEngineTest.java @@ -29,8 +29,7 @@ @ExtendWith(MockitoExtension.class) class PrometheusStorageEngineTest { - @Mock - private PrometheusClient client; + @Mock private PrometheusClient client; @Test public void getTable() { @@ -43,15 +42,12 @@ public void getTable() { @Test public void getFunctions() { PrometheusStorageEngine engine = new PrometheusStorageEngine(client); - Collection functionResolverCollection - = engine.getFunctions(); + Collection functionResolverCollection = engine.getFunctions(); assertNotNull(functionResolverCollection); assertEquals(2, functionResolverCollection.size()); Iterator iterator = functionResolverCollection.iterator(); - assertTrue( - iterator.next() instanceof QueryRangeTableFunctionResolver); - assertTrue( - iterator.next() instanceof QueryExemplarsTableFunctionResolver); + assertTrue(iterator.next() instanceof QueryRangeTableFunctionResolver); + assertTrue(iterator.next() instanceof QueryExemplarsTableFunctionResolver); } @Test @@ -65,8 +61,8 @@ public void getSystemTable() { @Test public void getSystemTableForAllTablesInfo() { PrometheusStorageEngine engine = new PrometheusStorageEngine(client); - Table table - = engine.getTable(new DataSourceSchemaName("prometheus", "information_schema"), "tables"); + Table table = + engine.getTable(new DataSourceSchemaName("prometheus", "information_schema"), "tables"); assertNotNull(table); assertTrue(table instanceof PrometheusSystemTable); } @@ -74,10 +70,12 @@ public void getSystemTableForAllTablesInfo() { @Test public void getSystemTableWithWrongInformationSchemaTable() { PrometheusStorageEngine engine = new PrometheusStorageEngine(client); - SemanticCheckException exception = assertThrows(SemanticCheckException.class, - () -> engine.getTable(new DataSourceSchemaName("prometheus", "information_schema"), - "test")); + SemanticCheckException exception = + assertThrows( + SemanticCheckException.class, + () -> + engine.getTable( + new DataSourceSchemaName("prometheus", "information_schema"), "test")); assertEquals("Information Schema doesn't contain test table", exception.getMessage()); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactoryTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactoryTest.java index c566ccdeb4..c2e8e5325a 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactoryTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactoryTest.java @@ -26,8 +26,7 @@ @ExtendWith(MockitoExtension.class) public class PrometheusStorageFactoryTest { - @Mock - private Settings settings; + @Mock private Settings settings; @Test void testGetConnectorType() { @@ -46,8 +45,7 @@ void testGetStorageEngineWithBasicAuth() { properties.put("prometheus.auth.type", "basicauth"); properties.put("prometheus.auth.username", "admin"); properties.put("prometheus.auth.password", "admin"); - StorageEngine storageEngine - = prometheusStorageFactory.getStorageEngine(properties); + StorageEngine storageEngine = prometheusStorageFactory.getStorageEngine(properties); Assertions.assertTrue(storageEngine instanceof PrometheusStorageEngine); } @@ -62,12 +60,10 @@ void testGetStorageEngineWithAWSSigV4Auth() { properties.put("prometheus.auth.region", "us-east-1"); properties.put("prometheus.auth.secret_key", "accessKey"); properties.put("prometheus.auth.access_key", "secretKey"); - StorageEngine storageEngine - = prometheusStorageFactory.getStorageEngine(properties); + StorageEngine storageEngine = prometheusStorageFactory.getStorageEngine(properties); Assertions.assertTrue(storageEngine instanceof PrometheusStorageEngine); } - @Test @SneakyThrows void testGetStorageEngineWithMissingURI() { @@ -77,10 +73,12 @@ void testGetStorageEngineWithMissingURI() { properties.put("prometheus.auth.region", "us-east-1"); properties.put("prometheus.auth.secret_key", "accessKey"); properties.put("prometheus.auth.access_key", "secretKey"); - IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, - () -> prometheusStorageFactory.getStorageEngine(properties)); - Assertions.assertEquals("Missing [prometheus.uri] fields " - + "in the Prometheus connector properties.", + IllegalArgumentException exception = + Assertions.assertThrows( + IllegalArgumentException.class, + () -> prometheusStorageFactory.getStorageEngine(properties)); + Assertions.assertEquals( + "Missing [prometheus.uri] fields " + "in the Prometheus connector properties.", exception.getMessage()); } @@ -93,14 +91,15 @@ void testGetStorageEngineWithMissingRegionInAWS() { properties.put("prometheus.auth.type", "awssigv4"); properties.put("prometheus.auth.secret_key", "accessKey"); properties.put("prometheus.auth.access_key", "secretKey"); - IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, - () -> prometheusStorageFactory.getStorageEngine(properties)); - Assertions.assertEquals("Missing [prometheus.auth.region] fields in the " - + "Prometheus connector properties.", + IllegalArgumentException exception = + Assertions.assertThrows( + IllegalArgumentException.class, + () -> prometheusStorageFactory.getStorageEngine(properties)); + Assertions.assertEquals( + "Missing [prometheus.auth.region] fields in the " + "Prometheus connector properties.", exception.getMessage()); } - @Test @SneakyThrows void testGetStorageEngineWithLongConfigProperties() { @@ -110,9 +109,12 @@ void testGetStorageEngineWithLongConfigProperties() { properties.put("prometheus.auth.type", "awssigv4"); properties.put("prometheus.auth.secret_key", "accessKey"); properties.put("prometheus.auth.access_key", "secretKey"); - IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, - () -> prometheusStorageFactory.getStorageEngine(properties)); - Assertions.assertEquals("Missing [prometheus.auth.region] fields in the " + IllegalArgumentException exception = + Assertions.assertThrows( + IllegalArgumentException.class, + () -> prometheusStorageFactory.getStorageEngine(properties)); + Assertions.assertEquals( + "Missing [prometheus.auth.region] fields in the " + "Prometheus connector properties." + "Fields [prometheus.uri] exceeds more than 1000 characters.", exception.getMessage()); @@ -129,13 +131,14 @@ void testGetStorageEngineWithWrongAuthType() { properties.put("prometheus.auth.region", "us-east-1"); properties.put("prometheus.auth.secret_key", "accessKey"); properties.put("prometheus.auth.access_key", "secretKey"); - IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, - () -> prometheusStorageFactory.getStorageEngine(properties)); - Assertions.assertEquals("AUTH Type : random is not supported with Prometheus Connector", - exception.getMessage()); + IllegalArgumentException exception = + Assertions.assertThrows( + IllegalArgumentException.class, + () -> prometheusStorageFactory.getStorageEngine(properties)); + Assertions.assertEquals( + "AUTH Type : random is not supported with Prometheus Connector", exception.getMessage()); } - @Test @SneakyThrows void testGetStorageEngineWithNONEAuthType() { @@ -143,8 +146,7 @@ void testGetStorageEngineWithNONEAuthType() { PrometheusStorageFactory prometheusStorageFactory = new PrometheusStorageFactory(settings); HashMap properties = new HashMap<>(); properties.put("prometheus.uri", "https://test.com"); - StorageEngine storageEngine - = prometheusStorageFactory.getStorageEngine(properties); + StorageEngine storageEngine = prometheusStorageFactory.getStorageEngine(properties); Assertions.assertTrue(storageEngine instanceof PrometheusStorageEngine); } @@ -157,8 +159,9 @@ void testGetStorageEngineWithInvalidURISyntax() { properties.put("prometheus.auth.type", "basicauth"); properties.put("prometheus.auth.username", "admin"); properties.put("prometheus.auth.password", "admin"); - RuntimeException exception = Assertions.assertThrows(RuntimeException.class, - () -> prometheusStorageFactory.getStorageEngine(properties)); + RuntimeException exception = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusStorageFactory.getStorageEngine(properties)); Assertions.assertTrue( exception.getMessage().contains("Invalid URI in prometheus properties: ")); } @@ -213,10 +216,13 @@ void createDataSourceWithInvalidHostname() { metadata.setProperties(properties); PrometheusStorageFactory prometheusStorageFactory = new PrometheusStorageFactory(settings); - RuntimeException exception = Assertions.assertThrows(RuntimeException.class, - () -> prometheusStorageFactory.createDataSource(metadata)); + RuntimeException exception = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusStorageFactory.createDataSource(metadata)); Assertions.assertTrue( - exception.getMessage().contains("Invalid hostname in the uri: http://dummyprometheus:9090")); + exception + .getMessage() + .contains("Invalid hostname in the uri: http://dummyprometheus:9090")); } @Test @@ -233,8 +239,9 @@ void createDataSourceWithInvalidIp() { metadata.setProperties(properties); PrometheusStorageFactory prometheusStorageFactory = new PrometheusStorageFactory(settings); - RuntimeException exception = Assertions.assertThrows(RuntimeException.class, - () -> prometheusStorageFactory.createDataSource(metadata)); + RuntimeException exception = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusStorageFactory.createDataSource(metadata)); Assertions.assertTrue( exception.getMessage().contains("Invalid hostname in the uri: http://231.54.11.987:9090")); } @@ -255,11 +262,15 @@ void createDataSourceWithHostnameNotMatchingWithAllowHostsConfig() { metadata.setProperties(properties); PrometheusStorageFactory prometheusStorageFactory = new PrometheusStorageFactory(settings); - RuntimeException exception = Assertions.assertThrows(RuntimeException.class, - () -> prometheusStorageFactory.createDataSource(metadata)); + RuntimeException exception = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusStorageFactory.createDataSource(metadata)); Assertions.assertTrue( - exception.getMessage().contains("Disallowed hostname in the uri: http://localhost.com:9090. " - + "Validate with plugins.query.datasources.uri.allowhosts config")); + exception + .getMessage() + .contains( + "Disallowed hostname in the uri: http://localhost.com:9090. " + + "Validate with plugins.query.datasources.uri.allowhosts config")); } @Test @@ -279,5 +290,4 @@ void createDataSourceSuccessWithHostnameRestrictions() { DataSource dataSource = new PrometheusStorageFactory(settings).createDataSource(metadata); Assertions.assertTrue(dataSource.getStorageEngine() instanceof PrometheusStorageEngine); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/QueryExemplarsTableTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/QueryExemplarsTableTest.java index 19876d398d..7f49de981a 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/QueryExemplarsTableTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/QueryExemplarsTableTest.java @@ -30,14 +30,12 @@ @ExtendWith(MockitoExtension.class) class QueryExemplarsTableTest { - @Mock - private PrometheusClient client; + @Mock private PrometheusClient client; @Test @SneakyThrows void testGetFieldTypes() { - PrometheusQueryExemplarsRequest exemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest exemplarsRequest = new PrometheusQueryExemplarsRequest(); exemplarsRequest.setQuery(QUERY); exemplarsRequest.setStartTime(STARTTIME); exemplarsRequest.setEndTime(ENDTIME); @@ -50,8 +48,7 @@ void testGetFieldTypes() { @Test void testImplementWithBasicMetricQuery() { - PrometheusQueryExemplarsRequest exemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest exemplarsRequest = new PrometheusQueryExemplarsRequest(); exemplarsRequest.setQuery(QUERY); exemplarsRequest.setStartTime(STARTTIME); exemplarsRequest.setEndTime(ENDTIME); @@ -67,8 +64,7 @@ void testImplementWithBasicMetricQuery() { @Test void testCreateScanBuilderWithQueryRangeTableFunction() { - PrometheusQueryExemplarsRequest exemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest exemplarsRequest = new PrometheusQueryExemplarsRequest(); exemplarsRequest.setQuery(QUERY); exemplarsRequest.setStartTime(STARTTIME); exemplarsRequest.setEndTime(ENDTIME); @@ -77,5 +73,4 @@ void testCreateScanBuilderWithQueryRangeTableFunction() { Assertions.assertNotNull(tableScanBuilder); Assertions.assertTrue(tableScanBuilder instanceof QueryExemplarsFunctionTableScanBuilder); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/querybuilders/StepParameterResolverTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/querybuilders/StepParameterResolverTest.java index 37e24a56b5..397b7146f7 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/querybuilders/StepParameterResolverTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/querybuilders/StepParameterResolverTest.java @@ -18,9 +18,11 @@ public class StepParameterResolverTest { @Test void testNullChecks() { StepParameterResolver stepParameterResolver = new StepParameterResolver(); - Assertions.assertThrows(NullPointerException.class, + Assertions.assertThrows( + NullPointerException.class, () -> stepParameterResolver.resolve(null, new Date().getTime(), Collections.emptyList())); - Assertions.assertThrows(NullPointerException.class, + Assertions.assertThrows( + NullPointerException.class, () -> stepParameterResolver.resolve(new Date().getTime(), null, Collections.emptyList())); } } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/querybuilders/TimeRangeParametersResolverTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/querybuilders/TimeRangeParametersResolverTest.java index 73839e2152..6a280b7d98 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/querybuilders/TimeRangeParametersResolverTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/querybuilders/TimeRangeParametersResolverTest.java @@ -21,9 +21,11 @@ public class TimeRangeParametersResolverTest { @Test void testTimeRangeParametersWithoutTimestampFilter() { TimeRangeParametersResolver timeRangeParametersResolver = new TimeRangeParametersResolver(); - Pair result = timeRangeParametersResolver.resolve( - DSL.and(DSL.less(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))))); + Pair result = + timeRangeParametersResolver.resolve( + DSL.and( + DSL.less(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))))); Assertions.assertNotNull(result); Assertions.assertEquals(3600, result.getSecond() - result.getFirst()); } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableScanTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableScanTest.java index 0d7ec4e2cc..ea299b87de 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableScanTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableScanTest.java @@ -22,8 +22,7 @@ @ExtendWith(MockitoExtension.class) public class PrometheusSystemTableScanTest { - @Mock - private PrometheusSystemRequest request; + @Mock private PrometheusSystemRequest request; @Test public void queryData() { diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableTest.java index 0721f82c07..7022ca9657 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableTest.java @@ -35,51 +35,41 @@ @ExtendWith(MockitoExtension.class) public class PrometheusSystemTableTest { - @Mock - private PrometheusClient client; + @Mock private PrometheusClient client; - @Mock - private Table table; + @Mock private Table table; @Test void testGetFieldTypesOfMetaTable() { - PrometheusSystemTable systemIndex = new PrometheusSystemTable(client, - new DataSourceSchemaName("prometheus", "information_schema"), TABLE_INFO); + PrometheusSystemTable systemIndex = + new PrometheusSystemTable( + client, new DataSourceSchemaName("prometheus", "information_schema"), TABLE_INFO); final Map fieldTypes = systemIndex.getFieldTypes(); - assertThat(fieldTypes, anyOf( - hasEntry("TABLE_CATALOG", STRING) - )); - assertThat(fieldTypes, anyOf( - hasEntry("UNIT", STRING) - )); + assertThat(fieldTypes, anyOf(hasEntry("TABLE_CATALOG", STRING))); + assertThat(fieldTypes, anyOf(hasEntry("UNIT", STRING))); } @Test void testGetFieldTypesOfMappingTable() { - PrometheusSystemTable systemIndex = new PrometheusSystemTable(client, - new DataSourceSchemaName("prometheus", "information_schema"), mappingTable( - "test_metric")); + PrometheusSystemTable systemIndex = + new PrometheusSystemTable( + client, + new DataSourceSchemaName("prometheus", "information_schema"), + mappingTable("test_metric")); final Map fieldTypes = systemIndex.getFieldTypes(); - assertThat(fieldTypes, anyOf( - hasEntry("COLUMN_NAME", STRING) - )); + assertThat(fieldTypes, anyOf(hasEntry("COLUMN_NAME", STRING))); } - - @Test void implement() { - PrometheusSystemTable systemIndex = new PrometheusSystemTable(client, - new DataSourceSchemaName("prometheus", "information_schema"), TABLE_INFO); + PrometheusSystemTable systemIndex = + new PrometheusSystemTable( + client, new DataSourceSchemaName("prometheus", "information_schema"), TABLE_INFO); NamedExpression projectExpr = named("TABLE_NAME", ref("TABLE_NAME", STRING)); - final PhysicalPlan plan = systemIndex.implement( - project( - relation(TABLE_INFO, table), - projectExpr - )); + final PhysicalPlan plan = + systemIndex.implement(project(relation(TABLE_INFO, table), projectExpr)); assertTrue(plan instanceof ProjectOperator); assertTrue(plan.getChild().get(0) instanceof PrometheusSystemTableScan); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/utils/LogicalPlanUtils.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/utils/LogicalPlanUtils.java index 5fcebf52e6..570a987889 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/utils/LogicalPlanUtils.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/utils/LogicalPlanUtils.java @@ -19,43 +19,36 @@ public class LogicalPlanUtils { - /** - * Build PrometheusLogicalMetricScan. - */ + /** Build PrometheusLogicalMetricScan. */ public static LogicalPlan indexScan(String metricName, Expression filter) { - return PrometheusLogicalMetricScan.builder().metricName(metricName) - .filter(filter) - .build(); + return PrometheusLogicalMetricScan.builder().metricName(metricName).filter(filter).build(); } - /** - * Build PrometheusLogicalMetricAgg. - */ - public static LogicalPlan indexScanAgg(String metricName, Expression filter, - List aggregators, - List groupByList) { - return PrometheusLogicalMetricAgg.builder().metricName(metricName) + /** Build PrometheusLogicalMetricAgg. */ + public static LogicalPlan indexScanAgg( + String metricName, + Expression filter, + List aggregators, + List groupByList) { + return PrometheusLogicalMetricAgg.builder() + .metricName(metricName) .filter(filter) .aggregatorList(aggregators) .groupByList(groupByList) .build(); } - /** - * Build PrometheusLogicalMetricAgg. - */ - public static LogicalPlan indexScanAgg(String metricName, - List aggregators, - List groupByList) { - return PrometheusLogicalMetricAgg.builder().metricName(metricName) + /** Build PrometheusLogicalMetricAgg. */ + public static LogicalPlan indexScanAgg( + String metricName, List aggregators, List groupByList) { + return PrometheusLogicalMetricAgg.builder() + .metricName(metricName) .aggregatorList(aggregators) .groupByList(groupByList) .build(); } - /** - * Build PrometheusLogicalMetricAgg. - */ + /** Build PrometheusLogicalMetricAgg. */ public static LogicalPlan testLogicalPlanNode() { return new TestLogicalPlan(); } @@ -71,7 +64,4 @@ public R accept(LogicalPlanNodeVisitor visitor, C context) { return visitor.visitNode(this, null); } } - - - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/utils/TestUtils.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/utils/TestUtils.java index 1683858c49..a9fcc26101 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/utils/TestUtils.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/utils/TestUtils.java @@ -12,6 +12,7 @@ public class TestUtils { /** * Get Json document from the files in resources folder. + * * @param filename filename. * @return String. * @throws IOException IOException. @@ -21,5 +22,4 @@ public static String getJson(String filename) throws IOException { return new String( Objects.requireNonNull(classLoader.getResourceAsStream(filename)).readAllBytes()); } - }