Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(iot): evaluate the topic rule SELECT clause (#1252)

maximoosemine57ad1b681193
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 47833ab0..08f2d7c4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,9 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
+### Fixed
+- **IoT — topic-rule SQL is now evaluated** — the rules engine routed a publish to its actions but ignored the rule's `SELECT` clause, so a rule declaring `SELECT deviceId AS id FROM 'sensors/+/telemetry'` delivered the whole message instead of the projection. The SELECT clause is now parsed and projected — `*`, attribute paths, `AS` aliases, `topic()` / `topic(n)`, `timestamp()`, and literals, with unaliased items named as AWS names them and missing attributes omitted — for both delivery paths, Basic Ingest (where `topic()` reports the topic after the rule prefix) and a publish matching the `FROM` filter. A JSON payload under `SELECT *` still arrives as the parsed object.
+
## [1.4.9] — 2026-08-01
### Added
diff --git a/ministack/services/iot.py b/ministack/services/iot.py
index 6b7780b3..bc6595d8 100644
--- a/ministack/services/iot.py
+++ b/ministack/services/iot.py
@@ -1372,6 +1372,121 @@ def _rule_topic_filter(sql: str) -> str:
return m.group(1) if m else ""
+_MISSING = object()
+
+_SELECT_CLAUSE_RE = re.compile(r"\bSELECT\s+(.*?)\s+FROM\s+'", re.IGNORECASE | re.DOTALL)
+_SELECT_ALIAS_RE = re.compile(
+ r"^(?P<expr>.+?)\s+AS\s+(?P<alias>[A-Za-z_][\w.]*)$", re.IGNORECASE | re.DOTALL
+)
+_SELECT_FUNC_RE = re.compile(r"^(?P<name>[A-Za-z_]\w*)\s*\((?P<args>.*)\)$", re.DOTALL)
+_SELECT_ATTR_RE = re.compile(r"^[A-Za-z_][\w]*(\.[A-Za-z_][\w]*)*$")
+
+
+def _rule_select_clause(sql: str) -> str:
+ """Extract the SELECT clause of a rule SQL statement."""
+ m = _SELECT_CLAUSE_RE.search(sql or "")
+ return m.group(1).strip() if m else "*"
+
+
+def _split_select_items(clause: str) -> list[str]:
+ """Split a SELECT (or function-argument) list on its top-level commas."""
+ items: list[str] = []
+ current: list[str] = []
+ depth = 0
+ quoted = False
+ for ch in clause:
+ if quoted:
+ current.append(ch)
+ if ch == "'":
+ quoted = False
+ elif ch == "'":
+ quoted = True
+ current.append(ch)
+ elif ch == "(":
+ depth += 1
+ current.append(ch)
+ elif ch == ")":
+ depth -= 1
+ current.append(ch)
+ elif ch == "," and depth == 0:
+ items.append("".join(current).strip())
+ current = []
+ else:
+ current.append(ch)
+ tail = "".join(current).strip()
+ if tail:
+ items.append(tail)
+ return [i for i in items if i]
+
+
+def _split_select_alias(item: str) -> tuple[str, str | None]:
+ m = _SELECT_ALIAS_RE.match(item.strip())
+ if m:
+ return m.group("expr").strip(), m.group("alias")
+ return item.strip(), None
+
+
+def _select_default_key(expr: str) -> str:
+ """Name an unaliased SELECT item the way AWS does — the trailing attribute
+ segment, or the function name for a function call."""
+ m = _SELECT_FUNC_RE.match(expr)
+ if m:
+ return m.group("name")
+ return expr.rsplit(".", 1)[-1]
+
+
+def _resolve_attribute(message, path: str):
+ value = message
+ for segment in path.split("."):
+ if not isinstance(value, dict) or segment not in value:
+ return _MISSING
+ value = value[segment]
+ return value
+
+
+def _eval_select_function(name: str, args: list[str], topic: str, message):
+ if name == "topic":
+ if not args:
+ return topic
+ try:
+ index = int(args[0].strip())
+ except ValueError:
+ return _MISSING
+ segments = topic.split("/")
+ if index < 1 or index > len(segments):
+ return _MISSING
+ return segments[index - 1]
+ if name == "timestamp" and not args:
+ return int(time.time() * 1000)
+ return _MISSING
+
+
+def _eval_select_expr(expr: str, topic: str, message):
+ expr = expr.strip()
+ if expr == "*":
+ return message
+ if len(expr) >= 2 and expr[0] == "'" and expr[-1] == "'":
+ return expr[1:-1]
+ m = _SELECT_FUNC_RE.match(expr)
+ if m:
+ return _eval_select_function(
+ m.group("name").lower(),
+ _split_select_items(m.group("args")),
+ topic,
+ message,
+ )
+ if _SELECT_ATTR_RE.match(expr):
+ return _resolve_attribute(message, expr)
+ try:
+ return int(expr)
+ except ValueError:
+ pass
+ try:
+ return float(expr)
+ except ValueError:
+ return _MISSING
+
+
def put_topic_rule(name: str, payload: dict, *, created_at: float | None = None) -> dict:
"""Store a topic rule from an API-shape (camelCase) ``TopicRulePayload``."""
rule = {
@@ -1807,14 +1922,39 @@ def _rules_for_account(account_id: str, region: str) -> list[dict]:
]
-def _rule_event(payload: bytes):
- """Decode a publish payload into a rule event (JSON, else raw text)."""
+def _rule_message(payload: bytes):
+ """Decode a publish payload into the message the SELECT clause reads."""
try:
return json.loads(payload.decode("utf-8"))
except (UnicodeDecodeError, json.JSONDecodeError):
return (payload or b"").decode("utf-8", "replace")
+def _rule_event(sql: str, topic: str, payload: bytes):
+ """Project a publish payload through a rule's SELECT clause."""
+ payload = payload or b""
+ message = _rule_message(payload)
+ items = _split_select_items(_rule_select_clause(sql)) or ["*"]
+
+ if len(items) == 1:
+ expr, alias = _split_select_alias(items[0])
+ if expr == "*" and alias is None:
+ return message
+
+ event: dict = {}
+ for item in items:
+ expr, alias = _split_select_alias(item)
+ value = _eval_select_expr(expr, topic, message)
+ if value is _MISSING:
+ continue
+ if expr == "*" and alias is None:
+ if isinstance(value, dict):
+ event.update(value)
+ continue
+ event[alias or _select_default_key(expr)] = value
+ return event
+
+
def _dispatch_rule_to_lambda(
account_id: str, region: str, function_arn: str, event
) -> None:
@@ -1834,10 +1974,12 @@ def _dispatch_rule_to_lambda(
).start()
-def _run_rule_actions(account_id: str, region: str, rule: dict, payload: bytes) -> None:
+def _run_rule_actions(
+ account_id: str, region: str, rule: dict, payload: bytes, topic: str = ""
+) -> None:
if not rule or rule.get("ruleDisabled"):
return
- event = _rule_event(payload)
+ event = _rule_event(rule.get("sql", ""), topic, payload)
for action in rule.get("actions", []) or []:
lam = action.get("lambda")
if lam and lam.get("functionArn"):
@@ -1850,7 +1992,7 @@ def _evaluate_topic_rules(
for rule in _rules_for_account(account_id, region):
filter_ = _rule_topic_filter(rule.get("sql", ""))
if filter_ and _topic_matches(filter_, topic):
- _run_rule_actions(account_id, region, rule, payload)
+ _run_rule_actions(account_id, region, rule, payload, topic)
async def broker_publish(
@@ -1864,12 +2006,16 @@ async def broker_publish(
# Basic Ingest: a publish to `$aws/rules/<ruleName>` is delivered straight
# to that rule's actions and bypasses pub/sub delivery entirely.
if topic.startswith(_BASIC_INGEST_PREFIX):
- rule_name = topic[len(_BASIC_INGEST_PREFIX):].split("/", 1)[0]
+ remainder = topic[len(_BASIC_INGEST_PREFIX):].split("/", 1)
+ rule_name = remainder[0]
_run_rule_actions(
account_id,
region,
_topic_rules.get_scoped(account_id, region, rule_name),
payload,
+ # Under Basic Ingest `topic()` reports the topic after the
+ # `$aws/rules/<ruleName>/` prefix, as it does on AWS.
+ remainder[1] if len(remainder) > 1 else "",
)
return
diff --git a/tests/test_iot.py b/tests/test_iot.py
index 14e3ba9b..cda69acc 100644
--- a/tests/test_iot.py
+++ b/tests/test_iot.py
@@ -958,7 +958,9 @@ def test_iot_topic_rules_and_basic_ingest_use_publish_region(monkeypatch):
)
dispatched = []
- def _capture_rule_action(dispatched_account_id, dispatched_region, rule, payload):
+ def _capture_rule_action(
+ dispatched_account_id, dispatched_region, rule, payload, topic=""
+ ):
dispatched.append((dispatched_account_id, dispatched_region, rule, payload))
monkeypatch.setattr(
@@ -2362,3 +2364,44 @@ def test_puback_for_first_of_multiple_in_flight():
asyncio.run(_run())
reset()
+
+
+# ----------------------------------------------------------------------
+# Rule SQL SELECT projection (white-box tests for _rule_event).
+# ----------------------------------------------------------------------
+
+def test_rule_event_select_star_returns_parsed_json():
+ from ministack.services.iot import _rule_event
+
+ event = _rule_event("SELECT * FROM 'telemetry'", "telemetry", b'{"temp": 22}')
+ assert event == {"temp": 22}
+
+
+def test_rule_event_projects_attributes_with_aliases():
+ from ministack.services.iot import _rule_event
+
+ event = _rule_event(
+ "SELECT deviceId AS id, state.temp AS temp, topic(2) AS device "
+ "FROM 'sensors/+/telemetry'",
+ "sensors/a1/telemetry",
+ json.dumps({"deviceId": "d1", "state": {"temp": 22}}).encode(),
+ )
+ assert event == {"id": "d1", "temp": 22, "device": "a1"}
+
+
+def test_rule_event_omits_missing_attributes():
+ from ministack.services.iot import _rule_event
+
+ event = _rule_event(
+ "SELECT deviceId, absent FROM 'telemetry'", "telemetry", b'{"deviceId": "d1"}'
+ )
+ assert event == {"deviceId": "d1"}
+
+
+def test_rule_event_aliased_star_nests_the_message():
+ from ministack.services.iot import _rule_event
+
+ event = _rule_event(
+ "SELECT * AS payload FROM 'telemetry'", "telemetry", b'{"temp": 22}'
+ )
+ assert event == {"payload": {"temp": 22}}
diff --git a/tests/test_iot_data.py b/tests/test_iot_data.py
index 17283324..a0fdcfd9 100644
--- a/tests/test_iot_data.py
+++ b/tests/test_iot_data.py
@@ -722,3 +722,25 @@ def test_iot_disabled_rule_does_not_fire(iot_client, iot_data_client, lam, sqs):
assert _poll_sink(sqs, sink, timeout=4) is None
iot_client.delete_topic_rule(ruleName=rule)
+
+
+def test_iot_rule_attribute_projection(iot_client, iot_data_client, lam, sqs):
+ sink = sqs.create_queue(QueueName=_unique("project-sink"))["QueueUrl"]
+ fn_arn = _make_sink_lambda(lam, sink)
+ rule = _unique("project").replace("-", "_")
+ iot_client.create_topic_rule(
+ ruleName=rule,
+ topicRulePayload={
+ "sql": "SELECT deviceId AS id, topic(2) AS device "
+ "FROM 'sensors/+/telemetry'",
+ "actions": [{"lambda": {"functionArn": fn_arn}}],
+ },
+ )
+
+ iot_data_client.publish(
+ topic="sensors/a1/telemetry",
+ payload=json.dumps({"deviceId": "d1", "temp": 22}).encode(),
+ )
+ assert _poll_sink(sqs, sink) == {"id": "d1", "device": "a1"}
+
+ iot_client.delete_topic_rule(ruleName=rule)