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

feat!: remove table_length from ProofExpr::result_evaluate and return ColumnarValue #334

Closed
wants to merge 0 commits into from

Conversation

iajoiner
Copy link
Contributor

@iajoiner iajoiner commented Nov 1, 2024

Please be sure to look over the pull request guidelines here: https://github.com/spaceandtimelabs/sxt-proof-of-sql/blob/main/CONTRIBUTING.md#submit-pr.

Please go through the following checklist

Rationale for this change

In order to make ProofPlans composable it is necessary to allow one or more ProofPlans as the input to another. As such it is a great idea to remove table_length as an argument in ProofPlan::result_evaluate which in turn requires removal in ProofExpr.

What changes are included in this PR?

  • Remove table_length from ProofExpr::result_evaluate and return ColumnarValue.
  • Remove table_length from ProofPlan::result_evaluate.

Are these changes tested?

Yes

@iajoiner iajoiner changed the title feat: remove table_length from ProofExpr::result_evaluate feat!: remove table_length from ProofExpr::result_evaluate Nov 1, 2024
@iajoiner iajoiner force-pushed the feat/remove-len branch 12 times, most recently from b1d74e6 to 327ebb1 Compare November 5, 2024 15:57
@iajoiner iajoiner marked this pull request as ready for review November 5, 2024 17:46
@iajoiner iajoiner requested a review from JayWhite2357 November 5, 2024 17:46
@iajoiner iajoiner force-pushed the feat/remove-len branch 2 times, most recently from 2b92209 to bbff8fb Compare November 5, 2024 18:31
@iajoiner iajoiner changed the title feat!: remove table_length from ProofExpr::result_evaluate feat!: remove table_length from ProofExpr::result_evaluate and return ColumnarValue Nov 6, 2024
@iajoiner iajoiner force-pushed the feat/remove-len branch 4 times, most recently from d17b716 to e8e94a5 Compare November 6, 2024 22:01
crates/proof-of-sql/src/base/database/columnar_value.rs Outdated Show resolved Hide resolved
crates/proof-of-sql/src/base/database/columnar_value.rs Outdated Show resolved Hide resolved
let rhs_columnar_value: ColumnarValue<'a, S> = self.rhs.result_evaluate(alloc, accessor);
lhs_columnar_value
.apply_boolean_binary_operator(&rhs_columnar_value, BinaryOperator::And, alloc)
.expect("Failed to apply boolean binary operator")
Copy link
Contributor

Choose a reason for hiding this comment

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

This expect also wouldn't be needed if you do my suggestion above.

Comment on lines 59 to 79
let is_literal = matches!(
(&lhs_columnar_value, &rhs_columnar_value),
(&ColumnarValue::Literal(_), &ColumnarValue::Literal(_))
);
let lhs_scale = self.lhs.data_type().scale().unwrap_or(0);
let rhs_scale = self.rhs.data_type().scale().unwrap_or(0);
let res = scale_and_subtract(alloc, lhs_column, rhs_column, lhs_scale, rhs_scale, true)
.expect("Failed to scale and subtract");
Column::Boolean(result_evaluate_equals_zero(table_length, alloc, res))
let res = scale_and_subtract_columnar_value(
alloc,
lhs_columnar_value,
rhs_columnar_value,
lhs_scale,
rhs_scale,
true,
)
.expect("Failed to scale and subtract");
let raw_result = result_evaluate_equals_zero(res.len(), alloc, res);
if is_literal {
ColumnarValue::Literal(LiteralValue::Boolean(raw_result[0]))
} else {
ColumnarValue::Column(Column::Boolean(raw_result))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Using an is_literal flag and returning a length 1 slice for Literals is very messy.
This code would be extremely difficult to modify in the future.

At this point, I think the result_evaluate_equals_zero is outdated and simply needs to be replaced with a proper solution.

There's no reason this can't look very similar to what the AndExpr::result_evaluate now looks like.

Copy link
Contributor

Choose a reason for hiding this comment

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

See #342 for a better alternative to the changes in this file.

Comment on lines 72 to 113
let is_literal = matches!(
(&lhs_columnar_value, &rhs_columnar_value),
(&ColumnarValue::Literal(_), &ColumnarValue::Literal(_))
);
let lhs_scale = self.lhs.data_type().scale().unwrap_or(0);
let rhs_scale = self.rhs.data_type().scale().unwrap_or(0);
let diff = if self.is_lte {
scale_and_subtract(alloc, lhs_column, rhs_column, lhs_scale, rhs_scale, false)
.expect("Failed to scale and subtract")
scale_and_subtract_columnar_value(
alloc,
lhs_columnar_value,
rhs_columnar_value,
lhs_scale,
rhs_scale,
false,
)
.expect("Failed to scale and subtract")
} else {
scale_and_subtract(alloc, rhs_column, lhs_column, rhs_scale, lhs_scale, false)
.expect("Failed to scale and subtract")
scale_and_subtract_columnar_value(
alloc,
rhs_columnar_value,
lhs_columnar_value,
rhs_scale,
lhs_scale,
false,
)
.expect("Failed to scale and subtract")
};
let diff_len = diff.len();

// diff == 0
let equals_zero = result_evaluate_equals_zero(table_length, alloc, diff);
let equals_zero = result_evaluate_equals_zero(diff_len, alloc, diff);

// sign(diff) == -1
let sign = result_evaluate_sign(table_length, alloc, diff);
let sign = result_evaluate_sign(diff_len, alloc, diff);

// (diff == 0) || (sign(diff) == -1)
Column::Boolean(result_evaluate_or(table_length, alloc, equals_zero, sign))
let raw_result = result_evaluate_or(diff_len, alloc, equals_zero, sign);
if is_literal {
ColumnarValue::Literal(LiteralValue::Boolean(raw_result[0]))
} else {
ColumnarValue::Column(Column::Boolean(raw_result))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Same concern here as with equality. This one is even worse.

.where_clause
.result_evaluate(alloc, accessor)
.into_column(input_length, alloc)
.expect("Failed to convert columnar value to column");
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a # Panics doc to ProverEvaluate indicating that the underlying columns must all have the same length?

@iajoiner iajoiner enabled auto-merge November 7, 2024 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants