|
| 1 | +/*jshint globalstrict:false, strict:false, maxlen: 500 */ |
| 2 | +/*global fail, assertEqual, assertTrue, assertFalse, AQL_EXPLAIN */ |
| 3 | +//////////////////////////////////////////////////////////////////////////////// |
| 4 | +/// @brief tests for optimizer rules |
| 5 | +/// |
| 6 | +/// @file |
| 7 | +/// |
| 8 | +/// DISCLAIMER |
| 9 | +/// |
| 10 | +/// Copyright 2010-2012 triagens GmbH, Cologne, Germany |
| 11 | +/// |
| 12 | +/// Licensed under the Apache License, Version 2.0 (the "License"); |
| 13 | +/// you may not use this file except in compliance with the License. |
| 14 | +/// You may obtain a copy of the License at |
| 15 | +/// |
| 16 | +/// http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | +/// |
| 18 | +/// Unless required by applicable law or agreed to in writing, software |
| 19 | +/// distributed under the License is distributed on an "AS IS" BASIS, |
| 20 | +/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | +/// See the License for the specific language governing permissions and |
| 22 | +/// limitations under the License. |
| 23 | +/// |
| 24 | +/// Copyright holder is triAGENS GmbH, Cologne, Germany |
| 25 | +/// |
| 26 | +/// @author Jan Steemann |
| 27 | +/// @author Copyright 2012, triAGENS GmbH, Cologne, Germany |
| 28 | +//////////////////////////////////////////////////////////////////////////////// |
| 29 | + |
| 30 | +let jsunity = require("jsunity"); |
| 31 | +let errors = require("internal").errors; |
| 32 | +let db = require("@arangodb").db; |
| 33 | + |
| 34 | +/// @brief test suite |
| 35 | +function explainSuite () { |
| 36 | + const cn = "UnitTestsAhuacatlExplain"; |
| 37 | + |
| 38 | + return { |
| 39 | + |
| 40 | + setUpAll : function () { |
| 41 | + db._drop(cn); |
| 42 | + db._create(cn); |
| 43 | + }, |
| 44 | + |
| 45 | + tearDownAll : function () { |
| 46 | + db._drop(cn); |
| 47 | + }, |
| 48 | + |
| 49 | + testExplainStats : function () { |
| 50 | + let query = "FOR doc IN " + cn + " FILTER doc.value > 33 RETURN doc"; |
| 51 | + let actual = AQL_EXPLAIN(query).stats; |
| 52 | + assertEqual(1, actual.plansCreated); |
| 53 | + assertTrue(actual.hasOwnProperty("plansCreated")); |
| 54 | + assertTrue(actual.hasOwnProperty("rulesExecuted")); |
| 55 | + assertTrue(actual.hasOwnProperty("rulesSkipped")); |
| 56 | + assertFalse(actual.hasOwnProperty("rules")); |
| 57 | + }, |
| 58 | + |
| 59 | + testExplainStatsRules : function () { |
| 60 | + let query = "FOR doc IN " + cn + " FILTER doc.value > 33 RETURN doc"; |
| 61 | + let actual = AQL_EXPLAIN(query, null, { profile: 2 }).stats; |
| 62 | + assertEqual(1, actual.plansCreated); |
| 63 | + assertTrue(actual.hasOwnProperty("plansCreated")); |
| 64 | + assertTrue(actual.hasOwnProperty("rulesExecuted")); |
| 65 | + assertTrue(actual.hasOwnProperty("rulesSkipped")); |
| 66 | + assertTrue(actual.hasOwnProperty("rules")); |
| 67 | + let runRules = actual.rules; |
| 68 | + assertTrue(runRules.hasOwnProperty("move-filters-into-enumerate")); |
| 69 | + assertTrue(runRules.hasOwnProperty("remove-filter-covered-by-index")); |
| 70 | + }, |
| 71 | + |
| 72 | +//////////////////////////////////////////////////////////////////////////////// |
| 73 | +/// @brief test bind parameters |
| 74 | +//////////////////////////////////////////////////////////////////////////////// |
| 75 | + |
| 76 | + testExplainBindMissing : function () { |
| 77 | + const query = "RETURN @foo"; |
| 78 | + |
| 79 | + try { |
| 80 | + AQL_EXPLAIN(query); |
| 81 | + fail(); |
| 82 | + } catch (err) { |
| 83 | + assertEqual(err.errorNum, errors.ERROR_QUERY_BIND_PARAMETER_MISSING.code); |
| 84 | + } |
| 85 | + }, |
| 86 | + |
| 87 | +//////////////////////////////////////////////////////////////////////////////// |
| 88 | +/// @brief test bind parameters |
| 89 | +//////////////////////////////////////////////////////////////////////////////// |
| 90 | + |
| 91 | + testExplainBindPresent : function () { |
| 92 | + const query = "RETURN @foo"; |
| 93 | + |
| 94 | + let actual = AQL_EXPLAIN(query, { foo: "bar" }); |
| 95 | + assertEqual(3, actual.plan.nodes.length); |
| 96 | + assertEqual("SingletonNode", actual.plan.nodes[0].type); |
| 97 | + assertEqual("CalculationNode", actual.plan.nodes[1].type); |
| 98 | + assertEqual("ReturnNode", actual.plan.nodes[2].type); |
| 99 | + }, |
| 100 | + |
| 101 | +//////////////////////////////////////////////////////////////////////////////// |
| 102 | +/// @brief test verbosity w/ single plan |
| 103 | +//////////////////////////////////////////////////////////////////////////////// |
| 104 | + |
| 105 | + testExplainVerbosity : function () { |
| 106 | + const query = "FOR i IN " + cn + " FOR j IN " + cn + " RETURN i"; |
| 107 | + |
| 108 | + // single plan, no options |
| 109 | + let actual = AQL_EXPLAIN(query); |
| 110 | + assertTrue(actual.hasOwnProperty("plan")); |
| 111 | + assertFalse(Array.isArray(actual.plan)); |
| 112 | + assertTrue(actual.plan.hasOwnProperty("nodes")); |
| 113 | + assertTrue(Array.isArray(actual.plan.nodes)); |
| 114 | + assertTrue(actual.plan.hasOwnProperty("rules")); |
| 115 | + assertTrue(Array.isArray(actual.plan.rules)); |
| 116 | + assertTrue(actual.plan.hasOwnProperty("estimatedCost")); |
| 117 | + |
| 118 | + actual.plan.nodes.forEach(function(node) { |
| 119 | + assertTrue(node.hasOwnProperty("type")); |
| 120 | + assertFalse(node.hasOwnProperty("typeID")); // deactivated if not verbose |
| 121 | + assertTrue(node.hasOwnProperty("dependencies")); |
| 122 | + assertTrue(Array.isArray(node.dependencies)); |
| 123 | + assertFalse(node.hasOwnProperty("parents")); // deactivated if not verbose |
| 124 | + assertTrue(node.hasOwnProperty("id")); |
| 125 | + assertTrue(node.hasOwnProperty("estimatedCost")); |
| 126 | + }); |
| 127 | + |
| 128 | + // single plan, verbose options |
| 129 | + actual = AQL_EXPLAIN(query, { }, { verbosePlans: true }); |
| 130 | + assertTrue(actual.hasOwnProperty("plan")); |
| 131 | + assertFalse(Array.isArray(actual.plan)); |
| 132 | + assertTrue(actual.plan.hasOwnProperty("nodes")); |
| 133 | + assertTrue(Array.isArray(actual.plan.nodes)); |
| 134 | + assertTrue(actual.plan.hasOwnProperty("rules")); |
| 135 | + assertTrue(Array.isArray(actual.plan.rules)); |
| 136 | + |
| 137 | + actual.plan.nodes.forEach(function(node) { |
| 138 | + assertTrue(node.hasOwnProperty("type")); |
| 139 | + assertTrue(node.hasOwnProperty("typeID")); |
| 140 | + assertTrue(node.hasOwnProperty("dependencies")); |
| 141 | + assertTrue(Array.isArray(node.dependencies)); |
| 142 | + assertTrue(node.hasOwnProperty("parents")); |
| 143 | + assertTrue(Array.isArray(node.parents)); |
| 144 | + assertTrue(node.hasOwnProperty("id")); |
| 145 | + assertTrue(node.hasOwnProperty("estimatedCost")); |
| 146 | + }); |
| 147 | + }, |
| 148 | + |
| 149 | +//////////////////////////////////////////////////////////////////////////////// |
| 150 | +/// @brief test explain w/ a signle plan vs. all plans |
| 151 | +//////////////////////////////////////////////////////////////////////////////// |
| 152 | + |
| 153 | + testExplainAllPlansVsSingle : function () { |
| 154 | + const query = "FOR i IN " + cn + " FOR j IN " + cn + " RETURN i"; |
| 155 | + |
| 156 | + // single plan |
| 157 | + let actual = AQL_EXPLAIN(query, { }, { verbosePlans: true }); |
| 158 | + assertTrue(actual.hasOwnProperty("plan")); |
| 159 | + assertFalse(actual.hasOwnProperty("plans")); |
| 160 | + assertFalse(Array.isArray(actual.plan)); |
| 161 | + |
| 162 | + assertTrue(actual.plan.hasOwnProperty("nodes")); |
| 163 | + assertTrue(Array.isArray(actual.plan.nodes)); |
| 164 | + |
| 165 | + actual.plan.nodes.forEach(function(node) { |
| 166 | + assertTrue(node.hasOwnProperty("type")); |
| 167 | + assertTrue(node.hasOwnProperty("typeID")); |
| 168 | + assertTrue(node.hasOwnProperty("dependencies")); |
| 169 | + assertTrue(Array.isArray(node.dependencies)); |
| 170 | + assertTrue(node.hasOwnProperty("parents")); |
| 171 | + assertTrue(node.hasOwnProperty("id")); |
| 172 | + assertTrue(node.hasOwnProperty("estimatedCost")); |
| 173 | + }); |
| 174 | + |
| 175 | + assertTrue(actual.plan.hasOwnProperty("rules")); |
| 176 | + assertTrue(Array.isArray(actual.plan.rules)); |
| 177 | + |
| 178 | + // multiple plans |
| 179 | + actual = AQL_EXPLAIN(query, { }, { allPlans: true, verbosePlans: true }); |
| 180 | + assertFalse(actual.hasOwnProperty("plan")); |
| 181 | + assertTrue(actual.hasOwnProperty("plans")); |
| 182 | + assertTrue(Array.isArray(actual.plans)); |
| 183 | + |
| 184 | + actual.plans.forEach(function (plan) { |
| 185 | + assertTrue(plan.hasOwnProperty("nodes")); |
| 186 | + assertTrue(Array.isArray(plan.nodes)); |
| 187 | + |
| 188 | + plan.nodes.forEach(function(node) { |
| 189 | + assertTrue(node.hasOwnProperty("type")); |
| 190 | + assertTrue(node.hasOwnProperty("typeID")); |
| 191 | + assertTrue(node.hasOwnProperty("dependencies")); |
| 192 | + assertTrue(Array.isArray(node.dependencies)); |
| 193 | + assertTrue(node.hasOwnProperty("parents")); |
| 194 | + assertTrue(node.hasOwnProperty("id")); |
| 195 | + assertTrue(node.hasOwnProperty("estimatedCost")); |
| 196 | + }); |
| 197 | + |
| 198 | + assertTrue(plan.hasOwnProperty("rules")); |
| 199 | + assertTrue(Array.isArray(plan.rules)); |
| 200 | + }); |
| 201 | + }, |
| 202 | + |
| 203 | + }; |
| 204 | +} |
| 205 | + |
| 206 | +jsunity.run(explainSuite); |
| 207 | + |
| 208 | +return jsunity.done(); |
0 commit comments