Skip to content

Commit

Permalink
Apply hackfmt with applicable args
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed May 8, 2023
1 parent 27a7a6a commit e8d106f
Show file tree
Hide file tree
Showing 19 changed files with 116 additions and 141 deletions.
3 changes: 1 addition & 2 deletions .hhconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
hackfmt.line_width=120
hackfmt.tabs=true
allowed_decl_fixme_codes=2053,3012,4045,4047,4341
allowed_fixme_codes_strict=2011,2049,2050,2053,2083,3012,3084,4027,4038,4045,4047,4104,4105,4106,4107,4108,4110,4128,4135,4188,4223,4240,4323,4341,4390,4401


2 changes: 1 addition & 1 deletion src/AsyncMysql/AsyncMysqlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function setPoolsConnectionLimit(int $_limit): void {}
int $_tcp_timeout_micros = 0,
string $_sni_server_name = '',
string $_server_cert_extension = '',
string $_server_cert_values = '',
string $_server_cert_values = '',
): Awaitable<\AsyncMysqlConnection> {
return new AsyncMysqlConnection($host, $port, $dbname);
}
Expand Down
5 changes: 2 additions & 3 deletions src/BuildSchemaCLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ protected function getSupportedOptions(): vec<CLIOptions\CLIOption> {
HackBuilderValues::shapeWithPerKeyRendering(
shape(
'name' => HackBuilderValues::export(),
'indexes' => HackBuilderValues::vec(
HackBuilderValues::shapeWithUniformRendering(HackBuilderValues::export()),
),
'indexes' =>
HackBuilderValues::vec(HackBuilderValues::shapeWithUniformRendering(HackBuilderValues::export())),
'fields' => HackBuilderValues::vec(HackBuilderValues::shapeWithPerKeyRendering(
shape(
'name' => HackBuilderValues::export(),
Expand Down
97 changes: 56 additions & 41 deletions src/DataIntegrity.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,51 +108,66 @@ public static function ensureFieldsPresent(dict<string, mixed> $row, table_schem
$row[$field_name] = (int)$row[$field_name];
}
} else {
$signed = !($field_unsigned);
$field_value = (int)$row[$field_name];
$signed = !($field_unsigned);
$field_value = (int)$row[$field_name];

switch($field_mysql_type) {
case DataType::TINYINT:
if ($field_value < (($signed) ? -\pow(2,7) : 0) || $field_value >= (($signed) ? \pow(2,7) : \pow(2,8))){
throw new SQLFakeRuntimeException(
"Column '{$field_name}' on '{$schema['name']}' expects a valid '{$field_mysql_type}'",
);
}
break;
case DataType::SMALLINT:
if ($field_value < (($signed) ? -\pow(2,15) : 0) || $field_value >= (($signed) ? \pow(2,15) : \pow(2,16))){
throw new SQLFakeRuntimeException(
"Column '{$field_name}' on '{$schema['name']}' expects a valid '{$field_mysql_type}'",
);
}
break;
case DataType::MEDIUMINT:
if ($field_value < (($signed) ? -\pow(2,23) : 0) || $field_value >= (($signed) ? \pow(2,23) : \pow(2,24))){
throw new SQLFakeRuntimeException(
"Column '{$field_name}' on '{$schema['name']}' expects a valid '{$field_mysql_type}'",
);
}
break;
case DataType::INT:
if ($field_value < (($signed) ? -\pow(2,31) : 0) || $field_value >= (($signed) ? \pow(2,31) : \pow(2,32))){
throw new SQLFakeRuntimeException(
"Column '{$field_name}' on '{$schema['name']}' expects a valid '{$field_mysql_type}'",
);
}
break;
case DataType::BIGINT:
if ($field_value < (($signed) ? -\pow(2,63) : 0) || $field_value >= (($signed) ? \pow(2,63) : \pow(2,64))){
throw new SQLFakeRuntimeException(
"Column '{$field_name}' on '{$schema['name']}' expects a valid '{$field_mysql_type}'",
);
}
break;
default:
switch ($field_mysql_type) {
case DataType::TINYINT:
if (
$field_value < (($signed) ? -\pow(2, 7) : 0) ||
$field_value >= (($signed) ? \pow(2, 7) : \pow(2, 8))
) {
throw new SQLFakeRuntimeException(
"Column '{$field_name}' on '{$schema['name']}' expects a valid '{$field_mysql_type}'",
);
break;
}
}
break;
case DataType::SMALLINT:
if (
$field_value < (($signed) ? -\pow(2, 15) : 0) ||
$field_value >= (($signed) ? \pow(2, 15) : \pow(2, 16))
) {
throw new SQLFakeRuntimeException(
"Column '{$field_name}' on '{$schema['name']}' expects a valid '{$field_mysql_type}'",
);
}
break;
case DataType::MEDIUMINT:
if (
$field_value < (($signed) ? -\pow(2, 23) : 0) ||
$field_value >= (($signed) ? \pow(2, 23) : \pow(2, 24))
) {
throw new SQLFakeRuntimeException(
"Column '{$field_name}' on '{$schema['name']}' expects a valid '{$field_mysql_type}'",
);
}
break;
case DataType::INT:
if (
$field_value < (($signed) ? -\pow(2, 31) : 0) ||
$field_value >= (($signed) ? \pow(2, 31) : \pow(2, 32))
) {
throw new SQLFakeRuntimeException(
"Column '{$field_name}' on '{$schema['name']}' expects a valid '{$field_mysql_type}'",
);
}
break;
case DataType::BIGINT:
if (
$field_value < (($signed) ? -\pow(2, 63) : 0) ||
$field_value >= (($signed) ? \pow(2, 63) : \pow(2, 64))
) {
throw new SQLFakeRuntimeException(
"Column '{$field_name}' on '{$schema['name']}' expects a valid '{$field_mysql_type}'",
);
}
break;
default:
throw new SQLFakeRuntimeException(
"Column '{$field_name}' on '{$schema['name']}' expects a valid '{$field_mysql_type}'",
);
break;
}
}
break;
case 'double':
Expand Down
5 changes: 2 additions & 3 deletions src/Expressions/ConstantExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ private function extractConstantValue(token $token): mixed {
case TokenType::NULL_CONSTANT:
return null;
default:
throw new SQLFakeRuntimeException(
"Attempted to assign invalid token type {$token['type']} to Constant Expression",
);
throw
new SQLFakeRuntimeException("Attempted to assign invalid token type {$token['type']} to Constant Expression");
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/Metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static function getTotalQueryCount(): int {
*/
public static function trackQuery(QueryType $type, string $host, string $table_name, string $sql): void {

if (!self::$enable){
if (!self::$enable) {
return;
}

Expand All @@ -117,7 +117,8 @@ protected static function getBacktrace(): string {

// filter out this library
if (
Str\contains($trace[0]['file'] ?? '', \realpath(__DIR__.'/..')) || ($trace[0]['class'] ?? '') === AsyncMysqlConnection::class
Str\contains($trace[0]['file'] ?? '', \realpath(__DIR__.'/..')) ||
($trace[0]['class'] ?? '') === AsyncMysqlConnection::class
) {
$trace = Vec\drop($trace, 1);
continue;
Expand Down
19 changes: 5 additions & 14 deletions src/Parser/CreateTableParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,8 @@ private function walk(vec<string> $tokens, string $sql, vec<(int, int)> $source_
if (C\count($temp)) {
$statements[] = shape(
'tuples' => $temp,
'sql' => Str\slice(
$sql,
$source_map[$start][0],
$source_map[$i][0] - $source_map[$start][0] + $source_map[$i][1],
),
'sql' =>
Str\slice($sql, $source_map[$start][0], $source_map[$i][0] - $source_map[$start][0] + $source_map[$i][1]),
);
}
$temp = vec[];
Expand All @@ -244,11 +241,8 @@ private function walk(vec<string> $tokens, string $sql, vec<(int, int)> $source_
if (C\count($temp)) {
$statements[] = shape(
'tuples' => $temp,
'sql' => Str\slice(
$sql,
$source_map[$start][0],
$source_map[$i][0] - $source_map[$start][0] + $source_map[$i][1],
),
'sql' =>
Str\slice($sql, $source_map[$start][0], $source_map[$i][0] - $source_map[$start][0] + $source_map[$i][1]),
);
}

Expand Down Expand Up @@ -859,10 +853,7 @@ private function extractTokens(string $sql, vec<(int, int)> $source_map): vec<st
// Extend the length of the first token to include everything
// up through the last in the sequence.
$j = $i + C\count($list) - 1;
$out_map[] = tuple(
$source_map[$i][0],
($source_map[$j][0] - $source_map[$i][0]) + $source_map[$j][1],
);
$out_map[] = tuple($source_map[$i][0], ($source_map[$j][0] - $source_map[$i][0]) + $source_map[$j][1]);

$i = $j + 1;
$found = true;
Expand Down
2 changes: 1 addition & 1 deletion src/Query/FromClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function process(AsyncMysqlConnection $conn, string $sql): dataset {

$new_dataset = vec[];
if ($schema is nonnull && QueryContext::$strictSchemaMode) {
foreach ($res as $row) {
foreach ($res as $row) {
$row as dict<_, _>;
$m = dict[];
foreach ($row as $field => $val) {
Expand Down
10 changes: 4 additions & 6 deletions src/Query/JoinProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,10 @@ protected static function addJoinFilterExpression(
string $right_column,
): BinaryOperatorExpression {

$left = new ColumnExpression(
shape('type' => TokenType::IDENTIFIER, 'value' => $left_column, 'raw' => $left_column),
);
$right = new ColumnExpression(
shape('type' => TokenType::IDENTIFIER, 'value' => $right_column, 'raw' => $right_column),
);
$left =
new ColumnExpression(shape('type' => TokenType::IDENTIFIER, 'value' => $left_column, 'raw' => $left_column));
$right =
new ColumnExpression(shape('type' => TokenType::IDENTIFIER, 'value' => $right_column, 'raw' => $right_column));

// making a binary expression ensuring those two tokens are equal
$expr = new BinaryOperatorExpression($left, /* $negated */ false, Operator::EQUALS, $right);
Expand Down
2 changes: 1 addition & 1 deletion src/SchemaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function generateFromString(string $sql): dict<string, table_schema> {
if ($default is nonnull && $default !== 'NULL') {
$f['default'] = $default;
}

$unsigned = ($field['unsigned'] ?? null);
if ($unsigned is nonnull) {
$f['unsigned'] = $unsigned;
Expand Down
8 changes: 3 additions & 5 deletions src/VitessQueryValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ public static function enablePrimaryVindexColumnValidator(): void {
public function getHandlers(): dict<string, (function(): Awaitable<void>)> {
$handlers = dict[];
if (static::$isPrimaryVindexColumnValidatorEnabled) {
$handlers[UnsupportedCases::PRIMARY_VINDEX_COLUMN] = async () ==>
await $this->updateChangesPrimaryVindexColumn();
$handlers[UnsupportedCases::PRIMARY_VINDEX_COLUMN] = async () ==> await $this->updateChangesPrimaryVindexColumn();
}

return $handlers;
Expand Down Expand Up @@ -205,9 +204,8 @@ private function isCrossShardQuery(): bool {
case MultiOperand::UNION_ALL:
case MultiOperand::INTERSECT:
case MultiOperand::EXCEPT:
throw new SQLFakeVitessQueryViolation(
Str\format('Vitess query validation error: %s', UnsupportedCases::UNIONS),
);
throw
new SQLFakeVitessQueryViolation(Str\format('Vitess query validation error: %s', UnsupportedCases::UNIONS));
}
}
}
Expand Down
15 changes: 6 additions & 9 deletions tests/JSONFunctionTest.hack
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,7 @@ final class JSONFunctionTest extends HackTest {
tuple("JSON_EXTRACT(JSON_EXTRACT('[true]', '$[0]'), '$')", shape('value' => 'true')),

// JSON as doc for JSON_REPLACE
tuple(
"JSON_REPLACE(JSON_EXTRACT('{\"a\":{\"b\": 2}}', '$.a'), '$.b', true)",
shape('value' => '{"b":true}'),
),
tuple("JSON_REPLACE(JSON_EXTRACT('{\"a\":{\"b\": 2}}', '$.a'), '$.b', true)", shape('value' => '{"b":true}')),
tuple("JSON_REPLACE(JSON_EXTRACT(\"[false]\", '$[0]'), '$.a', 'test')", shape('value' => 'false')),

tuple(
Expand All @@ -341,10 +338,7 @@ final class JSONFunctionTest extends HackTest {
tuple("JSON_REPLACE('{\"b\":2}', '$.b', 1 < 2)", shape('value' => '{"b":true}')),

// JSON value as replacement in JSON_REPLACE
tuple(
"JSON_REPLACE('[0,1]', '$[1]', JSON_EXTRACT('{\"a\": \"test\"}', '$.a'))",
shape('value' => '[0,"test"]'),
),
tuple("JSON_REPLACE('[0,1]', '$[1]', JSON_EXTRACT('{\"a\": \"test\"}', '$.a'))", shape('value' => '[0,"test"]')),
tuple(
"JSON_REPLACE('{\"a\":2}', JSON_UNQUOTE(JSON_EXTRACT('{\"b\":\"$.a\"}', '$.b')), 4)",
shape('value' => '{"a":4}'),
Expand Down Expand Up @@ -396,7 +390,10 @@ final class JSONFunctionTest extends HackTest {
tuple("JSON_CONTAINS('null', '2', '$.a')", shape('exception' => SQLFakeRuntimeException::class)),
tuple("JSON_CONTAINS('[]', 45, '$.a')", shape('exception' => SQLFakeRuntimeException::class)),
tuple("JSON_CONTAINS('2', 45, '$.a')", shape('exception' => SQLFakeRuntimeException::class)),
tuple("JSON_CONTAINS('{\"a\": {\"b\":\"test\"}}', '45', '$.b')", shape('exception' => SQLFakeRuntimeException::class)),
tuple(
"JSON_CONTAINS('{\"a\": {\"b\":\"test\"}}', '45', '$.b')",
shape('exception' => SQLFakeRuntimeException::class),
),

// existent path - array
tuple("JSON_CONTAINS('[]', '4', '$')", shape('value' => 0)),
Expand Down
15 changes: 6 additions & 9 deletions tests/JoinQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ final class JoinQueryTest extends HackTest {
$results = await $conn->query('SELECT * FROM table3 JOIN association_table ON id = table_3_id');
expect($results->rows())->toBeSame($expected, 'with no aliases and column names inferred from table schema');

$results = await $conn->query(
'SELECT * FROM table3 JOIN association_table ON table3.id = association_table.table_3_id',
);
$results =
await $conn->query('SELECT * FROM table3 JOIN association_table ON table3.id = association_table.table_3_id');
expect($results->rows())->toBeSame($expected, 'with columns using explicitly specified table names');

$results = await $conn->query(
Expand Down Expand Up @@ -91,9 +90,8 @@ final class JoinQueryTest extends HackTest {

public async function testLeftJoin(): Awaitable<void> {
$conn = static::$conn as nonnull;
$results = await $conn->query(
'SELECT id, table_4_id FROM table3 LEFT OUTER JOIN association_table ON id=table_3_id',
);
$results =
await $conn->query('SELECT id, table_4_id FROM table3 LEFT OUTER JOIN association_table ON id=table_3_id');
expect($results->rows())->toBeSame(vec[
dict['id' => 1, 'table_4_id' => 1000],
dict['id' => 1, 'table_4_id' => 1001],
Expand Down Expand Up @@ -121,9 +119,8 @@ final class JoinQueryTest extends HackTest {

public async function testCrossJoin(): Awaitable<void> {
$conn = static::$conn as nonnull;
$results = await $conn->query(
'SELECT table_3_id, id as table_4_id FROM association_table, table4 WHERE table4.id=1003',
);
$results =
await $conn->query('SELECT table_3_id, id as table_4_id FROM association_table, table4 WHERE table4.id=1003');
expect($results->rows())->toBeSame(vec[
dict['table_3_id' => 1, 'table_4_id' => 1003],
dict['table_3_id' => 1, 'table_4_id' => 1003],
Expand Down
5 changes: 2 additions & 3 deletions tests/MultiQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ final class MultiQueryTest extends HackTest {

public async function testSubqueryInSelect(): Awaitable<void> {
$conn = static::$conn as nonnull;
$results = await $conn->query(
'SELECT * FROM (SELECT id FROM table4 WHERE id = 1001 OR id = 1004) as sub WHERE id = 1001',
);
$results =
await $conn->query('SELECT * FROM (SELECT id FROM table4 WHERE id = 1001 OR id = 1004) as sub WHERE id = 1001');
expect($results->rows())->toBeSame(vec[
dict['id' => 1001],
]);
Expand Down

0 comments on commit e8d106f

Please sign in to comment.