Search in sources :

Example 1 with MySqlASTVisitorAdapter

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter in project druid by alibaba.

the class MySqlSelectTest_203_dla method test_0.

public void test_0() throws Exception {
    String sql = "SELECT $1, $2 FROM\n" + "TABLE temp_1\n" + "(\n" + "  $1 int,\n" + "  $2 string\n" + ")\n" + "TBLPROPERTIES (\n" + "  CATALOG='oss',\n" + "  LOCATION='oss://oss-cn-hangzhou-for-openanalytics-dailybuild/jinluo/tbl1_part/kv1.txt',\n" + "  SCHEMA='jinluo_test0810'\n" + ")\n" + "META LIFECYCLE 1";
    // System.out.println(sql);
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
    assertEquals(1, statementList.size());
    assertEquals("SELECT $1, $2\n" + "FROM TABLE temp_1 (\n" + "\t$1 int,\n" + "\t$2 string\n" + ")\n" + "TBLPROPERTIES (\n" + "\t'CATALOG' = 'oss',\n" + "\t'LOCATION' = 'oss://oss-cn-hangzhou-for-openanalytics-dailybuild/jinluo/tbl1_part/kv1.txt',\n" + "\t'SCHEMA' = 'jinluo_test0810'\n" + ")\n" + "META LIFECYCLE 1", stmt.toString());
    assertEquals("select $1, $2\n" + "from table temp_1 (\n" + "\t$1 int,\n" + "\t$2 string\n" + ")\n" + "tblproperties (\n" + "\t'CATALOG' = 'oss',\n" + "\t'LOCATION' = 'oss://oss-cn-hangzhou-for-openanalytics-dailybuild/jinluo/tbl1_part/kv1.txt',\n" + "\t'SCHEMA' = 'jinluo_test0810'\n" + ")\n" + "meta lifecycle 1", stmt.toLowerCaseString());
    final TempTableNameGen tempTableNameGen = new TempTableNameGen() {

        @Override
        public String generateName() {
            return "__temp_table_1";
        }
    };
    final List<SQLCreateTableStatement> createTableStatementList = new ArrayList<SQLCreateTableStatement>();
    SQLASTVisitorAdapter v = new MySqlASTVisitorAdapter() {

        public boolean visit(SQLAdhocTableSource x) {
            final String tableName = tempTableNameGen.generateName();
            HiveCreateTableStatement createStmt = (HiveCreateTableStatement) x.getDefinition();
            createStmt.setParent(null);
            createStmt.setTableName(tableName);
            createStmt.setExternal(true);
            SQLUtils.replaceInParent(x, new SQLExprTableSource(tableName));
            createTableStatementList.add(createStmt);
            return false;
        }

        public boolean visit(SQLVariantRefExpr x) {
            String name = x.getName();
            if (name != null && name.startsWith("$")) {
                SQLUtils.replaceInParent(x, new SQLIdentifierExpr(name));
            }
            return false;
        }
    };
    stmt.accept(v);
    for (SQLCreateTableStatement createStmt : createTableStatementList) {
        System.out.println(createStmt.toString(VisitorFeature.OutputNameQuote));
    }
    System.out.println();
    System.out.println(stmt.toString(VisitorFeature.OutputNameQuote));
// 
}
Also used : MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) SQLASTVisitorAdapter(com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) ArrayList(java.util.ArrayList) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) HiveCreateTableStatement(com.alibaba.druid.sql.dialect.hive.stmt.HiveCreateTableStatement) SQLAdhocTableSource(com.alibaba.druid.sql.ast.SQLAdhocTableSource) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)

Example 2 with MySqlASTVisitorAdapter

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter in project druid by alibaba.

the class MySqlASTVisitorAdapterTest method test_adapter.

public void test_adapter() throws Exception {
    MySqlASTVisitorAdapter adapter = new MySqlASTVisitorAdapter();
    new SQLBooleanExpr().accept(adapter);
    new SQLLimit().accept(adapter);
    new MySqlTableIndex().accept(adapter);
    new MySqlKey().accept(adapter);
    new MySqlPrimaryKey().accept(adapter);
    new SQLIntervalExpr().accept(adapter);
    new SQLBinaryExpr().accept(adapter);
    new MySqlPrepareStatement().accept(adapter);
    new MySqlExecuteStatement().accept(adapter);
    new MysqlDeallocatePrepareStatement().accept(adapter);
    new MySqlDeleteStatement().accept(adapter);
    new MySqlInsertStatement().accept(adapter);
    new MySqlLoadXmlStatement().accept(adapter);
    new SQLReplaceStatement().accept(adapter);
    new SQLStartTransactionStatement().accept(adapter);
    new SQLShowColumnsStatement().accept(adapter);
    new SQLShowDatabasesStatement().accept(adapter);
    new MySqlShowWarningsStatement().accept(adapter);
    new MySqlShowStatusStatement().accept(adapter);
    new CobarShowStatus().accept(adapter);
    new MySqlKillStatement().accept(adapter);
    new MySqlBinlogStatement().accept(adapter);
    new MySqlResetStatement().accept(adapter);
    new UserSpecification().accept(adapter);
    new MySqlPartitionByKey().accept(adapter);
    new MySqlOutFileExpr().accept(adapter);
    new MySqlUpdateStatement().accept(adapter);
    new MySqlSetTransactionStatement().accept(adapter);
    new MySqlShowMasterLogsStatement().accept(adapter);
    new MySqlShowAuthorsStatement().accept(adapter);
    new MySqlShowCollationStatement().accept(adapter);
    new MySqlShowBinLogEventsStatement().accept(adapter);
    new MySqlShowCharacterSetStatement().accept(adapter);
    new MySqlShowContributorsStatement().accept(adapter);
    new MySqlShowCreateDatabaseStatement().accept(adapter);
    new MySqlShowCreateEventStatement().accept(adapter);
    new MySqlShowCreateFunctionStatement().accept(adapter);
    new MySqlShowCreateProcedureStatement().accept(adapter);
    new SQLShowCreateTableStatement().accept(adapter);
    new MySqlShowCreateTriggerStatement().accept(adapter);
    new SQLShowCreateViewStatement().accept(adapter);
    new MySqlShowEngineStatement().accept(adapter);
    new MySqlShowEnginesStatement().accept(adapter);
    new MySqlShowErrorsStatement().accept(adapter);
    new MySqlShowEventsStatement().accept(adapter);
    new MySqlShowFunctionCodeStatement().accept(adapter);
    new MySqlShowFunctionStatusStatement().accept(adapter);
    new MySqlShowGrantsStatement().accept(adapter);
    new MySqlUserName().accept(adapter);
    new SQLShowIndexesStatement().accept(adapter);
    new MySqlShowMasterStatusStatement().accept(adapter);
    new MySqlShowOpenTablesStatement().accept(adapter);
    new MySqlShowBinaryLogsStatement().accept(adapter);
    new MySqlShowPluginsStatement().accept(adapter);
    new MySqlShowPrivilegesStatement().accept(adapter);
    new MySqlShowProcedureCodeStatement().accept(adapter);
    new MySqlShowProcedureStatusStatement().accept(adapter);
    new MySqlShowProcessListStatement().accept(adapter);
    new MySqlShowProfileStatement().accept(adapter);
    new MySqlShowSlaveHostsStatement().accept(adapter);
    new MySqlShowRelayLogEventsStatement().accept(adapter);
    new MySqlShowSlaveStatusStatement().accept(adapter);
    new MySqlShowTableStatusStatement().accept(adapter);
    new MySqlShowTriggersStatement().accept(adapter);
    new MySqlRenameTableStatement().accept(adapter);
    new MySqlUseIndexHint().accept(adapter);
    new MySqlIgnoreIndexHint().accept(adapter);
    new MySqlLockTableStatement().accept(adapter);
    new MySqlUnlockTablesStatement().accept(adapter);
    new MySqlForceIndexHint().accept(adapter);
    new MySqlAlterTableChangeColumn().accept(adapter);
    new SQLAlterCharacter().accept(adapter);
    new MySqlAlterTableOption().accept(adapter);
    new MySqlCreateTableStatement().accept(adapter);
    new MySqlCharExpr().accept(adapter);
    new MySqlUnique().accept(adapter);
    new MySqlAlterTableModifyColumn().accept(adapter);
    new MySqlAlterTableDiscardTablespace().accept(adapter);
    new MySqlAlterTableImportTablespace().accept(adapter);
    new TableSpaceOption().accept(adapter);
}
Also used : MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) SQLBinaryExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryExpr) UserSpecification(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement.UserSpecification) TableSpaceOption(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement.TableSpaceOption) SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) MySqlUserName(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName) SQLIntervalExpr(com.alibaba.druid.sql.ast.expr.SQLIntervalExpr) SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) MySqlCharExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr) MySqlOutFileExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr)

Example 3 with MySqlASTVisitorAdapter

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter in project druid by alibaba.

the class MySqlSelectTest_204_dla method test_0.

public void test_0() throws Exception {
    String sql = "SELECT temp_1.$1, temp_2.smallint_col\n" + "FROM \n" + "TABLE temp_1\n" + "(\n" + "  $1 int,\n" + "  $2 int\n" + ")\n" + "TBLPROPERTIES (\n" + "  TYPE='oss',\n" + "  LOCATION='oss//x.x.x.x:xxx/test_db',\n" + "  SCHEMA='test_db'\n" + ")\n" + "META LIFECYCLE 1\n" + "\n" + "JOIN\n" + "\n" + "TABLE temp_2\n" + "(\n" + "    id INT COMMENT 'default',\n" + "    bool_col BOOLEAN COMMENT 'default',\n" + "    tinyint_col TINYINT COMMENT 'default',\n" + "    smallint_col SMALLINT COMMENT 'default',\n" + "    int_col INT COMMENT 'default',\n" + "    bigint_col BIGINT COMMENT 'default',\n" + "    float_col FLOAT COMMENT 'default',\n" + "    double_col DOUBLE COMMENT 'default',\n" + "    date_string_col STRING COMMENT 'default',\n" + "    string_col STRING COMMENT 'default',\n" + "    timestamp_col TIMESTAMP COMMENT 'default'\n" + ")\n" + "ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' \n" + "WITH SERDEPROPERTIES ('field.delim'='|', 'serialization.format'='|') \n" + "STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat'\n" + "OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'\n" + "LOCATION 'oss://xxx/xxx/xxx.csv'\n" + "TBLPROPERTIES ('recursive.directories'='false')\n" + "META LIFECYCLE 1\n" + "\n" + "ON temp_1.$1 = temp_2.id\n" + "WHERE temp_2.bool_col = true;";
    // System.out.println(sql);
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
    assertEquals(1, statementList.size());
    assertEquals("SELECT temp_1.$1, temp_2.smallint_col\n" + "FROM TABLE temp_1 (\n" + "\t$1 int,\n" + "\t$2 int\n" + ")\n" + "TBLPROPERTIES (\n" + "\t'TYPE' = 'oss',\n" + "\t'LOCATION' = 'oss//x.x.x.x:xxx/test_db',\n" + "\t'SCHEMA' = 'test_db'\n" + ")\n" + "META LIFECYCLE 1\n" + "\tJOIN TABLE temp_2 (\n" + "\t\tid INT COMMENT 'default',\n" + "\t\tbool_col BOOLEAN COMMENT 'default',\n" + "\t\ttinyint_col TINYINT COMMENT 'default',\n" + "\t\tsmallint_col SMALLINT COMMENT 'default',\n" + "\t\tint_col INT COMMENT 'default',\n" + "\t\tbigint_col BIGINT COMMENT 'default',\n" + "\t\tfloat_col FLOAT COMMENT 'default',\n" + "\t\tdouble_col DOUBLE COMMENT 'default',\n" + "\t\tdate_string_col STRING COMMENT 'default',\n" + "\t\tstring_col STRING COMMENT 'default',\n" + "\t\ttimestamp_col TIMESTAMP COMMENT 'default'\n" + "\t)\n" + "\tROW FORMAT\n" + "\t\tSERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'\n" + "\tWITH SERDEPROPERTIES (\n" + "\t\t'field.delim' = '|',\n" + "\t\t'serialization.format' = '|'\n" + "\t)\n" + "\tSTORED AS\n" + "\t\tINPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat'\n" + "\t\tOUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'\n" + "\tLOCATION 'oss://xxx/xxx/xxx.csv'\n" + "\tTBLPROPERTIES (\n" + "\t\t'recursive.directories' = 'false'\n" + "\t)\n" + "\tMETA LIFECYCLE 1 ON temp_1.$1 = temp_2.id\n" + "WHERE temp_2.bool_col = true;", stmt.toString());
    assertEquals("select temp_1.$1, temp_2.smallint_col\n" + "from table temp_1 (\n" + "\t$1 int,\n" + "\t$2 int\n" + ")\n" + "tblproperties (\n" + "\t'TYPE' = 'oss',\n" + "\t'LOCATION' = 'oss//x.x.x.x:xxx/test_db',\n" + "\t'SCHEMA' = 'test_db'\n" + ")\n" + "meta lifecycle 1\n" + "\tjoin table temp_2 (\n" + "\t\tid INT comment 'default',\n" + "\t\tbool_col BOOLEAN comment 'default',\n" + "\t\ttinyint_col TINYINT comment 'default',\n" + "\t\tsmallint_col SMALLINT comment 'default',\n" + "\t\tint_col INT comment 'default',\n" + "\t\tbigint_col BIGINT comment 'default',\n" + "\t\tfloat_col FLOAT comment 'default',\n" + "\t\tdouble_col DOUBLE comment 'default',\n" + "\t\tdate_string_col STRING comment 'default',\n" + "\t\tstring_col STRING comment 'default',\n" + "\t\ttimestamp_col TIMESTAMP comment 'default'\n" + "\t)\n" + "\trow rowFormat\n" + "\t\tserde 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'\n" + "\twith serdeproperties (\n" + "\t\t'field.delim' = '|',\n" + "\t\t'serialization.format' = '|'\n" + "\t)\n" + "\tstored as\n" + "\t\tinputformat 'org.apache.hadoop.mapred.TextInputFormat'\n" + "\t\toutputformat 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'\n" + "\tlocation 'oss://xxx/xxx/xxx.csv'\n" + "\ttblproperties (\n" + "\t\t'recursive.directories' = 'false'\n" + "\t)\n" + "\tmeta lifecycle 1 on temp_1.$1 = temp_2.id\n" + "where temp_2.bool_col = true;", stmt.toLowerCaseString());
    final TempTableNameGen tempTableNameGen = new TempTableNameGen() {

        @Override
        public String generateName() {
            return "__temp_table_1";
        }
    };
    final List<SQLCreateTableStatement> createTableStatementList = new ArrayList<SQLCreateTableStatement>();
    SQLASTVisitorAdapter v = new MySqlASTVisitorAdapter() {

        public boolean visit(SQLAdhocTableSource x) {
            final String tableName = tempTableNameGen.generateName();
            HiveCreateTableStatement createStmt = (HiveCreateTableStatement) x.getDefinition();
            createStmt.setParent(null);
            createStmt.setTableName(tableName);
            createStmt.setExternal(true);
            SQLUtils.replaceInParent(x, new SQLExprTableSource(tableName));
            createTableStatementList.add(createStmt);
            return false;
        }

        public boolean visit(SQLVariantRefExpr x) {
            String name = x.getName();
            if (name != null && name.startsWith("$")) {
                SQLUtils.replaceInParent(x, new SQLIdentifierExpr(name));
            }
            return false;
        }
    };
    stmt.accept(v);
    for (SQLCreateTableStatement createStmt : createTableStatementList) {
        System.out.println(createStmt.toString(VisitorFeature.OutputNameQuote));
    }
    System.out.println();
    System.out.println(stmt.toString(VisitorFeature.OutputNameQuote));
    HiveCreateTableStatement createTableStatement = (HiveCreateTableStatement) createTableStatementList.get(0);
    SQLExpr lifeCycle = createTableStatement.getMetaLifeCycle();
    if (lifeCycle instanceof SQLIntegerExpr) {
        int intValue = ((SQLIntegerExpr) lifeCycle).getNumber().intValue();
    } else if (lifeCycle instanceof SQLIdentifierExpr && ((SQLIdentifierExpr) lifeCycle).nameHashCode64() == FnvHash.Constants.ALWAYS) {
    // always
    }
// 
}
Also used : MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) SQLASTVisitorAdapter(com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) ArrayList(java.util.ArrayList) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) HiveCreateTableStatement(com.alibaba.druid.sql.dialect.hive.stmt.HiveCreateTableStatement) SQLAdhocTableSource(com.alibaba.druid.sql.ast.SQLAdhocTableSource) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)

Example 4 with MySqlASTVisitorAdapter

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter in project druid by alibaba.

the class MySqlSelectTest_226 method test_0.

public void test_0() throws Exception {
    String sql = "SELECT sum(`price`) AS aggregate, `aa` \n" + "FROM `na_orders`\n" + "WHERE `updated_at` > '2018-12-03 00:00:00'\n" + "    AND `pay_status` = 1\n" + "    AND 0 = 1\n" + "ORDER BY 1";
    // System.out.println(sql);
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    assertEquals(1, statementList.size());
    SQLStatement stmt = statementList.get(0);
    SQLASTVisitor v = new MySqlASTVisitorAdapter() {

        public boolean visit(SQLIdentifierExpr x) {
            System.out.println(x.getName());
            return true;
        }
    };
    stmt.accept(v);
// assertEquals("SELECT userid, order, unionid\n" +
// "FROM t", stmt.toString());
// 
// assertEquals("select userid, order, unionid\n" +
// "from t", stmt.clone().toLowerCaseString());
}
Also used : MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) SQLASTVisitor(com.alibaba.druid.sql.visitor.SQLASTVisitor) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

MySqlASTVisitorAdapter (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)3 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)3 SQLAdhocTableSource (com.alibaba.druid.sql.ast.SQLAdhocTableSource)2 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)2 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)2 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)2 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)2 HiveCreateTableStatement (com.alibaba.druid.sql.dialect.hive.stmt.HiveCreateTableStatement)2 SQLASTVisitorAdapter (com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter)2 ArrayList (java.util.ArrayList)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)1 SQLBinaryExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryExpr)1 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)1 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)1 SQLIntervalExpr (com.alibaba.druid.sql.ast.expr.SQLIntervalExpr)1 MySqlCharExpr (com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr)1 MySqlOutFileExpr (com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr)1