3434_ORACLE_OPERATOR_TRANSFORM : _OperatorTransform | None = None
3535_BIGQUERY_OPERATOR_TRANSFORM : _OperatorTransform | None = None
3636_DUCKDB_OPERATOR_TRANSFORM : _OperatorTransform | None = None
37- _POSTGRES_VECTOR_OPERATOR_MAP : Final [dict [str , str ]] = {"euclidean" : "<->" , "cosine" : "<=>" , "inner_product" : "<#>" }
38- _MYSQL_VECTOR_METRIC_MAP : Final [dict [str , str ]] = {"euclidean" : "EUCLIDEAN" , "cosine" : "COSINE" , "inner_product" : "DOT" }
39- _ORACLE_VECTOR_METRIC_MAP : Final [dict [str , str ]] = {
40- "euclidean" : "EUCLIDEAN" ,
41- "cosine" : "COSINE" ,
42- "inner_product" : "DOT" ,
43- "euclidean_squared" : "EUCLIDEAN_SQUARED" ,
44- }
45- _BIGQUERY_VECTOR_FUNCTION_MAP : Final [dict [str , str ]] = {
46- "euclidean" : "EUCLIDEAN_DISTANCE" ,
47- "cosine" : "COSINE_DISTANCE" ,
48- "inner_product" : "DOT_PRODUCT" ,
49- }
50- _DUCKDB_VECTOR_FUNCTION_MAP : Final [dict [str , str ]] = {
51- "euclidean" : "array_distance" ,
52- "cosine" : "array_cosine_distance" ,
53- "inner_product" : "array_negative_inner_product" ,
54- }
5537
5638
5739def is_vector_distance_expression (expression : object ) -> bool :
@@ -108,7 +90,9 @@ def VectorDistance(*, this: exp.Expr, expression: exp.Expr, metric: Any = "eucli
10890
10991def render_vector_distance_postgres (left : str , right : str , metric : str ) -> str :
11092 """Render PostgreSQL pgvector operator syntax."""
111- operator = _POSTGRES_VECTOR_OPERATOR_MAP .get (metric )
93+ operator_map = {"euclidean" : "<->" , "cosine" : "<=>" , "inner_product" : "<#>" }
94+
95+ operator = operator_map .get (metric )
11296 if operator :
11397 return f"{ left } { operator } { right } "
11498
@@ -117,7 +101,9 @@ def render_vector_distance_postgres(left: str, right: str, metric: str) -> str:
117101
118102def render_vector_distance_mysql (left : str , right : str , metric : str ) -> str :
119103 """Render MySQL DISTANCE function syntax."""
120- mysql_metric = _MYSQL_VECTOR_METRIC_MAP .get (metric , "EUCLIDEAN" )
104+ metric_map = {"euclidean" : "EUCLIDEAN" , "cosine" : "COSINE" , "inner_product" : "DOT" }
105+
106+ mysql_metric = metric_map .get (metric , "EUCLIDEAN" )
121107
122108 if ("ARRAY" in right or "[" in right ) and "STRING_TO_VECTOR" not in right :
123109 right = f"STRING_TO_VECTOR({ right } )"
@@ -127,7 +113,14 @@ def render_vector_distance_mysql(left: str, right: str, metric: str) -> str:
127113
128114def render_vector_distance_oracle (left : str , right : str , metric : str ) -> str :
129115 """Render Oracle VECTOR_DISTANCE function syntax."""
130- oracle_metric = _ORACLE_VECTOR_METRIC_MAP .get (metric , "EUCLIDEAN" )
116+ metric_map = {
117+ "euclidean" : "EUCLIDEAN" ,
118+ "cosine" : "COSINE" ,
119+ "inner_product" : "DOT" ,
120+ "euclidean_squared" : "EUCLIDEAN_SQUARED" ,
121+ }
122+
123+ oracle_metric = metric_map .get (metric , "EUCLIDEAN" )
131124
132125 if ("[" in right or "ARRAY" in right ) and "TO_VECTOR" not in right :
133126 right = f"TO_VECTOR({ right } )"
@@ -137,7 +130,9 @@ def render_vector_distance_oracle(left: str, right: str, metric: str) -> str:
137130
138131def render_vector_distance_bigquery (left : str , right : str , metric : str ) -> str :
139132 """Render BigQuery vector distance function syntax."""
140- function_name = _BIGQUERY_VECTOR_FUNCTION_MAP .get (metric )
133+ function_map = {"euclidean" : "EUCLIDEAN_DISTANCE" , "cosine" : "COSINE_DISTANCE" , "inner_product" : "DOT_PRODUCT" }
134+
135+ function_name = function_map .get (metric )
141136 if function_name :
142137 return f"{ function_name } ({ left } , { right } )"
143138
@@ -146,7 +141,12 @@ def render_vector_distance_bigquery(left: str, right: str, metric: str) -> str:
146141
147142def render_vector_distance_duckdb (left : str , right : str , metric : str , * , dimension : int | None = None ) -> str :
148143 """Render DuckDB VSS extension function syntax."""
149- function_name = _DUCKDB_VECTOR_FUNCTION_MAP .get (metric )
144+ function_map = {
145+ "euclidean" : "array_distance" ,
146+ "cosine" : "array_cosine_distance" ,
147+ "inner_product" : "array_negative_inner_product" ,
148+ }
149+ function_name = function_map .get (metric )
150150 if function_name :
151151 target_type = f"DOUBLE[{ dimension } ]" if dimension is not None else "DOUBLE[]"
152152 return f"{ function_name } ({ left } , CAST({ right } AS { target_type } ))"
0 commit comments