Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactor for sql node convert logic #28673

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sqlfederation.compiler.converter.segment.expression.impl;
package org.apache.shardingsphere.sqlfederation.compiler.converter.function.dialect.mysql;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.calcite.sql.SqlBinaryOperator;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlPrefixOperator;
import org.apache.shardingsphere.sqlfederation.compiler.converter.function.dialect.mysql.MySQLMatchAgainstFunction;

/**
* SQL extension operator table.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.calcite.sql.parser.SqlParserPos;
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.compiler.converter.function.dialect.mysql.SQLExtensionOperatorTable;
import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.expression.ExpressionConverter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.CollateExpression;
import org.apache.shardingsphere.sqlfederation.compiler.converter.function.dialect.mysql.SQLExtensionOperatorTable;
import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.expression.ExpressionConverter;

Expand All @@ -37,7 +38,7 @@ public final class CollateExpressionConverter implements SQLSegmentConverter<Col
@Override
public Optional<SqlNode> convert(final CollateExpression segment) {
List<SqlNode> sqlNodes = new LinkedList<>();
sqlNodes.add(new ExpressionConverter().convert(segment.getExpr().get()).orElse(SqlNodeList.EMPTY));
sqlNodes.add(segment.getExpr().flatMap(optional -> new ExpressionConverter().convert(optional)).orElse(SqlNodeList.EMPTY));
sqlNodes.add(new ExpressionConverter().convert(segment.getCollateName()).orElse(SqlNodeList.EMPTY));
return Optional.of(new SqlBasicCall(SQLExtensionOperatorTable.COLLATE, sqlNodes, SqlParserPos.ZERO));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.MatchAgainstExpression;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
import org.apache.shardingsphere.sqlfederation.compiler.converter.function.dialect.mysql.SQLExtensionOperatorTable;
import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.expression.ExpressionConverter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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.NotExpression;
import org.apache.shardingsphere.sqlfederation.compiler.converter.function.dialect.mysql.SQLExtensionOperatorTable;
import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.expression.ExpressionConverter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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.UnaryOperationExpression;
import org.apache.shardingsphere.sqlfederation.compiler.converter.function.dialect.mysql.SQLExtensionOperatorTable;
import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.expression.ExpressionConverter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class WithConverter {
* @param query SqlNode
* @return SqlNodeList
*/
public Optional<SqlNodeList> convert(final WithSegment withSegment, final SqlNode query) {
public Optional<SqlNode> convert(final WithSegment withSegment, final SqlNode query) {
SqlIdentifier name = new SqlIdentifier(withSegment.getCommonTableExpressions().iterator().next().getIdentifier().getValue(), SqlParserPos.ZERO);
SqlNode selectSubquery = new SelectStatementConverter().convert(withSegment.getCommonTableExpressions().iterator().next().getSubquery().getSelect());
ExpressionConverter converter = new ExpressionConverter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.calcite.sql.SqlOrderBy;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WithSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DeleteStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml.DeleteStatementHandler;
import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.from.TableConverter;
Expand Down Expand Up @@ -59,10 +58,6 @@ private SqlNode convertDelete(final DeleteStatement deleteStatement) {
SqlNode condition = deleteStatement.getWhere().flatMap(optional -> new WhereConverter().convert(optional)).orElse(null);
SqlIdentifier alias = deleteStatement.getTable().getAliasName().map(optional -> new SqlIdentifier(optional, SqlParserPos.ZERO)).orElse(null);
SqlDelete sqlDelete = new SqlDelete(SqlParserPos.ZERO, deleteTable, condition, null, alias);
Optional<WithSegment> with = DeleteStatementHandler.getWithSegment(deleteStatement);
if (with.isPresent()) {
return new WithConverter().convert(DeleteStatementHandler.getWithSegment(deleteStatement).get(), sqlDelete).get();
}
return sqlDelete;
return DeleteStatementHandler.getWithSegment(deleteStatement).flatMap(optional -> new WithConverter().convert(optional, sqlDelete)).orElse(sqlDelete);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package org.apache.shardingsphere.sqlfederation.compiler.converter.statement.explain;

import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlExplain;
import org.apache.calcite.sql.SqlExplainLevel;
import org.apache.calcite.sql.SqlExplainFormat;
import org.apache.calcite.sql.SqlExplainLevel;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.ExplainStatement;
Expand All @@ -34,6 +34,8 @@
import org.apache.shardingsphere.sqlfederation.compiler.converter.statement.select.SelectStatementConverter;
import org.apache.shardingsphere.sqlfederation.compiler.converter.statement.update.UpdateStatementConverter;

import java.util.Optional;

/**
* Explain statement converter.
*/
Expand All @@ -46,19 +48,19 @@ public SqlNode convert(final ExplainStatement explainStatement) {
}

private SqlNode convertSQLStatement(final ExplainStatement explainStatement) {
return explainStatement.getStatement().map(this::convertSqlNode).orElseThrow(IllegalStateException::new);
return explainStatement.getStatement().flatMap(this::convertSqlNode).orElseThrow(IllegalStateException::new);
}

private SqlNode convertSqlNode(final SQLStatement sqlStatement) {
private Optional<SqlNode> convertSqlNode(final SQLStatement sqlStatement) {
if (sqlStatement instanceof SelectStatement) {
return new SelectStatementConverter().convert((SelectStatement) sqlStatement);
return Optional.of(new SelectStatementConverter().convert((SelectStatement) sqlStatement));
} else if (sqlStatement instanceof DeleteStatement) {
return new DeleteStatementConverter().convert((DeleteStatement) sqlStatement);
return Optional.of(new DeleteStatementConverter().convert((DeleteStatement) sqlStatement));
} else if (sqlStatement instanceof UpdateStatement) {
return new UpdateStatementConverter().convert((UpdateStatement) sqlStatement);
return Optional.of(new UpdateStatementConverter().convert((UpdateStatement) sqlStatement));
} else if (sqlStatement instanceof InsertStatement) {
return new InsertStatementConverter().convert((InsertStatement) sqlStatement);
return Optional.of(new InsertStatementConverter().convert((InsertStatement) sqlStatement));
}
return null;
return Optional.empty();
}
}