@Override public CompletableFuture<Void> create(RequestContext context, MaterializedView materializedView) { Query statement = (Query) sqlParser.createStatement(materializedView.query, new ParsingOptions()); StringBuilder builder = new StringBuilder(); HashMap<String, String> map = new HashMap<>(); new RakamSqlFormatter.Formatter(builder, qualifiedName -> queryExecutor.formatTableReference(context.project, qualifiedName, Optional.empty(), map), '"') .process(statement, 1); QueryExecution execution = queryExecutor .executeRawStatement(context, format("create table %s as %s limit 0", queryExecutor.formatTableReference(context.project, QualifiedName.of("materialized", materializedView.tableName), Optional.empty(), ImmutableMap.of()), builder.toString(), Optional.empty()), map); return execution.getResult().thenAccept(result -> { try { get(context.project, materializedView.tableName); throw new AlreadyExistsException("Materialized view", BAD_REQUEST); } catch (NotExistsException e) { } if (result.isFailed()) { throw new RakamException(result.getError().message, INTERNAL_SERVER_ERROR); } else { database.createMaterializedView(context.project, materializedView); } }); }