Skip to content

Commit

Permalink
for #1120, filter derived column for sharding-proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Aug 9, 2018
1 parent 5321e46 commit 0af2cbd
Showing 1 changed file with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.shardingsphere.core.parsing.parser.sql.dml.insert.InsertStatement;
import io.shardingsphere.core.routing.SQLRouteResult;
import io.shardingsphere.proxy.backend.AbstractBackendHandler;
import io.shardingsphere.proxy.backend.BackendExecutorContext;
import io.shardingsphere.proxy.backend.ResultPacket;
import io.shardingsphere.proxy.backend.jdbc.execute.JDBCExecuteEngine;
import io.shardingsphere.proxy.backend.jdbc.execute.response.ExecuteQueryResponse;
Expand All @@ -36,15 +37,18 @@
import io.shardingsphere.proxy.config.RuleRegistry;
import io.shardingsphere.proxy.transport.mysql.constant.ServerErrorCode;
import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets;
import io.shardingsphere.proxy.transport.mysql.packet.command.query.ColumnDefinition41Packet;
import io.shardingsphere.proxy.transport.mysql.packet.command.query.FieldCountPacket;
import io.shardingsphere.proxy.transport.mysql.packet.command.query.QueryResponsePackets;
import io.shardingsphere.proxy.transport.mysql.packet.generic.EofPacket;
import io.shardingsphere.proxy.transport.mysql.packet.generic.ErrPacket;
import io.shardingsphere.proxy.transport.mysql.packet.generic.OKPacket;
import io.shardingsphere.proxy.backend.BackendExecutorContext;
import lombok.RequiredArgsConstructor;

import javax.transaction.Status;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
Expand Down Expand Up @@ -103,11 +107,25 @@ private CommandResponsePackets merge(final SQLStatement sqlStatement) throws SQL
}
mergedResult = MergeEngineFactory.newInstance(
RULE_REGISTRY.getShardingRule(), ((ExecuteQueryResponse) executeResponse).getQueryResults(), sqlStatement, RULE_REGISTRY.getMetaData().getTable()).merge();
QueryResponsePackets result = ((ExecuteQueryResponse) executeResponse).getQueryResponsePackets();
QueryResponsePackets result = getQueryResponsePacketsWithoutDerivedColumns(((ExecuteQueryResponse) executeResponse).getQueryResponsePackets());
currentSequenceId = result.getPackets().size();
return result;
}

private QueryResponsePackets getQueryResponsePacketsWithoutDerivedColumns(final QueryResponsePackets queryResponsePackets) {
Collection<ColumnDefinition41Packet> columnDefinition41Packets = new ArrayList<>(queryResponsePackets.getColumnCount());
int columnCount = 0;
for (ColumnDefinition41Packet each : queryResponsePackets.getColumnDefinition41Packets()) {
if (!each.getName().startsWith("AVG_DERIVED_COUNT_") && !each.getName().startsWith("AVG_DERIVED_SUM_")
&& !each.getName().startsWith("ORDER_BY_DERIVED_") && !each.getName().startsWith("GROUP_BY_DERIVED_")) {
columnDefinition41Packets.add(each);
columnCount++;
}
}
FieldCountPacket fieldCountPacket = new FieldCountPacket(1, columnCount);
return new QueryResponsePackets(fieldCountPacket, columnDefinition41Packets, new EofPacket(columnCount + 2));
}

@Override
public boolean next() throws SQLException {
return null != mergedResult && mergedResult.next();
Expand Down

0 comments on commit 0af2cbd

Please sign in to comment.