Repository navigation
Description
What happened?
I tried my hand at converting the "heinous SQL CASE statement" at https://github.com/mattermost/mattermost-data-warehouse/blob/577c4e85adbe03e50bc989612a65120efda62823/transform/sql/snowflake/automated_nps_feedback_category_update.sql to PRQL.
There are a couple of problems with what I've done so far. For a start I think my string quoting with the r-strings isn't correct but let's set that aside for the moment (because I just started refactoring this and copied the strings "as-is" from the original SQL).
What I'm concerned about is the error below that I'm seeing in the playground. In other languages it is usual to be able to continue to current line without a line continuation character if you are inside an expression as would be after a && or || or when inside parentheses () as is the case here.
These expressions are already very hard to follow, forcing them onto a single line would make them really unreadable.
PRQL input
let prefix = r"""(^| |\\.|\;|,|:|\n|\t|\-|\"|\'|\/)"""
let suffix = r"""(^|\\.|\;|,| |\/|\n|\t|\-|\"|\'|\\?|\!|$)"""
let suffix2 = r"""(s|^|\\.|\;|,| |\/|\n|\t|\-|\"|\'|\\?|\!|$)"""
let matches = func term _prefix:f"{prefix}" _suffix:f"{suffix}" input -> ((lower input) ~= f"{_prefix}{term}{_suffix}") != null
from n1=analytics.mattermost.nps_user_daily_score
join n2=analytics.mattermost.nps_feedback_classification (n1.user_id == n2.user_id && n1.server_id == n2.server_id && n1.last_feedback_date == n2.last_feedback_date)
derive subcategory = case {
(n1.feedback | matches "oaut(h|h2|hs|hentication)") => 'Account Authorization',
(n1.feedback | matches "password" _suffix:f"{suffix2}") => 'Account Authorization',
(n1.feedback | matches "authenticat(e|ion|ed|es|ed|ing|or|er)") => 'Account Authorization',
(n1.feedback | matches "(sso|single sign on|single-sign-on|single signon|single sign-on|single-sign on)") => 'Account Authorization',
(n1.feedback | matches "(quit|stop) (bugging|annoying|spamming|surveying) me" _suffix:f"{suffix2}") => 'Invalid',
( (n1.feedback | matches "mobile" _suffix:f"{suffix2}") ||
(n1.feedback | matches "(i-p|ip|p)hone" _suffix:f"{suffix2}") ||
(n1.feedback | matches "ios" _suffix:f"{suffix2}") ||
(n1.feedback | matches "android" _suffix:f"{suffix2}")
) => 'Mobile Stability',
}SQL output
Expected SQL output
SELECT
n1.*,
n2.*,
CASE
WHEN REGEXP(
LOWER(n1.feedback),
CONCAT(
'(^| |\\.|\;|,|:|\n|\t|\-|\"|\'|\/)',
'oaut(h|h2|hs|hentication)',
'(^|\\.|\;|,| |\/|\n|\t|\-|\"|\'|\\?|\!|$)'
)
) IS NOT NULL THEN 'Account Authorization'
WHEN REGEXP(
LOWER(n1.feedback),
CONCAT(
'(^| |\\.|\;|,|:|\n|\t|\-|\"|\'|\/)',
'password',
'(s|^|\\.|\;|,| |\/|\n|\t|\-|\"|\'|\\?|\!|$)'
)
) IS NOT NULL THEN 'Account Authorization'
WHEN REGEXP(
LOWER(n1.feedback),
CONCAT(
'(^| |\\.|\;|,|:|\n|\t|\-|\"|\'|\/)',
'authenticat(e|ion|ed|es|ed|ing|or|er)',
'(^|\\.|\;|,| |\/|\n|\t|\-|\"|\'|\\?|\!|$)'
)
) IS NOT NULL THEN 'Account Authorization'
WHEN REGEXP(
LOWER(n1.feedback),
CONCAT(
'(^| |\\.|\;|,|:|\n|\t|\-|\"|\'|\/)',
'(sso|single sign on|single-sign-on|single signon|single sign-on|single-sign on)',
'(^|\\.|\;|,| |\/|\n|\t|\-|\"|\'|\\?|\!|$)'
)
) IS NOT NULL THEN 'Account Authorization'
WHEN REGEXP(
LOWER(n1.feedback),
CONCAT(
'(^| |\\.|\;|,|:|\n|\t|\-|\"|\'|\/)',
'(quit|stop) (bugging|annoying|spamming|surveying) me',
'(s|^|\\.|\;|,| |\/|\n|\t|\-|\"|\'|\\?|\!|$)'
)
) IS NOT NULL THEN 'Invalid'
WHEN REGEXP(
LOWER(n1.feedback),
CONCAT(
'(^| |\\.|\;|,|:|\n|\t|\-|\"|\'|\/)',
'mobile',
'(s|^|\\.|\;|,| |\/|\n|\t|\-|\"|\'|\\?|\!|$)'
)
) IS NOT NULL
OR REGEXP(
LOWER(n1.feedback),
CONCAT(
'(^| |\\.|\;|,|:|\n|\t|\-|\"|\'|\/)',
'(i-p|ip|p)hone',
'(s|^|\\.|\;|,| |\/|\n|\t|\-|\"|\'|\\?|\!|$)'
)
) IS NOT NULL
OR REGEXP(
LOWER(n1.feedback),
CONCAT(
'(^| |\\.|\;|,|:|\n|\t|\-|\"|\'|\/)',
'ios',
'(s|^|\\.|\;|,| |\/|\n|\t|\-|\"|\'|\\?|\!|$)'
)
) IS NOT NULL
OR REGEXP(
LOWER(n1.feedback),
CONCAT(
'(^| |\\.|\;|,|:|\n|\t|\-|\"|\'|\/)',
'android',
'(s|^|\\.|\;|,| |\/|\n|\t|\-|\"|\'|\\?|\!|$)'
)
) IS NOT NULL THEN 'Notifications Reliability'
ELSE NULL
END AS subcategory
FROM
analytics.mattermost.nps_user_daily_score AS n1
JOIN analytics.mattermost.nps_feedback_classification AS n2 ON n1.user_id = n2.user_id
AND n1.server_id = n2.server_id
AND n1.last_feedback_date = n2.last_feedback_date
-- Generated by PRQL compiler version:0.9.3 (https://prql-lang.org)MVCE confirmation
- Minimal example
- New issue
Anything else?
Also, where did we settle on a line continuation character? I recall there being a debate but not what the outcome was.

Activity
eitsupi commentedon Aug 23, 2023
Maybe duplicate of #819
snth commentedon Aug 23, 2023
Thanks @eitsupi - that is the discussion I was thinking of.
I guess what this example adds to the discussion is add a practical example where (IMHO) it's not feasible to make things shorter.