Skip to content

Commit

Permalink
Add more string comparisons for lexicographical sorting (#48)
Browse files Browse the repository at this point in the history
Co-authored-by: Madeline Shortt <[email protected]>
  • Loading branch information
2 people authored and Scott Sandler committed Jul 9, 2020
1 parent b599304 commit 16a53aa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Expressions/BinaryOperatorExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,25 +148,27 @@ public function evaluate(row $row, AsyncMysqlConnection $conn): mixed {
}
case Operator::GREATER_THAN:
if ($as_string) {
return ((string)$l_value > (string)$r_value) ? 1 : 0 ^ $this->negatedInt;
return (((Str\compare((string)$l_value, (string)$r_value)) > 0) ? 1 : 0) ^ $this->negatedInt;
} else {
return ((int)$l_value > (int)$r_value) ? 1 : 0 ^ $this->negatedInt;
}
case Operator::GREATER_THAN_EQUALS:
if ($as_string) {
return ((string)$l_value >= (string)$r_value) ? 1 : 0 ^ $this->negatedInt;
$comparison = Str\compare((string)$l_value, (string)$r_value);
return (($comparison > 0 || $comparison === 0) ? 1 : 0) ^ $this->negatedInt;
} else {
return ((int)$l_value >= (int)$r_value) ? 1 : 0 ^ $this->negatedInt;
}
case Operator::LESS_THAN:
if ($as_string) {
return ((string)$l_value < (string)$r_value) ? 1 : 0 ^ $this->negatedInt;
return (((Str\compare((string)$l_value, (string)$r_value)) < 0) ? 1 : 0) ^ $this->negatedInt;
} else {
return ((int)$l_value < (int)$r_value) ? 1 : 0 ^ $this->negatedInt;
}
case Operator::LESS_THAN_EQUALS:
if ($as_string) {
return ((string)$l_value <= (string)$r_value) ? 1 : 0 ^ $this->negatedInt;
$comparison = Str\compare((string)$l_value, (string)$r_value);
return (($comparison < 0 || $comparison === 0) ? 1 : 0) ^ $this->negatedInt;
} else {
return ((int)$l_value <= (int)$r_value) ? 1 : 0 ^ $this->negatedInt;
}
Expand Down
34 changes: 34 additions & 0 deletions tests/SelectExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,40 @@ final class SelectExpressionTest extends HackTest {
dict['id' => 2],
],
);

$results = await $conn->query('select id, position from table6 WHERE position<\'625\' ORDER BY position DESC');
expect($results->rows())->toBeSame(
vec[
dict['id' => 1000, 'position' => '5'],
dict['id' => 1004, 'position' => '25'],
dict['id' => 1001, 'position' => '125'],
],
);

$results = await $conn->query('select id, position from table6 WHERE position<=\'625\' ORDER BY position DESC');
expect($results->rows())->toBeSame(
vec[
dict['id' => 1003, 'position' => '625'],
dict['id' => 1000, 'position' => '5'],
dict['id' => 1004, 'position' => '25'],
dict['id' => 1001, 'position' => '125'],
],
);

$results = await $conn->query('select id, position from table6 WHERE position>\'625\' ORDER BY position DESC');
expect($results->rows())->toBeSame(
vec[
dict['id' => 1002, 'position' => '75'],
],
);

$results = await $conn->query('select id, position from table6 WHERE position>=\'625\' ORDER BY position DESC');
expect($results->rows())->toBeSame(
vec[
dict['id' => 1002, 'position' => '75'],
dict['id' => 1003, 'position' => '625'],
],
);
}

public async function testBetween(): Awaitable<void> {
Expand Down

0 comments on commit 16a53aa

Please sign in to comment.