Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(deps): upgrade promql-parser to 0.4 #4047

Merged
merged 4 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 79 additions & 118 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ parquet = { version = "51.0.0", default-features = false, features = ["arrow", "
paste = "1.0"
pin-project = "1.0"
prometheus = { version = "0.13.3", features = ["process"] }
promql-parser = { version = "0.4" }
prost = "0.12"
raft-engine = { version = "0.4.1", default-features = false }
rand = "0.8"
Expand Down
2 changes: 1 addition & 1 deletion src/promql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ greptime-proto.workspace = true
itertools.workspace = true
lazy_static.workspace = true
prometheus.workspace = true
promql-parser = "0.1.1"
promql-parser.workspace = true
prost.workspace = true
session.workspace = true
snafu.workspace = true
Expand Down
5 changes: 3 additions & 2 deletions src/promql/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use common_error::ext::ErrorExt;
use common_error::status_code::StatusCode;
use common_macro::stack_trace_debug;
use datafusion::error::DataFusionError;
use promql_parser::parser::{Expr as PromExpr, TokenType, VectorMatchCardinality};
use promql_parser::parser::token::TokenType;
use promql_parser::parser::{Expr as PromExpr, VectorMatchCardinality};
use snafu::{Location, Snafu};

#[derive(Snafu)]
Expand Down Expand Up @@ -49,7 +50,7 @@ pub enum Error {
#[snafu(display("Internal error during building DataFusion plan"))]
DataFusionPlanning {
#[snafu(source)]
error: datafusion::error::DataFusionError,
error: DataFusionError,
#[snafu(implicit)]
location: Location,
},
Expand Down
19 changes: 11 additions & 8 deletions src/promql/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ use datatypes::arrow::datatypes::{DataType as ArrowDataType, TimeUnit as ArrowTi
use datatypes::data_type::ConcreteDataType;
use itertools::Itertools;
use promql_parser::label::{MatchOp, Matcher, Matchers, METRIC_NAME};
use promql_parser::parser::token::TokenType;
use promql_parser::parser::{
token, AggregateExpr, BinModifier, BinaryExpr as PromBinaryExpr, Call, EvalStmt,
Expr as PromExpr, Function, FunctionArgs as PromFunctionArgs, LabelModifier, MatrixSelector,
NumberLiteral, Offset, ParenExpr, StringLiteral, SubqueryExpr, TokenType, UnaryExpr,
NumberLiteral, Offset, ParenExpr, StringLiteral, SubqueryExpr, UnaryExpr,
VectorMatchCardinality, VectorSelector,
};
use snafu::{ensure, OptionExt, ResultExt};
Expand Down Expand Up @@ -606,15 +607,14 @@ impl PromPlanner {
if let Some(name) = name.clone() {
metric_name = Some(name);
ensure!(
label_matchers.find_matcher(METRIC_NAME).is_none(),
label_matchers.find_matchers(METRIC_NAME).is_empty(),
MultipleMetricMatchersSnafu
);
} else {
metric_name = Some(
label_matchers
.find_matcher(METRIC_NAME)
.context(NoMetricMatcherSnafu)?,
);
let mut matches = label_matchers.find_matchers(METRIC_NAME);
ensure!(!matches.is_empty(), NoMetricMatcherSnafu);
ensure!(matches.len() == 1, MultipleMetricMatchersSnafu);
metric_name = matches.pop().map(|m| m.value);
}

self.ctx.table_name = metric_name;
Expand All @@ -641,7 +641,10 @@ impl PromPlanner {
}
}
let matchers = matchers.into_iter().collect();
Ok(Matchers { matchers })
Ok(Matchers {
tisonkun marked this conversation as resolved.
Show resolved Hide resolved
matchers,
or_matchers: vec![],
})
}

async fn selector_to_series_normalize_plan(
Expand Down
2 changes: 1 addition & 1 deletion src/query/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object-store.workspace = true
once_cell.workspace = true
prometheus.workspace = true
promql.workspace = true
promql-parser = "0.1.1"
promql-parser.workspace = true
regex.workspace = true
session.workspace = true
snafu.workspace = true
Expand Down
8 changes: 4 additions & 4 deletions src/query/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ use common_error::ext::{BoxedError, PlainError};
use common_error::status_code::StatusCode;
use common_telemetry::tracing;
use promql_parser::parser::ast::{Extension as NodeExtension, ExtensionExpr};
use promql_parser::parser::value::ValueType;
use promql_parser::parser::Expr::Extension;
use promql_parser::parser::{EvalStmt, Expr, ValueType};
use promql_parser::parser::{EvalStmt, Expr};
use session::context::QueryContextRef;
use snafu::{OptionExt, ResultExt};
use sql::dialect::GreptimeDbDialect;
Expand Down Expand Up @@ -372,8 +373,7 @@ mod test {
Promql(EvalStmt { \
expr: VectorSelector(VectorSelector { \
name: Some(\"http_request\"), \
matchers: Matchers { \
matchers: [] }, \
matchers: Matchers { matchers: [], or_matchers: [] }, \
offset: None, at: None }), \
start: SystemTime { tv_sec: 1644772440, tv_nsec: 0 }, \
end: SystemTime { tv_sec: 1676308440, tv_nsec: 0 }, \
Expand All @@ -389,7 +389,7 @@ mod test {
Promql(EvalStmt { \
expr: VectorSelector(VectorSelector { \
name: Some(\"http_request\"), \
matchers: Matchers { matchers: [] }, \
matchers: Matchers { matchers: [], or_matchers: [] }, \
offset: None, at: None }), \
start: SystemTime { intervals: 132892460400000000 }, \
end: SystemTime { intervals: 133207820400000000 }, \
Expand Down
2 changes: 1 addition & 1 deletion src/servers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pprof = { version = "0.13", features = [
"protobuf",
], optional = true }
prometheus.workspace = true
promql-parser = "0.1.1"
promql-parser.workspace = true
prost.workspace = true
query.workspace = true
rand.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion src/servers/src/grpc/prom_query_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use auth::UserProviderRef;
use common_error::ext::ErrorExt;
use common_error::status_code::StatusCode;
use common_time::util::current_time_rfc3339;
use promql_parser::parser::ValueType;
use promql_parser::parser::value::ValueType;
use query::parser::PromQuery;
use session::context::QueryContext;
use snafu::OptionExt;
Expand Down
25 changes: 20 additions & 5 deletions src/servers/src/http/prometheus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ use datatypes::scalars::ScalarVector;
use datatypes::vectors::{Float64Vector, StringVector};
use futures::StreamExt;
use promql_parser::label::METRIC_NAME;
use promql_parser::parser::value::ValueType;
use promql_parser::parser::{
AggregateExpr, BinaryExpr, Call, Expr as PromqlExpr, MatrixSelector, ParenExpr, SubqueryExpr,
UnaryExpr, ValueType, VectorSelector,
UnaryExpr, VectorSelector,
};
use query::parser::{PromQuery, DEFAULT_LOOKBACK_STRING};
use schemars::JsonSchema;
Expand Down Expand Up @@ -642,11 +643,19 @@ fn promql_expr_to_metric_name(expr: &PromqlExpr) -> Option<String> {
PromqlExpr::StringLiteral(_) => Some(String::new()),
PromqlExpr::Extension(_) => None,
PromqlExpr::VectorSelector(VectorSelector { name, matchers, .. }) => {
name.clone().or(matchers.find_matcher(METRIC_NAME))
name.clone().or(matchers
.find_matchers(METRIC_NAME)
.into_iter()
.next()
.map(|m| m.value))
}
PromqlExpr::MatrixSelector(MatrixSelector { vs, .. }) => {
let VectorSelector { name, matchers, .. } = vs;
name.clone().or(matchers.find_matcher(METRIC_NAME))
name.clone().or(matchers
.find_matchers(METRIC_NAME)
.into_iter()
.next()
.map(|m| m.value))
}
PromqlExpr::Call(Call { args, .. }) => {
args.args.iter().find_map(|e| promql_expr_to_metric_name(e))
Expand Down Expand Up @@ -880,12 +889,18 @@ fn retrieve_metric_name_from_promql(query: &str) -> Option<String> {
let query_metric_name = match plan {
PromqlExpr::VectorSelector(vs) => vs
.matchers
.find_matcher(promql_parser::label::METRIC_NAME)
.find_matchers(METRIC_NAME)
.into_iter()
.next()
.map(|m| m.value)
.or_else(|| vs.name.clone()),
PromqlExpr::MatrixSelector(ms) => ms
.vs
.matchers
.find_matcher(promql_parser::label::METRIC_NAME)
.find_matchers(METRIC_NAME)
.into_iter()
.next()
.map(|m| m.value)
.or_else(|| ms.vs.name.clone()),
_ => return Ok(true),
};
Expand Down
2 changes: 1 addition & 1 deletion src/servers/src/http/prometheus_resp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use datatypes::prelude::ConcreteDataType;
use datatypes::scalars::ScalarVector;
use datatypes::vectors::{Float64Vector, StringVector, TimestampMillisecondVector};
use promql_parser::label::METRIC_NAME;
use promql_parser::parser::ValueType;
use promql_parser::parser::value::ValueType;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand Down