Search in sources :

Example 1 with SqlCommandCall

use of org.apache.zeppelin.flink.sql.SqlCommandParser.SqlCommandCall in project zeppelin by apache.

the class Flink114Shims method parseSql.

/**
 * Parse it via flink SqlParser first, then fallback to regular expression matching.
 *
 * @param tableEnv
 * @param stmt
 * @return
 */
@Override
public Optional<SqlCommandParser.SqlCommandCall> parseSql(Object tableEnv, String stmt) {
    Parser sqlParser = ((TableEnvironmentInternal) tableEnv).getParser();
    SqlCommandCall sqlCommandCall = null;
    try {
        // parse statement via regex matching first
        Optional<SqlCommandCall> callOpt = parseByRegexMatching(stmt);
        if (callOpt.isPresent()) {
            sqlCommandCall = callOpt.get();
        } else {
            sqlCommandCall = parseBySqlParser(sqlParser, stmt);
        }
    } catch (Exception e) {
        return Optional.empty();
    }
    return Optional.of(sqlCommandCall);
}
Also used : TableEnvironmentInternal(org.apache.flink.table.api.internal.TableEnvironmentInternal) SqlCommandCall(org.apache.zeppelin.flink.sql.SqlCommandParser.SqlCommandCall) FlinkException(org.apache.flink.util.FlinkException) TableException(org.apache.flink.table.api.TableException) IOException(java.io.IOException) SqlCommandParser(org.apache.zeppelin.flink.sql.SqlCommandParser) Parser(org.apache.flink.table.delegation.Parser)

Example 2 with SqlCommandCall

use of org.apache.zeppelin.flink.sql.SqlCommandParser.SqlCommandCall in project zeppelin by apache.

the class Flink113Shims method parseBySqlParser.

private SqlCommandCall parseBySqlParser(Parser sqlParser, String stmt) throws Exception {
    List<Operation> operations;
    try {
        operations = sqlParser.parse(stmt);
    } catch (Throwable e) {
        throw new Exception("Invalidate SQL statement.", e);
    }
    if (operations.size() != 1) {
        throw new Exception("Only single statement is supported now.");
    }
    final SqlCommand cmd;
    String[] operands = new String[] { stmt };
    Operation operation = operations.get(0);
    if (operation instanceof CatalogSinkModifyOperation) {
        boolean overwrite = ((CatalogSinkModifyOperation) operation).isOverwrite();
        cmd = overwrite ? SqlCommand.INSERT_OVERWRITE : SqlCommand.INSERT_INTO;
    } else if (operation instanceof CreateTableOperation) {
        cmd = SqlCommand.CREATE_TABLE;
    } else if (operation instanceof DropTableOperation) {
        cmd = SqlCommand.DROP_TABLE;
    } else if (operation instanceof AlterTableOperation) {
        cmd = SqlCommand.ALTER_TABLE;
    } else if (operation instanceof CreateViewOperation) {
        cmd = SqlCommand.CREATE_VIEW;
    } else if (operation instanceof DropViewOperation) {
        cmd = SqlCommand.DROP_VIEW;
    } else if (operation instanceof CreateDatabaseOperation) {
        cmd = SqlCommand.CREATE_DATABASE;
    } else if (operation instanceof DropDatabaseOperation) {
        cmd = SqlCommand.DROP_DATABASE;
    } else if (operation instanceof AlterDatabaseOperation) {
        cmd = SqlCommand.ALTER_DATABASE;
    } else if (operation instanceof CreateCatalogOperation) {
        cmd = SqlCommand.CREATE_CATALOG;
    } else if (operation instanceof DropCatalogOperation) {
        cmd = SqlCommand.DROP_CATALOG;
    } else if (operation instanceof UseCatalogOperation) {
        cmd = SqlCommand.USE_CATALOG;
        operands = new String[] { ((UseCatalogOperation) operation).getCatalogName() };
    } else if (operation instanceof UseDatabaseOperation) {
        cmd = SqlCommand.USE;
        operands = new String[] { ((UseDatabaseOperation) operation).getDatabaseName() };
    } else if (operation instanceof ShowCatalogsOperation) {
        cmd = SqlCommand.SHOW_CATALOGS;
        operands = new String[0];
    } else if (operation instanceof ShowDatabasesOperation) {
        cmd = SqlCommand.SHOW_DATABASES;
        operands = new String[0];
    } else if (operation instanceof ShowTablesOperation) {
        cmd = SqlCommand.SHOW_TABLES;
        operands = new String[0];
    } else if (operation instanceof ShowFunctionsOperation) {
        cmd = SqlCommand.SHOW_FUNCTIONS;
        operands = new String[0];
    } else if (operation instanceof CreateCatalogFunctionOperation || operation instanceof CreateTempSystemFunctionOperation) {
        cmd = SqlCommand.CREATE_FUNCTION;
    } else if (operation instanceof DropCatalogFunctionOperation || operation instanceof DropTempSystemFunctionOperation) {
        cmd = SqlCommand.DROP_FUNCTION;
    } else if (operation instanceof AlterCatalogFunctionOperation) {
        cmd = SqlCommand.ALTER_FUNCTION;
    } else if (operation instanceof ExplainOperation) {
        cmd = SqlCommand.EXPLAIN;
    } else if (operation instanceof DescribeTableOperation) {
        cmd = SqlCommand.DESCRIBE;
        operands = new String[] { ((DescribeTableOperation) operation).getSqlIdentifier().asSerializableString() };
    } else if (operation instanceof QueryOperation) {
        cmd = SqlCommand.SELECT;
    } else {
        throw new Exception("Unknown operation: " + operation.asSummaryString());
    }
    return new SqlCommandCall(cmd, operands, stmt);
}
Also used : DropTempSystemFunctionOperation(org.apache.flink.table.operations.ddl.DropTempSystemFunctionOperation) AlterDatabaseOperation(org.apache.flink.table.operations.ddl.AlterDatabaseOperation) SqlCommandCall(org.apache.zeppelin.flink.sql.SqlCommandParser.SqlCommandCall) CreateCatalogOperation(org.apache.flink.table.operations.ddl.CreateCatalogOperation) DropCatalogFunctionOperation(org.apache.flink.table.operations.ddl.DropCatalogFunctionOperation) DropDatabaseOperation(org.apache.flink.table.operations.ddl.DropDatabaseOperation) CreateCatalogOperation(org.apache.flink.table.operations.ddl.CreateCatalogOperation) UseCatalogOperation(org.apache.flink.table.operations.UseCatalogOperation) UseDatabaseOperation(org.apache.flink.table.operations.UseDatabaseOperation) ShowCatalogsOperation(org.apache.flink.table.operations.ShowCatalogsOperation) CreateViewOperation(org.apache.flink.table.operations.ddl.CreateViewOperation) AlterDatabaseOperation(org.apache.flink.table.operations.ddl.AlterDatabaseOperation) QueryOperation(org.apache.flink.table.operations.QueryOperation) DropCatalogFunctionOperation(org.apache.flink.table.operations.ddl.DropCatalogFunctionOperation) ShowTablesOperation(org.apache.flink.table.operations.ShowTablesOperation) DescribeTableOperation(org.apache.flink.table.operations.DescribeTableOperation) AlterTableOperation(org.apache.flink.table.operations.ddl.AlterTableOperation) ShowFunctionsOperation(org.apache.flink.table.operations.ShowFunctionsOperation) CreateDatabaseOperation(org.apache.flink.table.operations.ddl.CreateDatabaseOperation) CatalogSinkModifyOperation(org.apache.flink.table.operations.CatalogSinkModifyOperation) Operation(org.apache.flink.table.operations.Operation) AlterCatalogFunctionOperation(org.apache.flink.table.operations.ddl.AlterCatalogFunctionOperation) DropTempSystemFunctionOperation(org.apache.flink.table.operations.ddl.DropTempSystemFunctionOperation) ShowDatabasesOperation(org.apache.flink.table.operations.ShowDatabasesOperation) DropTableOperation(org.apache.flink.table.operations.ddl.DropTableOperation) CreateTableOperation(org.apache.flink.table.operations.ddl.CreateTableOperation) DropViewOperation(org.apache.flink.table.operations.ddl.DropViewOperation) ExplainOperation(org.apache.flink.table.operations.ExplainOperation) DropCatalogOperation(org.apache.flink.table.operations.ddl.DropCatalogOperation) CreateCatalogFunctionOperation(org.apache.flink.table.operations.ddl.CreateCatalogFunctionOperation) CreateTempSystemFunctionOperation(org.apache.flink.table.operations.ddl.CreateTempSystemFunctionOperation) AttributedString(org.jline.utils.AttributedString) CreateTableOperation(org.apache.flink.table.operations.ddl.CreateTableOperation) CreateCatalogFunctionOperation(org.apache.flink.table.operations.ddl.CreateCatalogFunctionOperation) AlterCatalogFunctionOperation(org.apache.flink.table.operations.ddl.AlterCatalogFunctionOperation) CreateViewOperation(org.apache.flink.table.operations.ddl.CreateViewOperation) UseCatalogOperation(org.apache.flink.table.operations.UseCatalogOperation) CreateTempSystemFunctionOperation(org.apache.flink.table.operations.ddl.CreateTempSystemFunctionOperation) SqlCommand(org.apache.zeppelin.flink.sql.SqlCommandParser.SqlCommand) QueryOperation(org.apache.flink.table.operations.QueryOperation) ExplainOperation(org.apache.flink.table.operations.ExplainOperation) ShowCatalogsOperation(org.apache.flink.table.operations.ShowCatalogsOperation) DropCatalogOperation(org.apache.flink.table.operations.ddl.DropCatalogOperation) DropDatabaseOperation(org.apache.flink.table.operations.ddl.DropDatabaseOperation) CatalogSinkModifyOperation(org.apache.flink.table.operations.CatalogSinkModifyOperation) DescribeTableOperation(org.apache.flink.table.operations.DescribeTableOperation) UseDatabaseOperation(org.apache.flink.table.operations.UseDatabaseOperation) FlinkException(org.apache.flink.util.FlinkException) TableException(org.apache.flink.table.api.TableException) IOException(java.io.IOException) ShowFunctionsOperation(org.apache.flink.table.operations.ShowFunctionsOperation) AlterTableOperation(org.apache.flink.table.operations.ddl.AlterTableOperation) DropViewOperation(org.apache.flink.table.operations.ddl.DropViewOperation) DropTableOperation(org.apache.flink.table.operations.ddl.DropTableOperation) CreateDatabaseOperation(org.apache.flink.table.operations.ddl.CreateDatabaseOperation) ShowDatabasesOperation(org.apache.flink.table.operations.ShowDatabasesOperation) ShowTablesOperation(org.apache.flink.table.operations.ShowTablesOperation)

Example 3 with SqlCommandCall

use of org.apache.zeppelin.flink.sql.SqlCommandParser.SqlCommandCall in project zeppelin by apache.

the class Flink113Shims method parseSql.

/**
 * Parse it via flink SqlParser first, then fallback to regular expression matching.
 *
 * @param tableEnv
 * @param stmt
 * @return
 */
@Override
public Optional<SqlCommandParser.SqlCommandCall> parseSql(Object tableEnv, String stmt) {
    Parser sqlParser = ((TableEnvironmentInternal) tableEnv).getParser();
    SqlCommandCall sqlCommandCall = null;
    try {
        // parse statement via regex matching first
        Optional<SqlCommandCall> callOpt = parseByRegexMatching(stmt);
        if (callOpt.isPresent()) {
            sqlCommandCall = callOpt.get();
        } else {
            sqlCommandCall = parseBySqlParser(sqlParser, stmt);
        }
    } catch (Exception e) {
        return Optional.empty();
    }
    return Optional.of(sqlCommandCall);
}
Also used : TableEnvironmentInternal(org.apache.flink.table.api.internal.TableEnvironmentInternal) SqlCommandCall(org.apache.zeppelin.flink.sql.SqlCommandParser.SqlCommandCall) FlinkException(org.apache.flink.util.FlinkException) TableException(org.apache.flink.table.api.TableException) IOException(java.io.IOException) SqlCommandParser(org.apache.zeppelin.flink.sql.SqlCommandParser) Parser(org.apache.flink.table.delegation.Parser)

Example 4 with SqlCommandCall

use of org.apache.zeppelin.flink.sql.SqlCommandParser.SqlCommandCall in project zeppelin by apache.

the class Flink114Shims method parseBySqlParser.

private SqlCommandCall parseBySqlParser(Parser sqlParser, String stmt) throws Exception {
    List<Operation> operations;
    try {
        operations = sqlParser.parse(stmt);
    } catch (Throwable e) {
        throw new Exception("Invalidate SQL statement.", e);
    }
    if (operations.size() != 1) {
        throw new Exception("Only single statement is supported now.");
    }
    final SqlCommand cmd;
    String[] operands = new String[] { stmt };
    Operation operation = operations.get(0);
    if (operation instanceof CatalogSinkModifyOperation) {
        boolean overwrite = ((CatalogSinkModifyOperation) operation).isOverwrite();
        cmd = overwrite ? SqlCommand.INSERT_OVERWRITE : SqlCommand.INSERT_INTO;
    } else if (operation instanceof CreateTableOperation) {
        cmd = SqlCommand.CREATE_TABLE;
    } else if (operation instanceof DropTableOperation) {
        cmd = SqlCommand.DROP_TABLE;
    } else if (operation instanceof AlterTableOperation) {
        cmd = SqlCommand.ALTER_TABLE;
    } else if (operation instanceof CreateViewOperation) {
        cmd = SqlCommand.CREATE_VIEW;
    } else if (operation instanceof DropViewOperation) {
        cmd = SqlCommand.DROP_VIEW;
    } else if (operation instanceof CreateDatabaseOperation) {
        cmd = SqlCommand.CREATE_DATABASE;
    } else if (operation instanceof DropDatabaseOperation) {
        cmd = SqlCommand.DROP_DATABASE;
    } else if (operation instanceof AlterDatabaseOperation) {
        cmd = SqlCommand.ALTER_DATABASE;
    } else if (operation instanceof CreateCatalogOperation) {
        cmd = SqlCommand.CREATE_CATALOG;
    } else if (operation instanceof DropCatalogOperation) {
        cmd = SqlCommand.DROP_CATALOG;
    } else if (operation instanceof UseCatalogOperation) {
        cmd = SqlCommand.USE_CATALOG;
        operands = new String[] { ((UseCatalogOperation) operation).getCatalogName() };
    } else if (operation instanceof UseDatabaseOperation) {
        cmd = SqlCommand.USE;
        operands = new String[] { ((UseDatabaseOperation) operation).getDatabaseName() };
    } else if (operation instanceof ShowCatalogsOperation) {
        cmd = SqlCommand.SHOW_CATALOGS;
        operands = new String[0];
    } else if (operation instanceof ShowDatabasesOperation) {
        cmd = SqlCommand.SHOW_DATABASES;
        operands = new String[0];
    } else if (operation instanceof ShowTablesOperation) {
        cmd = SqlCommand.SHOW_TABLES;
        operands = new String[0];
    } else if (operation instanceof ShowFunctionsOperation) {
        cmd = SqlCommand.SHOW_FUNCTIONS;
        operands = new String[0];
    } else if (operation instanceof CreateCatalogFunctionOperation || operation instanceof CreateTempSystemFunctionOperation) {
        cmd = SqlCommand.CREATE_FUNCTION;
    } else if (operation instanceof DropCatalogFunctionOperation || operation instanceof DropTempSystemFunctionOperation) {
        cmd = SqlCommand.DROP_FUNCTION;
    } else if (operation instanceof AlterCatalogFunctionOperation) {
        cmd = SqlCommand.ALTER_FUNCTION;
    } else if (operation instanceof ExplainOperation) {
        cmd = SqlCommand.EXPLAIN;
    } else if (operation instanceof DescribeTableOperation) {
        cmd = SqlCommand.DESCRIBE;
        operands = new String[] { ((DescribeTableOperation) operation).getSqlIdentifier().asSerializableString() };
    } else if (operation instanceof QueryOperation) {
        cmd = SqlCommand.SELECT;
    } else {
        throw new Exception("Unknown operation: " + operation.asSummaryString());
    }
    return new SqlCommandCall(cmd, operands, stmt);
}
Also used : DropTempSystemFunctionOperation(org.apache.flink.table.operations.ddl.DropTempSystemFunctionOperation) AlterDatabaseOperation(org.apache.flink.table.operations.ddl.AlterDatabaseOperation) SqlCommandCall(org.apache.zeppelin.flink.sql.SqlCommandParser.SqlCommandCall) CreateCatalogOperation(org.apache.flink.table.operations.ddl.CreateCatalogOperation) DropCatalogFunctionOperation(org.apache.flink.table.operations.ddl.DropCatalogFunctionOperation) DropDatabaseOperation(org.apache.flink.table.operations.ddl.DropDatabaseOperation) CreateCatalogOperation(org.apache.flink.table.operations.ddl.CreateCatalogOperation) UseCatalogOperation(org.apache.flink.table.operations.UseCatalogOperation) UseDatabaseOperation(org.apache.flink.table.operations.UseDatabaseOperation) ShowCatalogsOperation(org.apache.flink.table.operations.ShowCatalogsOperation) CreateViewOperation(org.apache.flink.table.operations.ddl.CreateViewOperation) AlterDatabaseOperation(org.apache.flink.table.operations.ddl.AlterDatabaseOperation) QueryOperation(org.apache.flink.table.operations.QueryOperation) DropCatalogFunctionOperation(org.apache.flink.table.operations.ddl.DropCatalogFunctionOperation) ShowTablesOperation(org.apache.flink.table.operations.ShowTablesOperation) DescribeTableOperation(org.apache.flink.table.operations.DescribeTableOperation) AlterTableOperation(org.apache.flink.table.operations.ddl.AlterTableOperation) ShowFunctionsOperation(org.apache.flink.table.operations.ShowFunctionsOperation) CreateDatabaseOperation(org.apache.flink.table.operations.ddl.CreateDatabaseOperation) CatalogSinkModifyOperation(org.apache.flink.table.operations.CatalogSinkModifyOperation) Operation(org.apache.flink.table.operations.Operation) AlterCatalogFunctionOperation(org.apache.flink.table.operations.ddl.AlterCatalogFunctionOperation) DropTempSystemFunctionOperation(org.apache.flink.table.operations.ddl.DropTempSystemFunctionOperation) ShowDatabasesOperation(org.apache.flink.table.operations.ShowDatabasesOperation) DropTableOperation(org.apache.flink.table.operations.ddl.DropTableOperation) CreateTableOperation(org.apache.flink.table.operations.ddl.CreateTableOperation) DropViewOperation(org.apache.flink.table.operations.ddl.DropViewOperation) ExplainOperation(org.apache.flink.table.operations.ExplainOperation) DropCatalogOperation(org.apache.flink.table.operations.ddl.DropCatalogOperation) CreateCatalogFunctionOperation(org.apache.flink.table.operations.ddl.CreateCatalogFunctionOperation) CreateTempSystemFunctionOperation(org.apache.flink.table.operations.ddl.CreateTempSystemFunctionOperation) AttributedString(org.jline.utils.AttributedString) CreateTableOperation(org.apache.flink.table.operations.ddl.CreateTableOperation) CreateCatalogFunctionOperation(org.apache.flink.table.operations.ddl.CreateCatalogFunctionOperation) AlterCatalogFunctionOperation(org.apache.flink.table.operations.ddl.AlterCatalogFunctionOperation) CreateViewOperation(org.apache.flink.table.operations.ddl.CreateViewOperation) UseCatalogOperation(org.apache.flink.table.operations.UseCatalogOperation) CreateTempSystemFunctionOperation(org.apache.flink.table.operations.ddl.CreateTempSystemFunctionOperation) SqlCommand(org.apache.zeppelin.flink.sql.SqlCommandParser.SqlCommand) QueryOperation(org.apache.flink.table.operations.QueryOperation) ExplainOperation(org.apache.flink.table.operations.ExplainOperation) ShowCatalogsOperation(org.apache.flink.table.operations.ShowCatalogsOperation) DropCatalogOperation(org.apache.flink.table.operations.ddl.DropCatalogOperation) DropDatabaseOperation(org.apache.flink.table.operations.ddl.DropDatabaseOperation) CatalogSinkModifyOperation(org.apache.flink.table.operations.CatalogSinkModifyOperation) DescribeTableOperation(org.apache.flink.table.operations.DescribeTableOperation) UseDatabaseOperation(org.apache.flink.table.operations.UseDatabaseOperation) FlinkException(org.apache.flink.util.FlinkException) TableException(org.apache.flink.table.api.TableException) IOException(java.io.IOException) ShowFunctionsOperation(org.apache.flink.table.operations.ShowFunctionsOperation) AlterTableOperation(org.apache.flink.table.operations.ddl.AlterTableOperation) DropViewOperation(org.apache.flink.table.operations.ddl.DropViewOperation) DropTableOperation(org.apache.flink.table.operations.ddl.DropTableOperation) CreateDatabaseOperation(org.apache.flink.table.operations.ddl.CreateDatabaseOperation) ShowDatabasesOperation(org.apache.flink.table.operations.ShowDatabasesOperation) ShowTablesOperation(org.apache.flink.table.operations.ShowTablesOperation)

Example 5 with SqlCommandCall

use of org.apache.zeppelin.flink.sql.SqlCommandParser.SqlCommandCall in project zeppelin by apache.

the class Flink112Shims method parseSql.

/**
 * Parse it via flink SqlParser first, then fallback to regular expression matching.
 *
 * @param tableEnv
 * @param stmt
 * @return
 */
@Override
public Optional<SqlCommandParser.SqlCommandCall> parseSql(Object tableEnv, String stmt) {
    Parser sqlParser = ((TableEnvironmentInternal) tableEnv).getParser();
    SqlCommandCall sqlCommandCall = null;
    try {
        // parse statement via regex matching first
        Optional<SqlCommandCall> callOpt = parseByRegexMatching(stmt);
        if (callOpt.isPresent()) {
            sqlCommandCall = callOpt.get();
        } else {
            sqlCommandCall = parseBySqlParser(sqlParser, stmt);
        }
    } catch (Exception e) {
        return Optional.empty();
    }
    return Optional.of(sqlCommandCall);
}
Also used : TableEnvironmentInternal(org.apache.flink.table.api.internal.TableEnvironmentInternal) SqlCommandCall(org.apache.zeppelin.flink.sql.SqlCommandParser.SqlCommandCall) FlinkException(org.apache.flink.util.FlinkException) TableException(org.apache.flink.table.api.TableException) IOException(java.io.IOException) SqlCommandParser(org.apache.zeppelin.flink.sql.SqlCommandParser) Parser(org.apache.flink.table.delegation.Parser)

Aggregations

IOException (java.io.IOException)6 TableException (org.apache.flink.table.api.TableException)6 FlinkException (org.apache.flink.util.FlinkException)6 SqlCommandCall (org.apache.zeppelin.flink.sql.SqlCommandParser.SqlCommandCall)6 TableEnvironmentInternal (org.apache.flink.table.api.internal.TableEnvironmentInternal)3 Parser (org.apache.flink.table.delegation.Parser)3 CatalogSinkModifyOperation (org.apache.flink.table.operations.CatalogSinkModifyOperation)3 DescribeTableOperation (org.apache.flink.table.operations.DescribeTableOperation)3 ExplainOperation (org.apache.flink.table.operations.ExplainOperation)3 Operation (org.apache.flink.table.operations.Operation)3 QueryOperation (org.apache.flink.table.operations.QueryOperation)3 ShowCatalogsOperation (org.apache.flink.table.operations.ShowCatalogsOperation)3 ShowDatabasesOperation (org.apache.flink.table.operations.ShowDatabasesOperation)3 ShowFunctionsOperation (org.apache.flink.table.operations.ShowFunctionsOperation)3 ShowTablesOperation (org.apache.flink.table.operations.ShowTablesOperation)3 UseCatalogOperation (org.apache.flink.table.operations.UseCatalogOperation)3 UseDatabaseOperation (org.apache.flink.table.operations.UseDatabaseOperation)3 AlterCatalogFunctionOperation (org.apache.flink.table.operations.ddl.AlterCatalogFunctionOperation)3 AlterDatabaseOperation (org.apache.flink.table.operations.ddl.AlterDatabaseOperation)3 AlterTableOperation (org.apache.flink.table.operations.ddl.AlterTableOperation)3