diff --git a/C/plugins/storage/postgres/connection.cpp b/C/plugins/storage/postgres/connection.cpp index 22fa83cbc6..982a15320e 100644 --- a/C/plugins/storage/postgres/connection.cpp +++ b/C/plugins/storage/postgres/connection.cpp @@ -2551,6 +2551,7 @@ bool Connection::jsonAggregates(const Value& payload, sql.append(column_name); sql.append("\""); } + sql.append(") AS \""); } else if (itr->HasMember("json")) { @@ -2620,8 +2621,9 @@ bool Connection::jsonAggregates(const Value& payload, sql.append(")::float"); } + sql.append(" END) AS \""); } - sql.append(" END) AS \""); + if (itr->HasMember("alias")) { sql.append((*itr)["alias"].GetString()); @@ -3112,10 +3114,25 @@ bool Connection::jsonWhereClause(const Value& whereClause, sql.append(whereClause["value"].GetInt()); } else if (whereClause["value"].IsString()) { - sql.append('\''); + if (whereColumnName != "history_ts") + { + sql.append('\''); + } + else + { + sql.append("TO_TIMESTAMP('"); + } + string value = whereClause["value"].GetString(); sql.append(escape(value)); - sql.append('\''); + if (whereColumnName != "history_ts") + { + sql.append('\''); + } + else + { + sql.append("')"); + } // Identify a specific operation to restrinct the tables involved if (whereColumnName.compare("asset_code") == 0) diff --git a/tests/unit/C/services/storage/postgres/expected_ETC_UTC/117 b/tests/unit/C/services/storage/postgres/expected_ETC_UTC/117 new file mode 100644 index 0000000000..e982f81cc7 --- /dev/null +++ b/tests/unit/C/services/storage/postgres/expected_ETC_UTC/117 @@ -0,0 +1 @@ +{"count":1,"rows":[{"sum_value":"6582","count_value":439,"key":"READINGS"}]} diff --git a/tests/unit/C/services/storage/postgres/expected_ETC_UTC_PG12/117 b/tests/unit/C/services/storage/postgres/expected_ETC_UTC_PG12/117 new file mode 100644 index 0000000000..e982f81cc7 --- /dev/null +++ b/tests/unit/C/services/storage/postgres/expected_ETC_UTC_PG12/117 @@ -0,0 +1 @@ +{"count":1,"rows":[{"sum_value":"6582","count_value":439,"key":"READINGS"}]} diff --git a/tests/unit/C/services/storage/postgres/expected_EUROPE_ROME/117 b/tests/unit/C/services/storage/postgres/expected_EUROPE_ROME/117 new file mode 100644 index 0000000000..e982f81cc7 --- /dev/null +++ b/tests/unit/C/services/storage/postgres/expected_EUROPE_ROME/117 @@ -0,0 +1 @@ +{"count":1,"rows":[{"sum_value":"6582","count_value":439,"key":"READINGS"}]} diff --git a/tests/unit/C/services/storage/postgres/expected_EUROPE_ROME_PG12/117 b/tests/unit/C/services/storage/postgres/expected_EUROPE_ROME_PG12/117 new file mode 100644 index 0000000000..e982f81cc7 --- /dev/null +++ b/tests/unit/C/services/storage/postgres/expected_EUROPE_ROME_PG12/117 @@ -0,0 +1 @@ +{"count":1,"rows":[{"sum_value":"6582","count_value":439,"key":"READINGS"}]} diff --git a/tests/unit/C/services/storage/postgres/payloads/FOGL-4102.json b/tests/unit/C/services/storage/postgres/payloads/FOGL-4102.json new file mode 100644 index 0000000000..a2d527a5cc --- /dev/null +++ b/tests/unit/C/services/storage/postgres/payloads/FOGL-4102.json @@ -0,0 +1,25 @@ +{ + "return" : ["key"], + "aggregate" : [ + { + "operation" : "sum", + "column" : "value" + }, + { + "operation" : "count", + "column" : "value" + } + ], + "where" : { + "column" : "history_ts", + "condition" : ">=", + "value" : "1762764219.123255", + "and" : { + "column" : "key", + "condition" : "=", + "value" : "READINGS" + } + }, + "group" : "key" +} + diff --git a/tests/unit/C/services/storage/postgres/testset b/tests/unit/C/services/storage/postgres/testset index 9fc7a87442..adef4944dc 100644 --- a/tests/unit/C/services/storage/postgres/testset +++ b/tests/unit/C/services/storage/postgres/testset @@ -114,4 +114,5 @@ Delete table snapshot,DELETE,http://localhost:8080/storage/table/test2/snapshot/ Jira FOGL-690,POST,http://localhost:8080/storage/table/configuration,fogl690-error.json Add more Readings,POST,http://localhost:8080/storage/reading,readings_timebucket.json Query Readings Timebucket,PUT,http://localhost:8080/storage/reading/query,query_timebucket_datapoints.json +FOGL-4102 return aggregate sum count where and group,PUT,http://localhost:8080/storage/table/statistics_history/query,FOGL-4102.json Shutdown,POST,http://localhost:1081/fledge/service/shutdown,,checkstate diff --git a/tests/unit/C/services/storage/sqlite/expected_ETC_UTC/129 b/tests/unit/C/services/storage/sqlite/expected_ETC_UTC/129 new file mode 100644 index 0000000000..2ee0d1732d --- /dev/null +++ b/tests/unit/C/services/storage/sqlite/expected_ETC_UTC/129 @@ -0,0 +1 @@ +{"count":1,"rows":[{"sum_value":5756,"count_value":384,"key":"READINGS"}]} diff --git a/tests/unit/C/services/storage/sqlite/expected_EUROPE_ROME/129 b/tests/unit/C/services/storage/sqlite/expected_EUROPE_ROME/129 new file mode 100644 index 0000000000..2ee0d1732d --- /dev/null +++ b/tests/unit/C/services/storage/sqlite/expected_EUROPE_ROME/129 @@ -0,0 +1 @@ +{"count":1,"rows":[{"sum_value":5756,"count_value":384,"key":"READINGS"}]} diff --git a/tests/unit/C/services/storage/sqlite/payloads/FOGL-4102.json b/tests/unit/C/services/storage/sqlite/payloads/FOGL-4102.json new file mode 100644 index 0000000000..d89c64a3a1 --- /dev/null +++ b/tests/unit/C/services/storage/sqlite/payloads/FOGL-4102.json @@ -0,0 +1,25 @@ +{ + "return" : ["key"], + "aggregate" : [ + { + "operation" : "sum", + "column" : "id" + }, + { + "operation" : "count", + "column" : "id" + } + ], + "where" : { + "column" : "ts", + "condition" : ">=", + "value" : "2017-10-10 12:14:26.622315", + "and" : { + "column" : "key", + "condition" : "=", + "value" : "TEST1" + } + }, + "group" : "key" +} + diff --git a/tests/unit/C/services/storage/sqlite/testset b/tests/unit/C/services/storage/sqlite/testset index 1794c34483..553def1ee5 100644 --- a/tests/unit/C/services/storage/sqlite/testset +++ b/tests/unit/C/services/storage/sqlite/testset @@ -126,4 +126,5 @@ timezone - readings - read 3,PUT,http://localhost:8080/storage/reading/query,t timezone - readings - read 4,PUT,http://localhost:8080/storage/reading/query,tz_readings_read_4.json Add more Readings,POST,http://localhost:8080/storage/reading,readings_timebucket.json Query Readings Timebucket,PUT,http://localhost:8080/storage/reading/query,query_timebucket_datapoints.json +FOGL-4102 return aggregate sum count where and group,PUT,http://localhost:8080/storage/table/statistics_history/query,FOGL-4102.json Shutdown,POST,http://localhost:1081/fledge/service/shutdown,,checkstate diff --git a/tests/unit/python/fledge/common/storage_client/data/payload_FOGL-4102.json b/tests/unit/python/fledge/common/storage_client/data/payload_FOGL-4102.json new file mode 100644 index 0000000000..481dc495f0 --- /dev/null +++ b/tests/unit/python/fledge/common/storage_client/data/payload_FOGL-4102.json @@ -0,0 +1,25 @@ +{ + "return": ["key"], + "aggregate": [ + { + "operation": "sum", + "column": "value" + }, + { + "operation": "count", + "column": "value" + } + ], + "where": { + "column": "history_ts", + "condition": ">=", + "value": "1762764219.123255", + "and": { + "column": "key", + "condition": "=", + "value": "READINGS" + } + }, + "group": "key" +} + diff --git a/tests/unit/python/fledge/common/storage_client/test_payload_builder.py b/tests/unit/python/fledge/common/storage_client/test_payload_builder.py index fe02e2b726..d6c84382c1 100644 --- a/tests/unit/python/fledge/common/storage_client/test_payload_builder.py +++ b/tests/unit/python/fledge/common/storage_client/test_payload_builder.py @@ -406,6 +406,17 @@ def test_aggregate_with_where(self): res = PayloadBuilder().WHERE(["ts", "newer", 60]).AGGREGATE(["count", "*"]).payload() assert _payload("data/payload_aggregate_where.json") == json.loads(res) + def test_FOGL_4102(self): + """Test query with return, multiple aggregates (sum and count), where with and condition, and group""" + res = PayloadBuilder() \ + .SELECT("key") \ + .AGGREGATE(["sum", "value"], ["count", "value"]) \ + .WHERE(["history_ts", ">=", "1762764219.123255"]) \ + .AND_WHERE(["key", "=", "READINGS"]) \ + .GROUP_BY("key") \ + .payload() + assert _payload("data/payload_FOGL-4102.json") == json.loads(res) + def test_join_without_query(self): res = PayloadBuilder().JOIN("table1", "table1_id").ON("table2_id").payload() assert _payload("data/payload_join_without_query.json") == json.loads(res)