Jinja DBT for loop union with some different columns across tables - sql

I'm creating a master events table, where almost all of the same columns are in every table, but a few tables are missing one or two columns. In those cases I'd like to replace those columns with null whenever the column doesn't exist in the table. When I run the code below, every cell in the output table is NULL.
Assume columns 1 and 2 are in every table, but column 3 is in table 1 and 2 but not 3.
{{ config(schema='MYSCHEMA', materialized='table') }}
{% set tables = ['table1', 'table2', 'table3'] %}
{% set possible_columns = ['col1', 'col2', 'col3'] %}
{% for table in tables %}
{%- set table_columns = adapter.get_columns_in_relation( ref(table) ) -%}
select
{% for pc in possible_columns %}
{% if not loop.last -%}
{% if pc in table_columns %}
{{ pc }},
{% else %}
null as {{ pc }},
{%- endif %}
{% else %}
{% if pc in table_columns %}
{{ pc }}
{% else %}
null as {{ pc }}
{%- endif %}
{% endif %}
{%- endfor %}
from
{{ ref(table) }}
{% if not loop.last -%}
union all
{%- endif %}
{% endfor %}

I'd recommend using dbt_utils.union_relations for this. It does exactly what you describe -- it creates a superset of columns from all the tables and fills in nulls where appropriate.
{{ dbt_utils.union_relations(
relations=[
ref('table1'),
ref('table2'),
ref('table3'),
],
include = ['col1', 'col2', 'col3']
) }}
BUT, if you want to roll your own...
The problem with your code is this line:
{% if pc in table_columns %}
adapter.get_columns_in_relation returns a list of Column objects, not a list of strings. To compare pc to the name of a column, you could use:
{% set cols = [] %}
{% for col in table_columns %}
{% do cols.append(col.name) %}
{% endfor %}
...
{% if pc in cols %}
You can also remove a bunch of redundant code by pushing down the if not loop.last block to just the comma, so this all becomes:
{% for table in tables %}
{%- set table_columns = adapter.get_columns_in_relation( ref(table) ) -%}
{% set cols = [] %}
{% for col in table_columns %}
{% do cols.append(col.name) %}
{% endfor %}
select
{% for pc in possible_columns %}
{% if pc in cols %}
{{ pc -}}
{% else %}
null as {{ pc -}}
{%- endif %}{%- if not loop.last -%},{% endif %}
{%- endfor %}
...

Related

shopoify liquid tags if else unless

I need to customize the shipping confirmation email. I want to use a tag to determine which of two text sections are included in the email. The problem is there is usually an array of tags. I can get section "A" like this...
{% for tag in tags %}
{% if tag == 'a' %}
A
{% endif %}
{% endfor %}
There is only a single 'a' tag in the array so I only get the "A" text once.
But I can't figure out how to get the "B" text to appear just one time.
If I do this, it appears for every tag that does not == 'a'...
{% for tag in tags %}
{% unless tag contains 'a' %}
B
{% endunless %}
{% endfor %}
Is there a way to get one instance of "B"?
You could repeat the same logic you did for A:
{% for tag in tags %}
{% if tag == 'a' %}
A
{% endif %}
{% if tag == 'b' %}
B
{% endif %}
{% endfor %}
Alternatively you could do a switch/case statement, I'd prefer this approach because it's easy to read, and if sometime in the future you would like to add another condition (tag), it would be easy and the code would still keep its elegance.
{% for tag in tags %}
{% case tag %}
{% when 'a' %}
A
{% when 'b' %}
B
{% when 'c' %}
C
{% endcase %}
{% endfor %}
If you have many tags it can become tricky, but if it's just two tags this is the general idea.
{% assign a_not_found = true %}
{% for tag in tags %}
{% if tag == 'a' %}
...
{% assign a_not_found = false %}
{% endif %}
{% endfor %}
{% if a_not_found %}
{... show b... }
{% endif %}
Otherwise
{% if tags contains 'a' %}
{...show a...%}
{% else %}
{...show b...%}
{% endif %}

Liquid. Looping through an array with an if condition then want to store the output of that in another array

{% assign leg = {{Predept.legs}} %}
{% for legs in {{leg}} %}
{% if {{leg[forloop.index0].direction}} == 'R' %}
{{leg[forloop.index0].arr_station_name}}
{%endif %}
{%endfor %}
I want to put the output of this for loop into another array. Any ideas?
Welcome, Jeet!
First, you don't ever nest the Liquid tags. Your basic loop should look something like this:
{% assign legs = Predept.legs %}
{% for leg in legs %}
<!-- Cool code & stuff here! -->
{% endfor %}
Now, in Liquid you can only create a new array by using the split filter on a string. We can also create a string by wrapping other commands with the capture tag. Putting that together, we get something like:
{% capture leg_data %}
{% assign legs = Predept.legs %}
{% for leg in legs %}
{% if leg.direction == 'R' %}
{% comment %}Output delimiter if needed {% endcomment %}
{% unless found_one %},{% endunless %}
{% assign found_one = true %}
{{ leg.arr_station_name }}
{% endif %}
{% endfor %}
{% endcapture %}
{% assign leg_data = leg_data | split: ',' %}
That will give us an array of all of our arr_station_name for the desired legs, but you may note that capture is also capturing all of the whitespace in there as well. If we need to prevent that from getting into our nice little array, we can use the whitespace-stripping - character on our tags to control that, giving:
{%- capture leg_data -%}
{%- assign legs = Predept.legs -%}
{%- for leg in legs -%}
{%- if leg.direction == 'R' -%}
{%- comment -%}Output delimiter if needed {%- endcomment -%}
{%- unless found_one -%},{%- endunless -%}
{%- assign found_one = true -%}
{{- leg.arr_station_name -}}
{%- endif -%}
{%- endfor -%}
{%- endcapture -%}
{%- assign leg_data = leg_data | split: ',' -%}
Hope this helps!

How scan an Order with Shopify Liquid - for customised Invoice

We are producing Invoices with Shopify's 'Order Printer' app.
and want to customise the Invoice.
For instance, if they have bought a 'book' - we want it to say "Enjoy your book"
and if a 'CD' - "Enjoy the music".
I've found I can test the first item they purchased with 'limit:1' :
{% for line_item in unfulfilled_line_items limit:1 %}
productType: {{ line_item.product.type }} - prodtype:{{product.type}} <br/>
{% if line_item.product.type contains "cd" %}
its a CD <br/>
{% else %}
it's not a CD?)<br/>
{% endif %}
{% endfor %}
But I would really like to scan the whole of the product.type array to determine how many of each product type there are - and output either/both messages - with plural 's' as appropriate.
Any ideas?
You're on the right track instead of limiting though you basically want to count.
{% assign cd_count = 0 %}
{% assign book_count = 0 %}
{% for line_item in unfulfilled_line_items %}
{% if line_item.product.type == "cd" %}
{% assign cd_count = cd_count | plus: 1%}
{% endif %}
{% if line_item.product.type == "book" %}
{% assign book_count = book_count | plus: 1 %}
{% endif %}
{% endfor %}
cd count: {{ cd_count }}
book count: {{ book_count}}
Now that you have a count you should be able to just do an if statement of the count numbers.
Thanks #trowse - solved the zeros issues, they were due to OrderPrinter cache problems and limitations. Just in case anyone needs it. Here's our solution:
<!-- count how many of each product type we are/have????? fullfilling -->
{% assign count_cd = 0 %}
{% assign count_bk = 0 %}
{% for line_item in unfulfilled_line_items %}
{% if line_item.product.type contains "cd" %}
{% assign count_cd = count_cd | plus:1 %}
{% endif %}
{% if line_item.product.type contains "Book" %}
{% assign count_bk = count_bk | plus:1 %}
{% endif %}
{% endfor %}
<!--- end of counting -->
<!-- Enjoy.. message -->
{% if {{count_cd > 0 %}
Enjoy the music
{% if {{count_bk > 0 %}
and the {{ count_bk | pluralize: 'book', 'books' }}<br/>
{% endif %}
{% else %}
{% if {{count_bk > 0 %}
Enjoy the {{ count_bk | pluralize: 'book', 'books' }}<br/>
{% endif %}
{% endif %}

Shopify: calling swatch.liquid 'colors' on collection.liquid and product-loop.liquid

I have not been able to find a fix for my issue. I want to show a simple "More Colors" option beneath the product prices.
Screenshot of Product example
I have been testing code such as:
{% if product.variants 'color' > 1 %}
Has more than one variant.
{% else %}Only one variant.
{% endif %}
But haven't had any luck. Everything either calls "Has more..." or "Only one..." regarless of the swatch count.
I am editing product-loop.liquid which is being called from collections.liquid
It has only outputed "Only one variant" or "Has more than one variant" for every product regardless of swatch/color count.
Thanks for any help..
This statement is nonsense:
{% if product.variants 'color' > 1 %}
Instead, you want to check the options set for your variants, and if you detect an option set to color, and you have more than one variant, then clearly, you are in more than one color territory.
{% assign has_more_colors = false %}
{% for option in product.options %}
{% if option contains 'color' and product.variants.length > 1 %}
{% assign has_more_colors = true %}
{% endif %}
{% endfor %}
So now you can do whatever it is you need to do... since you now know you a) have an option called colors, and b) more than one variant, hence more than one color...
The following snippet was posted to the Shopify boards. It solved this issue:
{% assign option_title = "Color" %}
{% assign option_index = "" %}
{% assign option_values = "" %}
{% for option in product.options %}
{% if option == option_title %}
{% assign option_index = forloop.index %}
{% endif %}
{% endfor %}
{% for variant in product.variants %}
{% assign variant_option_value = "" %}
{% if option_index == 1 %}
{% assign variant_option_value = variant.option1 | handleize %}
{% elsif option_index == 2 %}
{% assign variant_option_value = variant.option2 | handleize %}
{% elsif option_index == 3 %}
{% assign variant_option_value = variant.option3 | handleize %}
{% endif %}
{% assign option_values = option_values | append:"," | append:variant_option_value %}
{% endfor %}
{% assign option_values = option_values | remove_first:"," | split:"," | uniq %}
{% if option_values.size > 1 %}
MORE COLORS
{% else %}
{% endif %}

shopify pass a variable to settings

i want to do something like this in Shopify:
{% for i in (0..10) %}
{% capture slide %}slide{{i}}{% endcapture %}
{{ settings.slide }}//i need the value of this one
// i want to get the values for settings.slide1, settings.slide2 etc
{% endfor %}
Another example:
{% for i in (0..10) %}
{{ settings.slide[i] }}//i need the value of this one
{% endfor %}
This is a simplified version of what im trying to achieve.
Thanks
Try this:
{% for i in (0..10) %}
{% assign current_slide = 'slide' | append: i %}
{{ settings[current_slide] }}
{% endfor %}