{"id":22152139,"url":"https://github.com/findinpath/dbt_match_recognize","last_synced_at":"2026-03-19T21:58:32.231Z","repository":{"id":112790879,"uuid":"366866868","full_name":"findinpath/dbt_match_recognize","owner":"findinpath","description":"Primer for `MATCH_RECOGNIZE` SQL:2016 query","archived":false,"fork":false,"pushed_at":"2021-05-12T22:20:20.000Z","size":77,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-24T13:27:22.380Z","etag":null,"topics":["dbt","snowflake","sql2016"],"latest_commit_sha":null,"homepage":"","language":"PLSQL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/findinpath.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-12T22:14:13.000Z","updated_at":"2024-01-15T10:58:55.000Z","dependencies_parsed_at":null,"dependency_job_id":"b4688745-3356-486a-8288-5d71553e9fc5","html_url":"https://github.com/findinpath/dbt_match_recognize","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/findinpath/dbt_match_recognize","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/findinpath%2Fdbt_match_recognize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/findinpath%2Fdbt_match_recognize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/findinpath%2Fdbt_match_recognize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/findinpath%2Fdbt_match_recognize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/findinpath","download_url":"https://codeload.github.com/findinpath/dbt_match_recognize/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/findinpath%2Fdbt_match_recognize/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29854138,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-25T22:37:40.667Z","status":"online","status_checked_at":"2026-02-26T02:00:06.774Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dbt","snowflake","sql2016"],"created_at":"2024-12-02T00:47:24.611Z","updated_at":"2026-02-26T08:42:25.020Z","avatar_url":"https://github.com/findinpath.png","language":"PLSQL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Primer in using `MATCH_RECOGNIZE` in Snowflake with dbt\n\nThis project is thought as a primer in using `MATCH_RECOGNIZE` `SQL:2016` query.\n\nAs described in the Snowflake [documentation](https://docs.snowflake.com/en/sql-reference/constructs/match_recognize.html)\n`MATCH_RECOGNIZE`:\n\n\u003e Recognizes matches of a pattern in a set of rows. It accepts a set of rows \n\u003e (from a table, view, subquery, or other source) as input, and returns all matches \n\u003e for a given row pattern within this set. \n\u003e The pattern is defined similarly to a regular expression.\n\u003e The clause can return either:\n\u003e     All the rows belonging to each match. \n\u003e     One summary row per match.\n\n\nBelow can be found a teaser for `MATCH_RECOGNIZE` usage that is being employed to find\nstats about the sessions of a visitor on a fictional website: \n\n\n```sql\nselect start_ts,\n       datediff(minute, start_ts, end_ts) as minutes,\n       log_count\nfrom logs\nmatch_recognize(\n   order by visit_ts\n   measures\n        count(*) as log_count,\n        first(visit_ts) as start_ts,\n        last(visit_ts) as end_ts\n   one row per match\n   pattern ( new_session_event next_event* )\n   define\n        next_event as visit_ts \u003c dateadd(minutes, 30, lag(visit_ts))\n)\n```\n\nNOTE in the query above that by using `MATCH_RECOGNIZE` can be identified:\n\n- beginning of a session\n- duration of the session\n- number of visits made during the session\n\nRow pattern matching allows a number of use cases which were either very difficult or rather inefficient\nto implement previously in SQL.\n\n\nA detailed description of the ROw Pattern Recognition in SQL can be found on the [ISO](https://standards.iso.org/ittf/PubliclyAvailableStandards/c065143_ISO_IEC_TR_19075-5_2016.zip) website.\n\n\nThe initial inspiration in writing this primer came while browsing the webpage https://modern-sql.com/ .\nThe page [match_recognize - Regular Expression over Rows](https://modern-sql.com/feature/match_recognize) provides \na very detailed hands-on visual demonstration of the areas where `MATCH_RECOGNIZE` can be applied.\nThe associated presentation for the query `MATCH_RECOGNIZE`\ncan be found on [slideshare](https://www.slideshare.net/MarkusWinand/row-pattern-matching-in-sql2016?ref=https://modern-sql.com/).\n\nThe `MATCHED_RECOGNIZE` queries from the presentation are tailored for Oracle database.\nThis project rewrites most of the presented queries with slight modifications made in order to achieve compatibility\nwith Snowflake database.\n\nIn order to spare the user of the project of executing lots of `CREATE TABLE`, `INSERT` and `SELECT` statements,\nthis project makes use of [dbt](https://www.getdbt.com/) for seeding and filling models that use `MATCH_RECOGNIZE`\nquery.\n\n[dbt](https://www.getdbt.com/) offers also the advantage of being able to add HTML presentation for each\nof the models (you know, to remember what the internals of the model are about in a few months/years).\n\n\nThe base tables definitions on top of which are applied the `MATCH_RECOGNIZE` queries can be found under the\n[sources](./sources) directory.\n\nThe `dbt` models containing showcases of usage for `MATCH_RECOGNIZE` can be found under [marts](./models/marts) directory.\n\n## Getting started with dbt\n\nAs described in the [introduction to dbt](https://docs.getdbt.com/docs/introduction) :\n\n\u003e dbt (data build tool) enables analytics engineers to transform data in their warehouses by simply writing select statements. \n\u003e dbt handles turning these select statements into tables and views.\n  \n\u003e dbt does the T in ELT (Extract, Load, Transform) processes – it doesn't extract or load data, \n\u003e but it’s extremely good at transforming data that’s already loaded into your warehouse.\n\nThe [jaffle_shop](https://github.com/fishtown-analytics/jaffle_shop)\nproject is a useful minimum viable dbt project to get new [dbt](https://www.getdbt.com/) users \nup and running with their first dbt project. It includes [seed](https://docs.getdbt.com/docs/building-a-dbt-project/seeds)\nfiles with generated data so that a user can run this project on their own warehouse.\n\n---\nFor more information on dbt:\n\n* Read the [introduction to dbt](https://docs.getdbt.com/docs/introduction).\n* Read the [dbt viewpoint](https://docs.getdbt.com/docs/about/viewpoint).\n---\n\n## Demo\n\nUse [virtualenv](https://pypi.org/project/virtualenv/) for creating a `virtual` python environment:\n\n```bash\npip3 install virtualenv\nvirtualenv venv\nsource venv/bin/activate\n```\n\nOnce virtualenv is set, proceed to install the requirements for the project:\n\n```bash\n(venv) ➜ pip3 install -r requirements.txt\n```\n\nPlace in `~/.dbt/profiles.yml` file the following content for interacting via dbt with [Snowflake](https://www.snowflake.com/) database:\n**NOTE** be sure to change the coordinates of the database according to your Snowflake account. \n\n```\n# For more information on how to configure this file, please see:\n# https://docs.getdbt.com/docs/profile\nmatch_recognize:\n  target: dev\n  outputs:\n    dev:\n      type: snowflake\n      account: your-account.your-snowflake-region\n      port: 443\n      user: \"your-username\"\n      password: \"your-password\"\n      role: accountadmin\n      threads: 4\n      database: playground\n      warehouse: your-warehouse-name\n      schema: dbt_match_recognize\nconfig:\n  send_anonymous_usage_stats: False\n```\n\nIf everything is setup correctly, dbt can be used to seed the database with test data and also to fill the models:\n\n```bash\n(venv) ➜ dbt seed --profile match_recognize\n(venv) ➜ dbt run  --profile match_recognize\n```\n\nBy using the Snowflake Query Browser, can be easily consulted the output of each\n`MATCH_RECOGNIZE` query in the corresponding models.\n\n```sql\nSELECT * from playground.dbt_match_recognize.fct_v_shape_stock_price;\n```\n\nConsult the dbt documentation of the models by doing:\n\n```bash\n(venv) ➜ dbt docs generate\n(venv) ➜ dbt docs serve\n```\n\nand visit http://localhost:8080\n\n\n\n\nOnce the demo session is over, make sure to deactivate the Python virtual environment\n\n```bash\n(venv) ➜ deactivate\n```\n\n\n## Conclusion\n\nThe `MATCH_RECOGNIZE` is a wonderful addition to the SQL:2016 standard.\nBy using this query, there can be avoided to some extent whole [Apache Spark](https://spark.apache.org/)\nbatch jobs.\n\nFeel free to provide feedback or alternative implementations to any of the topics presented in this project.\n`MATCH_RECOGNIZE` applies to a truly wide array of problems. In case you have already solved an interesting\nproblem with this query, feel free to create an issue containing the SQL statement or even a PR in\norder to enrich the array of examples presented in this primer.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffindinpath%2Fdbt_match_recognize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffindinpath%2Fdbt_match_recognize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffindinpath%2Fdbt_match_recognize/lists"}