@Override public CompletableFuture<?> execute(CreateView statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine) { Session session = stateMachine.getSession(); QualifiedObjectName name = createQualifiedObjectName(session, statement, statement.getName()); accessControl.checkCanCreateView(session.getRequiredTransactionId(), session.getIdentity(), name); String sql = getFormattedSql(statement); Analysis analysis = analyzeStatement(statement, session, metadata); List<ViewColumn> columns = analysis.getOutputDescriptor() .getVisibleFields().stream() .map(field -> new ViewColumn(field.getName().get(), field.getType())) .collect(toImmutableList()); String data = codec.toJson(new ViewDefinition(sql, session.getCatalog(), session.getSchema(), columns, Optional.of(session.getUser()))); metadata.createView(session, name, data, statement.isReplace()); return completedFuture(null); }
private RelationPlan createTableCreationPlan(Analysis analysis) { QualifiedObjectName destination = analysis.getCreateTableDestination().get(); RelationPlan plan = createRelationPlan(analysis); TableMetadata tableMetadata = createTableMetadata(destination, getOutputTableColumns(plan), analysis.getCreateTableProperties(), plan.getSampleWeight().isPresent()); if (plan.getSampleWeight().isPresent() && !metadata.canCreateSampledTables(session, destination.getCatalogName())) { throw new PrestoException(NOT_SUPPORTED, "Cannot write sampled data to a store that doesn't support sampling"); } return createTableWriterPlan( analysis, plan, new CreateName(destination.getCatalogName(), tableMetadata), tableMetadata.getVisibleColumnNames()); }
private PlanNode createOutputPlan(RelationPlan plan, Analysis analysis) { ImmutableList.Builder<Symbol> outputs = ImmutableList.builder(); ImmutableList.Builder<String> names = ImmutableList.builder(); int columnNumber = 0; RelationType outputDescriptor = analysis.getOutputDescriptor(); for (Field field : outputDescriptor.getVisibleFields()) { String name = field.getName().orElse("_col" + columnNumber); names.add(name); int fieldIndex = outputDescriptor.indexOf(field); Symbol symbol = plan.getSymbol(fieldIndex); outputs.add(symbol); columnNumber++; } return new OutputNode(idAllocator.getNextId(), plan.getRoot(), names.build(), outputs.build()); }
@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()); }
private SubPlan doAnalyzeQuery() { // time analysis phase long analysisStart = System.nanoTime(); // analyze query Analyzer analyzer = new Analyzer(stateMachine.getSession(), metadata, sqlParser, accessControl, Optional.of(queryExplainer), experimentalSyntaxEnabled); Analysis analysis = analyzer.analyze(statement); stateMachine.setUpdateType(analysis.getUpdateType()); // plan query PlanNodeIdAllocator idAllocator = new PlanNodeIdAllocator(); LogicalPlanner logicalPlanner = new LogicalPlanner(stateMachine.getSession(), planOptimizers, idAllocator, metadata); Plan plan = logicalPlanner.plan(analysis); // extract inputs List<Input> inputs = new InputExtractor(metadata, stateMachine.getSession()).extract(plan.getRoot()); stateMachine.setInputs(inputs); // fragment the plan SubPlan subplan = new PlanFragmenter().createSubPlans(plan); // record analysis time stateMachine.recordAnalysisTime(analysisStart); return subplan; }
QueryPlanner(Analysis analysis, SymbolAllocator symbolAllocator, PlanNodeIdAllocator idAllocator, Metadata metadata, Session session) { requireNonNull(analysis, "analysis is null"); requireNonNull(symbolAllocator, "symbolAllocator is null"); requireNonNull(idAllocator, "idAllocator is null"); requireNonNull(metadata, "metadata is null"); requireNonNull(session, "session is null"); this.analysis = analysis; this.symbolAllocator = symbolAllocator; this.idAllocator = idAllocator; this.metadata = metadata; this.session = session; }
private RelationPlan createTableWriterPlan(Analysis analysis, RelationPlan plan, WriterTarget target, List<String> columnNames) { List<Symbol> writerOutputs = ImmutableList.of( symbolAllocator.newSymbol("partialrows", BIGINT), symbolAllocator.newSymbol("fragment", VARBINARY)); PlanNode source = plan.getRoot(); if (!analysis.isCreateTableAsSelectWithData()) { source = new LimitNode(idAllocator.getNextId(), source, 0L); } PlanNode writerNode = new TableWriterNode( idAllocator.getNextId(), source, target, plan.getOutputSymbols(), columnNames, writerOutputs, plan.getSampleWeight()); List<Symbol> outputs = ImmutableList.of(symbolAllocator.newSymbol("rows", BIGINT)); TableFinishNode commitNode = new TableFinishNode( idAllocator.getNextId(), writerNode, target, outputs); return new RelationPlan(commitNode, analysis.getOutputDescriptor(), outputs, Optional.empty()); }
private RelationPlan createDeletePlan(Analysis analysis) { QueryPlanner planner = new QueryPlanner(analysis, symbolAllocator, idAllocator, metadata, session); DeleteNode deleteNode = planner.planDelete(analysis.getDelete().get()); List<Symbol> outputs = ImmutableList.of(symbolAllocator.newSymbol("rows", BIGINT)); TableFinishNode commitNode = new TableFinishNode(idAllocator.getNextId(), deleteNode, deleteNode.getTarget(), outputs); return new RelationPlan(commitNode, analysis.getOutputDescriptor(), commitNode.getOutputSymbols(), Optional.empty()); }
RelationPlanner(Analysis analysis, SymbolAllocator symbolAllocator, PlanNodeIdAllocator idAllocator, Metadata metadata, Session session) { requireNonNull(analysis, "analysis is null"); requireNonNull(symbolAllocator, "symbolAllocator is null"); requireNonNull(idAllocator, "idAllocator is null"); requireNonNull(metadata, "metadata is null"); requireNonNull(session, "session is null"); this.analysis = analysis; this.symbolAllocator = symbolAllocator; this.idAllocator = idAllocator; this.metadata = metadata; this.session = session; }
public TranslationMap(RelationPlan rewriteBase, Analysis analysis) { this.rewriteBase = rewriteBase; this.analysis = analysis; fieldSymbols = new Symbol[rewriteBase.getOutputSymbols().size()]; }
public SqlStatement parse(SqlSession session, String sql) { log.debug("Parsing sql: {}", sql); Statement statement = SqlParser.createStatement(sql); QueryExplainer queryExplainer = new QueryExplainer(session.prestoSession, planOptimizers, metadataManager, periodicImportManager, storageManager); // analyze query Analyzer analyzer = new Analyzer(session.prestoSession, metadataManager, Optional.of(queryExplainer)); Analysis analysis = analyzer.analyze(statement); // System.out.println("analysis: " + analysis); PlanNodeIdAllocator idAllocator = new PlanNodeIdAllocator(); // plan query LogicalPlanner logicalPlanner = new LogicalPlanner(session.prestoSession, planOptimizers, idAllocator, metadataManager, periodicImportManager, storageManager); Plan plan = logicalPlanner.plan(analysis); return new SqlStatement(session, sql, plan); // // TableScanCountVisitor visitor = new TableScanCountVisitor(); // plan.getRoot().accept(visitor, 0); // Assert.assertEquals(1, visitor.count); // String p = PlanPrinter.textLogicalPlan(plan.getRoot(), plan.getTypes()); // // System.out.println("plan: " + p); }
private Plan parse(String sql) { InMemoryNodeManager nodeManager = new InMemoryNodeManager(); MetadataManager metadata = buildMetadata(); StorageManager storageManager = new MockStorageManager(); PeriodicImportManager periodicImportManager = new StubPeriodicImportManager(); SplitManager splitManager = buildSplitManager(nodeManager); List<PlanOptimizer> planOptimizers = buildPlanOptimizers(metadata, splitManager); Statement statement = SqlParser.createStatement(sql); // System.out.println("Statement: " + statement); Session session = buildSession(); QueryExplainer queryExplainer = new QueryExplainer(session, planOptimizers, metadata, periodicImportManager, storageManager); // analyze query Analyzer analyzer = new Analyzer(session, metadata, Optional.of(queryExplainer)); Analysis analysis = analyzer.analyze(statement); // System.out.println("analysis: " + analysis); PlanNodeIdAllocator idAllocator = new PlanNodeIdAllocator(); // plan query LogicalPlanner logicalPlanner = new LogicalPlanner(session, planOptimizers, idAllocator, metadata, periodicImportManager, storageManager); Plan plan = logicalPlanner.plan(analysis); return plan; }
private Analysis analyzeStatement(Statement statement, Session session, Metadata metadata) { Analyzer analyzer = new Analyzer(session, metadata, sqlParser, accessControl, Optional.<QueryExplainer>empty(), experimentalSyntaxEnabled); return analyzer.analyze(statement); }
private RelationPlan createInsertPlan(Analysis analysis) { Analysis.Insert insert = analysis.getInsert().get(); TableMetadata tableMetadata = metadata.getTableMetadata(session, insert.getTarget()); List<String> visibleTableColumnNames = tableMetadata.getVisibleColumnNames(); List<ColumnMetadata> visibleTableColumns = tableMetadata.getVisibleColumns(); RelationPlan plan = createRelationPlan(analysis); Map<String, ColumnHandle> columns = metadata.getColumnHandles(session, insert.getTarget()); ImmutableMap.Builder<Symbol, Expression> assignments = ImmutableMap.builder(); for (ColumnMetadata column : tableMetadata.getVisibleColumns()) { Symbol output = symbolAllocator.newSymbol(column.getName(), column.getType()); int index = insert.getColumns().indexOf(columns.get(column.getName())); if (index < 0) { assignments.put(output, new NullLiteral()); } else { assignments.put(output, plan.getSymbol(index).toQualifiedNameReference()); } } ProjectNode projectNode = new ProjectNode(idAllocator.getNextId(), plan.getRoot(), assignments.build()); RelationType tupleDescriptor = new RelationType(visibleTableColumns.stream() .map(column -> Field.newUnqualified(column.getName(), column.getType())) .collect(toImmutableList())); plan = new RelationPlan( projectNode, tupleDescriptor, projectNode.getOutputSymbols(), plan.getSampleWeight()); return createTableWriterPlan( analysis, plan, new InsertReference(insert.getTarget()), visibleTableColumnNames); }
private RelationPlan createRelationPlan(Analysis analysis) { return new RelationPlanner(analysis, symbolAllocator, idAllocator, metadata, session) .process(analysis.getQuery(), null); }