Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redefine FieldName as (TableId, ColId) #1165

Merged
merged 2 commits into from
Apr 30, 2024
Merged

Conversation

Centril
Copy link
Contributor

@Centril Centril commented Apr 26, 2024

Description of Changes

Do not store table names and field names in FieldName.
Rather, store the table id and the column id which is substantially cheaper (8 bytes).
Fixes a bug in fn column_pos along the way and does some drive-by cleanup.

The second commit adds both necessary changes to tests and refactoring around them, and similar changes which I had to do to understand and be confident in the changes to the tests. The diff got larger than I wanted, but once merged, I think it will help us long term.

Fixes #1118.
Fixes #1117.

API and ABI breaking changes

None

Expected complexity level and risk

2

@Centril Centril force-pushed the centril/simplify-fieldname branch from 7d231a5 to d96e452 Compare April 26, 2024 09:23
@@ -351,7 +349,7 @@ impl InstanceEnv {
let tx = &mut *self.tx.get()?;

let schema = stdb.schema_for_table_mut(tx, table_id)?;
let row_type = ProductType::from(&*schema);
let row_type = schema.get_row_type();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by optimization

Comment on lines +201 to +207
let mut iter = ident.rsplit('.');
let field = iter.next();
let table = iter.next();
let more = iter.next();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoids allocation now.

Comment on lines +223 to +230
if let Some(f_table) = f_table {
// Qualified field `{f_table}.{f_field}`.
// Narrow search to first table with name `f_table`.
return if let Some(col) = tables
.find(|t| &*t.table_name == f_table)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoids allocation in the qualified path now.

@@ -626,10 +630,9 @@ fn compile_select_item(from: &From, select_item: SelectItem) -> Result<Column, P
fn compile_select<T: TableSchemaView>(db: &RelationalDB, tx: &T, select: Select) -> Result<SqlAst, PlanError> {
let from = compile_from(db, tx, &select.from)?;
// SELECT ...
let mut project = Vec::new();
let mut project = Vec::with_capacity(select.projection.len());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by optimization

Comment on lines -53 to -78
fn check_field(table: &From, field: &FieldExpr) -> Result<(), PlanError> {
if let FieldExpr::Name(field) = field {
table.find_field(&field.to_string())?;
}
Ok(())
}

fn check_field_column(table: &From, field: &ColumnOp) -> Result<(), PlanError> {
if let ColumnOp::Field(field) = field {
check_field(table, field)?;
}
Ok(())
}

/// Verify the `fields` inside the `expr` are valid
fn check_cmp_expr(table: &From, expr: &ColumnOp) -> Result<(), PlanError> {
match expr {
ColumnOp::Field(field) => check_field(table, field)?,
ColumnOp::Cmp { op: _, lhs, rhs } => {
check_field_column(table, lhs)?;
check_field_column(table, rhs)?;
}
}

Ok(())
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By having FieldName we've already verified, so no need to do it again.

@@ -529,7 +532,8 @@ fn compile_from<T: TableSchemaView>(db: &RelationalDB, tx: &T, from: &[TableWith

match constraint {
JoinConstraint::On(x) => {
let expr = compile_expr_value(&base, None, x.clone())?;
let tables = base.iter_tables().chain([&*join]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the reason find_field had to become a free function.
We must extend the context with the joined table before adding it to the returned base.

@Centril Centril force-pushed the centril/simplify-fieldname branch 4 times, most recently from af99b17 to 5b5274a Compare April 26, 2024 16:23
Comment on lines 615 to 620
let result = run_for_testing(
&db,
"insert into inventory (id, name) values (1, 'Kiley');
insert into inventory (id, name) values (2, 'Terza');
insert into inventory (id, name) values (3, 'Alvie');
"insert into inventory (inventory_id, name) values (1, 'Kiley');
insert into inventory (inventory_id, name) values (2, 'Terza');
insert into inventory (inventory_id, name) values (3, 'Alvie');
SELECT * FROM inventory",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we have a bug in master as the column name is inventory_id and not id.

.iter()
.map(|x| {
let ty = x.algebraic_type.clone();
let name = translate_col(tx, x.field);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to do this to match the old output as the header no longer contains the column names.

@Centril Centril force-pushed the centril/simplify-fieldname branch from 5b5274a to dc59598 Compare April 26, 2024 16:40
@Centril Centril force-pushed the centril/simplify-fieldname branch from dc59598 to 1ce322c Compare April 26, 2024 17:08
@bfops bfops added breaks library compatibility This change breaks the SpacetimeDB library interface release-any To be landed in any release window labels Apr 29, 2024
@Centril Centril force-pushed the centril/simplify-fieldname branch from 578bd6c to 4b99880 Compare April 30, 2024 13:00
@Centril Centril requested a review from gefjon April 30, 2024 18:04
Copy link
Contributor

@gefjon gefjon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests look cleaner, column accesses look more efficient.

@Centril Centril added this pull request to the merge queue Apr 30, 2024
Merged via the queue into master with commit e144c40 Apr 30, 2024
6 checks passed
@Centril Centril deleted the centril/simplify-fieldname branch April 30, 2024 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaks library compatibility This change breaks the SpacetimeDB library interface release-any To be landed in any release window
Projects
None yet
3 participants