Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
23 changes: 20 additions & 3 deletions C/plugins/storage/postgres/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2551,6 +2551,7 @@ bool Connection::jsonAggregates(const Value& payload,
sql.append(column_name);
sql.append("\"");
}
sql.append(") AS \"");
}
else if (itr->HasMember("json"))
{
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"count":1,"rows":[{"sum_value":"6582","count_value":439,"key":"READINGS"}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"count":1,"rows":[{"sum_value":"6582","count_value":439,"key":"READINGS"}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"count":1,"rows":[{"sum_value":"6582","count_value":439,"key":"READINGS"}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"count":1,"rows":[{"sum_value":"6582","count_value":439,"key":"READINGS"}]}
25 changes: 25 additions & 0 deletions tests/unit/C/services/storage/postgres/payloads/FOGL-4102.json
Original file line number Diff line number Diff line change
@@ -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"
}

1 change: 1 addition & 0 deletions tests/unit/C/services/storage/postgres/testset
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions tests/unit/C/services/storage/sqlite/expected_ETC_UTC/129
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"count":1,"rows":[{"sum_value":5756,"count_value":384,"key":"READINGS"}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"count":1,"rows":[{"sum_value":5756,"count_value":384,"key":"READINGS"}]}
25 changes: 25 additions & 0 deletions tests/unit/C/services/storage/sqlite/payloads/FOGL-4102.json
Original file line number Diff line number Diff line change
@@ -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"
}

1 change: 1 addition & 0 deletions tests/unit/C/services/storage/sqlite/testset
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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"
}

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down