package io.mycat.sqlengine.mpp.tmp;

import java.util.Comparator;

import io.mycat.net.mysql.RowDataPacket;
import io.mycat.sqlengine.mpp.OrderCol;
import io.mycat.sqlengine.mpp.RowDataPacketSorter;

/**
 * 
 * @author coderczp-2014-12-8
 */
public class RowDataCmp implements Comparator<RowDataPacket> {

	private OrderCol[] orderCols;

	public RowDataCmp(OrderCol[] orderCols) {
		this.orderCols = orderCols;
	}

	@Override
	public int compare(RowDataPacket o1, RowDataPacket o2) {
		OrderCol[] tmp = this.orderCols;
		int cmp = 0;
		int len = tmp.length;
		//依次比较order by语句上的多个排序字段的值
		int type = OrderCol.COL_ORDER_TYPE_ASC;
		for (int i = 0; i < len; i++) {
			int colIndex = tmp[i].colMeta.colIndex;
			byte[] left = o1.fieldValues.get(colIndex);
			byte[] right = o2.fieldValues.get(colIndex);
			if (tmp[i].orderType == type) {
				cmp = RowDataPacketSorter.compareObject(left, right, tmp[i]);
			} else {
				cmp = RowDataPacketSorter.compareObject(right, left, tmp[i]);
			}
			if (cmp != 0) {
				return cmp;
			}
		}
		return cmp;
	}

}

@@ -794,16 +794,145 @@
 Index);%0A
+%09%09%09// fix bug %E5%BD%93 order by %E5%88%97 %E4%B8%BA  null %E6%97%B6, %E6%8A%A5%E7%A9%BA%E6%8C%87%E9%92%88%E7%9A%84%E5%BC%82%E5%B8%B8.%0A%09%09%09if(left==null)%7B left = new byte%5B0%5D;%7D%0A%09%09%09if(right==null)%7B right = new byte%5B0%5D;%7D%0A
 %09%09%09if (t

fix bug 当 order by 列 为 null 时, 报空指针的异常.