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

Evaluate expressions in IN predicates when performing index checks #108

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ protected function applyWhere(
$all_matched = false;

if ($columns is nonnull && $indexes) {
$data = QueryPlanner::filterWithIndexes($data, $index_refs, $columns, $indexes, $where, inout $all_matched);
$data = QueryPlanner::filterWithIndexes(
$conn,
$data,
$index_refs,
$columns,
$indexes,
$where,
inout $all_matched,
);
}

if (!$all_matched) {
Expand Down
17 changes: 10 additions & 7 deletions src/Query/QueryPlanner.hack
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use namespace HH\Lib\{C, Dict, Keyset, Str, Vec};

abstract class QueryPlanner {
public static function filterWithIndexes(
AsyncMysqlConnection $conn,
dataset $data,
index_refs $index_refs,
dict<string, Column> $columns,
Expand All @@ -17,7 +18,7 @@ abstract class QueryPlanner {

if (C\count($ored_wheres) === 1) {
$matched_all_expressions = true;
$candidates = self::getIndexCandidates($where, $columns, inout $matched_all_expressions);
$candidates = self::getIndexCandidates($conn, $where, $columns, inout $matched_all_expressions);
if ($candidates) {
$all_expressions_indexed = true;
$filtered_keys = self::getKeysForConditional(
Expand Down Expand Up @@ -48,7 +49,7 @@ abstract class QueryPlanner {

foreach ($ored_wheres as $ored_where) {
$matched_all_expressions = true;
$candidates = self::getIndexCandidates($ored_where, $columns, inout $matched_all_expressions);
$candidates = self::getIndexCandidates($conn, $ored_where, $columns, inout $matched_all_expressions);

if ($candidates) {
$filtered_keys = self::getKeysForConditional(
Expand Down Expand Up @@ -249,12 +250,13 @@ abstract class QueryPlanner {
}

private static function getIndexCandidates(
AsyncMysqlConnection $conn,
Expression $expr,
dict<string, Column> $columns,
inout bool $matched_all_expressions,
): dict<string, vec<mixed>> {
if ($expr is BinaryOperatorExpression) {
return self::getIndexCandidatesFromBinop($expr, $columns, inout $matched_all_expressions);
return self::getIndexCandidatesFromBinop($conn, $expr, $columns, inout $matched_all_expressions);
}

if ($expr is InOperatorExpression && !$expr->negated) {
Expand Down Expand Up @@ -282,8 +284,8 @@ abstract class QueryPlanner {
foreach (($expr->inList as nonnull) as $in_expr) {
// found it? return the opposite of "negated". so if negated is false, return true.
// if it's a subquery, we have to iterate over the results and extract the field from each row
if ($in_expr is ConstantExpression) {
$value = $in_expr->value;
if ($in_expr is ConstantExpression || $in_expr is BinaryOperatorExpression) {
$value = $in_expr->evaluateImpl(dict[], $conn);
if (isset($columns[$column_name])) {
if ($columns[$column_name]->hack_type === 'int' && $value is string) {
$value = (int)$value;
Expand All @@ -310,6 +312,7 @@ abstract class QueryPlanner {
}

private static function getIndexCandidatesFromBinop(
AsyncMysqlConnection $conn,
BinaryOperatorExpression $expr,
dict<string, Column> $columns,
inout bool $matched_all_expressions,
Expand Down Expand Up @@ -359,10 +362,10 @@ abstract class QueryPlanner {
}

if ($expr->operator === Operator::AND) {
$column_names = self::getIndexCandidates($expr->left, $columns, inout $matched_all_expressions);
$column_names = self::getIndexCandidates($conn, $expr->left, $columns, inout $matched_all_expressions);
$column_names = Dict\merge(
$column_names,
self::getIndexCandidates($expr->right as nonnull, $columns, inout $matched_all_expressions),
self::getIndexCandidates($conn, $expr->right as nonnull, $columns, inout $matched_all_expressions),
);

return $column_names;
Expand Down
Loading