Skip to content

Commit

Permalink
PHOENIX-1359 Backward compatibility fails with 4.1 client and current…
Browse files Browse the repository at this point in the history
… 4.0 branch on server
  • Loading branch information
jtaylor-sfdc committed Oct 22, 2014
1 parent 7e51158 commit 079c319
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ public enum ExpressionType {
LowerFunction(LowerFunction.class),
TrimFunction(TrimFunction.class),
DistinctCountAggregateFunction(DistinctCountAggregateFunction.class),
DistinctValueAggregateFunction(DistinctValueAggregateFunction.class),
PercentileContAggregateFunction(PercentileContAggregateFunction.class),
PercentRankAggregateFunction(PercentRankAggregateFunction.class),
StddevPopFunction(StddevPopFunction.class),
Expand Down Expand Up @@ -185,6 +184,7 @@ public enum ExpressionType {
InlineArrayElemRefExpression(InlineArrayElemRefExpression.class),
SQLIndexTypeFunction(SQLIndexTypeFunction.class),
ModulusExpression(ModulusExpression.class),
DistinctValueAggregateFunction(DistinctValueAggregateFunction.class),
RegexpSplitFunctiond(RegexpSplitFunction.class);
ExpressionType(Class<? extends Expression> clazz) {
this.clazz = clazz;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@

import java.io.DataInput;
import java.io.DataOutput;
import java.io.EOFException;
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.io.WritableUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.phoenix.expression.visitor.ExpressionVisitor;
import org.apache.phoenix.parse.LikeParseNode.LikeType;
import org.apache.phoenix.schema.PDataType;
import org.apache.phoenix.schema.tuple.Tuple;
import org.apache.phoenix.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;


/**
Expand Down Expand Up @@ -193,13 +193,27 @@ private static String toPattern(String s) {
// return sb.toString();
// }

private static final int LIKE_TYPE_INDEX = 2;
private static final LiteralExpression[] LIKE_TYPE_LITERAL = new LiteralExpression[LikeType.values().length];
static {
for (LikeType likeType : LikeType.values()) {
LIKE_TYPE_LITERAL[likeType.ordinal()] = LiteralExpression.newConstant(likeType.name());
}
}
private Pattern pattern;

public LikeExpression() {
}

private static List<Expression> addLikeTypeChild(List<Expression> children, LikeType likeType) {
List<Expression> newChildren = Lists.newArrayListWithExpectedSize(children.size()+1);
newChildren.addAll(children);
newChildren.add(LIKE_TYPE_LITERAL[likeType.ordinal()]);
return newChildren;
}

public LikeExpression(List<Expression> children, LikeType likeType) {
super(children);
super(addLikeTypeChild(children,likeType));
this.likeType = likeType;
init();
}
Expand All @@ -213,6 +227,13 @@ public boolean startsWithWildcard() {
}

private void init() {
List<Expression> children = getChildren();
if (children.size() <= LIKE_TYPE_INDEX) {
this.likeType = LikeType.CASE_SENSITIVE;
} else {
LiteralExpression likeTypeExpression = (LiteralExpression)children.get(LIKE_TYPE_INDEX);
this.likeType = LikeType.valueOf((String)likeTypeExpression.getValue());
}
Expression e = getPatternExpression();
if (e instanceof LiteralExpression) {
LiteralExpression patternExpression = (LiteralExpression)e;
Expand Down Expand Up @@ -276,17 +297,11 @@ public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
public void readFields(DataInput input) throws IOException {
super.readFields(input);
init();
try {
likeType = LikeType.values()[WritableUtils.readVInt(input)];
} catch (EOFException e) {
likeType = LikeType.CASE_SENSITIVE;
}
}

@Override
public void write(DataOutput output) throws IOException {
super.write(output);
WritableUtils.writeVInt(output, likeType.ordinal());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,14 +514,30 @@ private static byte[] copyKey(byte[] targetKey, int targetLength, byte[] sourceK
return targetKey;
}

private static final int KEY_RANGE_LENGTH_BITS = 21;
private static final int SLOT_SPAN_BITS = 32 - KEY_RANGE_LENGTH_BITS;

@Override
public void readFields(DataInput in) throws IOException {
RowKeySchema schema = new RowKeySchema();
schema.readFields(in);
int andLen = in.readInt();
int[] slotSpan = new int[andLen];
List<List<KeyRange>> slots = Lists.newArrayListWithExpectedSize(andLen);
for (int i=0; i<andLen; i++) {
int orLen = in.readInt();
int orLenWithSlotSpan = in.readInt();
int orLen = orLenWithSlotSpan;
/*
* For 4.2+ clients, we serialize the slotSpan array. To maintain backward
* compatibility, we encode the slotSpan values with the size of the list
* of key ranges. We reserve 21 bits for the key range list and 10 bits
* for the slotSpan value (up to 1024 which should be plenty).
*/
if (orLenWithSlotSpan < 0) {
orLenWithSlotSpan = -orLenWithSlotSpan - 1;
slotSpan[i] = orLenWithSlotSpan >>> KEY_RANGE_LENGTH_BITS;
orLen = (orLenWithSlotSpan << SLOT_SPAN_BITS) >>> SLOT_SPAN_BITS;
}
List<KeyRange> orClause = Lists.newArrayListWithExpectedSize(orLen);
slots.add(orClause);
for (int j=0; j<orLen; j++) {
Expand All @@ -530,29 +546,23 @@ public void readFields(DataInput in) throws IOException {
orClause.add(range);
}
}
int[] slotSpan = new int[andLen];
try {
for (int i = 0; i < andLen; i++) {
slotSpan[i] = in.readInt();
}
} catch (IOException e) { // Ignore as this must be a 3.1 client
}
this.init(slots, slotSpan, schema);
}

@Override
public void write(DataOutput out) throws IOException {
assert(slots.size() == slotSpan.length);
schema.write(out);
out.writeInt(slots.size());
for (List<KeyRange> orClause : slots) {
out.writeInt(orClause.size());
for (KeyRange range : orClause) {
for (int i = 0; i < slots.size(); i++) {
List<KeyRange> orLen = slots.get(i);
int span = slotSpan[i];
int orLenWithSlotSpan = -( ( (span << KEY_RANGE_LENGTH_BITS) | orLen.size() ) + 1);
out.writeInt(orLenWithSlotSpan);
for (KeyRange range : orLen) {
range.write(out);
}
}
for (int span : slotSpan) {
out.writeInt(span);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ private void serialize(ImmutableBytesWritable ptr, ResultIterator iterator, long
WritableUtils.writeVInt(out, ExpressionType.valueOf(expression).ordinal());
expression.write(out);
}
out.writeBoolean(singleValueOnly);
int exprSize = baOut.size() + Bytes.SIZEOF_INT;
out.writeInt(exprSize);
out.writeInt(exprSize * (singleValueOnly ? -1 : 1));
int nRows = 0;
out.writeInt(nRows); // In the end will be replaced with total number of rows
for (Tuple result = iterator.next(); result != null; result = iterator.next()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,14 @@ private HashCacheImpl(byte[] hashCacheBytes, MemoryChunk memoryChunk) {
expression.readFields(dataInput);
onExpressions.add(expression);
}
this.singleValueOnly = dataInput.readBoolean();
int exprSize = dataInput.readInt();
boolean singleValueOnly = false;
int exprSizeAndSingleValueOnly = dataInput.readInt();
int exprSize = exprSizeAndSingleValueOnly;
if (exprSize < 0) {
exprSize *= -1;
singleValueOnly = true;
}
this.singleValueOnly = singleValueOnly;
offset += exprSize;
int nRows = dataInput.readInt();
long estimatedSize = SizedUtil.sizeOfMap(nRows, SizedUtil.IMMUTABLE_BYTES_WRITABLE_SIZE, SizedUtil.RESULT_SIZE) + hashCacheBytes.length;
Expand Down

0 comments on commit 079c319

Please sign in to comment.