Skip to content

Commit

Permalink
Merge pull request #39 from YotpoLtd/KOALA-895-add-pk-and-fix-spaces
Browse files Browse the repository at this point in the history
add primary key support and fix spaces problem
  • Loading branch information
dkruh1 authored Jan 28, 2024
2 parents 7e5386d + 0daf9e3 commit 00eae83
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ explore: ugc__shopper_experience_store_aggregation {
join: ugc__shopper_experience_order_aggregation {
type: left_outer
relationship: one_to_many
sql_on: ${ugc__shopper_experience_store_aggregation.date} = ${ugc__shopper_experience_order_aggregation.date} AND ${ugc__shopper_experience_store_aggregation.app_key} = ${ugc__shopper_experience_order_aggregation.app_key} ;;
sql_on: ${ugc__shopper_experience_store_aggregation.date} = ${ugc__shopper_experience_order_aggregation.date} AND ${ugc__shopper_experience_store_aggregation.app_key} = ${ugc__shopper_experience_order_aggregation.app_key} ;;
}

join: ugc__shopper_experience_widget_engagement_aggregation {
type: left_outer
relationship: one_to_many
sql_on: ${ref('ugc__shopper_experience_store_aggregation).date} = ${ugc__shopper_experience_widget_engagement_aggregation.date} AND ${ugc__shopper_experience_store_aggregation.app_key} = ${ref('ugc__shopper_experience_widget_engagement_aggregation).app_key} ;;
sql_on: ${ref('ugc__shopper_experience_store_aggregation).date} = ${ugc__shopper_experience_widget_engagement_aggregation.date} AND ${ugc__shopper_experience_store_aggregation.app_key} = ${ref('ugc__shopper_experience_widget_engagement_aggregation).app_key} ;;
}

join: ugc__shopper_experience_widget_order_aggregation {
type: left_outer
relationship: one_to_many
sql_on: ${'ugc__shopper_experience_store_aggregation').date} = ${ugc__shopper_experience_widget_order_aggregation.date} AND ${ugc__shopper_experience_store_aggregation.app_key} = ${ugc__shopper_experience_widget_order_aggregation.app_key} ;;
sql_on: ${'ugc__shopper_experience_store_aggregation').date} = ${ugc__shopper_experience_widget_order_aggregation.date} AND ${ugc__shopper_experience_store_aggregation.app_key} = ${ugc__shopper_experience_widget_order_aggregation.app_key} ;;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ view: ugc__shopper_experience_store_aggregation {
description: ""
type: number
sql: case when ${currency} is not null then ${orders_count} else 0 end ;;
primary_key: no
}

measure: engagement {
Expand Down
38 changes: 34 additions & 4 deletions tests/yoda_dbt2looker/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,32 @@ def test__convert_all_refs_to_relation_name():
result = generator._convert_all_refs_to_relation_name(
" ${ref('model1').key1} = ${ref('model2').key2}"
)
assert result == "${model1.key1} = ${model2.key2}"
assert result == "${model1.key1} = ${model2.key2}"
result = generator._convert_all_refs_to_relation_name(
" ${ref('model1').key1} = ${ref('model2').key2} and ${ref('model1').key2} = ${ref('model3').key1}"
)
assert (
result
== "${model1.key1} = ${model2.key2} and ${model1.key2} = ${model3.key1}"
result == "${model1.key1} = ${model2.key2} and ${model1.key2} = ${model3.key1}"
)
result = generator._convert_all_refs_to_relation_name(
" (${ref('model1').key1} = ${ref('model2').key2} and ${ref('model1').key2} = ${ref('model3').key1}) or (${ref('model4').key1} = ${ref('model5').key2} or ${ref('model6').key2} = ${ref('model7').key1})"
)
assert (
result
== "(${model1.key1} = ${model2.key2} and ${model1.key2} = ${model3.key1} )or( ${model4.key1} = ${model5.key2} or ${model6.key2} = ${model7.key1})"
== "(${model1.key1} = ${model2.key2} and ${model1.key2} = ${model3.key1} )or( ${model4.key1} = ${model5.key2} or ${model6.key2} = ${model7.key1})"
)
result = generator._convert_all_refs_to_relation_name(
" ${ref('model1').key1} >= ${ref('model2').key2} and ${ref('model1').key2} <= ${ref('model3').key1}"
)
assert (
result
== "${model1.key1} >= ${model2.key2} and ${model1.key2} <= ${model3.key1}"
)
result = generator._convert_all_refs_to_relation_name(
" ${ref('model1').key1} != ${ref('model2').key2} and ${ref('model1').key2} > ${ref('model3').key1}"
)
assert (
result == "${model1.key1} != ${model2.key2} and ${model1.key2} > ${model3.key1}"
)


Expand Down Expand Up @@ -388,13 +400,31 @@ def test_lookml_calculated_dimension():
sql="case when 1=1 then 1 else 0 end",
description="custom dimension",
type="number",
primary_key=False,
)
value = generator.lookml_calculated_dimension(dimension1)
assert value == {
"name": "custom_dimension_1",
"type": "number",
"sql": "case when 1=1 then 1 else 0 end",
"description": "custom dimension",
"primary_key": "no",
}
dimension1 = models.Dbt2LookerExploreDimension(
name="custom_dimension_1",
model="ref('model_1')",
sql="case when 1=1 then 1 else 0 end",
description="custom dimension",
type="number",
primary_key=True,
)
value = generator.lookml_calculated_dimension(dimension1)
assert value == {
"name": "custom_dimension_1",
"type": "number",
"sql": "case when 1=1 then 1 else 0 end",
"description": "custom dimension",
"primary_key": "yes",
}


Expand Down
12 changes: 11 additions & 1 deletion yoda_dbt2looker/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ def lookml_calculated_dimension(dimension: models.Dbt2LookerExploreDimension):
"description": dimension.description,
"type": dimension.type.value,
"sql": _remove_escape_characters(dimension.sql),
"primary_key": "yes" if dimension.primary_key else "no",
}

if dimension.hidden:
Expand Down Expand Up @@ -543,7 +544,7 @@ def _convert_all_refs_to_relation_name(ref_str: str, handle_spaces: bool = True)
return ref_str

if handle_spaces:
ref_str = ref_str.replace(" ", "").replace("=", " = ")
ref_str = _add_leading_spaces_before_and_after_operators(ref_str.replace(" ", ""))
for group_value in matches:
ref_str = ref_str.replace(f"ref('{group_value}')", group_value)
# in case of a compound expression with logical operator , i.e : ${join1} and ${join2} - we would like
Expand All @@ -552,6 +553,15 @@ def _convert_all_refs_to_relation_name(ref_str: str, handle_spaces: bool = True)
return ref_str


def _add_leading_spaces_before_and_after_operators(sql: str) -> str:
pattern = re.compile(r"\b(\w+)\s*([=<>!]+)\s*(\w+)\b")
return pattern.sub(replace_operator_match, sql)


def replace_operator_match(match) -> str:
return f"{match.group(1)} {match.group(2)} {match.group(3)}"


def _extract_all_refs(ref_str: str) -> list[str]:
reg_ref = r"ref\(\s*\'(\w*)\'\s*\)"
matches = re.findall(reg_ref, ref_str)
Expand Down
1 change: 1 addition & 0 deletions yoda_dbt2looker/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class Dbt2LookerExploreDimension(BaseModel):
model: str
type: LookerCustomDimensions
sql: str
primary_key: bool = False
hidden: Optional[str] = None
description: Optional[str] = ""

Expand Down

0 comments on commit 00eae83

Please sign in to comment.