@@ -1784,4 +1784,265 @@ var AggregationTests = []testdata.AggregationTestCase{
17841784 WHERE "aggr__0__order_1_rank"<=13
17851785 ORDER BY "aggr__0__order_1_rank" ASC, "aggr__0__1__order_1_rank" ASC` ,
17861786 },
1787+ { // [9]
1788+ TestName : "Terms with order by top metrics" ,
1789+ QueryRequestJson : `
1790+ {
1791+ "aggs": {
1792+ "timeseries": {
1793+ "aggs": {
1794+ "badccb5b-5077-4bc9-9cbb-8a13abb0e33f": {
1795+ "aggs": {
1796+ "docs": {
1797+ "top_hits": {
1798+ "fields": [
1799+ "DistanceMiles"
1800+ ],
1801+ "size": 1
1802+ }
1803+ }
1804+ },
1805+ "filter": {
1806+ "exists": {
1807+ "field": "message"
1808+ }
1809+ }
1810+ }
1811+ },
1812+ "date_histogram": {
1813+ "extended_bounds": {
1814+ "max": 1731580657318,
1815+ "min": 1731526657318
1816+ },
1817+ "field": "timestamp",
1818+ "fixed_interval": "600s",
1819+ "min_doc_count": 0,
1820+ "time_zone": "Europe/Warsaw"
1821+ },
1822+ "meta": {
1823+ "dataViewId": "d3d7af60-4c81-11e8-b3d7-01146121b73d",
1824+ "indexPatternString": "kibana_sample_data_flights",
1825+ "intervalString": "600s",
1826+ "normalized": true,
1827+ "panelId": "a7039566-0e44-48bb-8677-ff54cc3d8d20",
1828+ "seriesId": "4dd4251c-43c2-4e53-b07a-e7562564c70e",
1829+ "timeField": "timestamp"
1830+ }
1831+ }
1832+ },
1833+ "query": {
1834+ "bool": {
1835+ "filter": [],
1836+ "must": [
1837+ {
1838+ "range": {
1839+ "timestamp": {
1840+ "format": "strict_date_optional_time",
1841+ "gte": "2024-11-13T19:37:37.318Z",
1842+ "lte": "2024-11-14T10:37:37.318Z"
1843+ }
1844+ }
1845+ }
1846+ ],
1847+ "must_not": [],
1848+ "should": []
1849+ }
1850+ },
1851+ "runtime_mappings": {
1852+ "hour_of_day": {
1853+ "script": {
1854+ "source": "emit(doc['timestamp'].value.getHour());"
1855+ },
1856+ "type": "long"
1857+ }
1858+ },
1859+ "size": 0,
1860+ "timeout": "30000ms",
1861+ "track_total_hits": true
1862+ }` ,
1863+ ExpectedResponse : `
1864+ {
1865+ "completion_time_in_millis": 1726937127128,
1866+ "expiration_time_in_millis": 1726937187124,
1867+ "id": "Fm9QLU5BRXFoVEwyQk1WWC1XalJ1R2cccjdQX1ljN3hSYktWdjNya1RCY3BSdzoxMjM5Mg==",
1868+ "is_partial": false,
1869+ "is_running": false,
1870+ "response": {
1871+ "_shards": {
1872+ "failed": 0,
1873+ "skipped": 0,
1874+ "successful": 1,
1875+ "total": 1
1876+ },
1877+ "aggregations": {
1878+ "other-filter": {
1879+ "buckets": {
1880+ "": {
1881+ "1": {
1882+ "buckets": [
1883+ {
1884+ "2-bucket": {
1885+ "2-metric": {
1886+ "top": [
1887+ {
1888+ "metrics": {
1889+ "DistanceKilometers": 8619.34375
1890+ },
1891+ "sort": [
1892+ "2024-09-09T09:56:35.000Z"
1893+ ]
1894+ }
1895+ ]
1896+ },
1897+ "doc_count": 140
1898+ },
1899+ "doc_count": 140,
1900+ "key": 1725832800000,
1901+ "key_as_string": "2024-09-09T00:00:00.000+02:00"
1902+ },
1903+ {
1904+ "2-bucket": {
1905+ "2-metric": {
1906+ "top": [
1907+ {
1908+ "metrics": {
1909+ "DistanceKilometers": 11549.353515625
1910+ },
1911+ "sort": [
1912+ "2024-09-09T21:58:01.000Z"
1913+ ]
1914+ }
1915+ ]
1916+ },
1917+ "doc_count": 178
1918+ },
1919+ "doc_count": 178,
1920+ "key": 1725876000000,
1921+ "key_as_string": "2024-09-09T12:00:00.000+02:00"
1922+ },
1923+ {
1924+ "2-bucket": {
1925+ "2-metric": {
1926+ "top": [
1927+ {
1928+ "metrics": {
1929+ "DistanceKilometers": 10641.537109375
1930+ },
1931+ "sort": [
1932+ "2024-09-10T09:59:52.000Z"
1933+ ]
1934+ }
1935+ ]
1936+ },
1937+ "doc_count": 146
1938+ },
1939+ "doc_count": 146,
1940+ "key": 1725919200000,
1941+ "key_as_string": "2024-09-10T00:00:00.000+02:00"
1942+ }
1943+ ]
1944+ },
1945+ "2-bucket": {
1946+ "2-metric": {
1947+ "top": [
1948+ {
1949+ "metrics": {
1950+ "DistanceKilometers": 11077.248046875
1951+ },
1952+ "sort": [
1953+ "2024-09-21T16:42:22.000Z"
1954+ ]
1955+ }
1956+ ]
1957+ },
1958+ "doc_count": 4032
1959+ },
1960+ "doc_count": 4032
1961+ }
1962+ }
1963+ }
1964+ },
1965+ "hits": {
1966+ "hits": [],
1967+ "max_score": null,
1968+ "total": {
1969+ "relation": "eq",
1970+ "value": 4044
1971+ }
1972+ },
1973+ "timed_out": false,
1974+ "took": 4
1975+ },
1976+ "start_time_in_millis": 1726937127124
1977+ }` ,
1978+ ExpectedPancakeResults : []model.QueryResultRow { // incorrect
1979+ {Cols : []model.QueryResultCol {
1980+ model .NewQueryResultCol ("aggr__0__key_0" , int64 (1716834210000 / 30000 )),
1981+ model .NewQueryResultCol ("aggr__0__count" , 4 ),
1982+ model .NewQueryResultCol ("aggr__0__1__parent_count" , uint64 (4 )),
1983+ model .NewQueryResultCol ("aggr__0__1__key_0" , "artemis" ),
1984+ model .NewQueryResultCol ("aggr__0__1__key_1" , "error" ),
1985+ model .NewQueryResultCol ("aggr__0__1__count" , 1 ),
1986+ }},
1987+ {Cols : []model.QueryResultCol {
1988+ model .NewQueryResultCol ("aggr__0__key_0" , int64 (1716834210000 / 30000 )),
1989+ model .NewQueryResultCol ("aggr__0__count" , 4 ),
1990+ model .NewQueryResultCol ("aggr__0__1__parent_count" , uint64 (4 )),
1991+ model .NewQueryResultCol ("aggr__0__1__key_0" , "artemis" ),
1992+ model .NewQueryResultCol ("aggr__0__1__key_1" , "info" ),
1993+ model .NewQueryResultCol ("aggr__0__1__count" , 1 ),
1994+ }},
1995+ {Cols : []model.QueryResultCol {
1996+ model .NewQueryResultCol ("aggr__0__key_0" , int64 (1716834210000 / 30000 )),
1997+ model .NewQueryResultCol ("aggr__0__count" , 4 ),
1998+ model .NewQueryResultCol ("aggr__0__1__parent_count" , uint64 (4 )),
1999+ model .NewQueryResultCol ("aggr__0__1__key_0" , "jupiter" ),
2000+ model .NewQueryResultCol ("aggr__0__1__key_1" , "info" ),
2001+ model .NewQueryResultCol ("aggr__0__1__count" , 1 ),
2002+ }},
2003+ {Cols : []model.QueryResultCol {
2004+ model .NewQueryResultCol ("aggr__0__key_0" , int64 (1716834270000 / 30000 )),
2005+ model .NewQueryResultCol ("aggr__0__count" , 16 ),
2006+ model .NewQueryResultCol ("aggr__0__1__parent_count" , uint64 (15 )),
2007+ model .NewQueryResultCol ("aggr__0__1__key_0" , "apollo" ),
2008+ model .NewQueryResultCol ("aggr__0__1__key_1" , "info" ),
2009+ model .NewQueryResultCol ("aggr__0__1__count" , 2 ),
2010+ }},
2011+ {Cols : []model.QueryResultCol {
2012+ model .NewQueryResultCol ("aggr__0__key_0" , int64 (1716834270000 / 30000 )),
2013+ model .NewQueryResultCol ("aggr__0__count" , 16 ),
2014+ model .NewQueryResultCol ("aggr__0__1__parent_count" , uint64 (15 )),
2015+ model .NewQueryResultCol ("aggr__0__1__key_0" , "cassandra" ),
2016+ model .NewQueryResultCol ("aggr__0__1__key_1" , "debug" ),
2017+ model .NewQueryResultCol ("aggr__0__1__count" , 1 ),
2018+ }},
2019+ },
2020+ ExpectedPancakeSQL : `
2021+ SELECT "aggr__0__parent_count", "aggr__0__key_0", "aggr__0__count",
2022+ "aggr__0__order_1", "aggr__0__1__key_0", "aggr__0__1__count",
2023+ "aggr__0__1__2-bucket__count"
2024+ FROM (
2025+ SELECT "aggr__0__parent_count", "aggr__0__key_0", "aggr__0__count",
2026+ "aggr__0__order_1", "aggr__0__1__key_0", "aggr__0__1__count",
2027+ "aggr__0__1__2-bucket__count",
2028+ dense_rank() OVER (ORDER BY "aggr__0__order_1" DESC, "aggr__0__key_0" ASC)
2029+ AS "aggr__0__order_1_rank",
2030+ dense_rank() OVER (PARTITION BY "aggr__0__key_0" ORDER BY
2031+ "aggr__0__1__key_0" ASC) AS "aggr__0__1__order_1_rank"
2032+ FROM (
2033+ SELECT sum(count(*)) OVER () AS "aggr__0__parent_count",
2034+ "AvgTicketPrice" AS "aggr__0__key_0",
2035+ sum(count(*)) OVER (PARTITION BY "aggr__0__key_0") AS "aggr__0__count",
2036+ "top_metrics__0__2-bucket__2-metric_col_0" AS "aggr__0__order_1",
2037+ toInt64((toUnixTimestamp64Milli("timestamp")+timeZoneOffset(toTimezone(
2038+ "timestamp", 'Europe/Warsaw'))*1000) / 43200000) AS "aggr__0__1__key_0",
2039+ count(*) AS "aggr__0__1__count",
2040+ countIf("bytes_gauge" IS NOT NULL) AS "aggr__0__1__2-bucket__count"
2041+ FROM __quesma_table_name
2042+ GROUP BY "AvgTicketPrice" AS "aggr__0__key_0",
2043+ toInt64((toUnixTimestamp64Milli("timestamp")+timeZoneOffset(toTimezone(
2044+ "timestamp", 'Europe/Warsaw'))*1000) / 43200000) AS "aggr__0__1__key_0"))
2045+ WHERE "aggr__0__order_1_rank"<=13
2046+ ORDER BY "aggr__0__order_1_rank" ASC, "aggr__0__1__order_1_rank" ASC` ,
2047+ },
17872048}
0 commit comments