test new transformation func
-from splink.sql_transform import move_l_r_table_prefix_to_column_suffix
+import sqlglot
+from splink.sql_transform import (
+ move_l_r_table_prefix_to_column_suffix,
+ cast_concat_as_varchar,
+)
def test_move_l_r_table_prefix_to_column_suffix():
@@ -17,3 +21,34 @@ def test_move_l_r_table_prefix_to_column_suffix():
res = move_l_r_table_prefix_to_column_suffix(br)
expected = "name_l['first'] = name_r['first'] and levenshtein(dob_l, dob_r) < 2"
assert res.lower() == expected.lower()
+
+
+def test_cast_concat_as_varchar():
+
+ output = """
+ select cast(l.source_dataset as varchar) || '-__-' ||
+ cast(l.unique_id as varchar) as concat_id
+ """
+ output = sqlglot.parse_one(output).sql()
+
+ sql = "select l.source_dataset || '-__-' || l.unique_id as concat_id"
+ transformed_sql = cast_concat_as_varchar(sql)
+ assert transformed_sql == output
+
+ sql = """
+ select cast(l.source_dataset as varchar) || '-__-' ||
+ l.unique_id as concat_id
+ """
+ transformed_sql = cast_concat_as_varchar(sql)
+ assert transformed_sql == output
+
+ sql = """
+ select cast(l.source_dataset as varchar) || '-__-' ||
+ cast(l.unique_id as varchar) as concat_id
+ """
+ transformed_sql = cast_concat_as_varchar(sql)
+ assert transformed_sql == output
+
+ sql = "select source_dataset || '-__-' || unique_id as concat_id"
+ transformed_sql = cast_concat_as_varchar(sql)
+ assert transformed_sql == output.replace("l.", "")
