Skip to content

Commit a14e784

Browse files
Revert "Re-order the columns in the auto-created DuckDB view to have the indexed column first (#390)"
This reverts commit c2307be.
1 parent 2f3389a commit a14e784

2 files changed

Lines changed: 11 additions & 101 deletions

File tree

src/duckdb/creator.rs

Lines changed: 11 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -468,20 +468,15 @@ impl TableManager {
468468
return Ok(());
469469
}
470470

471-
let table_columns = self.get_table_columns(tx)?;
472-
let ordered_columns = self.order_columns_by_index(table_columns);
473-
474-
let view_creation_sql = format!(
475-
"CREATE OR REPLACE VIEW {base_table} AS SELECT {columns} FROM {internal_table}",
476-
base_table = quote_identifier(&self.definition_name().to_string()),
477-
columns = ordered_columns.join(", "),
478-
internal_table = quote_identifier(&self.table_name().to_string())
479-
);
480-
481-
tracing::debug!("{view_creation_sql}");
482-
483-
tx.execute(&view_creation_sql, [])
484-
.context(super::UnableToCreateDuckDBTableSnafu)?;
471+
tx.execute(
472+
&format!(
473+
"CREATE OR REPLACE VIEW {base_table} AS SELECT * FROM {internal_table}",
474+
base_table = quote_identifier(&self.definition_name().to_string()),
475+
internal_table = quote_identifier(&self.table_name().to_string())
476+
),
477+
[],
478+
)
479+
.context(super::UnableToCreateDuckDBTableSnafu)?;
485480

486481
Ok(())
487482
}
@@ -683,63 +678,6 @@ impl TableManager {
683678

684679
Ok(count)
685680
}
686-
687-
fn get_table_columns(&self, tx: &Transaction<'_>) -> super::Result<Vec<String>> {
688-
let sql = "SELECT name FROM pragma_table_info(?)".to_string();
689-
690-
let owned_table_name = self.table_name().to_string();
691-
let table_name = quote_identifier(&owned_table_name);
692-
693-
tracing::debug!("{sql}; ?={table_name}");
694-
695-
let mut stmt = tx.prepare(&sql).context(super::UnableToQueryDataSnafu)?;
696-
let columns_iter = stmt
697-
.query_map([table_name], |row| row.get::<usize, String>(0))
698-
.context(super::UnableToQueryDataSnafu)?;
699-
700-
let mut columns = Vec::new();
701-
for column in columns_iter {
702-
columns.push(column.context(super::UnableToQueryDataSnafu)?);
703-
}
704-
705-
Ok(columns)
706-
}
707-
708-
/// Orders the given columns such that indexed single columns are first.
709-
/// If there is an index defined on a single column, that column should come first in the list.
710-
/// Multi-column indexes are not considered for ordering.
711-
pub(crate) fn order_columns_by_index(&self, columns: Vec<String>) -> Vec<String> {
712-
let mut ordered_columns = Vec::new();
713-
let mut non_indexed_columns = Vec::new();
714-
715-
// Get single-column indexes
716-
let single_column_indexes: HashSet<String> = self
717-
.table_definition
718-
.indexes
719-
.iter()
720-
.filter_map(|(column_ref, _)| {
721-
let cols: Vec<&str> = column_ref.iter().collect();
722-
if cols.len() == 1 {
723-
Some(cols[0].to_string())
724-
} else {
725-
None
726-
}
727-
})
728-
.collect();
729-
730-
// Separate columns into indexed and non-indexed
731-
for column in columns {
732-
if single_column_indexes.contains(&column) {
733-
ordered_columns.push(column);
734-
} else {
735-
non_indexed_columns.push(column);
736-
}
737-
}
738-
739-
// Return indexed columns first, then non-indexed columns
740-
ordered_columns.extend(non_indexed_columns);
741-
ordered_columns
742-
}
743681
}
744682

745683
fn create_empty_record_batch_reader(schema: SchemaRef) -> impl RecordBatchReader {
@@ -883,7 +821,7 @@ pub(crate) mod tests {
883821
}
884822

885823
#[tokio::test]
886-
async fn test_table_creator_indexes() {
824+
async fn test_table_creator() {
887825
let _guard = init_tracing(None);
888826
let batches = get_logs_batches().await;
889827

@@ -1761,9 +1699,7 @@ pub(crate) mod tests {
17611699
.with_internal(true)
17621700
.expect("to create table creator");
17631701

1764-
table_creator
1765-
.create_table(Arc::clone(&pool), &tx)
1766-
.expect("to create table");
1702+
table_creator.expect("to create table");
17671703

17681704
let columns = table_creator
17691705
.get_table_columns(&tx)

src/duckdb/snapshots/datafusion_table_providers__duckdb__creator__tests__explain_analyze_with_index_and_view.snap.new

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)