Skip to content

Commit

Permalink
Merge pull request #174 from CassioML/fix_metadata_prep
Browse files Browse the repository at this point in the history
fix metadata preparation
  • Loading branch information
epinzur authored Oct 3, 2024
2 parents 9256e4b + b02358d commit 975e4d6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 33 deletions.
5 changes: 2 additions & 3 deletions src/cassio/table/mixins/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,8 @@ def _extract_where_clause_blocks(
these_wc_vals_list: List[Any] = []
# WHERE creation:
for k, v in sorted(split_metadata.get("metadata_s", {}).items()):
escaped_k = k.replace("{", "{{").replace("}", "}}")
these_wc_blocks.append(f"metadata_s['{escaped_k}'] = %s")
these_wc_vals_list.append(v)
these_wc_blocks.append("metadata_s[%s] = %s")
these_wc_vals_list.extend([k, v])
# no new kwargs keys are created, all goes to WHERE
this_args_dict: Dict[str, Any] = {}
these_wc_vals = tuple(these_wc_vals_list)
Expand Down
93 changes: 63 additions & 30 deletions tests/unit/test_tableclasses_cql_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ def test_metadata_routing(self, mock_db_session: MockDBSession) -> None:
mock_db_session.assert_last_equal(
[
(
"SELECT * FROM k.tn WHERE metadata_s['indexed'] = ? AND row_id = ?;", # noqa: E501
("i", "ROWID"),
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND row_id = ?;", # noqa: E501
("indexed", "i", "ROWID"),
),
]
)
Expand Down Expand Up @@ -437,8 +437,8 @@ def test_multi_clustering_column_metadata_vector_cassandra_table(
mock_db_session.assert_last_equal(
[
(
"SELECT * FROM k.tn WHERE metadata_s['mdke'] = ? AND metadata_s['mdkn'] = ? AND metadata_s['mdks'] = ? AND partition_id = ? AND row_id_0 = ? AND row_id_1 = ?;", # noqa: E501
("true", "123.0", "mdv", "MDPART", 1, 2),
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND metadata_s[?] = ? AND metadata_s[?] = ? AND partition_id = ? AND row_id_0 = ? AND row_id_1 = ?;", # noqa: E501
("mdke", "true", "mdkn", "123.0", "mdks", "mdv", "MDPART", 1, 2),
),
]
)
Expand All @@ -449,10 +449,13 @@ def test_multi_clustering_column_metadata_vector_cassandra_table(
mock_db_session.assert_last_equal(
[
(
"SELECT * FROM k.tn WHERE metadata_s['mdke'] = ? AND metadata_s['mdkn'] = ? AND metadata_s['mdks'] = ? AND partition_id = ? AND row_id_0 = ? AND row_id_1 = ? ORDER BY vector ANN OF ? LIMIT ?;", # noqa: E501
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND metadata_s[?] = ? AND metadata_s[?] = ? AND partition_id = ? AND row_id_0 = ? AND row_id_1 = ? ORDER BY vector ANN OF ? LIMIT ?;", # noqa: E501
(
"mdke",
"true",
"mdkn",
"123.0",
"mdks",
"mdv",
"MDPART",
1,
Expand All @@ -468,8 +471,8 @@ def test_multi_clustering_column_metadata_vector_cassandra_table(
mock_db_session.assert_last_equal(
[
(
"SELECT * FROM k.tn WHERE metadata_s['mdke'] = ? AND metadata_s['mdkn'] = ? AND metadata_s['mdks'] = ? AND partition_id = ? ;", # noqa: E501
("true", "123.0", "mdv", "MDPART"),
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND metadata_s[?] = ? AND metadata_s[?] = ? AND partition_id = ? ;", # noqa: E501
("mdke", "true", "mdkn", "123.0", "mdks", "mdv", "MDPART"),
),
]
)
Expand All @@ -479,8 +482,8 @@ def test_multi_clustering_column_metadata_vector_cassandra_table(
mock_db_session.assert_last_equal(
[
(
"SELECT * FROM k.tn WHERE metadata_s['mdke'] = ? AND metadata_s['mdke2'] = ? AND partition_id = ? AND row_id_0 = ? AND row_id_1 = ? ;", # noqa: E501
("true", "true", "MDPART", 1, 2),
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND metadata_s[?] = ? AND partition_id = ? AND row_id_0 = ? AND row_id_1 = ? ;", # noqa: E501
("mdke", "true", "mdke2", "true", "MDPART", 1, 2),
),
]
)
Expand All @@ -495,9 +498,11 @@ def test_multi_clustering_column_metadata_vector_cassandra_table(
mock_db_session.assert_last_equal(
[
(
"SELECT * FROM k.tn WHERE metadata_s['mdke'] = ? AND metadata_s['mdke2'] = ? AND partition_id = ? AND row_id_0 = ? AND row_id_1 = ? ORDER BY vector ANN OF ? LIMIT ?;", # noqa: E501
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND metadata_s[?] = ? AND partition_id = ? AND row_id_0 = ? AND row_id_1 = ? ORDER BY vector ANN OF ? LIMIT ?;", # noqa: E501
(
"mdke",
"true",
"mdke2",
"true",
"MDPART",
1,
Expand All @@ -513,8 +518,8 @@ def test_multi_clustering_column_metadata_vector_cassandra_table(
mock_db_session.assert_last_equal(
[
(
"SELECT * FROM k.tn WHERE metadata_s['mdke'] = ? AND metadata_s['mdke2'] = ? AND partition_id = ? ;", # noqa: E501
("true", "true", "MDPART"),
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND metadata_s[?] = ? AND partition_id = ? ;", # noqa: E501
("mdke", "true", "mdke2", "true", "MDPART"),
),
]
)
Expand All @@ -523,8 +528,8 @@ def test_multi_clustering_column_metadata_vector_cassandra_table(
mock_db_session.assert_last_equal(
[
(
'SELECT * FROM k.tn WHERE metadata_s[\'link_{"kind": "kw"}\'] = ? AND partition_id = ? ;', # noqa: E501
("link", "MD_JSON_KEY"),
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND partition_id = ? ;", # noqa: E501
('link_{"kind": "kw"}', "link", "MD_JSON_KEY"),
),
]
)
Expand Down Expand Up @@ -753,8 +758,18 @@ def test_clustered_elastic_metadata_vector_cassandra_table(
mock_db_session.assert_last_equal(
[
(
"SELECT * FROM k.tn WHERE metadata_s['mdke'] = ? AND metadata_s['mdkn'] = ? AND metadata_s['mdks'] = ? AND key_desc = ? AND key_vals = ? AND partition_id = ? ;", # noqa: E501
("true", "123.0", "mdv", '["a","b"]', '["MDA","MDB"]', "MDPART"),
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND metadata_s[?] = ? AND metadata_s[?] = ? AND key_desc = ? AND key_vals = ? AND partition_id = ? ;", # noqa: E501
(
"mdke",
"true",
"mdkn",
"123.0",
"mdks",
"mdv",
'["a","b"]',
'["MDA","MDB"]',
"MDPART",
),
),
]
)
Expand All @@ -765,10 +780,13 @@ def test_clustered_elastic_metadata_vector_cassandra_table(
mock_db_session.assert_last_equal(
[
(
"SELECT * FROM k.tn WHERE metadata_s['mdke'] = ? AND metadata_s['mdkn'] = ? AND metadata_s['mdks'] = ? AND key_desc = ? AND key_vals = ? AND partition_id = ? ORDER BY vector ANN OF ? LIMIT ?;", # noqa: E501
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND metadata_s[?] = ? AND metadata_s[?] = ? AND key_desc = ? AND key_vals = ? AND partition_id = ? ORDER BY vector ANN OF ? LIMIT ?;", # noqa: E501
(
"mdke",
"true",
"mdkn",
"123.0",
"mdks",
"mdv",
'["a","b"]',
'["MDA","MDB"]',
Expand All @@ -784,10 +802,13 @@ def test_clustered_elastic_metadata_vector_cassandra_table(
mock_db_session.assert_last_equal(
[
(
"SELECT * FROM k.tn WHERE metadata_s['mdke'] = ? AND metadata_s['mdkn'] = ? AND metadata_s['mdks'] = ? AND key_desc = ? AND key_vals = ? AND partition_id = ? ORDER BY vector ANN OF ? LIMIT ?;", # noqa: E501
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND metadata_s[?] = ? AND metadata_s[?] = ? AND key_desc = ? AND key_vals = ? AND partition_id = ? ORDER BY vector ANN OF ? LIMIT ?;", # noqa: E501
(
"mdke",
"true",
"mdkn",
"123.0",
"mdks",
"mdv",
'["a","b"]',
'["MDA","MDB"]',
Expand All @@ -803,8 +824,8 @@ def test_clustered_elastic_metadata_vector_cassandra_table(
mock_db_session.assert_last_equal(
[
(
"SELECT * FROM k.tn WHERE metadata_s['mdke'] = ? AND metadata_s['mdkn'] = ? AND metadata_s['mdks'] = ? AND partition_id = ? ;", # noqa: E501
("true", "123.0", "mdv", "MDPART"),
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND metadata_s[?] = ? AND metadata_s[?] = ? AND partition_id = ? ;", # noqa: E501
("mdke", "true", "mdkn", "123.0", "mdks", "mdv", "MDPART"),
),
]
)
Expand All @@ -813,8 +834,8 @@ def test_clustered_elastic_metadata_vector_cassandra_table(
mock_db_session.assert_last_equal(
[
(
"SELECT * FROM k.tn WHERE metadata_s['mdke'] = ? AND metadata_s['mdkn'] = ? AND metadata_s['mdks'] = ? AND partition_id = ? ;", # noqa: E501
("true", "123.0", "mdv", "PRE-PART-ID"),
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND metadata_s[?] = ? AND metadata_s[?] = ? AND partition_id = ? ;", # noqa: E501
("mdke", "true", "mdkn", "123.0", "mdks", "mdv", "PRE-PART-ID"),
),
]
)
Expand All @@ -824,8 +845,16 @@ def test_clustered_elastic_metadata_vector_cassandra_table(
mock_db_session.assert_last_equal(
[
(
"SELECT * FROM k.tn WHERE metadata_s['mdke'] = ? AND metadata_s['mdke2'] = ? AND key_desc = ? AND key_vals = ? AND partition_id = ? ;", # noqa: E501
("true", "true", '["a","b"]', '["MDA","MDB"]', "MDPART"),
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND metadata_s[?] = ? AND key_desc = ? AND key_vals = ? AND partition_id = ? ;", # noqa: E501
(
"mdke",
"true",
"mdke2",
"true",
'["a","b"]',
'["MDA","MDB"]',
"MDPART",
),
),
]
)
Expand All @@ -841,9 +870,11 @@ def test_clustered_elastic_metadata_vector_cassandra_table(
mock_db_session.assert_last_equal(
[
(
"SELECT * FROM k.tn WHERE metadata_s['mdke'] = ? AND metadata_s['mdke2'] = ? AND key_desc = ? AND key_vals = ? AND partition_id = ? ORDER BY vector ANN OF ? LIMIT ?;", # noqa: E501
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND metadata_s[?] = ? AND key_desc = ? AND key_vals = ? AND partition_id = ? ORDER BY vector ANN OF ? LIMIT ?;", # noqa: E501
(
"mdke",
"true",
"mdke2",
"true",
'["a","b"]',
'["MDA","MDB"]',
Expand All @@ -859,9 +890,11 @@ def test_clustered_elastic_metadata_vector_cassandra_table(
mock_db_session.assert_last_equal(
[
(
"SELECT * FROM k.tn WHERE metadata_s['mdke'] = ? AND metadata_s['mdke2'] = ? AND key_desc = ? AND key_vals = ? AND partition_id = ? ORDER BY vector ANN OF ? LIMIT ?;", # noqa: E501
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND metadata_s[?] = ? AND key_desc = ? AND key_vals = ? AND partition_id = ? ORDER BY vector ANN OF ? LIMIT ?;", # noqa: E501
(
"mdke",
"true",
"mdke2",
"true",
'["a","b"]',
'["MDA","MDB"]',
Expand All @@ -877,8 +910,8 @@ def test_clustered_elastic_metadata_vector_cassandra_table(
mock_db_session.assert_last_equal(
[
(
"SELECT * FROM k.tn WHERE metadata_s['mdke'] = ? AND metadata_s['mdke2'] = ? AND partition_id = ? ;", # noqa: E501
("true", "true", "MDPART"),
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND metadata_s[?] = ? AND partition_id = ? ;", # noqa: E501
("mdke", "true", "mdke2", "true", "MDPART"),
),
]
)
Expand All @@ -887,8 +920,8 @@ def test_clustered_elastic_metadata_vector_cassandra_table(
mock_db_session.assert_last_equal(
[
(
"SELECT * FROM k.tn WHERE metadata_s['mdke'] = ? AND metadata_s['mdke2'] = ? AND partition_id = ? ;", # noqa: E501
("true", "true", "PRE-PART-ID"),
"SELECT * FROM k.tn WHERE metadata_s[?] = ? AND metadata_s[?] = ? AND partition_id = ? ;", # noqa: E501
("mdke", "true", "mdke2", "true", "PRE-PART-ID"),
),
]
)
Expand Down

0 comments on commit 975e4d6

Please sign in to comment.