|
13 | 13 | data_type_double_precision, |
14 | 14 | data_type_integer, |
15 | 15 | delete, |
| 16 | + eq, |
16 | 17 | func, |
17 | 18 | gt, |
18 | 19 | insert, |
19 | 20 | literal, |
20 | 21 | param, |
21 | 22 | row_expr, |
22 | 23 | select, |
| 24 | + sql_explain_config, |
23 | 25 | star, |
| 26 | + table, |
24 | 27 | values_table |
25 | 28 | }; |
26 | 29 | use function Flow\Types\DSL\type_integer; |
27 | 30 | use Flow\PostgreSql\Client\Exception\QueryException; |
28 | 31 | use Flow\PostgreSql\Client\TypedValue; |
| 32 | +use Flow\PostgreSql\Explain\Plan\{Plan, PlanNodeType}; |
29 | 33 | use Flow\PostgreSql\QueryBuilder\Schema\{ColumnDefinition, DataType}; |
30 | 34 |
|
31 | 35 | final class PgSqlClientTest extends ClientTestCase |
@@ -57,6 +61,91 @@ public function test_execute_returns_affected_rows() : void |
57 | 61 | self::assertSame(2, $affected); |
58 | 62 | } |
59 | 63 |
|
| 64 | + public function test_explain_returns_plan_for_select_query() : void |
| 65 | + { |
| 66 | + $this->client->execute( |
| 67 | + create()->temporaryTable('test_explain') |
| 68 | + ->column(ColumnDefinition::create('id', DataType::integer())) |
| 69 | + ->column(ColumnDefinition::create('name', DataType::text())) |
| 70 | + ); |
| 71 | + |
| 72 | + $plan = $this->client->explain( |
| 73 | + select(star())->from(table('test_explain')) |
| 74 | + ); |
| 75 | + |
| 76 | + self::assertInstanceOf(Plan::class, $plan); |
| 77 | + self::assertInstanceOf(PlanNodeType::class, $plan->rootNode()->nodeType()); |
| 78 | + self::assertNotNull($plan->executionTime()); |
| 79 | + self::assertNotNull($plan->planningTime()); |
| 80 | + } |
| 81 | + |
| 82 | + public function test_explain_returns_plan_with_parameters() : void |
| 83 | + { |
| 84 | + $this->client->execute( |
| 85 | + create()->temporaryTable('test_explain_params') |
| 86 | + ->column(ColumnDefinition::create('id', DataType::integer())) |
| 87 | + ->column(ColumnDefinition::create('name', DataType::text())) |
| 88 | + ); |
| 89 | + |
| 90 | + $plan = $this->client->explain( |
| 91 | + select(star())->from(table('test_explain_params'))->where(eq(col('id'), param(1))), |
| 92 | + [42] |
| 93 | + ); |
| 94 | + |
| 95 | + self::assertInstanceOf(Plan::class, $plan); |
| 96 | + self::assertGreaterThanOrEqual(0.0, $plan->totalCost()); |
| 97 | + } |
| 98 | + |
| 99 | + public function test_explain_returns_plan_with_raw_sql() : void |
| 100 | + { |
| 101 | + $plan = $this->client->explain( |
| 102 | + 'SELECT $1::int + $2::int', |
| 103 | + [10, 32] |
| 104 | + ); |
| 105 | + |
| 106 | + self::assertInstanceOf(Plan::class, $plan); |
| 107 | + self::assertSame(PlanNodeType::RESULT, $plan->rootNode()->nodeType()); |
| 108 | + } |
| 109 | + |
| 110 | + public function test_explain_with_custom_config() : void |
| 111 | + { |
| 112 | + $this->client->execute( |
| 113 | + create()->temporaryTable('test_explain_config') |
| 114 | + ->column(ColumnDefinition::create('id', DataType::integer())) |
| 115 | + ); |
| 116 | + |
| 117 | + $plan = $this->client->explain( |
| 118 | + select(star())->from(table('test_explain_config')), |
| 119 | + config: sql_explain_config( |
| 120 | + analyze: true, |
| 121 | + buffers: true, |
| 122 | + timing: true, |
| 123 | + costs: true, |
| 124 | + ) |
| 125 | + ); |
| 126 | + |
| 127 | + self::assertInstanceOf(Plan::class, $plan); |
| 128 | + self::assertNotNull($plan->executionTime()); |
| 129 | + self::assertGreaterThanOrEqual(0.0, $plan->rootNode()->cost()->totalCost()); |
| 130 | + } |
| 131 | + |
| 132 | + public function test_explain_without_analyze() : void |
| 133 | + { |
| 134 | + $plan = $this->client->explain( |
| 135 | + select(literal(1)), |
| 136 | + config: sql_explain_config( |
| 137 | + analyze: false, |
| 138 | + buffers: false, |
| 139 | + timing: false, |
| 140 | + ) |
| 141 | + ); |
| 142 | + |
| 143 | + self::assertInstanceOf(Plan::class, $plan); |
| 144 | + self::assertGreaterThanOrEqual(0.0, $plan->totalCost()); |
| 145 | + self::assertNull($plan->executionTime()); |
| 146 | + self::assertNull($plan->rootNode()->timing()); |
| 147 | + } |
| 148 | + |
60 | 149 | public function test_fetch_all_into_maps_to_objects() : void |
61 | 150 | { |
62 | 151 | $users = $this->client->fetchAllInto( |
|
0 commit comments