Skip to content

Commit 5a5570f

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 443e6b5 commit 5a5570f

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
@@ -2803,31 +2803,40 @@ def _build_table_options(self, row):
28032803
return dict((o, row.get(o)) for o in self.recognized_table_options if o in row)
28042804

28052805
def _build_table_columns(self, meta, col_rows, compact_static=False, is_dense=False, virtual=False):
2806-
# partition key
2807-
partition_rows = [r for r in col_rows
2808-
if r.get('kind', None) == "partition_key"]
2806+
# Single-pass classification of column rows by kind
2807+
partition_rows = []
2808+
clustering_rows = []
2809+
other_rows = []
2810+
for r in col_rows:
2811+
kind = r.get('kind', None)
2812+
if kind == "partition_key":
2813+
partition_rows.append(r)
2814+
elif kind == "clustering":
2815+
if not compact_static:
2816+
clustering_rows.append(r)
2817+
# else: skip clustering rows entirely for compact_static tables
2818+
else:
2819+
other_rows.append(r)
2820+
2821+
# partition key - must be inserted first into meta.columns for CQL export ordering
28092822
if len(partition_rows) > 1:
2810-
partition_rows = sorted(partition_rows, key=lambda row: row.get('position'))
2823+
partition_rows.sort(key=lambda row: row.get('position'))
28112824
for r in partition_rows:
2812-
# we have to add meta here (and not in the later loop) because TableMetadata.columns is an
2813-
# dict (ordered since Python 3.7), and it assumes keys are inserted first, in order, when exporting CQL
28142825
column_meta = self._build_column_metadata(meta, r)
28152826
meta.columns[column_meta.name] = column_meta
2816-
meta.partition_key.append(meta.columns[r.get('column_name')])
2827+
meta.partition_key.append(column_meta)
28172828

28182829
# clustering key
2819-
if not compact_static:
2820-
clustering_rows = [r for r in col_rows
2821-
if r.get('kind', None) == "clustering"]
2830+
if clustering_rows:
28222831
if len(clustering_rows) > 1:
2823-
clustering_rows = sorted(clustering_rows, key=lambda row: row.get('position'))
2832+
clustering_rows.sort(key=lambda row: row.get('position'))
28242833
for r in clustering_rows:
28252834
column_meta = self._build_column_metadata(meta, r)
28262835
meta.columns[column_meta.name] = column_meta
2827-
meta.clustering_key.append(meta.columns[r.get('column_name')])
2836+
meta.clustering_key.append(column_meta)
28282837

2829-
for col_row in (r for r in col_rows
2830-
if r.get('kind', None) not in ('partition_key', 'clustering')):
2838+
# remaining columns (static, regular, etc.)
2839+
for col_row in other_rows:
28312840
column_meta = self._build_column_metadata(meta, col_row)
28322841
if is_dense and column_meta.cql_type == types.cql_empty_type:
28332842
continue

0 commit comments

Comments
 (0)