strongduanmu commented on code in PR #27599: URL: https://github.com/apache/shardingsphere/pull/27599#discussion_r1280013462
########## kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/from/impl/DeleteMultiTableConverter.java: ########## @@ -0,0 +1,58 @@ +/* + * 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.sqlfederation.compiler.converter.segment.from.impl; + +import org.apache.calcite.sql.SqlIdentifier; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlNodeList; +import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.DeleteMultiTableSegment; +import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment; +import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.SQLSegmentConverter; +import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.from.TableConverter; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.Optional; +import java.util.ArrayList; + +/** + * Delete multi table converter. + */ +public final class DeleteMultiTableConverter implements SQLSegmentConverter<DeleteMultiTableSegment, SqlNode> { + + @Override + public Optional<SqlNode> convert(final DeleteMultiTableSegment segment) { + if (segment == null) { + return Optional.empty(); + } + Collection<SqlNode> sqlNodeList = new LinkedList<>(); + SqlNode table = new TableConverter().convert(segment.getRelationTable()).get(); + sqlNodeList.add(table); + List<SimpleTableSegment> actualDeleteTables = segment.getActualDeleteTables(); + List<String> names = new ArrayList<>(); + for (SimpleTableSegment tableSegment : actualDeleteTables) { + String tableName = tableSegment.getTableName().getIdentifier().getValue(); + names.add(tableName); + } + sqlNodeList.add(new SqlIdentifier(names, SqlParserPos.ZERO)); + SqlNodeList sqlNodeList1 = new SqlNodeList(sqlNodeList, SqlParserPos.ZERO); Review Comment: Please change sqlNodeList1 to more meaningful name. ########## kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/from/impl/DeleteMultiTableConverter.java: ########## @@ -0,0 +1,58 @@ +/* + * 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.sqlfederation.compiler.converter.segment.from.impl; + +import org.apache.calcite.sql.SqlIdentifier; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlNodeList; +import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.DeleteMultiTableSegment; +import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment; +import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.SQLSegmentConverter; +import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.from.TableConverter; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.Optional; +import java.util.ArrayList; + +/** + * Delete multi table converter. + */ +public final class DeleteMultiTableConverter implements SQLSegmentConverter<DeleteMultiTableSegment, SqlNode> { + + @Override + public Optional<SqlNode> convert(final DeleteMultiTableSegment segment) { + if (segment == null) { + return Optional.empty(); + } + Collection<SqlNode> sqlNodeList = new LinkedList<>(); + SqlNode table = new TableConverter().convert(segment.getRelationTable()).get(); + sqlNodeList.add(table); + List<SimpleTableSegment> actualDeleteTables = segment.getActualDeleteTables(); + List<String> names = new ArrayList<>(); + for (SimpleTableSegment tableSegment : actualDeleteTables) { Review Comment: Please rename tableSegment to each. ########## kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/from/impl/DeleteMultiTableConverter.java: ########## @@ -0,0 +1,58 @@ +/* + * 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.sqlfederation.compiler.converter.segment.from.impl; + +import org.apache.calcite.sql.SqlIdentifier; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlNodeList; +import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.DeleteMultiTableSegment; +import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment; +import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.SQLSegmentConverter; +import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.from.TableConverter; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.Optional; +import java.util.ArrayList; + +/** + * Delete multi table converter. + */ +public final class DeleteMultiTableConverter implements SQLSegmentConverter<DeleteMultiTableSegment, SqlNode> { + + @Override + public Optional<SqlNode> convert(final DeleteMultiTableSegment segment) { + if (segment == null) { Review Comment: Please put null condition on the left. ########## kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/statement/delete/DeleteStatementConverter.java: ########## @@ -0,0 +1,66 @@ +/* + * 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.sqlfederation.compiler.converter.statement.delete; + +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlDelete; +import org.apache.calcite.sql.SqlNodeList; +import org.apache.calcite.sql.SqlOrderBy; +import org.apache.calcite.sql.SqlIdentifier; +import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.OrderBySegment; +import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitSegment; +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; +import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.limit.PaginationValueSQLConverter; +import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.orderby.OrderByConverter; +import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.where.WhereConverter; +import org.apache.shardingsphere.sqlfederation.compiler.converter.statement.SQLStatementConverter; + +import java.util.Optional; + +/** + * Delete statement converter. + */ +public final class DeleteStatementConverter implements SQLStatementConverter<DeleteStatement, SqlNode> { + + @Override + public SqlNode convert(final DeleteStatement deleteStatement) { + SqlDelete sqlDelete = convertDelete(deleteStatement); + Optional<OrderBySegment> orderByfetcher = DeleteStatementHandler.getOrderBySegment(deleteStatement); + SqlNodeList orderBy = orderByfetcher.flatMap(optional -> new OrderByConverter().convert(optional)).orElse(SqlNodeList.EMPTY); + Optional<LimitSegment> limit = DeleteStatementHandler.getLimitSegment(deleteStatement); + if (limit.isPresent()) { + SqlNode offset = limit.get().getOffset().flatMap(optional -> new PaginationValueSQLConverter().convert(optional)).orElse(null); + SqlNode rowCount = limit.get().getRowCount().flatMap(optional -> new PaginationValueSQLConverter().convert(optional)).orElse(null); + return new SqlOrderBy(SqlParserPos.ZERO, sqlDelete, orderBy, offset, rowCount); + } + return orderBy.isEmpty() ? sqlDelete : new SqlOrderBy(SqlParserPos.ZERO, sqlDelete, orderBy, null, null); + } + + private SqlDelete convertDelete(final DeleteStatement deleteStatement) { + SqlNode targetTable = new TableConverter().convert(deleteStatement.getTable()).get(); + SqlNode condition = deleteStatement.getWhere().flatMap(optional -> new WhereConverter().convert(optional)).orElse(null); + if (deleteStatement.getTable().getAliasName().isPresent()) { + SqlIdentifier alias1 = new SqlIdentifier(deleteStatement.getTable().getAliasName().toString(), SqlParserPos.ZERO); Review Comment: Please rename alias1 to more meaningful name. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
