Search in sources :

Example 1 with StatementSplitter

use of io.prestosql.sql.parser.StatementSplitter 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 StatementSplitter

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

the class Console method executeCommand.

public boolean executeCommand(String query, String outputFile, SessionProperties session) {
    JSONArray output = new JSONArray();
    try {
        StatementSplitter splitter = new StatementSplitter(query);
        SqlSyntaxConverter sqlConverter = SqlConverterFactory.getSqlConverter(session);
        for (StatementSplitter.Statement split : splitter.getCompleteStatements()) {
            if (session.isDebugEnable()) {
                log.info(String.format("Processing sql: %s", split.toString()));
            }
            JSONObject result = sqlConverter.convert(split.statement());
            output.put(result);
            if (session.isConsolePrintEnable()) {
                consolePrint(result);
            }
        }
        log.info(format("Migration Completed."));
        // write the result to file if output file was specified
        if (outputFile != null) {
            SqlResultHandleUtils.writeToHtmlFile(output, outputFile);
        }
        return true;
    } catch (ParsingException | SqlMigrationException e) {
        log.error(format("Failed to migrate the sql due to error:", e.getMessage()));
    }
    return false;
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) StatementSplitter(io.prestosql.sql.parser.StatementSplitter) SqlMigrationException(io.hetu.core.sql.migration.SqlMigrationException) ParsingException(io.prestosql.sql.parser.ParsingException) JSONArray(org.codehaus.jettison.json.JSONArray)

Example 3 with StatementSplitter

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

the class TestHiveSqlMigrate method testHiveTpcdsSql.

@Test
public void testHiveTpcdsSql() throws Exception {
    String sqlFile = getClass().getClassLoader().getResource("hive-tpcds.sql").getFile();
    String query = asCharSource(new File(sqlFile), UTF_8).read();
    StatementSplitter splitter = new StatementSplitter(query);
    for (StatementSplitter.Statement split : splitter.getCompleteStatements()) {
        JSONObject result = sqlConverter.convert(split.statement());
        assertEquals(getConversionStatus(result), "Success");
        checkCanParsedByHetu(getConvertedSql(result));
    }
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) StatementSplitter(io.prestosql.sql.parser.StatementSplitter) File(java.io.File) Test(org.testng.annotations.Test)

Example 4 with StatementSplitter

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

the class TestImpalaSqlMigrate method testImpalaTpcdsSql.

@Test
public void testImpalaTpcdsSql() throws Exception {
    String sqlFile = getClass().getClassLoader().getResource("impala-tpcds-queries.sql").getFile();
    String query = asCharSource(new File(sqlFile), UTF_8).read();
    StatementSplitter splitter = new StatementSplitter(query);
    for (StatementSplitter.Statement split : splitter.getCompleteStatements()) {
        JSONObject result = sqlConverter.convert(split.statement());
        assertEquals(getConversionStatus(result), Constants.SUCCESS);
        checkCanParsedByHetu(getConvertedSql(result));
    }
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) StatementSplitter(io.prestosql.sql.parser.StatementSplitter) File(java.io.File) Test(org.testng.annotations.Test)

Example 5 with StatementSplitter

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

the class InputParser method parse.

@Override
public ParsedLine parse(String line, int cursor, ParseContext context) throws SyntaxError {
    String command = whitespace().trimFrom(line);
    if (command.isEmpty() || SPECIAL.contains(command.toLowerCase(ENGLISH))) {
        return new DefaultParser().parse(line, cursor, context);
    }
    StatementSplitter splitter = new StatementSplitter(line, STATEMENT_DELIMITERS);
    if (splitter.getCompleteStatements().isEmpty()) {
        throw new EOFError(-1, -1, null);
    }
    return new DefaultParser().parse(line, cursor, context);
}
Also used : StatementSplitter(io.prestosql.sql.parser.StatementSplitter) EOFError(org.jline.reader.EOFError) DefaultParser(org.jline.reader.impl.DefaultParser)

Aggregations

StatementSplitter (io.prestosql.sql.parser.StatementSplitter)7 JSONObject (org.codehaus.jettison.json.JSONObject)4 IOException (java.io.IOException)3 SqlMigrationException (io.hetu.core.sql.migration.SqlMigrationException)2 ParsingException (io.prestosql.sql.parser.ParsingException)2 Statement (io.prestosql.sql.parser.StatementSplitter.Statement)2 StatementSplitter.isEmptyStatement (io.prestosql.sql.parser.StatementSplitter.isEmptyStatement)2 File (java.io.File)2 PrintStream (java.io.PrintStream)2 UncheckedIOException (java.io.UncheckedIOException)2 History (org.jline.reader.History)2 UserInterruptException (org.jline.reader.UserInterruptException)2 Test (org.testng.annotations.Test)2 InputReader (io.prestosql.cli.InputReader)1 JSONArray (org.codehaus.jettison.json.JSONArray)1 EOFError (org.jline.reader.EOFError)1 EndOfFileException (org.jline.reader.EndOfFileException)1 DefaultParser (org.jline.reader.impl.DefaultParser)1 Terminal (org.jline.terminal.Terminal)1 AttributedStringBuilder (org.jline.utils.AttributedStringBuilder)1