Skip to content

Commit

Permalink
Remove useless SQLSegmentConverter and refactor SQLNodeConverterEngin…
Browse files Browse the repository at this point in the history
…e logic
  • Loading branch information
strongduanmu committed Oct 19, 2023
1 parent 299fecc commit 77dd48c
Show file tree
Hide file tree
Showing 53 changed files with 668 additions and 377 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
import lombok.NoArgsConstructor;
import org.apache.calcite.sql.SqlNode;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.DALStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.ExplainStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DDLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DMLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DeleteStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.MergeStatement;
Expand All @@ -35,38 +38,60 @@
import org.apache.shardingsphere.sqlfederation.optimizer.converter.statement.update.UpdateStatementConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.exception.OptimizationSQLNodeConvertException;

import java.util.Optional;

/**
* SQL node converter engine.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SQLNodeConverterEngine {

/**
* Convert SQL statement to SQL node.
* Convert SQL sql statement to SQL node.
*
* @param statement SQL statement to be converted
* @param sqlStatement SQL sqlStatement to be converted
* @return sqlNode converted SQL node
* @throws OptimizationSQLNodeConvertException optimization SQL node convert exception
*/
public static SqlNode convert(final SQLStatement statement) {
if (statement instanceof SelectStatement) {
return new SelectStatementConverter().convert((SelectStatement) statement);
public static SqlNode convert(final SQLStatement sqlStatement) {
Optional<SqlNode> result = Optional.empty();
if (sqlStatement instanceof DMLStatement) {
result = convert((DMLStatement) sqlStatement);
} else if (sqlStatement instanceof DDLStatement) {
result = convert((DDLStatement) sqlStatement);
} else if (sqlStatement instanceof DALStatement) {
result = convert((DALStatement) sqlStatement);
}
return result.orElseThrow(() -> new OptimizationSQLNodeConvertException(sqlStatement));
}

private static Optional<SqlNode> convert(final DMLStatement sqlStatement) {
if (sqlStatement instanceof SelectStatement) {
return Optional.of(new SelectStatementConverter().convert((SelectStatement) sqlStatement));
}
if (statement instanceof DeleteStatement) {
return new DeleteStatementConverter().convert((DeleteStatement) statement);
if (sqlStatement instanceof DeleteStatement) {
return Optional.of(new DeleteStatementConverter().convert((DeleteStatement) sqlStatement));
}
if (statement instanceof ExplainStatement) {
return new ExplainStatementConverter().convert((ExplainStatement) statement);
return Optional.empty();
}

private static Optional<SqlNode> convert(final DDLStatement sqlStatement) {
if (sqlStatement instanceof UpdateStatement) {
return Optional.of(new UpdateStatementConverter().convert((UpdateStatement) sqlStatement));
}
if (statement instanceof UpdateStatement) {
return new UpdateStatementConverter().convert((UpdateStatement) statement);
if (sqlStatement instanceof InsertStatement) {
return Optional.of(new InsertStatementConverter().convert((InsertStatement) sqlStatement));
}
if (statement instanceof InsertStatement) {
return new InsertStatementConverter().convert((InsertStatement) statement);
if (sqlStatement instanceof MergeStatement) {
return Optional.of(new MergeStatementConverter().convert((MergeStatement) sqlStatement));
}
if (statement instanceof MergeStatement) {
return new MergeStatementConverter().convert((MergeStatement) statement);
return Optional.empty();
}

private static Optional<SqlNode> convert(final DALStatement sqlStatement) {
if (sqlStatement instanceof ExplainStatement) {
return Optional.of(new ExplainStatementConverter().convert((ExplainStatement) sqlStatement));
}
throw new OptimizationSQLNodeConvertException(statement);
return Optional.empty();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.expression;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.calcite.sql.SqlNode;
import org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dal.VariableSegment;
Expand All @@ -42,7 +44,6 @@
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubqueryExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DataTypeSegment;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.expression.impl.BetweenExpressionConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.expression.impl.BinaryOperationExpressionConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.expression.impl.CaseWhenExpressionConverter;
Expand Down Expand Up @@ -70,79 +71,86 @@
/**
* Expression converter.
*/
public final class ExpressionConverter implements SQLSegmentConverter<ExpressionSegment, SqlNode> {
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ExpressionConverter {

@Override
public Optional<SqlNode> convert(final ExpressionSegment segment) {
/**
* Convert expression segment to sql node.
*
* @param segment expression segment
* @return sql node
* @throws UnsupportedSQLOperationException unsupported SQL operation exception
*/
public static Optional<SqlNode> convert(final ExpressionSegment segment) {
if (null == segment) {
return Optional.empty();
}
if (segment instanceof LiteralExpressionSegment) {
return new LiteralExpressionConverter().convert((LiteralExpressionSegment) segment);
return LiteralExpressionConverter.convert((LiteralExpressionSegment) segment);
}
if (segment instanceof CommonExpressionSegment) {
// TODO
throw new UnsupportedSQLOperationException("unsupported CommonExpressionSegment");
}
if (segment instanceof ListExpression) {
return new ListExpressionConverter().convert((ListExpression) segment);
return ListExpressionConverter.convert((ListExpression) segment);
}
if (segment instanceof BinaryOperationExpression) {
return new BinaryOperationExpressionConverter().convert((BinaryOperationExpression) segment);
return BinaryOperationExpressionConverter.convert((BinaryOperationExpression) segment);
}
if (segment instanceof ColumnSegment) {
return new ColumnConverter().convert((ColumnSegment) segment);
return ColumnConverter.convert((ColumnSegment) segment);
}
if (segment instanceof ExistsSubqueryExpression) {
return new ExistsSubqueryExpressionConverter().convert((ExistsSubqueryExpression) segment);
return ExistsSubqueryExpressionConverter.convert((ExistsSubqueryExpression) segment);
}
if (segment instanceof SubqueryExpressionSegment) {
return new SubqueryExpressionConverter().convert((SubqueryExpressionSegment) segment);
return SubqueryExpressionConverter.convert((SubqueryExpressionSegment) segment);
}
if (segment instanceof InExpression) {
return new InExpressionConverter().convert((InExpression) segment);
return InExpressionConverter.convert((InExpression) segment);
}
if (segment instanceof BetweenExpression) {
return new BetweenExpressionConverter().convert((BetweenExpression) segment);
return BetweenExpressionConverter.convert((BetweenExpression) segment);
}
if (segment instanceof ParameterMarkerExpressionSegment) {
return new ParameterMarkerExpressionConverter().convert((ParameterMarkerExpressionSegment) segment);
return ParameterMarkerExpressionConverter.convert((ParameterMarkerExpressionSegment) segment);
}
if (segment instanceof FunctionSegment) {
return new FunctionConverter().convert((FunctionSegment) segment);
return FunctionConverter.convert((FunctionSegment) segment);
}
if (segment instanceof AggregationProjectionSegment) {
return new AggregationProjectionConverter().convert((AggregationProjectionSegment) segment);
return AggregationProjectionConverter.convert((AggregationProjectionSegment) segment);
}
if (segment instanceof DataTypeSegment) {
return new DataTypeConverter().convert((DataTypeSegment) segment);
return DataTypeConverter.convert((DataTypeSegment) segment);
}
if (segment instanceof CaseWhenExpression) {
return new CaseWhenExpressionConverter().convert((CaseWhenExpression) segment);
return CaseWhenExpressionConverter.convert((CaseWhenExpression) segment);
}
if (segment instanceof NotExpression) {
return new NotExpressionConverter().convert((NotExpression) segment);
return NotExpressionConverter.convert((NotExpression) segment);
}
if (segment instanceof TypeCastExpression) {
return new TypeCastExpressionConverter().convert((TypeCastExpression) segment);
return TypeCastExpressionConverter.convert((TypeCastExpression) segment);
}
if (segment instanceof ExtractArgExpression) {
return new ExtractArgExpressionConverter().convert((ExtractArgExpression) segment);
return ExtractArgExpressionConverter.convert((ExtractArgExpression) segment);
}
if (segment instanceof MatchAgainstExpression) {
return new MatchExpressionConverter().convert((MatchAgainstExpression) segment);
return MatchExpressionConverter.convert((MatchAgainstExpression) segment);
}
if (segment instanceof CollateExpression) {
return new CollateExpressionConverter().convert((CollateExpression) segment);
return CollateExpressionConverter.convert((CollateExpression) segment);
}
if (segment instanceof RowExpression) {
return new RowExpressionConverter().convert((RowExpression) segment);
return RowExpressionConverter.convert((RowExpression) segment);
}
if (segment instanceof VariableSegment) {
return new VariableSegmentConverter().convert((VariableSegment) segment);
return VariableSegmentConverter.convert((VariableSegment) segment);
}
if (segment instanceof UnaryOperationExpression) {
return new UnaryOperationExpressionConverter().convert((UnaryOperationExpression) segment);
return UnaryOperationExpressionConverter.convert((UnaryOperationExpression) segment);
}
throw new UnsupportedSQLOperationException("unsupported TableSegment type: " + segment.getClass());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@

package org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.expression.impl;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BetweenExpression;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.expression.ExpressionConverter;

import java.util.ArrayList;
Expand All @@ -33,18 +34,23 @@
/**
* Between expression converter.
*/
public final class BetweenExpressionConverter implements SQLSegmentConverter<BetweenExpression, SqlNode> {
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class BetweenExpressionConverter {

@Override
public Optional<SqlNode> convert(final BetweenExpression expression) {
/**
* Convert between expression to sql node.
*
* @param expression between expression
* @return sql node
*/
public static Optional<SqlNode> convert(final BetweenExpression expression) {
if (null == expression) {
return Optional.empty();
}
Collection<SqlNode> sqlNodes = new LinkedList<>();
ExpressionConverter expressionConverter = new ExpressionConverter();
expressionConverter.convert(expression.getLeft()).ifPresent(sqlNodes::add);
expressionConverter.convert(expression.getBetweenExpr()).ifPresent(sqlNodes::add);
expressionConverter.convert(expression.getAndExpr()).ifPresent(sqlNodes::add);
ExpressionConverter.convert(expression.getLeft()).ifPresent(sqlNodes::add);
ExpressionConverter.convert(expression.getBetweenExpr()).ifPresent(sqlNodes::add);
ExpressionConverter.convert(expression.getAndExpr()).ifPresent(sqlNodes::add);
return Optional.of(expression.isNot() ? new SqlBasicCall(SqlStdOperatorTable.NOT_BETWEEN, new ArrayList<>(sqlNodes), SqlParserPos.ZERO)
: new SqlBasicCall(SqlStdOperatorTable.BETWEEN, new ArrayList<>(sqlNodes), SqlParserPos.ZERO));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.expression.impl;

import com.google.common.base.Preconditions;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlNodeList;
Expand All @@ -28,7 +30,6 @@
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.function.dialect.mysql.SQLExtensionOperatorTable;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.expression.ExpressionConverter;

import java.util.Collections;
Expand All @@ -40,7 +41,8 @@
/**
* Binary operation expression converter.
*/
public final class BinaryOperationExpressionConverter implements SQLSegmentConverter<BinaryOperationExpression, SqlNode> {
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class BinaryOperationExpressionConverter {

private static final Map<String, SqlOperator> REGISTRY = new CaseInsensitiveMap<>();

Expand Down Expand Up @@ -99,14 +101,19 @@ private static void registerAlias() {
REGISTRY.put("!~*", SqlStdOperatorTable.NEGATED_POSIX_REGEX_CASE_INSENSITIVE);
}

@Override
public Optional<SqlNode> convert(final BinaryOperationExpression segment) {
/**
* Convert binary operation expression to sql node.
*
* @param segment binary operation expression
* @return sql node
*/
public static Optional<SqlNode> convert(final BinaryOperationExpression segment) {
SqlOperator operator = convertOperator(segment);
List<SqlNode> sqlNodes = convertSqlNodes(segment, operator);
return Optional.of(new SqlBasicCall(operator, sqlNodes, SqlParserPos.ZERO));
}

private SqlOperator convertOperator(final BinaryOperationExpression segment) {
private static SqlOperator convertOperator(final BinaryOperationExpression segment) {
String operator = segment.getOperator();
if ("IS".equalsIgnoreCase(operator)) {
String literals = String.valueOf(((LiteralExpressionSegment) segment.getRight()).getLiterals());
Expand All @@ -124,13 +131,13 @@ private SqlOperator convertOperator(final BinaryOperationExpression segment) {
return REGISTRY.get(operator);
}

private List<SqlNode> convertSqlNodes(final BinaryOperationExpression segment, final SqlOperator operator) {
SqlNode left = new ExpressionConverter().convert(segment.getLeft()).orElseThrow(IllegalStateException::new);
private static List<SqlNode> convertSqlNodes(final BinaryOperationExpression segment, final SqlOperator operator) {
SqlNode left = ExpressionConverter.convert(segment.getLeft()).orElseThrow(IllegalStateException::new);
List<SqlNode> result = new LinkedList<>();
result.add(left);
if (!SqlStdOperatorTable.IS_NULL.equals(operator) && !SqlStdOperatorTable.IS_NOT_NULL.equals(operator) && !SqlStdOperatorTable.IS_FALSE.equals(operator)
&& !SqlStdOperatorTable.IS_NOT_FALSE.equals(operator)) {
SqlNode right = new ExpressionConverter().convert(segment.getRight()).orElseThrow(IllegalStateException::new);
SqlNode right = ExpressionConverter.convert(segment.getRight()).orElseThrow(IllegalStateException::new);
result.addAll(right instanceof SqlNodeList ? ((SqlNodeList) right).getList() : Collections.singletonList(right));
}
return result;
Expand Down
Loading

0 comments on commit 77dd48c

Please sign in to comment.