Skip to content

Commit a38bbfe

Browse files
committed
(improvement) metadata: single-pass _build_table_columns
Replace three list comprehension passes over col_rows with a single classification loop that sorts columns into partition, clustering, and other buckets. Also use in-place sort() instead of sorted() and reuse the already-built column_meta instead of a redundant dict lookup.
1 parent b23c7ae commit a38bbfe

1 file changed

Lines changed: 23 additions & 14 deletions

File tree

cassandra/metadata.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2792,31 +2792,40 @@ def _build_table_options(self, row):
27922792
return dict((o, row.get(o)) for o in self.recognized_table_options if o in row)
27932793

27942794
def _build_table_columns(self, meta, col_rows, compact_static=False, is_dense=False, virtual=False):
2795-
# partition key
2796-
partition_rows = [r for r in col_rows
2797-
if r.get('kind', None) == "partition_key"]
2795+
# Single-pass classification of column rows by kind
2796+
partition_rows = []
2797+
clustering_rows = []
2798+
other_rows = []
2799+
for r in col_rows:
2800+
kind = r.get('kind', None)
2801+
if kind == "partition_key":
2802+
partition_rows.append(r)
2803+
elif kind == "clustering":
2804+
if not compact_static:
2805+
clustering_rows.append(r)
2806+
# else: skip clustering rows entirely for compact_static tables
2807+
else:
2808+
other_rows.append(r)
2809+
2810+
# partition key - must be inserted first into meta.columns for CQL export ordering
27982811
if len(partition_rows) > 1:
2799-
partition_rows = sorted(partition_rows, key=lambda row: row.get('position'))
2812+
partition_rows.sort(key=lambda row: row.get('position'))
28002813
for r in partition_rows:
2801-
# we have to add meta here (and not in the later loop) because TableMetadata.columns is an
2802-
# dict (ordered since Python 3.7), and it assumes keys are inserted first, in order, when exporting CQL
28032814
column_meta = self._build_column_metadata(meta, r)
28042815
meta.columns[column_meta.name] = column_meta
2805-
meta.partition_key.append(meta.columns[r.get('column_name')])
2816+
meta.partition_key.append(column_meta)
28062817

28072818
# clustering key
2808-
if not compact_static:
2809-
clustering_rows = [r for r in col_rows
2810-
if r.get('kind', None) == "clustering"]
2819+
if clustering_rows:
28112820
if len(clustering_rows) > 1:
2812-
clustering_rows = sorted(clustering_rows, key=lambda row: row.get('position'))
2821+
clustering_rows.sort(key=lambda row: row.get('position'))
28132822
for r in clustering_rows:
28142823
column_meta = self._build_column_metadata(meta, r)
28152824
meta.columns[column_meta.name] = column_meta
2816-
meta.clustering_key.append(meta.columns[r.get('column_name')])
2825+
meta.clustering_key.append(column_meta)
28172826

2818-
for col_row in (r for r in col_rows
2819-
if r.get('kind', None) not in ('partition_key', 'clustering')):
2827+
# remaining columns (static, regular, etc.)
2828+
for col_row in other_rows:
28202829
column_meta = self._build_column_metadata(meta, col_row)
28212830
if is_dense and column_meta.cql_type == types.cql_empty_type:
28222831
continue

0 commit comments

Comments
 (0)