-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement charindex macro DNA-15944 [DNA-20680] #78
Conversation
8a1e64e
to
b1e9ca3
Compare
0e33c68
to
e5b8c10
Compare
{%- macro charindex(expression_to_find, expression_to_search, start_location=None) -%} | ||
{%- if start_location is none -%} | ||
{%- if target.type == 'databricks' -%} | ||
position('{{ expression_to_find }}', '{{ expression_to_search }}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see everywhere single quotes, but the input is already text/string. Is putting here quotes not breaking the functionality?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, on the contrary, it saves us from using extra escaping in the models. I.e., we can use
{{ pm_utils.charindex('Approve purchase order', 'Purchase_order_item_event_log_with_internal_id."Activity"') }} > 0
instead of
{{ pm_utils.charindex("'Approve purchase order'", 'Purchase_order_item_event_log_with_internal_id."Activity"') }} > 0
You could say this is redundant in the expression_to_search
if we suppose that that is always a field in a table and not a hardcoded string, but we cover more use cases this way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you compile now the code we are getting position('-', 'EKPO_input.NETWR_input
')
while I believe we want position('-', EKPO_input.NETWR_input
)
which you get if the macro implementation is position('{{ expression_to_find }}', {{ expression_to_search }})
You mentioned in the Jira task the following: "Note that line 4 yields NULL in SQL Server and 0 in Databricks and line 7 yields 1 in Databricks and 0 in SQL Server." |
a9f47a4
to
0c9a32f
Compare
Line 4 is already covered in the macro, it was more as a reference info for the integration tests that we'll implement later. I updated the macro to also support Line 7. |
82d97f1
to
1cdeb35
Compare
README.md
Outdated
This macro returns the starting position of the first occurrence of a string in another string. The search is not case-sensitive. If the string is not found, the function returns 0. This macro can be used to check whether a string contains another string. | ||
|
||
Usage: | ||
`{{ pm_utils.charindex('[expression_to_find]', '[expression_to_search]', '[start_location]') }}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to field
1d77ee7
to
188cfd5
Compare
{% endif -%} | ||
{% else -%} | ||
{% if target.type == 'databricks' -%} | ||
position('{{ expression_to_find }}', {{ field }}, '{{ start_location }}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also the start location is expected to be an integer, in other words it does not work correctly. It runs, it compiles, but the result is incorrect.
ce87e6e
to
d95fd9a
Compare
Description
Release
main
)dev
(or other) branchDid you consider?
What is the performance impact?The version number indbt_project.yml