Skip to content

Commit 87fea78

Browse files
committed
fixup! feat(Snowflake): Add Snowflake dialect
1 parent 0161ef6 commit 87fea78

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

tests/test_snowflake_dialect_unit.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def _ctx() -> TranslateContext:
2626

2727

2828
def test_translate_segment__equal_on_string_trait__emits_variant_path() -> None:
29+
# Given a segment with a single EQUAL on a string trait
2930
seg: SegmentContext = {
3031
"key": "1",
3132
"name": "s",
@@ -37,14 +38,17 @@ def test_translate_segment__equal_on_string_trait__emits_variant_path() -> None:
3738
],
3839
}
3940

41+
# When the segment is translated
4042
sql = translate_segment(seg, _ctx())
4143

44+
# Then the predicate uses VARIANT path-extraction with quoted key, cast to STRING
4245
assert sql is not None
4346
assert 'i.traits:"plan"' in sql
4447
assert "(i.traits:\"plan\")::STRING = 'growth'" in sql
4548

4649

4750
def test_translate_segment__in_with_csv_value__emits_variant_in_clause() -> None:
51+
# Given a segment using IN with a comma-separated value
4852
seg: SegmentContext = {
4953
"key": "2",
5054
"name": "s",
@@ -56,14 +60,17 @@ def test_translate_segment__in_with_csv_value__emits_variant_in_clause() -> None
5660
],
5761
}
5862

63+
# When translated
5964
sql = translate_segment(seg, _ctx())
6065

66+
# Then each item is split into a SQL IN list against the VARIANT path
6167
assert sql is not None
6268
assert 'i.traits:"country"' in sql
6369
assert "IN ('GB','US','DE')" in sql
6470

6571

6672
def test_translate_segment__is_set__emits_is_not_null_on_variant_path() -> None:
73+
# Given an IS_SET condition on a trait key
6774
seg: SegmentContext = {
6875
"key": "3",
6976
"name": "s",
@@ -75,13 +82,16 @@ def test_translate_segment__is_set__emits_is_not_null_on_variant_path() -> None:
7582
],
7683
}
7784

85+
# When translated
7886
sql = translate_segment(seg, _ctx())
7987

88+
# Then the predicate is a VARIANT path-nullness check
8089
assert sql is not None
8190
assert 'i.traits:"beta_cohort" IS NOT NULL' in sql
8291

8392

8493
def test_translate_segment__is_not_set__emits_is_null_on_variant_path() -> None:
94+
# Given an IS_NOT_SET condition on a trait key
8595
seg: SegmentContext = {
8696
"key": "4",
8797
"name": "s",
@@ -93,13 +103,16 @@ def test_translate_segment__is_not_set__emits_is_null_on_variant_path() -> None:
93103
],
94104
}
95105

106+
# When translated
96107
sql = translate_segment(seg, _ctx())
97108

109+
# Then the predicate is `IS NULL` on the VARIANT path
98110
assert sql is not None
99111
assert 'i.traits:"x" IS NULL' in sql
100112

101113

102114
def test_translate_segment__percentage_split_no_property__uses_md5_hex_and_to_number() -> None:
115+
# Given a PERCENTAGE_SPLIT with no property (engine hashes the identity key)
103116
seg: SegmentContext = {
104117
"key": "100",
105118
"name": "s",
@@ -111,15 +124,18 @@ def test_translate_segment__percentage_split_no_property__uses_md5_hex_and_to_nu
111124
],
112125
}
113126

127+
# When translated
114128
sql = translate_segment(seg, _ctx())
115129

130+
# Then the SQL contains inline MD5_HEX / TO_NUMBER arithmetic and the threshold literal
116131
assert sql is not None
117132
assert "MD5_HEX" in sql
118133
assert "TO_NUMBER" in sql
119134
assert "<= 50.0" in sql
120135

121136

122137
def test_translate_segment__percentage_split_on_trait__hashes_variant_path() -> None:
138+
# Given a PERCENTAGE_SPLIT keyed on a trait value
123139
seg: SegmentContext = {
124140
"key": "101",
125141
"name": "s",
@@ -133,14 +149,17 @@ def test_translate_segment__percentage_split_on_trait__hashes_variant_path() ->
133149
],
134150
}
135151

152+
# When translated
136153
sql = translate_segment(seg, _ctx())
137154

155+
# Then the hash subject pulls from the trait's VARIANT path
138156
assert sql is not None
139157
assert 'i.traits:"uuid_attr"' in sql
140158
assert "MD5_HEX" in sql
141159

142160

143161
def test_translate_segment__trait_key_with_hyphens__double_quotes_variant_key() -> None:
162+
# Given a trait key with a hyphen (illegal as an unquoted SQL identifier)
144163
seg: SegmentContext = {
145164
"key": "11",
146165
"name": "s",
@@ -152,13 +171,16 @@ def test_translate_segment__trait_key_with_hyphens__double_quotes_variant_key()
152171
],
153172
}
154173

174+
# When translated
155175
sql = translate_segment(seg, _ctx())
156176

177+
# Then the trait key is double-quoted inside the VARIANT path
157178
assert sql is not None
158179
assert 'i.traits:"user-name"' in sql
159180

160181

161182
def test_translate_segment__contains_on_identity_identifier__uses_position_needle_first() -> None:
183+
# Given a CONTAINS on the identifier column
162184
seg: SegmentContext = {
163185
"key": "j4",
164186
"name": "s",
@@ -172,13 +194,16 @@ def test_translate_segment__contains_on_identity_identifier__uses_position_needl
172194
],
173195
}
174196

197+
# When translated
175198
sql = translate_segment(seg, _ctx())
176199

200+
# Then the predicate uses Snowflake's `POSITION(needle, haystack)`
177201
assert sql is not None
178202
assert "POSITION('@', i.identifier) > 0" in sql
179203

180204

181205
def test_translate_segment__contains_on_trait__uses_position_over_variant_string_cast() -> None:
206+
# Given a CONTAINS condition on a trait
182207
seg: SegmentContext = {
183208
"key": "tc1",
184209
"name": "s",
@@ -190,13 +215,16 @@ def test_translate_segment__contains_on_trait__uses_position_over_variant_string
190215
],
191216
}
192217

218+
# When translated
193219
sql = translate_segment(seg, _ctx())
194220

221+
# Then the predicate uses POSITION on the cast-to-string VARIANT value
195222
assert sql is not None
196223
assert "POSITION('G', (i.traits:\"country\")::STRING) > 0" in sql
197224

198225

199226
def test_translate_segment__percentage_split_on_identity_identifier__hashes_column() -> None:
227+
# Given a PERCENTAGE_SPLIT keyed on `$.identity.identifier`
200228
seg: SegmentContext = {
201229
"key": "ps7",
202230
"name": "s",
@@ -214,8 +242,10 @@ def test_translate_segment__percentage_split_on_identity_identifier__hashes_colu
214242
],
215243
}
216244

245+
# When translated
217246
sql = translate_segment(seg, _ctx())
218247

248+
# Then the hash subject is the identifier column ref via MD5_HEX
219249
assert sql is not None
220250
assert "i.identifier" in sql
221251
assert "MD5_HEX" in sql

0 commit comments

Comments
 (0)