MakeExplain parses the EXPLAIN option strings and generates an explain statement.
(options []string, stmt Statement)
| 171 | // MakeExplain parses the EXPLAIN option strings and generates an explain |
| 172 | // statement. |
| 173 | func MakeExplain(options []string, stmt Statement) (Statement, error) { |
| 174 | for i := range options { |
| 175 | options[i] = strings.ToUpper(options[i]) |
| 176 | } |
| 177 | find := func(o string) bool { |
| 178 | for i := range options { |
| 179 | if options[i] == o { |
| 180 | return true |
| 181 | } |
| 182 | } |
| 183 | return false |
| 184 | } |
| 185 | |
| 186 | if find("DEBUG") { |
| 187 | if !find("ANALYZE") { |
| 188 | return nil, pgerror.Newf(pgcode.Syntax, "DEBUG flag can only be used with EXPLAIN ANALYZE") |
| 189 | } |
| 190 | if len(options) != 2 { |
| 191 | return nil, pgerror.Newf( |
| 192 | pgcode.Syntax, "EXPLAIN ANALYZE (DEBUG) cannot be used in conjunction with other flags") |
| 193 | } |
| 194 | return &ExplainAnalyzeDebug{Statement: stmt}, nil |
| 195 | } |
| 196 | |
| 197 | var opts ExplainOptions |
| 198 | for _, opt := range options { |
| 199 | opt = strings.ToUpper(opt) |
| 200 | if m, ok := explainModeStringMap[opt]; ok { |
| 201 | if opts.Mode != 0 { |
| 202 | return nil, pgerror.Newf(pgcode.Syntax, "cannot set EXPLAIN mode more than once: %s", opt) |
| 203 | } |
| 204 | opts.Mode = m |
| 205 | continue |
| 206 | } |
| 207 | flag, ok := explainFlagStringMap[opt] |
| 208 | if !ok { |
| 209 | return nil, pgerror.Newf(pgcode.Syntax, "unsupported EXPLAIN option: %s", opt) |
| 210 | } |
| 211 | opts.Flags[flag] = true |
| 212 | } |
| 213 | analyze := opts.Flags[ExplainFlagAnalyze] |
| 214 | if opts.Mode == 0 { |
| 215 | if analyze { |
| 216 | // ANALYZE implies DISTSQL. |
| 217 | opts.Mode = ExplainDistSQL |
| 218 | } else { |
| 219 | // Default mode is ExplainPlan. |
| 220 | opts.Mode = ExplainPlan |
| 221 | } |
| 222 | } else if analyze && opts.Mode != ExplainDistSQL { |
| 223 | return nil, pgerror.Newf(pgcode.Syntax, "EXPLAIN ANALYZE cannot be used with %s", opts.Mode) |
| 224 | } |
| 225 | |
| 226 | return &Explain{ |
| 227 | ExplainOptions: opts, |
| 228 | Statement: stmt, |
| 229 | }, nil |
| 230 | } |