diff --git a/influxdb_influxql_parser/src/select.rs b/influxdb_influxql_parser/src/select.rs index 0ef658ba57..b49c49425e 100644 --- a/influxdb_influxql_parser/src/select.rs +++ b/influxdb_influxql_parser/src/select.rs @@ -45,7 +45,7 @@ pub struct SelectStatement { pub group_by: Option, /// The [fill] clause specifies the fill behaviour for the selection. If the value is [`None`], - /// it is the same behavior as `fill(none)`. + /// it is the same behavior as `fill(null)`. /// /// [fill]: https://docs.influxdata.com/influxdb/v1.8/query_language/explore-data/#group-by-time-intervals-and-fill pub fill: Option, diff --git a/influxdb_iox/tests/query_tests2/cases/in/issue_6112.influxql.expected b/influxdb_iox/tests/query_tests2/cases/in/issue_6112.influxql.expected index 5430dbdd58..b8310946e3 100644 --- a/influxdb_iox/tests/query_tests2/cases/in/issue_6112.influxql.expected +++ b/influxdb_iox/tests/query_tests2/cases/in/issue_6112.influxql.expected @@ -1003,11 +1003,11 @@ tags: non_existent= +---------------------+-------+--------------------+--------------------+ -- InfluxQL: SELECT COUNT(f64), COUNT(f64) + COUNT(f64), COUNT(f64) * 3 FROM m0; name: m0 -+---------------------+-------+---------------------+-----------+ -| time | count | count_f64_count_f64 | count_f64 | -+---------------------+-------+---------------------+-----------+ -| 1970-01-01T00:00:00 | 7 | 14 | 21 | -+---------------------+-------+---------------------+-----------+ ++---------------------+-------+-------------+---------+ +| time | count | count_count | count_1 | ++---------------------+-------+-------------+---------+ +| 1970-01-01T00:00:00 | 7 | 14 | 21 | ++---------------------+-------+-------------+---------+ -- InfluxQL: SELECT COUNT(f64) as the_count, SUM(non_existent) as foo FROM m0; name: m0 +---------------------+-----------+-----+ @@ -1052,31 +1052,31 @@ name: disk +---------------------+------+--------+ -- InfluxQL: SELECT MEAN(usage_idle) + MEAN(bytes_free) FROM cpu, disk; name: cpu -+---------------------+---------------------------------+ -| time | mean_usage_idle_mean_bytes_free | -+---------------------+---------------------------------+ -| 1970-01-01T00:00:00 | | -+---------------------+---------------------------------+ ++---------------------+-----------+ +| time | mean_mean | ++---------------------+-----------+ +| 1970-01-01T00:00:00 | | ++---------------------+-----------+ name: disk -+---------------------+---------------------------------+ -| time | mean_usage_idle_mean_bytes_free | -+---------------------+---------------------------------+ -| 1970-01-01T00:00:00 | | -+---------------------+---------------------------------+ ++---------------------+-----------+ +| time | mean_mean | ++---------------------+-----------+ +| 1970-01-01T00:00:00 | | ++---------------------+-----------+ -- InfluxQL: SELECT MEAN(usage_idle) + MEAN(foo) FROM cpu; name: cpu -+---------------------+--------------------------+ -| time | mean_usage_idle_mean_foo | -+---------------------+--------------------------+ -| 1970-01-01T00:00:00 | | -+---------------------+--------------------------+ ++---------------------+-----------+ +| time | mean_mean | ++---------------------+-----------+ +| 1970-01-01T00:00:00 | | ++---------------------+-----------+ -- InfluxQL: SELECT MEAN(usage_idle), MEAN(usage_idle) + MEAN(foo) FROM cpu; name: cpu -+---------------------+--------------------+--------------------------+ -| time | mean | mean_usage_idle_mean_foo | -+---------------------+--------------------+--------------------------+ -| 1970-01-01T00:00:00 | 1.9850000000000003 | | -+---------------------+--------------------+--------------------------+ ++---------------------+--------------------+-----------+ +| time | mean | mean_mean | ++---------------------+--------------------+-----------+ +| 1970-01-01T00:00:00 | 1.9850000000000003 | | ++---------------------+--------------------+-----------+ -- InfluxQL: SELECT MEAN(foo) FROM cpu; ++ ++ @@ -1084,47 +1084,47 @@ name: cpu -- InfluxQL: SELECT MEAN(usage_idle) + MEAN(foo) FROM cpu GROUP BY cpu; name: cpu tags: cpu=cpu-total -+---------------------+--------------------------+ -| time | mean_usage_idle_mean_foo | -+---------------------+--------------------------+ -| 1970-01-01T00:00:00 | | -+---------------------+--------------------------+ ++---------------------+-----------+ +| time | mean_mean | ++---------------------+-----------+ +| 1970-01-01T00:00:00 | | ++---------------------+-----------+ name: cpu tags: cpu=cpu0 -+---------------------+--------------------------+ -| time | mean_usage_idle_mean_foo | -+---------------------+--------------------------+ -| 1970-01-01T00:00:00 | | -+---------------------+--------------------------+ ++---------------------+-----------+ +| time | mean_mean | ++---------------------+-----------+ +| 1970-01-01T00:00:00 | | ++---------------------+-----------+ name: cpu tags: cpu=cpu1 -+---------------------+--------------------------+ -| time | mean_usage_idle_mean_foo | -+---------------------+--------------------------+ -| 1970-01-01T00:00:00 | | -+---------------------+--------------------------+ ++---------------------+-----------+ +| time | mean_mean | ++---------------------+-----------+ +| 1970-01-01T00:00:00 | | ++---------------------+-----------+ -- InfluxQL: SELECT MEAN(usage_idle), MEAN(usage_idle) + MEAN(foo) FROM cpu GROUP BY cpu; name: cpu tags: cpu=cpu-total -+---------------------+--------------------+--------------------------+ -| time | mean | mean_usage_idle_mean_foo | -+---------------------+--------------------+--------------------------+ -| 1970-01-01T00:00:00 | 2.9850000000000003 | | -+---------------------+--------------------+--------------------------+ ++---------------------+--------------------+-----------+ +| time | mean | mean_mean | ++---------------------+--------------------+-----------+ +| 1970-01-01T00:00:00 | 2.9850000000000003 | | ++---------------------+--------------------+-----------+ name: cpu tags: cpu=cpu0 -+---------------------+-------+--------------------------+ -| time | mean | mean_usage_idle_mean_foo | -+---------------------+-------+--------------------------+ -| 1970-01-01T00:00:00 | 0.985 | | -+---------------------+-------+--------------------------+ ++---------------------+-------+-----------+ +| time | mean | mean_mean | ++---------------------+-------+-----------+ +| 1970-01-01T00:00:00 | 0.985 | | ++---------------------+-------+-----------+ name: cpu tags: cpu=cpu1 -+---------------------+--------------------+--------------------------+ -| time | mean | mean_usage_idle_mean_foo | -+---------------------+--------------------+--------------------------+ -| 1970-01-01T00:00:00 | 1.9849999999999999 | | -+---------------------+--------------------+--------------------------+ ++---------------------+--------------------+-----------+ +| time | mean | mean_mean | ++---------------------+--------------------+-----------+ +| 1970-01-01T00:00:00 | 1.9849999999999999 | | ++---------------------+--------------------+-----------+ -- InfluxQL: SELECT MEAN(foo) FROM cpu GROUP BY cpu; ++ ++ diff --git a/iox_query_influxql/src/plan/expr_type_evaluator.rs b/iox_query_influxql/src/plan/expr_type_evaluator.rs index b563ef97b1..177d80ae01 100644 --- a/iox_query_influxql/src/plan/expr_type_evaluator.rs +++ b/iox_query_influxql/src/plan/expr_type_evaluator.rs @@ -1,6 +1,6 @@ use crate::plan::field::field_by_name; use crate::plan::field_mapper::map_type; -use crate::plan::ir::TableReference; +use crate::plan::ir::DataSource; use crate::plan::{error, SchemaProvider}; use datafusion::common::Result; use influxdb_influxql_parser::expression::{ @@ -16,22 +16,25 @@ use itertools::Itertools; pub(super) fn evaluate_type( s: &dyn SchemaProvider, expr: &Expr, - from: &[TableReference], + from: &[DataSource], ) -> Result> { - TypeEvaluator::new(from, s).eval_type(expr) + TypeEvaluator::new(s, from).eval_type(expr) } -struct TypeEvaluator<'a> { +/// Evaluate the type of the specified expression. +/// +/// Derived from [Go implementation](https://github.com/influxdata/influxql/blob/1ba470371ec093d57a726b143fe6ccbacf1b452b/ast.go#L4796-L4797). +pub(super) struct TypeEvaluator<'a> { s: &'a dyn SchemaProvider, - from: &'a [TableReference], + from: &'a [DataSource], } impl<'a> TypeEvaluator<'a> { - fn new(from: &'a [TableReference], s: &'a dyn SchemaProvider) -> Self { + pub(super) fn new(s: &'a dyn SchemaProvider, from: &'a [DataSource]) -> Self { Self { from, s } } - fn eval_type(&self, expr: &Expr) -> Result> { + pub(super) fn eval_type(&self, expr: &Expr) -> Result> { Ok(match expr { Expr::VarRef(v) => self.eval_var_ref(v)?, Expr::Call(v) => self.eval_call(v)?, @@ -80,9 +83,10 @@ impl<'a> TypeEvaluator<'a> { } } - /// Returns the type for the specified [`Expr`]. + /// Returns the type for the specified [`VarRef`]. + /// /// This function assumes that the expression has already been reduced. - fn eval_var_ref(&self, expr: &VarRef) -> Result> { + pub(super) fn eval_var_ref(&self, expr: &VarRef) -> Result> { Ok(match expr.data_type { Some(dt) if matches!( @@ -100,7 +104,7 @@ impl<'a> TypeEvaluator<'a> { let mut data_type: Option = None; for tr in self.from.iter() { match tr { - TableReference::Name(name) => match ( + DataSource::Table(name) => match ( data_type, map_type(self.s, name.as_str(), expr.name.as_str())?, ) { @@ -112,7 +116,7 @@ impl<'a> TypeEvaluator<'a> { (None, Some(res)) => data_type = Some(res), _ => continue, }, - TableReference::Subquery(select) => { + DataSource::Subquery(select) => { // find the field by name if let Some(field) = field_by_name(&select.fields, expr.name.as_str()) { match (data_type, evaluate_type(self.s, &field.expr, &select.from)?) diff --git a/iox_query_influxql/src/plan/field.rs b/iox_query_influxql/src/plan/field.rs index c7cad73ccc..158636a550 100644 --- a/iox_query_influxql/src/plan/field.rs +++ b/iox_query_influxql/src/plan/field.rs @@ -66,7 +66,7 @@ impl<'a> Visitor for BinaryExprNameVisitor<'a> { fn pre_visit_call(self, n: &Call) -> Result, Self::Error> { self.0.push(n.name.clone()); - Ok(Recursion::Continue(self)) + Ok(Recursion::Stop(self)) } } @@ -102,7 +102,7 @@ mod test { assert_eq!(field_name(&f), "count"); let f = get_first_field("SELECT COUNT(usage) + SUM(usage_idle) FROM cpu"); - assert_eq!(field_name(&f), "count_usage_sum_usage_idle"); + assert_eq!(field_name(&f), "count_sum"); let f = get_first_field("SELECT 1+2 FROM cpu"); assert_eq!(field_name(&f), ""); diff --git a/iox_query_influxql/src/plan/ir.rs b/iox_query_influxql/src/plan/ir.rs index 11547c8342..53ca5e18ca 100644 --- a/iox_query_influxql/src/plan/ir.rs +++ b/iox_query_influxql/src/plan/ir.rs @@ -5,7 +5,6 @@ use influxdb_influxql_parser::common::{ LimitClause, MeasurementName, OffsetClause, OrderByClause, QualifiedMeasurementName, WhereClause, }; -use influxdb_influxql_parser::expression::ConditionalExpression; use influxdb_influxql_parser::select::{ Field, FieldList, FillClause, FromMeasurementClause, GroupByClause, MeasurementSelection, SelectStatement, TimeZoneClause, @@ -19,17 +18,17 @@ pub(super) struct Select { /// Projection clause of the selection. pub(super) fields: Vec, - /// A list of tables or subqueries used as the source data for the selection. - pub(super) from: Vec, + /// A list of data sources for the selection. + pub(super) from: Vec, /// A conditional expression to filter the selection. - pub(super) condition: Option, + pub(super) condition: Option, /// The GROUP BY clause of the selection. pub(super) group_by: Option, /// The [fill] clause specifies the fill behaviour for the selection. If the value is [`None`], - /// it is the same behavior as `fill(none)`. + /// it is the same behavior as `fill(null)`. /// /// [fill]: https://docs.influxdata.com/influxdb/v1.8/query_language/explore-data/#group-by-time-intervals-and-fill pub(super) fill: Option, @@ -38,10 +37,10 @@ pub(super) struct Select { pub(super) order_by: Option, /// A value to restrict the number of rows returned. - pub(super) limit: Option, + pub(super) limit: Option, /// A value to specify an offset to start retrieving rows. - pub(super) offset: Option, + pub(super) offset: Option, /// The timezone for the query, specified as [`tz('