Skip to main content

SQL Query Reference

Eventflux uses an extended SQL syntax designed for stream processing. This reference covers all supported query constructs.

Stream Definitions

DEFINE STREAM

Create a new stream with a schema:

DEFINE STREAM StreamName (
attribute1 TYPE,
attribute2 TYPE,
...
);

Supported Types:

TypeDescriptionExample
INT32-bit integer42
LONG64-bit integer9223372036854775807
FLOAT32-bit floating point3.14
DOUBLE64-bit floating point3.14159265359
STRINGUTF-8 text'hello'
BOOLBooleantrue, false

Example:

DEFINE STREAM StockTrades (
symbol STRING,
price DOUBLE,
volume INT,
timestamp LONG
);

DEFINE TABLE

Create a table for storing and querying reference data:

DEFINE TABLE TableName (
attribute1 TYPE,
attribute2 TYPE,
...
);

Tables can be joined with streams for enrichment:

DEFINE TABLE StockInfo (
symbol STRING,
company_name STRING,
sector STRING
);

SELECT t.symbol, t.price, s.company_name
FROM Trades AS t
JOIN StockInfo AS s
ON t.symbol = s.symbol
INSERT INTO EnrichedTrades;

Basic Queries

SELECT Statement

SELECT attribute1, attribute2, expression AS alias
FROM StreamName
WHERE condition
INSERT INTO OutputStream;

Example:

SELECT symbol, price, volume * price AS total_value
FROM StockTrades
WHERE price > 100.0
INSERT INTO HighValueTrades;

SELECT with Aliases

SELECT
s.symbol,
s.price AS trade_price,
s.volume * s.price AS notional
FROM StockTrades AS s
INSERT INTO ProcessedTrades;

Operators

OperatorDescriptionExample
+Additionprice + fee
-Subtractionhigh - low
*Multiplicationprice * volume
/Divisiontotal / count
%Moduloid % 10

Expressions

Mathematical Functions

SELECT
ABS(price_change) AS abs_change,
SQRT(variance) AS std_dev,
POWER(growth_rate, 2) AS squared_growth,
ROUND(price, 2) AS rounded_price
FROM DataStream
INSERT INTO Calculations;

String Functions

SELECT
UPPER(symbol) AS upper_symbol,
LOWER(name) AS lower_name,
LENGTH(description) AS desc_length,
CONCAT(first_name, ' ', last_name) AS full_name
FROM DataStream
INSERT INTO Processed;

Conditional Expressions

SELECT
symbol,
CASE
WHEN price > 100 THEN 'HIGH'
WHEN price > 50 THEN 'MEDIUM'
ELSE 'LOW'
END AS price_tier
FROM StockTrades
INSERT INTO Categorized;

COALESCE and NULLIF

SELECT
COALESCE(nickname, name, 'Unknown') AS display_name,
NULLIF(status, 'UNKNOWN') AS valid_status
FROM Users
INSERT INTO ProcessedUsers;

INSERT INTO

All queries must specify an output destination:

-- Insert into a stream
SELECT * FROM Input INSERT INTO OutputStream;

-- The output stream is automatically created if not defined

Multiple Queries

An EventFlux application can contain multiple queries:

DEFINE STREAM RawTrades (symbol STRING, price DOUBLE, volume INT);

-- Query 1: Filter high-value trades
SELECT symbol, price, volume
FROM RawTrades
WHERE price * volume > 10000
INSERT INTO HighValueTrades;

-- Query 2: Compute statistics
SELECT symbol, AVG(price) AS avg_price
FROM RawTrades
WINDOW TUMBLING(1 min)
GROUP BY symbol
INSERT INTO TradeStats;

-- Query 3: Alert on anomalies
SELECT symbol, avg_price
FROM TradeStats
WHERE avg_price > 500
INSERT INTO PriceAlerts;

Comments

-- This is a single-line comment

/* This is a
multi-line comment */

DEFINE STREAM Input (
value INT -- inline comment
);

Best Practices

Query Design Tips
  1. Filter early - Apply WHERE clauses as close to the source as possible
  2. Use appropriate windows - Choose window type based on your use case
  3. Avoid SELECT * - Explicitly list needed columns for better performance
  4. Name your outputs - Use meaningful INSERT INTO targets for clarity

Next Steps

  • Windows - Time and count-based windowing
  • Aggregations - GROUP BY and aggregate functions
  • Joins - Stream-to-stream and stream-to-table joins
  • Patterns - Complex event pattern detection
  • Functions - Built-in function reference