Search in sources :

Example 1 with Statement

use of io.prestosql.sql.parser.StatementSplitter.Statement in project hetu-core by openlookeng.

the class Console method runConsole.

private void runConsole(QueryRunner queryRunner, AtomicBoolean exiting) {
    try (TableNameCompleter tableNameCompleter = new TableNameCompleter(queryRunner);
        InputReader reader = new InputReader(getHistoryFile(), Completion.commandCompleter(), tableNameCompleter)) {
        tableNameCompleter.populateCache();
        String remaining = "";
        while (!exiting.get()) {
            // setup prompt
            String prompt = PROMPT_NAME;
            String schema = queryRunner.getSession().getSchema();
            if (schema != null) {
                prompt += ":" + schema.replace("%", "%%");
            }
            String commandPrompt = prompt + "> ";
            // read a line of input from user
            String line;
            try {
                line = reader.readLine(commandPrompt, remaining);
            } catch (UserInterruptException e) {
                if (!e.getPartialLine().isEmpty()) {
                    reader.getHistory().add(e.getPartialLine());
                }
                remaining = "";
                continue;
            } catch (EndOfFileException e) {
                System.out.println();
                return;
            }
            // check for special commands -- must match InputParser
            String command = CharMatcher.is(';').or(whitespace()).trimTrailingFrom(line);
            switch(command.toLowerCase(ENGLISH)) {
                case "exit":
                case "quit":
                    return;
                case "history":
                    for (History.Entry entry : reader.getHistory()) {
                        System.out.println(new AttributedStringBuilder().style(DEFAULT.foreground(CYAN)).append(format("%5d", entry.index() + 1)).style(DEFAULT).append("  ").append(entry.line()).toAnsi(reader.getTerminal()));
                    }
                    continue;
                case "help":
                    System.out.println();
                    System.out.println(getHelpText());
                    continue;
                default:
                    break;
            }
            // execute any complete statements
            StatementSplitter splitter = new StatementSplitter(line, STATEMENT_DELIMITERS);
            for (Statement split : splitter.getCompleteStatements()) {
                ClientOptions.OutputFormat outputFormat = ClientOptions.OutputFormat.ALIGNED;
                if (split.terminator().equals("\\G")) {
                    outputFormat = ClientOptions.OutputFormat.VERTICAL;
                }
                if (CREATE_CUBE_PATTERN.matcher(split.statement()).matches()) {
                    PrintStream out = System.out;
                    PrintStream errorChannel = System.out;
                    CubeConsole cubeConsole = new CubeConsole(this);
                    queryRunner.setCubeConsole(cubeConsole);
                    boolean isCreateCubeSucceed = cubeConsole.createCubeCommand(split.statement(), queryRunner, ClientOptions.OutputFormat.NULL, tableNameCompleter::populateCache, false, true, reader.getTerminal(), out, errorChannel);
                } else if (RELOAD_CUBE_PATTERN.matcher(split.statement()).matches()) {
                    PrintStream out = System.out;
                    PrintStream errorChannel = System.out;
                    ReloadCubeConsole reloadCubeConsole = new ReloadCubeConsole(this);
                    queryRunner.setReloadCubeConsole(reloadCubeConsole);
                    boolean isReloadCubeSucceed = reloadCubeConsole.reload(split.statement(), queryRunner, ClientOptions.OutputFormat.VERTICAL, tableNameCompleter::populateCache, true, true, reader.getTerminal(), out, errorChannel);
                    if (isReloadCubeSucceed) {
                        CubeConsole cubeConsole = new CubeConsole(this);
                        queryRunner.setCubeConsole(cubeConsole);
                        if (CREATE_CUBE_PATTERN.matcher(reloadCubeConsole.getNewQuery()).matches()) {
                            boolean isCreateCubeSucceed = cubeConsole.createCubeCommand(reloadCubeConsole.getNewQuery(), queryRunner, ClientOptions.OutputFormat.NULL, tableNameCompleter::populateCache, false, true, reader.getTerminal(), out, errorChannel);
                            if (!isCreateCubeSucceed) {
                                System.out.println("An Error occurred. The cube needs to be created manually. Query to create the cube: " + reloadCubeConsole.getNewQuery());
                            }
                        } else {
                            process(queryRunner, reloadCubeConsole.getNewQuery(), outputFormat, tableNameCompleter::populateCache, true, true, reader.getTerminal(), System.out, System.out);
                        }
                    }
                } else {
                    process(queryRunner, split.statement(), outputFormat, tableNameCompleter::populateCache, true, true, reader.getTerminal(), System.out, System.out);
                }
            }
            // replace remaining with trailing partial statement
            remaining = whitespace().trimTrailingFrom(splitter.getPartialStatement());
        }
    } catch (IOException e) {
        System.err.println("Readline error: " + e.getMessage());
    }
}
Also used : PrintStream(java.io.PrintStream) EndOfFileException(org.jline.reader.EndOfFileException) StatementSplitter(io.prestosql.sql.parser.StatementSplitter) Statement(io.prestosql.sql.parser.StatementSplitter.Statement) StatementSplitter.isEmptyStatement(io.prestosql.sql.parser.StatementSplitter.isEmptyStatement) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UserInterruptException(org.jline.reader.UserInterruptException) History(org.jline.reader.History)

Example 2 with Statement

use of io.prestosql.sql.parser.StatementSplitter.Statement in project hetu-core by openlookeng.

the class Console method executeCommand.

private boolean executeCommand(QueryRunner queryRunner, AtomicBoolean exiting, String query, ClientOptions.OutputFormat outputFormat, boolean ignoreErrors, boolean showProgress) {
    boolean success = true;
    StatementSplitter splitter = new StatementSplitter(query);
    for (Statement split : splitter.getCompleteStatements()) {
        if (!isEmptyStatement(split.statement())) {
            try (Terminal terminal = terminal()) {
                String statement = split.statement();
                CubeConsole cubeConsole = new CubeConsole(this);
                queryRunner.setCubeConsole(cubeConsole);
                ReloadCubeConsole reloadCubeConsole = new ReloadCubeConsole(this);
                queryRunner.setReloadCubeConsole(reloadCubeConsole);
                if (CREATE_CUBE_PATTERN.matcher(statement).matches()) {
                    PrintStream out = System.out;
                    PrintStream errorChannel = System.out;
                    if (!cubeConsole.createCubeCommand(statement, queryRunner, ClientOptions.OutputFormat.NULL, () -> {
                    }, false, showProgress, terminal, out, errorChannel)) {
                        if (!ignoreErrors) {
                            return false;
                        }
                        success = false;
                    }
                }
                if (RELOAD_CUBE_PATTERN.matcher(statement).matches()) {
                    PrintStream out = System.out;
                    PrintStream errorChannel = System.out;
                    boolean isReloadCubeSucceed = reloadCubeConsole.reload(statement, queryRunner, ClientOptions.OutputFormat.NULL, () -> {
                    }, false, showProgress, terminal, out, errorChannel);
                    if (!isReloadCubeSucceed) {
                        if (!ignoreErrors) {
                            return false;
                        }
                        success = false;
                    } else {
                        boolean isCreateCubeSucceed = cubeConsole.createCubeCommand(reloadCubeConsole.getNewQuery(), queryRunner, ClientOptions.OutputFormat.NULL, () -> {
                        }, false, showProgress, terminal, out, errorChannel);
                        if (!isCreateCubeSucceed) {
                            System.err.println("An Error occurred. The cube needs to be created manually. Query to create the cube: " + reloadCubeConsole.getNewQuery());
                            if (!ignoreErrors) {
                                return false;
                            }
                            success = false;
                        }
                    }
                } else {
                    if (!process(queryRunner, statement, outputFormat, () -> {
                    }, false, showProgress, terminal, System.out, System.err)) {
                        if (!ignoreErrors) {
                            return false;
                        }
                        success = false;
                    }
                }
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }
        if (exiting.get()) {
            return success;
        }
    }
    if (!isEmptyStatement(splitter.getPartialStatement())) {
        System.err.println("Non-terminated statement: " + splitter.getPartialStatement());
        return false;
    }
    return success;
}
Also used : PrintStream(java.io.PrintStream) StatementSplitter(io.prestosql.sql.parser.StatementSplitter) Statement(io.prestosql.sql.parser.StatementSplitter.Statement) StatementSplitter.isEmptyStatement(io.prestosql.sql.parser.StatementSplitter.isEmptyStatement) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) Terminal(org.jline.terminal.Terminal)

Example 3 with Statement

use of io.prestosql.sql.parser.StatementSplitter.Statement in project hetu-core by openlookeng.

the class TestStatementSplitter method statements.

private static List<Statement> statements(String... args) {
    checkArgument(args.length % 2 == 0, "arguments not paired");
    ImmutableList.Builder<Statement> list = ImmutableList.builder();
    for (int i = 0; i < args.length; i += 2) {
        list.add(new Statement(args[i], args[i + 1]));
    }
    return list.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Statement(io.prestosql.sql.parser.StatementSplitter.Statement) StatementSplitter.squeezeStatement(io.prestosql.sql.parser.StatementSplitter.squeezeStatement) StatementSplitter.isEmptyStatement(io.prestosql.sql.parser.StatementSplitter.isEmptyStatement)

Aggregations

Statement (io.prestosql.sql.parser.StatementSplitter.Statement)3 StatementSplitter.isEmptyStatement (io.prestosql.sql.parser.StatementSplitter.isEmptyStatement)3 StatementSplitter (io.prestosql.sql.parser.StatementSplitter)2 IOException (java.io.IOException)2 PrintStream (java.io.PrintStream)2 UncheckedIOException (java.io.UncheckedIOException)2 ImmutableList (com.google.common.collect.ImmutableList)1 StatementSplitter.squeezeStatement (io.prestosql.sql.parser.StatementSplitter.squeezeStatement)1 EndOfFileException (org.jline.reader.EndOfFileException)1 History (org.jline.reader.History)1 UserInterruptException (org.jline.reader.UserInterruptException)1 Terminal (org.jline.terminal.Terminal)1 AttributedStringBuilder (org.jline.utils.AttributedStringBuilder)1