Databricks SQL 新增 MATCH_RECOGNIZE 序列检测
DataHot 速览
Databricks 官方博客介绍,其计算引擎(包括 Lakehouse Real-Time)现已支持 SQL 的 MATCH_RECOGNIZE,可用类似正则表达式的方式匹配行序列。文章以网络安全登录日志为例:若要在大量失败尝试中识别“5 次或更多失败后立即成功”等可疑模式,传统 SQL 计数和窗口函数难以锚定首个失败并隔离后续成功。MATCH_RECOGNIZE 可直接描述关心的事件序列,避免依赖 gaps-and-islands 的多层 CTE 复杂查询。该能力适用于跨行业的事件序列检测与异常监测。
为什么值得关注:数据从业者可了解 Databricks 对 SQL 序列模式匹配的原生支持,以及如何用 MATCH_RECOGNIZE 替代复杂 CTE 处理登录异常等事件序列分析。
原文
Imagine you work in cybersecurity and you have a table that tracks login attempts. This table includes each login attempt as a success or failure and when the attempt took place. You want to find strange login patterns, so you might ask the question, “which users had consecutive login failures, followed by success?” Finding this type of suspicious activity with standard SQL is challenging. SQL treats rows as unordered sets of facts without a timeline and there is no inherent concept of a sequence of events.
You could count failed logins per user, but counts won’t help you understand if these login attempts were a narrow timespan or spread out over a month; and it can’t tell you if a successful login occurred right after the failures. To do that in SQL, you will end up with a complex query that chains multiple common table expressions together, anchoring the time window to the first failure, then checking each subsequent row.
MATCH_RECOGNIZE simplifies this. Now available in Databricks compute (including Lakehouse Real-Time), MATCH_RECOGNIZE lets you describe the sequence you care about directly, like a regular expression for rows. One SQL clause now handles the pattern matching and you’ve eliminated overly complicated SQL reliant on “gaps and islands” logic.
Let’s look at industry-specific examples of how MATCH_RECOGNIZE makes sequence detection simple across different industries.
Cybersecurity: Identifying suspicious log-in anomalies

If you’re searching for credential stuffing in authorization logs, simply counting login attempts can lead to false positives. You specifically need to detect high-frequency spikes, such as 5 or more failed login attempts within a specific narrow time-window, immediately followed by a successful login.
Standard COUNT() OVER (PARTITION BY user_id ORDER BY event_time) window functions can tell you how many failures occurred in a time frame, but they cannot easily anchor a sliding time window to the first failure in a specific sequence, nor can they cleanly isolate the sequence once a success occurs.
With MATCH_RECOGNIZE, you can use FIRST(FAIL.event_time) directly inside the DEFINE block to anchor the timestamp of the initial failed attempt. Every subsequent FAIL event is dynamically checked to ensure it falls within 1 hour of that first attempt before transitioning to the SUCCESS state.
Financial Analysis: Detecting v-shaped stock trends

Every market data analyst cares about price reversals, moments when a stock loses value, then suddenly starts gaining it back. This shape is known as a "V-shape," and finding it in standard SQL means reaching for a technique called "gaps and islands": since SQL has no native idea of a trend, you first have to manually carve your rows into "islands" (consecutive stretches where the price is moving in the same direction) before you can even ask where a V-shape starts and ends.
In practice, that means using LAG and LEAD to compare each row to its neighbors, building a running counter that increments every time the direction flips (so you have a group ID for each island), and then writing HAVING filters to confirm each island's shape and boundaries. It's a lot of scaffolding just to answer a simple question: "where did the price dip and recover?"
The MATCH_RECOGNIZE clause eliminates the need for this scaffolding. You simply partition the data by symbol, order it by time, and define the shape of the V-trend as a sequence of regex-like states.
E-Commerce: Detecting check-out abandonment

Product managers want to find users with high-intent, but who never complete the purchase. Users who demonstrate real purchase intent, but then go silent, is a valuable signal. Identifying this set of users can help: determine which users to send a reminder, easily measure the opportunity and what percentage is recoverable with follow-up actions, and as a point of comparison with other users in this cohort, discovering a new insight (like a certain product is priced too high). A high-value failed conversion funnel tracks users who:
- Viewed a product page two or more times (VIEW 2 or more times, indicating high interest)
- Added the item to their cart (ADD_TO_CART)
- Ultimately abandoned the session (using a time-filter)
The last step is not based on a value, but a time-range based on user activity. There is no “abandon” row to match or “check out error”, the user simply stops. In traditional SQL you prove a negative with NOT EXISTS subqueries, self-joins, and window functions to show that nothing happened after the items were added to the cart, and enough idle time had passed to consider the cart abandoned.
MATCH_RECOGNIZE expresses "nothing happened after this" directly with the end-of-partition anchor $, which forces the cart add to be the last recorded event in the session. Add a time filter for the idle window and you have a timeout-based abandonment rule with no self-joins.
Manufacturing / IoT: Predicting equipment failures from sensor data

Predictive maintenance depends on spotting trends and patterns. With any machine in-use, its internal temperature tends to increase, but a sequence of steady temperature increase, followed by a vibration spike could signal a pending failure.
Traditional SQL requires rolling row-by-row comparisons to continuously attempt to detect a dangerous trend. MATCH_RECOGNIZE handles row-by-row logic natively. Inside the DEFINE clause you can use PREV and NEXT functions (which act similar to LAG and LEAD). This means setting up a rising temperature rule is as simple as writing temperature > PREV(temperature).
Try Match Recognize on Lakehouse today
It's now easier than ever to uncover data patterns and simplify event-sequence analytics. MATCH_RECOGNIZE allows you to write less code for pattern matching in a logical way. This clause is easier to validate, easier to maintain, and straightforward to update.
- Explore the Documentation: Dive into the official SQL reference documentation to learn more about advanced pattern syntax, quantifiers, and measure aggregates.
- Try It in Your Workspace: Test out the examples above on your own log streams, clickstream sessions, or time-series telemetry in Databricks SQL or Lakehouse//RT.
- Migrate Legacy Pipelines: Identify your most complex window-function and self-join CTEs and let Genie Code assist in rewriting them with simpler MATCH_RECOGNIZE queries.
The best data warehouse is a Lakehouse. Our native capabilities continue to expand and allow you to do more powerful analytics on a single, unified platform.
这篇内容对你有用吗?
反馈只用于改善内容筛选,不等同于收藏