Skip to content

SAMPLE BY query gives Aggregate function cannot be passed as an argument error #6549

@puzpuzpuz

Description

@puzpuzpuz

To reproduce

On demo the following query

SELECT
  timestamp, symbol,
  first(price),
  last(price),
  CASE
   when symbol = 'BTC-USD' THEN first(price)
   else last(price)
  END
FROM (trades where timestamp in today())
SAMPLE BY 1h
ALIGN TO CALENDAR WITH OFFSET '-00:15';

gives Aggregate function cannot be passed as an argument. We should improve virtual model rewrite to extract expressions like the above CASE into an outer virtual model.

There is a similar limitation in WINDOW JOIN:

SELECT
    t.symbol,
    t.timestamp,
    t.side,
    t.price AS exec_price,
    avg(p.bid_price) AS avg_bid,
    avg(p.ask_price) AS avg_ask,
    avg(p.ask_price - p.bid_price) AS avg_spread,
    -- Slippage: did you pay more than ask (buy) or receive less than bid (sell)?
    CASE 
        WHEN t.side = 'buy' THEN t.price - avg(p.ask_price)
        ELSE avg(p.bid_price) - t.price
    END AS slippage
FROM trades t
WINDOW JOIN prices p
    ON (t.symbol = p.symbol)
    RANGE BETWEEN 5 minutes PRECEDING AND 0 seconds PRECEDING
    EXCLUDE PREVAILING
ORDER BY t.timestamp;

gives WINDOW join cannot reference right table non-aggregate column: p.bid_price.

QuestDB version:

latest master

OS, in case of Docker specify Docker and the Host OS:

Linux

File System, in case of Docker specify Host File System:

ext4

Full Name:

Andrei Pechkurov

Affiliation:

QuestDB

Have you followed Linux, MacOs kernel configuration steps to increase Maximum open files and Maximum virtual memory areas limit?

  • Yes, I have

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementEnhance existing functionalitySQLIssues or changes relating to SQL execution

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions