From 98a9b7c90c98606d04e730ce2a510e79c0518daa Mon Sep 17 00:00:00 2001 From: Tobias Guggenmos Date: Mon, 3 Feb 2020 17:21:46 +0100 Subject: [PATCH] Remove engine specific types Signed-off-by: Tobias Guggenmos --- promql/parser/functions.go | 277 +++++++++++++++++++++++++++++++++++++ promql/parser/value.go | 45 ++++++ 2 files changed, 322 insertions(+) create mode 100644 promql/parser/functions.go create mode 100644 promql/parser/value.go diff --git a/promql/parser/functions.go b/promql/parser/functions.go new file mode 100644 index 000000000..3eb1ca15b --- /dev/null +++ b/promql/parser/functions.go @@ -0,0 +1,277 @@ +// Copyright 2015 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package promql + +// Function represents a function of the expression language and is +// used by function nodes. +type Function struct { + Name string + ArgTypes []ValueType + Variadic int + ReturnType ValueType +} + +// Functions is a list of all functions supported by PromQL, including their types. +var Functions = map[string]*Function{ + "abs": { + Name: "abs", + ArgTypes: []ValueType{ValueTypeVector}, + ReturnType: ValueTypeVector, + }, + "absent": { + Name: "absent", + ArgTypes: []ValueType{ValueTypeVector}, + ReturnType: ValueTypeVector, + }, + "absent_over_time": { + Name: "absent_over_time", + ArgTypes: []ValueType{ValueTypeMatrix}, + ReturnType: ValueTypeVector, + }, + "avg_over_time": { + Name: "avg_over_time", + ArgTypes: []ValueType{ValueTypeMatrix}, + ReturnType: ValueTypeVector, + }, + "ceil": { + Name: "ceil", + ArgTypes: []ValueType{ValueTypeVector}, + ReturnType: ValueTypeVector, + }, + "changes": { + Name: "changes", + ArgTypes: []ValueType{ValueTypeMatrix}, + ReturnType: ValueTypeVector, + }, + "clamp_max": { + Name: "clamp_max", + ArgTypes: []ValueType{ValueTypeVector, ValueTypeScalar}, + ReturnType: ValueTypeVector, + }, + "clamp_min": { + Name: "clamp_min", + ArgTypes: []ValueType{ValueTypeVector, ValueTypeScalar}, + ReturnType: ValueTypeVector, + }, + "count_over_time": { + Name: "count_over_time", + ArgTypes: []ValueType{ValueTypeMatrix}, + ReturnType: ValueTypeVector, + }, + "days_in_month": { + Name: "days_in_month", + ArgTypes: []ValueType{ValueTypeVector}, + Variadic: 1, + ReturnType: ValueTypeVector, + }, + "day_of_month": { + Name: "day_of_month", + ArgTypes: []ValueType{ValueTypeVector}, + Variadic: 1, + ReturnType: ValueTypeVector, + }, + "day_of_week": { + Name: "day_of_week", + ArgTypes: []ValueType{ValueTypeVector}, + Variadic: 1, + ReturnType: ValueTypeVector, + }, + "delta": { + Name: "delta", + ArgTypes: []ValueType{ValueTypeMatrix}, + ReturnType: ValueTypeVector, + }, + "deriv": { + Name: "deriv", + ArgTypes: []ValueType{ValueTypeMatrix}, + ReturnType: ValueTypeVector, + }, + "exp": { + Name: "exp", + ArgTypes: []ValueType{ValueTypeVector}, + ReturnType: ValueTypeVector, + }, + "floor": { + Name: "floor", + ArgTypes: []ValueType{ValueTypeVector}, + ReturnType: ValueTypeVector, + }, + "histogram_quantile": { + Name: "histogram_quantile", + ArgTypes: []ValueType{ValueTypeScalar, ValueTypeVector}, + ReturnType: ValueTypeVector, + }, + "holt_winters": { + Name: "holt_winters", + ArgTypes: []ValueType{ValueTypeMatrix, ValueTypeScalar, ValueTypeScalar}, + ReturnType: ValueTypeVector, + }, + "hour": { + Name: "hour", + ArgTypes: []ValueType{ValueTypeVector}, + Variadic: 1, + ReturnType: ValueTypeVector, + }, + "idelta": { + Name: "idelta", + ArgTypes: []ValueType{ValueTypeMatrix}, + ReturnType: ValueTypeVector, + }, + "increase": { + Name: "increase", + ArgTypes: []ValueType{ValueTypeMatrix}, + ReturnType: ValueTypeVector, + }, + "irate": { + Name: "irate", + ArgTypes: []ValueType{ValueTypeMatrix}, + ReturnType: ValueTypeVector, + }, + "label_replace": { + Name: "label_replace", + ArgTypes: []ValueType{ValueTypeVector, ValueTypeString, ValueTypeString, ValueTypeString, ValueTypeString}, + ReturnType: ValueTypeVector, + }, + "label_join": { + Name: "label_join", + ArgTypes: []ValueType{ValueTypeVector, ValueTypeString, ValueTypeString, ValueTypeString}, + Variadic: -1, + ReturnType: ValueTypeVector, + }, + "ln": { + Name: "ln", + ArgTypes: []ValueType{ValueTypeVector}, + ReturnType: ValueTypeVector, + }, + "log10": { + Name: "log10", + ArgTypes: []ValueType{ValueTypeVector}, + ReturnType: ValueTypeVector, + }, + "log2": { + Name: "log2", + ArgTypes: []ValueType{ValueTypeVector}, + ReturnType: ValueTypeVector, + }, + "max_over_time": { + Name: "max_over_time", + ArgTypes: []ValueType{ValueTypeMatrix}, + ReturnType: ValueTypeVector, + }, + "min_over_time": { + Name: "min_over_time", + ArgTypes: []ValueType{ValueTypeMatrix}, + ReturnType: ValueTypeVector, + }, + "minute": { + Name: "minute", + ArgTypes: []ValueType{ValueTypeVector}, + Variadic: 1, + ReturnType: ValueTypeVector, + }, + "month": { + Name: "month", + ArgTypes: []ValueType{ValueTypeVector}, + Variadic: 1, + ReturnType: ValueTypeVector, + }, + "predict_linear": { + Name: "predict_linear", + ArgTypes: []ValueType{ValueTypeMatrix, ValueTypeScalar}, + ReturnType: ValueTypeVector, + }, + "quantile_over_time": { + Name: "quantile_over_time", + ArgTypes: []ValueType{ValueTypeScalar, ValueTypeMatrix}, + ReturnType: ValueTypeVector, + }, + "rate": { + Name: "rate", + ArgTypes: []ValueType{ValueTypeMatrix}, + ReturnType: ValueTypeVector, + }, + "resets": { + Name: "resets", + ArgTypes: []ValueType{ValueTypeMatrix}, + ReturnType: ValueTypeVector, + }, + "round": { + Name: "round", + ArgTypes: []ValueType{ValueTypeVector, ValueTypeScalar}, + Variadic: 1, + ReturnType: ValueTypeVector, + }, + "scalar": { + Name: "scalar", + ArgTypes: []ValueType{ValueTypeVector}, + ReturnType: ValueTypeScalar, + }, + "sort": { + Name: "sort", + ArgTypes: []ValueType{ValueTypeVector}, + ReturnType: ValueTypeVector, + }, + "sort_desc": { + Name: "sort_desc", + ArgTypes: []ValueType{ValueTypeVector}, + ReturnType: ValueTypeVector, + }, + "sqrt": { + Name: "sqrt", + ArgTypes: []ValueType{ValueTypeVector}, + ReturnType: ValueTypeVector, + }, + "stddev_over_time": { + Name: "stddev_over_time", + ArgTypes: []ValueType{ValueTypeMatrix}, + ReturnType: ValueTypeVector, + }, + "stdvar_over_time": { + Name: "stdvar_over_time", + ArgTypes: []ValueType{ValueTypeMatrix}, + ReturnType: ValueTypeVector, + }, + "sum_over_time": { + Name: "sum_over_time", + ArgTypes: []ValueType{ValueTypeMatrix}, + ReturnType: ValueTypeVector, + }, + "time": { + Name: "time", + ArgTypes: []ValueType{}, + ReturnType: ValueTypeScalar, + }, + "timestamp": { + Name: "timestamp", + ArgTypes: []ValueType{ValueTypeVector}, + ReturnType: ValueTypeVector, + }, + "vector": { + Name: "vector", + ArgTypes: []ValueType{ValueTypeScalar}, + ReturnType: ValueTypeVector, + }, + "year": { + Name: "year", + ArgTypes: []ValueType{ValueTypeVector}, + Variadic: 1, + ReturnType: ValueTypeVector, + }, +} + +// getFunction returns a predefined Function object for the given name. +func getFunction(name string) (*Function, bool) { + function, ok := Functions[name] + return function, ok +} diff --git a/promql/parser/value.go b/promql/parser/value.go new file mode 100644 index 000000000..aaa71bc00 --- /dev/null +++ b/promql/parser/value.go @@ -0,0 +1,45 @@ +// Copyright 2017 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package promql + +// Value is a generic interface for values resulting from a query evaluation. +type Value interface { + Type() ValueType + String() string +} + +// ValueType describes a type of a value. +type ValueType string + +// The valid value types. +const ( + ValueTypeNone = "none" + ValueTypeVector = "vector" + ValueTypeScalar = "scalar" + ValueTypeMatrix = "matrix" + ValueTypeString = "string" +) + +// documentedType returns the internal type to the equivalent +// user facing terminology as defined in the documentation. +func documentedType(t ValueType) string { + switch t { + case "vector": + return "instant vector" + case "matrix": + return "range vector" + default: + return string(t) + } +}