shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

Improve mysql Statemen definition and Visitor

Open jingshanglu opened this issue 6 years ago • 10 comments

Background

Now, the Statement definition of mysql is not perfect yet. We need to refine the Statement definition and construct reasonable statements through visitor. For example, some statements are empty

// shardingsphere/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/mysql/dml/MySQLCallStatement.java
package org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml;

/**
 * MySQL call statement.
 */
public final class MySQLCallStatement extends CallStatement implements MySQLStatement {
}


// shardingsphere/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/impl/MySQLDMLVisitor.java
package org.apache.shardingsphere.sql.parser.mysql.visitor.impl

/**
 * DML visitor for MySQL.
 */
public final class MySQLDMLVisitor extends MySQLVisitor implements DMLVisitor {
    
    @Override
    public ASTNode visitCall(final CallContext ctx) {
        return new MySQLCallStatement();
    }
    
    @Override
    public ASTNode visitDoStatement(final DoStatementContext ctx) {
        return new MySQLDoStatement();
    }
}

Tasks

  1. Refine statement definition.
  2. Implementation corresponding method in Visitor to builds a reasonable Statement.

Solutions

  1. Think about a reasonable Statement definition
  2. Complete the specify Statement Class
  3. Implementation corresponding method in Visitor(eg:shardingsphere/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/impl/MySQLDMLVisitor.java)
  4. Add unit test for it.

Note

We can discuss the reasonableness of the Statement definition and then implement it.

Subtask

  • [x] MySQL CallStatement
  • [x] MySQL AlterTableStatemen
  • [x] MySQL CreateTableStatement
  • [x] MySQL DropTableStatement

jingshanglu avatar Oct 14 '20 09:10 jingshanglu

Please assgin to me, i want to have a try

wenweibin avatar Oct 16 '20 11:10 wenweibin

Please assgin to me, i want to have a try

@wenweibin OK, we can discuss it first and then implement it to avoid unnecessary work.

jingshanglu avatar Oct 19 '20 09:10 jingshanglu

@wenweibin We can start with the example above(MySQLCallStatement,MySQLDoStatement) or the statement you are interested in.

jingshanglu avatar Oct 19 '20 09:10 jingshanglu

  1. Considering that the synax of MySQL CALL Statement is CALL sp_name([parameter[,...]]),in my opinion MySQLCallStatement class should add two properties: procedureName and parameters,the codes are as below:
@ToString
@AllArgsConstructor
@Getter
public final class MySQLCallStatement extends CallStatement implements MySQLStatement {

    private String procedureName;

    private List<ExpressionSegment> parameters;

}
  1. The Synax of MySQL DO_Statement_ is DO expr [, expr] ...,so i think MySQLDoStatementclass should add one propertie:expressionSegments` ,the codes are as below:
@ToString
@AllArgsConstructor
@Getter
public final class MySQLDoStatement extends DoStatement implements MySQLStatement {

    private List<ExpressionSegment> expressionSegments;
}

  1. Correspondingly,the MySQLDMLVisitor class is refactored as below:
public final class MySQLDMLVisitor extends MySQLVisitor implements DMLVisitor {
    
    @Override
    public ASTNode visitCall(final CallContext ctx) {
        String procedureName = ctx.identifier().getText();
        List<ExpressionSegment> parameters = new ArrayList<>();
        ctx.expr().forEach(each -> parameters.add((ExpressionSegment) visit(each)));
        return new MySQLCallStatement(procedureName,parameters);
    }
    
    @Override
    public ASTNode visitDoStatement(final DoStatementContext ctx) {
        List<ExpressionSegment> expressions = new ArrayList<>();
        ctx.expr().forEach(each -> expressions.add((ExpressionSegment) visit(each)));
        return new MySQLDoStatement(expressions);
    }
}

Can you give some suggestions?@jingshanglu

wenweibin avatar Oct 21 '20 15:10 wenweibin

@wenweibin I think your design is ok.

jingshanglu avatar Oct 22 '20 02:10 jingshanglu

Which project should unit tests be placed? shardingsphere-sql-parser-test or shardingsphere-sql-parser-mysql? If they are placed in the shardingsphere-sql-parser-test directory, should these refactored codes migrate to CallStatement and DoStatement? Hope to get your advices. @jingshanglu

wenweibin avatar Oct 24 '20 14:10 wenweibin

Which project should unit tests be placed? shardingsphere-sql-parser-test or shardingsphere-sql-parser-mysql? If they are placed in the shardingsphere-sql-parser-test directory, should these refactored codes migrate to CallStatement and DoStatement? Hope to get your advices. @jingshanglu

For the logic of the test framework, please refer to #7880 . Test sql and expected result should place in shardingsphere/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/, and then add the assert code on project shardingsphere-sql-parser-test. @wenweibin

jingshanglu avatar Oct 26 '20 06:10 jingshanglu

@wenweibin Hello, i add some subtask about table,we can discuss the definition first.

jingshanglu avatar Nov 09 '20 03:11 jingshanglu

Firstlly, I'm going to start with MySQLDropTableStatement of subtask 4.According the synax of dropTable in DDLStatement.g4, I think the refactor is like below:

  1. Revise MySQLDropTableStatement class
/**
 * MySQL drop table statement.
 */
@ToString
@Getter
@Setter
public final class MySQLDropTableStatement extends DropTableStatement implements MySQLStatement {
    
    private boolean temporary;
    
    private boolean isExisted;
    
    private Restrict restrict;
    
}
  1. Add Restrict enum
/**
 * Restrict type enum.
 */
public enum Restrict {
    
    RESTRICT, CASCADE
}
  1. Revise MySQLDDLStatementSQLVisitor visitDropTable method
   @Override
    public ASTNode visitDropTable(final DropTableContext ctx) {
        MySQLDropTableStatement result = new MySQLDropTableStatement();
        result.setTemporary(Objects.nonNull(ctx.TEMPORARY()));
        result.setExisted(Objects.nonNull(ctx.existClause()));
        if (null != ctx.restrict()) {
            result.setRestrict(Objects.nonNull(ctx.restrict()) ? Restrict.RESTRICT : Restrict.CASCADE);
        }
        result.getTables().addAll(((CollectionValue<SimpleTableSegment>) visit(ctx.tableNames())).getValue());
        return result;
    }

Can you give some suggestions?@jingshanglu

wenweibin avatar Nov 13 '20 15:11 wenweibin

@wenweibin I agree with your design.

jingshanglu avatar Dec 21 '20 07:12 jingshanglu