You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2023/04/22 08:06:41 UTC

[shardingsphere] branch master updated: Remove useless MySQLSQLStatVisitor (#25272)

This is an automated email from the ASF dual-hosted git repository.

sunnianjun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new a1d914f5d04 Remove useless MySQLSQLStatVisitor (#25272)
a1d914f5d04 is described below

commit a1d914f5d04ef1fd333092c6315c7cb8dba2cc94
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Apr 22 16:06:34 2023 +0800

    Remove useless MySQLSQLStatVisitor (#25272)
---
 .../mysql/visitor/stat/MySQLSQLStatVisitor.java    | 137 ---------------------
 .../visitor/stat/MySQLSQLStatVisitorTest.java      |  74 -----------
 2 files changed, 211 deletions(-)

diff --git a/sql-parser/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/stat/MySQLSQLStatVisitor.java b/sql-parser/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/stat/MySQLSQLStatVisitor.java
deleted file mode 100644
index 3673127153b..00000000000
--- a/sql-parser/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/stat/MySQLSQLStatVisitor.java
+++ /dev/null
@@ -1,137 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.sql.parser.mysql.visitor.stat;
-
-import lombok.Getter;
-import org.antlr.v4.runtime.tree.TerminalNode;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementBaseVisitor;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.AliasContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ColumnDefinitionContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ColumnRefContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.IdentifierContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.InsertContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.OwnerContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableFactorContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableNameContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableWildContext;
-import org.apache.shardingsphere.sql.parser.sql.common.SQLStats;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
-
-/**
- * SQL stats visitor for MySQL.
- */
-@Getter
-public final class MySQLSQLStatVisitor extends MySQLStatementBaseVisitor<SQLStats> {
-    
-    private final SQLStats sqlStats = new SQLStats();
-    
-    @Override
-    public SQLStats visitTableFactor(final TableFactorContext ctx) {
-        if (null != ctx.tableName()) {
-            SimpleTableSegment tableSegment = getTableName(ctx.tableName());
-            if (null != ctx.alias()) {
-                AliasContext aliasContext = ctx.alias();
-                tableSegment.setAlias(new AliasSegment(aliasContext.start.getStartIndex(), aliasContext.stop.getStopIndex(), new IdentifierValue(aliasContext.textOrIdentifier().getText())));
-            }
-            sqlStats.addTable(tableSegment);
-            return sqlStats;
-        }
-        super.visitTableFactor(ctx);
-        return sqlStats;
-    }
-    
-    @Override
-    public SQLStats visitInsert(final InsertContext ctx) {
-        SimpleTableSegment tableSegment = getTableName(ctx.tableName());
-        sqlStats.addTable(tableSegment);
-        if (null != ctx.insertValuesClause()) {
-            visit(ctx.insertValuesClause());
-        } else if (null != ctx.insertSelectClause()) {
-            visit(ctx.insertSelectClause());
-        } else {
-            visit(ctx.setAssignmentsClause());
-        }
-        return sqlStats;
-    }
-    
-    @Override
-    public SQLStats visitColumnRef(final ColumnRefContext ctx) {
-        sqlStats.addColumn(getColumn(ctx));
-        return sqlStats;
-    }
-    
-    @Override
-    public SQLStats visitColumnDefinition(final ColumnDefinitionContext ctx) {
-        sqlStats.addColumn(new ColumnSegment(ctx.column_name.start.getStartIndex(), ctx.column_name.stop.getStopIndex(), new IdentifierValue(ctx.column_name.getText())));
-        return sqlStats;
-    }
-    
-    private ColumnSegment getColumn(final ColumnRefContext ctx) {
-        IdentifierValue name;
-        OwnerSegment owner = null;
-        if (2 == ctx.identifier().size()) {
-            name = new IdentifierValue(ctx.identifier(1).getText());
-            owner = new OwnerSegment(ctx.identifier(0).start.getStartIndex(), ctx.identifier(0).stop.getStopIndex(), new IdentifierValue(ctx.identifier(0).getText()));
-        } else if (3 == ctx.identifier().size()) {
-            name = new IdentifierValue(ctx.identifier(2).getText());
-            owner = new OwnerSegment(ctx.identifier(1).start.getStartIndex(), ctx.identifier(1).stop.getStopIndex(), new IdentifierValue(ctx.identifier(1).getText()));
-        } else {
-            name = new IdentifierValue(ctx.identifier(0).getText());
-        }
-        ColumnSegment result = new ColumnSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), name);
-        result.setOwner(owner);
-        return result;
-    }
-    
-    private SimpleTableSegment getTableName(final TableNameContext ctx) {
-        SimpleTableSegment result = new SimpleTableSegment(new TableNameSegment(ctx.name().getStart().getStartIndex(),
-                ctx.name().getStop().getStopIndex(), new IdentifierValue(ctx.name().identifier().getText())));
-        OwnerContext owner = ctx.owner();
-        if (null != owner) {
-            result.setOwner(new OwnerSegment(owner.getStart().getStartIndex(), owner.getStop().getStopIndex(), new IdentifierValue(owner.identifier().getText())));
-        }
-        return result;
-    }
-    
-    @Override
-    public SQLStats visitTableName(final TableNameContext ctx) {
-        SimpleTableSegment tableSegment = getTableName(ctx);
-        sqlStats.addTable(tableSegment);
-        return sqlStats;
-    }
-    
-    @Override
-    public SQLStats visitTableWild(final TableWildContext ctx) {
-        ColumnSegment column = new ColumnSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), new IdentifierValue("*"));
-        IdentifierContext owner = ctx.identifier().get(ctx.identifier().size() - 1);
-        column.setOwner(new OwnerSegment(owner.start.getStartIndex(), owner.stop.getStopIndex(), new IdentifierValue(owner.getText())));
-        sqlStats.addColumn(column);
-        return sqlStats;
-    }
-    
-    @Override
-    public SQLStats visitTerminal(final TerminalNode node) {
-        super.visitTerminal(node);
-        return sqlStats;
-    }
-}
diff --git a/sql-parser/dialect/mysql/src/test/java/org/apache/shardingsphere/sql/parser/mysql/visitor/stat/MySQLSQLStatVisitorTest.java b/sql-parser/dialect/mysql/src/test/java/org/apache/shardingsphere/sql/parser/mysql/visitor/stat/MySQLSQLStatVisitorTest.java
deleted file mode 100644
index eed818ab390..00000000000
--- a/sql-parser/dialect/mysql/src/test/java/org/apache/shardingsphere/sql/parser/mysql/visitor/stat/MySQLSQLStatVisitorTest.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.sql.parser.mysql.visitor.stat;
-
-import org.antlr.v4.runtime.CodePointBuffer;
-import org.antlr.v4.runtime.CodePointCharStream;
-import org.antlr.v4.runtime.CommonTokenStream;
-import org.antlr.v4.runtime.tree.ParseTree;
-import org.apache.shardingsphere.sql.parser.core.ParseASTNode;
-import org.apache.shardingsphere.sql.parser.mysql.parser.MySQLLexer;
-import org.apache.shardingsphere.sql.parser.mysql.parser.MySQLParser;
-import org.apache.shardingsphere.sql.parser.sql.common.SQLStats;
-import org.junit.jupiter.api.extension.ExtensionContext;
-import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.Arguments;
-import org.junit.jupiter.params.provider.ArgumentsProvider;
-import org.junit.jupiter.params.provider.ArgumentsSource;
-
-import java.nio.CharBuffer;
-import java.util.stream.Stream;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-
-class MySQLSQLStatVisitorTest {
-    
-    @ParameterizedTest(name = "{0}")
-    @ArgumentsSource(TestCaseArgumentsProvider.class)
-    void assertSQLStats(final String caseId, final String inputSql, final int tableNum, final int columnNum) {
-        CodePointBuffer buffer = CodePointBuffer.withChars(CharBuffer.wrap(inputSql.toCharArray()));
-        MySQLLexer lexer = new MySQLLexer(CodePointCharStream.fromBuffer(buffer));
-        MySQLParser parser = new MySQLParser(new CommonTokenStream(lexer));
-        ParseTree parseTree = ((ParseASTNode) parser.parse()).getRootNode();
-        SQLStats sqlStats = new MySQLSQLStatVisitor().visit(parseTree);
-        assertThat(sqlStats.getTables().keySet().size(), is(tableNum));
-        assertThat(sqlStats.getColumns().keySet().size(), is(columnNum));
-    }
-    
-    private static class TestCaseArgumentsProvider implements ArgumentsProvider {
-        
-        @Override
-        public Stream<? extends Arguments> provideArguments(final ExtensionContext extensionContext) {
-            return Stream.of(Arguments.of("select_with_union", "select a+1 as b, name n from table1 join table2 where id=1 and name='lu';", 2, 3),
-                    Arguments.of("select_item_nums", "select id, name, age, sex, ss, yy from table1 where id=1", 1, 6),
-                    Arguments.of("select_with_subquery", "select id, name, age, count(*) as n, (select id, name, age, sex from table2 where id=2) as sid, yyyy from table1 where id=1", 2, 5),
-                    Arguments.of("select_where_num", "select id, name, age, sex, ss, yy from table1 where id=1 and name=1 and a=1 and b=2 and c=4 and d=3", 1, 10),
-                    Arguments.of("alter_table", "ALTER TABLE t_order ADD column4 DATE, ADD column5 DATETIME, engine ss max_rows 10,min_rows 2, ADD column6 TIMESTAMP, ADD column7 TIME;", 1, 4),
-                    Arguments.of("create_table", "CREATE TABLE IF NOT EXISTS `runoob_tbl`(\n"
-                            + "`runoob_id` INT UNSIGNED AUTO_INCREMENT,\n"
-                            + "`runoob_title` VARCHAR(100) NOT NULL,\n"
-                            + "`runoob_author` VARCHAR(40) NOT NULL,\n"
-                            + "`runoob_test` NATIONAL CHAR(40),\n"
-                            + "`submission_date` DATE,\n"
-                            + "PRIMARY KEY ( `runoob_id` )\n"
-                            + ")ENGINE=InnoDB DEFAULT CHARSET=utf8;",
-                            1, 5));
-        }
-    }
-}