@Test public void testExplain() throws Exception { assertStatement("EXPLAIN SELECT * FROM t", new Explain(simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))), ImmutableList.of())); assertStatement("EXPLAIN (TYPE LOGICAL) SELECT * FROM t", new Explain( simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))), ImmutableList.of(new ExplainType(ExplainType.Type.LOGICAL)))); assertStatement("EXPLAIN (TYPE LOGICAL, FORMAT TEXT) SELECT * FROM t", new Explain( simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))), ImmutableList.of( new ExplainType(ExplainType.Type.LOGICAL), new ExplainFormat(ExplainFormat.Type.TEXT)))); }
@Override protected Void visitExplain(Explain node, Integer indent) { builder.append("EXPLAIN "); if (node.isAnalyze()) { builder.append("ANALYZE "); } List<String> options = new ArrayList<>(); for (ExplainOption option : node.getOptions()) { if (option instanceof ExplainType) { options.add("TYPE " + ((ExplainType) option).getType()); } else if (option instanceof ExplainFormat) { options.add("FORMAT " + ((ExplainFormat) option).getType()); } else { throw new UnsupportedOperationException("unhandled explain option: " + option); } } if (!options.isEmpty()) { builder.append("("); Joiner.on(", ").appendTo(builder, options); builder.append(")"); } builder.append("\n"); process(node.getStatement(), indent); return null; }
public String getExplainPlan(String query, ExplainType.Type planType) { QueryExplainer explainer = getQueryExplainer(); return transaction(queryRunner.getTransactionManager()) .singleStatement() .execute(queryRunner.getDefaultSession(), session -> { return explainer.getPlan(session, sqlParser.createStatement(query), planType); }); }
public String getGraphvizExplainPlan(String query, ExplainType.Type planType) { QueryExplainer explainer = getQueryExplainer(); return transaction(queryRunner.getTransactionManager()) .singleStatement() .execute(queryRunner.getDefaultSession(), session -> { return explainer.getGraphvizPlan(session, sqlParser.createStatement(query), planType); }); }
private String getQueryPlan(Explain node, ExplainType.Type planType, ExplainFormat.Type planFormat) throws IllegalArgumentException { switch (planFormat) { case GRAPHVIZ: return queryExplainer.get().getGraphvizPlan(session, node.getStatement(), planType); case TEXT: return queryExplainer.get().getPlan(session, node.getStatement(), planType); } throw new IllegalArgumentException("Invalid Explain Format: " + planFormat.toString()); }
@Override protected Void visitExplain(Explain node, Integer indent) { builder.append("EXPLAIN "); List<String> options = new ArrayList<>(); for (ExplainOption option : node.getOptions()) { if (option instanceof ExplainType) { options.add("TYPE " + ((ExplainType) option).getType()); } else if (option instanceof ExplainFormat) { options.add("FORMAT " + ((ExplainFormat) option).getType()); } else { throw new UnsupportedOperationException("unhandled explain option: " + option); } } if (!options.isEmpty()) { builder.append("("); Joiner.on(", ").appendTo(builder, options); builder.append(")"); } builder.append("\n"); process(node.getStatement(), indent); return null; }
@Override public Node visitExplainType(SqlBaseParser.ExplainTypeContext context) { switch (context.value.getType()) { case SqlBaseLexer.LOGICAL: return new ExplainType(getLocation(context), ExplainType.Type.LOGICAL); case SqlBaseLexer.DISTRIBUTED: return new ExplainType(getLocation(context), ExplainType.Type.DISTRIBUTED); } throw new IllegalArgumentException("Unsupported EXPLAIN type: " + context.value.getText()); }