Overpass API regression suite coverage (Saxony) / overpass_api/statements/aggregators.h
overpass_api/statements/aggregators.h
81 of 95 instrumented lines run by the suite (85.3%) at 8edf11b0419c (this file on GitHub).
runnever runcolumns: line, executions, queries (click for the queries that run the line)
1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.2 *3 * This file is part of Overpass_API.4 *5 * Overpass_API is free software: you can redistribute it and/or modify6 * it under the terms of the GNU Affero General Public License as7 * published by the Free Software Foundation, either version 3 of the8 * License, or (at your option) any later version.9 *10 * Overpass_API is distributed in the hope that it will be useful,11 * but WITHOUT ANY WARRANTY; without even the implied warranty of12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13 * GNU General Public License for more details.14 *15 * You should have received a copy of the GNU Affero General Public License16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.17 */19#ifndef DE__OSM3S___OVERPASS_API__STATEMENTS__AGGREGATORS_H20#define DE__OSM3S___OVERPASS_API__STATEMENTS__AGGREGATORS_H23#include "../data/tag_store.h"24#include "../data/utils.h"25#include "evaluator.h"26#include "statement.h"28#include <map>29#include <string>30#include <vector>33struct Value_Aggregator34{35 // The code of min and max relies on the relative order to gracefully degrade the type36 enum Type_Indicator { type_void = 0, type_int64 = 1, type_double = 2, type_string = 3 };38 virtual void update_value(const std::string& value) = 0;39 virtual std::string get_value() = 0;41};44struct Geometry_Aggregator45{46 virtual void consume_value(Opaque_Geometry* geom) = 0;47 virtual Opaque_Geometry* move_value() = 0;49};52/* === Aggregators ===54Aggregators need for execution both a set to operate on and an evaluator as argument.55That evaulator will loop over each element of the set, and the aggregator will combine its results.56*/58struct Evaluator_Aggregator : public Evaluator59{60 enum Object_Type { tag, generic, id, type };63 Evaluator_Aggregator(const std::string& func_name,64 int line_number_, const std::map< std::string, std::string >& input_attributes,65 Parsed_Query& global_settings);66 virtual void add_statement(Statement* statement, std::string text);69 virtual Requested_Context request_context() const;71 virtual Eval_Task* get_string_task(Prepare_Task_Context& context, const std::string* key);72 virtual Eval_Geometry_Task* get_geometry_task(Prepare_Task_Context& context);74 virtual Value_Aggregator* get_aggregator() = 0;75 virtual Geometry_Aggregator* get_geometry_aggregator() = 0;77 std::string input;78 Evaluator* rhs;79};82bool try_parse_input_set(const Token_Node_Ptr& tree_it, Error_Output* error_output, const std::string& message,83 std::string& input_set, bool& explicit_input_set);86template< typename Evaluator_ >87struct Aggregator_Statement_Maker : public Generic_Statement_Maker< Evaluator_ >88{8916608692 q Aggregator_Statement_Maker() : Generic_Statement_Maker< Evaluator_ >(Evaluator_::stmt_name()) {}90};93template< typename Evaluator_ >94struct Aggregator_Evaluator_Maker : Statement::Evaluator_Maker95{96 virtual Statement* create_evaluator(97 const Token_Node_Ptr& tree_it, Statement::QL_Context tree_context,98 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output)10286862 q if (!try_parse_input_set(tree_it, error_output, Evaluator_::stmt_func_name() + "(...) needs an argument",10686462 q Statement* result = new Evaluator_(tree_it->line_col.first, attributes, global_settings);11186462 q Statement::elem_eval_possible, Statement::Single_Return_Type_Checker(Evaluator_::argument_type()));121 Aggregator_Evaluator_Maker()125};128template< typename Evaluator_ >129struct Evaluator_Aggregator_Syntax : public Evaluator_Aggregator130{131 Evaluator_Aggregator_Syntax(int line_number_, const std::map< std::string, std::string >& input_attributes,132 Parsed_Query& global_settings)13386462 q : Evaluator_Aggregator(Evaluator_::stmt_name(), line_number_, input_attributes, global_settings) {}135 virtual std::string dump_xml(const std::string& indent) const142 virtual std::string dump_compact_ql(const std::string&) const15093662 q virtual Statement::Eval_Return_Type return_type() const { return Evaluator_::argument_type(); };153};156/* ==== Union and Set ====158The basic syntax is160 <Set>.u(<Evaluator>)162resp.164 <Set>.set(<Evaluator>)166If the set is the default set <em>_</em> then you can drop the set parameter:168 u(<Evaluator>)170resp.172 set(<Evaluator>)174These two evaluators execute their right hand side evulators on each element of the specified set.175<em>set</em> makes a semi-colon separated list of all distinct values that appear.176<em>u</em> returns the single found value if only one value is found.177If no value is found then <em>u</em> returns an empty string.178If multiple different values are found then <em>set</em> returns the text "< multiple values found >".179*/181class Evaluator_Union_Value : public Evaluator_Aggregator_Syntax< Evaluator_Union_Value >182{183public:184 static Aggregator_Statement_Maker< Evaluator_Union_Value > statement_maker;185 static Aggregator_Evaluator_Maker< Evaluator_Union_Value > evaluator_maker;190 Evaluator_Union_Value(int line_number_, const std::map< std::string, std::string >& input_attributes,191 Parsed_Query& global_settings)19218436 q : Evaluator_Aggregator_Syntax< Evaluator_Union_Value >(line_number_, input_attributes, global_settings) {}194 struct Aggregator : Value_Aggregator195 {196 virtual void update_value(const std::string& value);198 std::string agg_value;199 };202};205class Evaluator_Set_Value : public Evaluator_Aggregator_Syntax< Evaluator_Set_Value >206{207public:208 static Aggregator_Statement_Maker< Evaluator_Set_Value > statement_maker;209 static Aggregator_Evaluator_Maker< Evaluator_Set_Value > evaluator_maker;214 Evaluator_Set_Value(int line_number_, const std::map< std::string, std::string >& input_attributes,215 Parsed_Query& global_settings)2167615 q : Evaluator_Aggregator_Syntax< Evaluator_Set_Value >(line_number_, input_attributes, global_settings) {}218 struct Aggregator : Value_Aggregator219 {220 virtual void update_value(const std::string& value);221 virtual std::string get_value();222 std::set< std::string > values;223 };226};229/* ==== Min and Max ====231The basic syntax is233 <Set>.min(<Evaluator>)235resp.237 <Set>.max(<Evaluator>)239If the set is the default set <em>_</em> then you can drop the set parameter:241 min(<Evaluator>)243resp.245 max(<Evaluator>)247These two evaluators execute their right hand side evaluators on each element of the specified set.248If all return values are valid numbers then <em>min</em> returns the minimal amongst the numbers.249Likewise, if all return values are valid numbers then <em>max</em> returns the maximal amongst the numbers.250If not all return values are valid numbers then <em>min</em> returns the lexicographically first string.251Likewise, if not all return values are valid numbers then <em>max</em> returns the lexicographically last string.252If no value is found then each <em>min</em> and <em>max </em> return an empty string.253*/255class Evaluator_Min_Value : public Evaluator_Aggregator_Syntax< Evaluator_Min_Value >256{257public:258 static Aggregator_Statement_Maker< Evaluator_Min_Value > statement_maker;259 static Aggregator_Evaluator_Maker< Evaluator_Min_Value > evaluator_maker;264 Evaluator_Min_Value(int line_number_, const std::map< std::string, std::string >& input_attributes,265 Parsed_Query& global_settings)26616832 q : Evaluator_Aggregator_Syntax< Evaluator_Min_Value >(line_number_, input_attributes, global_settings) {}268 struct Aggregator : Value_Aggregator269 {272 virtual void update_value(const std::string& value);273 virtual std::string get_value();274 Type_Indicator relevant_type;275 int64 result_l;276 double result_d;277 std::string result_s;278 };281};284class Evaluator_Max_Value : public Evaluator_Aggregator_Syntax< Evaluator_Max_Value >285{286public:287 static Aggregator_Statement_Maker< Evaluator_Max_Value > statement_maker;288 static Aggregator_Evaluator_Maker< Evaluator_Max_Value > evaluator_maker;293 Evaluator_Max_Value(int line_number_, const std::map< std::string, std::string >& input_attributes,294 Parsed_Query& global_settings)29519233 q : Evaluator_Aggregator_Syntax< Evaluator_Max_Value >(line_number_, input_attributes, global_settings) {}297 struct Aggregator : Value_Aggregator298 {301 virtual void update_value(const std::string& value);302 virtual std::string get_value();303 Type_Indicator relevant_type;304 int64 result_l;305 double result_d;306 std::string result_s;307 };310};313/* ==== Sum ====315The basic syntax is317 <Set>.sum(<Evaluator>)319If the set is the default set <em>_</em> then you can drop the set parameter:321 sum(<Evaluator>)323This evaluator executes its right hand side evaluators on each element of the specified set.324If all return values are valid numbers then <em>sum</em> returns their sum.325If not all return values are valid numbers then <em>sum</em> returns "NaN".326*/328class Evaluator_Sum_Value : public Evaluator_Aggregator_Syntax< Evaluator_Sum_Value >329{330public:331 static Aggregator_Statement_Maker< Evaluator_Sum_Value > statement_maker;332 static Aggregator_Evaluator_Maker< Evaluator_Sum_Value > evaluator_maker;337 Evaluator_Sum_Value(int line_number_, const std::map< std::string, std::string >& input_attributes,338 Parsed_Query& global_settings)33915229 q : Evaluator_Aggregator_Syntax< Evaluator_Sum_Value >(line_number_, input_attributes, global_settings) {}341 struct Aggregator : Value_Aggregator342 {344 virtual void update_value(const std::string& value);345 virtual std::string get_value();346 Type_Indicator relevant_type;347 int64 result_l;348 double result_d;349 };352};355/* === Statistical Count ===357This variant of the <em>count</em> operator counts elements of a given type in a set.359The syntax variants361 count(nodes)362 count(ways)363 count(relations)364 count(deriveds)365 count(nwr)366 count(nw)367 count(wr)368 count(nr)370counts elements in the default set <em>_</em>, and the syntax variant372 <Set>.count(nodes)373 <Set>.count(ways)374 <Set>.count(relations)375 <Set>.count(deriveds)376 <Set>.count(nwr)377 <Set>.count(nw)378 <Set>.count(wr)379 <Set>.count(nr)381counts elements in the set <Set>.382*/384class Evaluator_Set_Count : public Evaluator385{386public:387 enum Objects { nothing, nodes, ways, relations, deriveds, nwr, nw, wr, nr };388 static std::string to_string(Objects objects);389 static bool try_parse_object_type(const std::string& input, Evaluator_Set_Count::Objects& result);391 struct Statement_Maker : public Generic_Statement_Maker< Evaluator_Set_Count >392 {3932768692 q Statement_Maker() : Generic_Statement_Maker< Evaluator_Set_Count >("eval-set-count") {}394 };395 static Statement_Maker statement_maker;397 struct Evaluator_Maker : public Statement::Evaluator_Maker398 {399 virtual Statement* create_evaluator(const Token_Node_Ptr& tree_it, QL_Context tree_context,400 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output);402 };403 static Evaluator_Maker evaluator_maker;405 virtual std::string dump_xml(const std::string& indent) const4063829 q { return indent + "<eval-set-count from=\"" + input + "\" type=\"" + to_string(to_count) + "\"/>\n"; }407 virtual std::string dump_compact_ql(const std::string&) const410 Evaluator_Set_Count(int line_number_, const std::map< std::string, std::string >& input_attributes,411 Parsed_Query& global_settings);417 virtual Requested_Context request_context() const;420 virtual Eval_Task* get_string_task(Prepare_Task_Context& context, const std::string* key);422private:423 std::string input;424 Objects to_count;425};428/* === Union of Geometry ===430The basic syntax is432 <Set>.gcat(<Evaluator>)434If the set is the default set <em>_</em> then you can drop the set parameter:436 gcat(<Evaluator>)438The evaluator executes its right hand side evaluator on each element of the specified set.439It then combines the obtained geometries in one large object.440If geometries are contained multiple times in the group441then they are as well repeated as members of the result.442*/444class Evaluator_Geom_Concat_Value : public Evaluator_Aggregator_Syntax< Evaluator_Geom_Concat_Value >445{446public:447 static Aggregator_Statement_Maker< Evaluator_Geom_Concat_Value > statement_maker;448 static Aggregator_Evaluator_Maker< Evaluator_Geom_Concat_Value > evaluator_maker;453 Evaluator_Geom_Concat_Value(int line_number_, const std::map< std::string, std::string >& input_attributes,454 Parsed_Query& global_settings)458 struct Aggregator : Geometry_Aggregator459 {461 virtual void consume_value(Opaque_Geometry* geom);462 virtual Opaque_Geometry* move_value();463 Compound_Geometry* result;464 };467};470#endif