-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vamsi Manohar <reddyvam@amazon.com> (cherry picked from commit 430d7a9)
- Loading branch information
Showing
35 changed files
with
1,504 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
integ-test/src/test/resources/expectedOutput/ppl/explain_query_exemplars.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"root": { | ||
"name": "QueryExemplarsFunctionTableScanOperator", | ||
"description": { | ||
"request": "query_exemplars(app_ads_ad_requests_total, 1689228292, 1689232299)" | ||
}, | ||
"children": [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...ensearch/sql/prometheus/functions/implementation/QueryExemplarFunctionImplementation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* | ||
* * Copyright OpenSearch Contributors | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.opensearch.sql.prometheus.functions.implementation; | ||
|
||
import static org.opensearch.sql.prometheus.functions.resolver.QueryRangeTableFunctionResolver.ENDTIME; | ||
import static org.opensearch.sql.prometheus.functions.resolver.QueryRangeTableFunctionResolver.QUERY; | ||
import static org.opensearch.sql.prometheus.functions.resolver.QueryRangeTableFunctionResolver.STARTTIME; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.data.type.ExprCoreType; | ||
import org.opensearch.sql.data.type.ExprType; | ||
import org.opensearch.sql.exception.ExpressionEvaluationException; | ||
import org.opensearch.sql.expression.Expression; | ||
import org.opensearch.sql.expression.FunctionExpression; | ||
import org.opensearch.sql.expression.NamedArgumentExpression; | ||
import org.opensearch.sql.expression.env.Environment; | ||
import org.opensearch.sql.expression.function.FunctionName; | ||
import org.opensearch.sql.expression.function.TableFunctionImplementation; | ||
import org.opensearch.sql.prometheus.client.PrometheusClient; | ||
import org.opensearch.sql.prometheus.request.PrometheusQueryExemplarsRequest; | ||
import org.opensearch.sql.prometheus.storage.QueryExemplarsTable; | ||
import org.opensearch.sql.storage.Table; | ||
|
||
public class QueryExemplarFunctionImplementation extends FunctionExpression implements | ||
TableFunctionImplementation { | ||
|
||
private final FunctionName functionName; | ||
private final List<Expression> arguments; | ||
private final PrometheusClient prometheusClient; | ||
|
||
/** | ||
* Required argument constructor. | ||
* | ||
* @param functionName name of the function | ||
* @param arguments a list of arguments provided | ||
*/ | ||
public QueryExemplarFunctionImplementation(FunctionName functionName, List<Expression> arguments, | ||
PrometheusClient prometheusClient) { | ||
super(functionName, arguments); | ||
this.functionName = functionName; | ||
this.arguments = arguments; | ||
this.prometheusClient = prometheusClient; | ||
} | ||
|
||
@Override | ||
public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) { | ||
throw new UnsupportedOperationException(String.format( | ||
"Prometheus defined function [%s] is only " | ||
+ "supported in SOURCE clause with prometheus connector catalog", | ||
functionName)); | ||
} | ||
|
||
@Override | ||
public ExprType type() { | ||
return ExprCoreType.STRUCT; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
List<String> 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)); | ||
} | ||
|
||
@Override | ||
public Table applyArguments() { | ||
return new QueryExemplarsTable(prometheusClient, buildExemplarsQueryRequest(arguments)); | ||
} | ||
|
||
private PrometheusQueryExemplarsRequest buildExemplarsQueryRequest(List<Expression> 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)); | ||
} | ||
}); | ||
return request; | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
...org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.prometheus.functions.resolver; | ||
|
||
import static org.opensearch.sql.data.type.ExprCoreType.LONG; | ||
import static org.opensearch.sql.data.type.ExprCoreType.STRING; | ||
import static org.opensearch.sql.prometheus.utils.TableFunctionUtils.getNamedArgumentsOfTableFunction; | ||
import static org.opensearch.sql.prometheus.utils.TableFunctionUtils.validatePrometheusTableFunctionArguments; | ||
|
||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.opensearch.sql.expression.Expression; | ||
import org.opensearch.sql.expression.function.FunctionBuilder; | ||
import org.opensearch.sql.expression.function.FunctionName; | ||
import org.opensearch.sql.expression.function.FunctionResolver; | ||
import org.opensearch.sql.expression.function.FunctionSignature; | ||
import org.opensearch.sql.prometheus.client.PrometheusClient; | ||
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. | ||
*/ | ||
@RequiredArgsConstructor | ||
public class QueryExemplarsTableFunctionResolver implements FunctionResolver { | ||
|
||
private final PrometheusClient prometheusClient; | ||
public static final String QUERY_EXEMPLARS = "query_exemplars"; | ||
|
||
public static final String QUERY = "query"; | ||
public static final String STARTTIME = "starttime"; | ||
public static final String ENDTIME = "endtime"; | ||
|
||
@Override | ||
public Pair<FunctionSignature, FunctionBuilder> resolve(FunctionSignature unresolvedSignature) { | ||
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<String> argumentNames = List.of(QUERY, STARTTIME, ENDTIME); | ||
validatePrometheusTableFunctionArguments(arguments, argumentNames); | ||
List<Expression> namedArguments = getNamedArgumentsOfTableFunction(arguments, argumentNames); | ||
return new QueryExemplarFunctionImplementation(functionName, | ||
namedArguments, prometheusClient); | ||
}; | ||
return Pair.of(functionSignature, functionBuilder); | ||
} | ||
|
||
@Override | ||
public FunctionName getFunctionName() { | ||
return FunctionName.of(QUERY_EXEMPLARS); | ||
} | ||
} |
Oops, something went wrong.