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());
}
}
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;
}
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));
}
}
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));
}
}
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);
}
Aggregations