public static void printStatement(String sql) { println(sql.trim()); println(""); System.out.println("STATE Printing CommonTree toString..."); CommonTree tree = VeroSqlParser.parseStatement(sql); println(TreePrinter.treeToString(tree)); println(""); System.out.println("STATE Printing AST toString..."); Statement statement = VeroSqlParser.createStatement(tree); println(statement.toString()); println(""); System.out.println("STATE Printing Format sql toString..."); // TODO: support formatting all statement types if (statement instanceof Query) { println(FormatterFactory.getSqlFormatter().formatSql(statement)); println(""); } println(repeat("=", 60)); println(""); }
private String getFormattedSql(CreateView statement) { Query query = statement.getQuery(); String sql = formatSql(query); // verify round-trip Statement parsed; try { parsed = sqlParser.createStatement(sql); } catch (ParsingException e) { throw new PrestoException(INTERNAL_ERROR, "Formatted query does not parse: " + query); } if (!query.equals(parsed)) { throw new PrestoException(INTERNAL_ERROR, "Query does not round-trip: " + query); } return sql; }
@Inject public DataDefinitionExecutionFactory( LocationFactory locationFactory, TransactionManager transactionManager, MetadataManager metadata, AccessControl accessControl, @ForQueryExecution ExecutorService executor, Map<Class<? extends Statement>, DataDefinitionTask<?>> tasks) { this.locationFactory = requireNonNull(locationFactory, "locationFactory is null"); this.transactionManager = requireNonNull(transactionManager, "transactionManager is null"); this.metadata = requireNonNull(metadata, "metadata is null"); this.accessControl = requireNonNull(accessControl, "accessControl is null"); this.executor = requireNonNull(executor, "executor is null"); this.tasks = requireNonNull(tasks, "tasks is null"); }
@Override public DataDefinitionExecution<?> createQueryExecution( QueryId queryId, String query, Session session, Statement statement) { URI self = locationFactory.createQueryLocation(queryId); DataDefinitionTask<Statement> task = getTask(statement); checkArgument(task != null, "no task for statement: %s", statement.getClass().getSimpleName()); QueryStateMachine stateMachine = QueryStateMachine.begin(queryId, query, session, self, task.isTransactionControl(), transactionManager, executor); stateMachine.setUpdateType(task.getName()); return new DataDefinitionExecution<>(task, statement, transactionManager, metadata, accessControl, stateMachine); }
@Inject public QueryExplainer( List<PlanOptimizer> planOptimizers, Metadata metadata, AccessControl accessControl, SqlParser sqlParser, Map<Class<? extends Statement>, DataDefinitionTask<?>> dataDefinitionTask, FeaturesConfig featuresConfig) { this(planOptimizers, metadata, accessControl, sqlParser, dataDefinitionTask, featuresConfig.isExperimentalSyntaxEnabled()); }
public String getPlan(Session session, Statement statement, Type planType) { DataDefinitionTask<?> task = dataDefinitionTask.get(statement.getClass()); if (task != null) { return explainTask(statement, task); } switch (planType) { case LOGICAL: Plan plan = getLogicalPlan(session, statement); return PlanPrinter.textLogicalPlan(plan.getRoot(), plan.getTypes(), metadata, session); case DISTRIBUTED: SubPlan subPlan = getDistributedPlan(session, statement); return PlanPrinter.textDistributedPlan(subPlan, metadata, session); } throw new IllegalArgumentException("Unhandled plan type: " + planType); }
public String getGraphvizPlan(Session session, Statement statement, Type planType) { DataDefinitionTask<?> task = dataDefinitionTask.get(statement.getClass()); if (task != null) { // todo format as graphviz return explainTask(statement, task); } switch (planType) { case LOGICAL: Plan plan = getLogicalPlan(session, statement); return PlanPrinter.graphvizLogicalPlan(plan.getRoot(), plan.getTypes()); case DISTRIBUTED: SubPlan subPlan = getDistributedPlan(session, statement); return PlanPrinter.graphvizDistributedPlan(subPlan); } throw new IllegalArgumentException("Unhandled plan type: " + planType); }
private static void printStatement(String sql) { println(sql.trim()); println(""); Statement statement = SQL_PARSER.createStatement(sql); println(statement.toString()); println(""); println(SqlFormatter.formatSql(statement)); println(""); assertFormattedSql(SQL_PARSER, statement); println(repeat("=", 60)); println(""); }
@Test public void testSimpleQuery() throws Exception { Statement statement = new SqlParser().createStatement("select * from testcollection"); assertEquals(formatQuery(statement, name -> "dummy", '"').trim(), "SELECT *\n" + " FROM\n" + " dummy"); }
@Test(enabled = false) public void testName() throws Exception { String s = "select count(*) from test where ali = 5"; Statement statement = new SqlParser().createStatement(s); Analysis analysis = new Analysis(statement, ImmutableList.of(), false); Session build = Session.builder(new SessionPropertyManager()) .setQueryId(QueryId.valueOf("test")) .setCatalog("test") .setCatalog("test") .setCatalog("test") .build(); QueryPlanner queryPlanner = new QueryPlanner(analysis, new SymbolAllocator(), new PlanNodeIdAllocator(), null, null, build); RelationPlan plan = queryPlanner.plan((Query) statement); // EffectivePredicateExtractor.extract(plan.getRoot(), ImmutableMap.of(new Symbol("ali"), BIGINT)); EffectivePredicateExtractor.extract(plan.getRoot()); }
public static Set<Table> tablesUsedByQuery(String query, String defaultConnector, String defaultSchema) { List<String> statements = STATEMENT_SPLITTER.splitToList(query); ImmutableSet.Builder<Table> tables = ImmutableSet.builder(); CatalogSchemaContext context = new CatalogSchemaContext(defaultConnector, defaultSchema); for (String strStatement : statements) { InputReferenceExtractor extractor = new InputReferenceExtractor(); Statement statement = SQL_PARSER.createStatement(strStatement); context = statement.accept(extractor, context); tables.addAll(extractor.getReferences()); } return tables.build(); }
public String formatSql(Node root) { StringBuilder builder = new StringBuilder(); if (root instanceof Statement) { setOutputPassFunc(false); setOutputDivideByZeroGuard(true); } else { // Expression setOutputPassFunc(true); setOutputDivideByZeroGuard(true); } new Formatter(builder).process(root, 0); return builder.toString(); }
public String formatSql(Node root) { StringBuilder builder = new StringBuilder(); if (root instanceof Statement) { setOutputPassFunc(false); setOutputDivideByZeroGuard(false); } else { // Expression setOutputPassFunc(true); setOutputDivideByZeroGuard(false); } new Formatter(builder).process(root, 0); return builder.toString(); }
private static void format(String sql) { Statement statement = SQL_PARSER.createStatement(sql); String formattedSql = formatSql(statement); checkState(statement.equals(SQL_PARSER.createStatement(formattedSql)), "Formatted SQL is different than original"); System.out.println(formattedSql + ";"); }
private void testQuery(String query) { System.out.println(">>>> Testing: " + query); Statement statement = SQL_PARSER.createStatement(query); String formmatted = formatSql(statement); System.out.println(formmatted); assertEquals(formmatted, formatSql(SQL_PARSER.createStatement(formmatted))); }
@Override public SqlQueryExecution createQueryExecution(QueryId queryId, String query, Session session, Statement statement) { String executionPolicyName = SystemSessionProperties.getExecutionPolicy(session); ExecutionPolicy executionPolicy = executionPolicies.get(executionPolicyName); checkArgument(executionPolicy != null, "No execution policy %s", executionPolicy); return new SqlQueryExecution( queryId, query, session, locationFactory.createQueryLocation(queryId), statement, transactionManager, metadata, accessControl, sqlParser, splitManager, nodeScheduler, planOptimizers, remoteTaskFactory, locationFactory, scheduleSplitBatchSize, experimentalSyntaxEnabled, executor, nodeTaskMap, queryExplainer, executionPolicy); }
private static <T extends Statement> void bindDataDefinitionTask( Binder binder, MapBinder<Class<? extends Statement>, QueryExecutionFactory<?>> executionBinder, Class<T> statement, Class<? extends DataDefinitionTask<T>> task) { MapBinder<Class<? extends Statement>, DataDefinitionTask<?>> taskBinder = newMapBinder(binder, new TypeLiteral<Class<? extends Statement>>() {}, new TypeLiteral<DataDefinitionTask<?>>() {}); taskBinder.addBinding(statement).to(task).in(Scopes.SINGLETON); executionBinder.addBinding(statement).to(DataDefinitionExecutionFactory.class).in(Scopes.SINGLETON); }
public Analysis analyze(Statement statement) { Analysis analysis = new Analysis(); StatementAnalyzer analyzer = new StatementAnalyzer(analysis, metadata, sqlParser, accessControl, session, experimentalSyntaxEnabled, queryExplainer); RelationType outputDescriptor = analyzer.process(statement, new AnalysisContext()); analysis.setOutputDescriptor(outputDescriptor); return analysis; }
private static void validateColumnNames(Statement node, RelationType descriptor) { // verify that all column names are specified and unique // TODO: collect errors and return them all at once Set<String> names = new HashSet<>(); for (Field field : descriptor.getVisibleFields()) { Optional<String> fieldName = field.getName(); if (!fieldName.isPresent()) { throw new SemanticException(COLUMN_NAME_NOT_SPECIFIED, node, "Column name not specified at position %s", descriptor.indexOf(field) + 1); } if (!names.add(fieldName.get())) { throw new SemanticException(DUPLICATE_COLUMN_NAME, node, "Column name '%s' specified more than once", fieldName.get()); } } }
private Query parseView(String view, QualifiedObjectName name, Table node) { try { Statement statement = sqlParser.createStatement(view); return checkType(statement, Query.class, "parsed view"); } catch (ParsingException e) { throw new SemanticException(VIEW_PARSE_ERROR, node, "Failed parsing stored view '%s': %s", name, e.getMessage()); } }
public QueryExplainer( List<PlanOptimizer> planOptimizers, Metadata metadata, AccessControl accessControl, SqlParser sqlParser, Map<Class<? extends Statement>, DataDefinitionTask<?>> dataDefinitionTask, boolean experimentalSyntaxEnabled) { this.planOptimizers = requireNonNull(planOptimizers, "planOptimizers is null"); this.metadata = requireNonNull(metadata, "metadata is null"); this.accessControl = requireNonNull(accessControl, "accessControl is null"); this.sqlParser = requireNonNull(sqlParser, "sqlParser is null"); this.experimentalSyntaxEnabled = experimentalSyntaxEnabled; this.dataDefinitionTask = ImmutableMap.copyOf(requireNonNull(dataDefinitionTask, "dataDefinitionTask is null")); }
private Plan getLogicalPlan(Session session, Statement statement) { // analyze statement Analyzer analyzer = new Analyzer(session, metadata, sqlParser, accessControl, Optional.of(this), experimentalSyntaxEnabled); Analysis analysis = analyzer.analyze(statement); PlanNodeIdAllocator idAllocator = new PlanNodeIdAllocator(); // plan statement LogicalPlanner logicalPlanner = new LogicalPlanner(session, planOptimizers, idAllocator, metadata); return logicalPlanner.plan(analysis); }
private SubPlan getDistributedPlan(Session session, Statement statement) { // analyze statement Analyzer analyzer = new Analyzer(session, metadata, sqlParser, accessControl, Optional.of(this), experimentalSyntaxEnabled); Analysis analysis = analyzer.analyze(statement); PlanNodeIdAllocator idAllocator = new PlanNodeIdAllocator(); // plan statement LogicalPlanner logicalPlanner = new LogicalPlanner(session, planOptimizers, idAllocator, metadata); Plan plan = logicalPlanner.plan(analysis); return new PlanFragmenter().createSubPlans(plan); }
private void analyze(Session clientSession, @Language("SQL") String query) { transaction(transactionManager) .singleStatement() .readUncommitted() .readOnly() .execute(clientSession, session -> { Analyzer analyzer = createAnalyzer(session, metadata, true); Statement statement = SQL_PARSER.createStatement(query); analyzer.analyze(statement); }); }
private void analyzeWithoutExperimentalSyntax(@Language("SQL") String query) { transaction(transactionManager) .singleStatement() .readUncommitted() .readOnly() .execute(CLIENT_SESSION, session -> { Analyzer analyzer = createAnalyzer(session, metadata, false); Statement statement = SQL_PARSER.createStatement(query); analyzer.analyze(statement); }); }
public static void assertFormattedSql(SqlParser sqlParser, Node expected) { String formatted = formatSql(expected); // verify round-trip of formatting already-formatted SQL Statement actual = parseFormatted(sqlParser, formatted, expected); assertEquals(formatSql(actual), formatted); // compare parsed tree with parsed tree of formatted SQL if (!actual.equals(expected)) { // simplify finding the non-equal part of the tree assertListEquals(linearizeTree(actual), linearizeTree(expected)); } assertEquals(actual, expected); }
private static Statement parseFormatted(SqlParser sqlParser, String sql, Node tree) { try { return sqlParser.createStatement(sql); } catch (ParsingException e) { throw new AssertionError(format( "failed to parse formatted SQL: %s\nerror: %s\ntree: %s", sql, e.getMessage(), tree)); } }