Skip to content

Commit 616f3ea

Browse files
committed
ensure unique obj
1 parent 56b01e3 commit 616f3ea

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

src/sempy_labs/tom/_model.py

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4810,7 +4810,9 @@ def add_relationship(rel_key, table_name, t_name, o_name):
48104810

48114811
@staticmethod
48124812
def _get_synonym_info(
4813-
lm: dict, object: Union["TOM.Table", "TOM.Column"], synonym_name: str
4813+
lm: dict,
4814+
object: Union["TOM.Table", "TOM.Column", "TOM.Measure", "TOM.Hierarchy"],
4815+
synonym_name: str,
48144816
):
48154817

48164818
import Microsoft.AnalysisServices.Tabular as TOM
@@ -4824,18 +4826,25 @@ def _get_synonym_info(
48244826
t_name = binding.get("ConceptualEntity")
48254827
o_name = binding.get("ConceptualProperty")
48264828

4827-
if object_type == TOM.ObjectType.Table:
4828-
if t_name == object.Name and o_name is None:
4829-
obj = key
4830-
for term in v.get("Terms", []):
4831-
if synonym_name in term:
4832-
syn_exists = True
4833-
elif object_type == TOM.ObjectType.Column:
4834-
if t_name == object.Parent.Name and o_name == object.Name:
4835-
obj = key
4836-
for term in v.get("Terms", []):
4837-
if synonym_name in term:
4838-
syn_exists = True
4829+
if (
4830+
object_type == TOM.ObjectType.Table
4831+
and t_name == object.Name
4832+
and o_name is None
4833+
) or (
4834+
object_type
4835+
in [
4836+
TOM.ObjectType.Column,
4837+
TOM.ObjectType.Measure,
4838+
TOM.ObjectType.Hierarchy,
4839+
]
4840+
and t_name == object.Parent.Name
4841+
and o_name == object.Name
4842+
):
4843+
obj = key
4844+
terms = v.get("Terms", [])
4845+
syn_exists = any(synonym_name in term for term in terms)
4846+
# optionally break early if match is found
4847+
break
48394848

48404849
return obj, syn_exists
48414850

@@ -4880,8 +4889,31 @@ def set_synonym(
48804889
lm=lm, object=object, synonym_name=synonym_name
48814890
)
48824891

4892+
entities = lm.get("Entities", {})
4893+
4894+
def get_unique_entity_key(object, object_type, entities):
4895+
4896+
if object_type == TOM.ObjectType.Table:
4897+
base_obj = object.Name.lower().replace(" ", "_")
4898+
else:
4899+
base_obj = f"{object.Parent.Name}.{object.Name}".lower().replace(
4900+
" ", "_"
4901+
)
4902+
4903+
obj = base_obj
4904+
counter = 1
4905+
existing_keys = set(entities.keys())
4906+
4907+
# Make sure the object name is unique
4908+
while obj in existing_keys:
4909+
obj = f"{base_obj}_{counter}"
4910+
counter += 1
4911+
4912+
return obj
4913+
48834914
# Update linguistic metadata content
48844915
if obj is None:
4916+
obj = get_unique_entity_key(object, object_type, entities)
48854917
lm["Entities"][obj] = {
48864918
"Definition": {"Binding": {}},
48874919
"State": "Authored",

0 commit comments

Comments
 (0)