You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by sl...@apache.org on 2012/01/25 13:35:05 UTC

[4/5] CQL 3.0 beta

http://git-wip-us.apache.org/repos/asf/cassandra/blob/655ccc3a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
new file mode 100644
index 0000000..71b22bc
--- /dev/null
+++ b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java
@@ -0,0 +1,972 @@
+/*
+ * 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.cassandra.cql3.statements;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.*;
+import java.util.concurrent.TimeoutException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.auth.Permission;
+import org.apache.cassandra.cql3.*;
+import org.apache.cassandra.config.CFMetaData;
+import org.apache.cassandra.db.IColumn;
+import org.apache.cassandra.db.CounterColumn;
+import org.apache.cassandra.db.RangeSliceCommand;
+import org.apache.cassandra.db.ReadCommand;
+import org.apache.cassandra.db.Row;
+import org.apache.cassandra.db.RowPosition;
+import org.apache.cassandra.db.SliceByNamesReadCommand;
+import org.apache.cassandra.db.SliceFromReadCommand;
+import org.apache.cassandra.db.Table;
+import org.apache.cassandra.db.context.CounterContext;
+import org.apache.cassandra.db.filter.QueryPath;
+import org.apache.cassandra.db.marshal.CompositeType;
+import org.apache.cassandra.db.marshal.TypeParser;
+import org.apache.cassandra.dht.AbstractBounds;
+import org.apache.cassandra.dht.Bounds;
+import org.apache.cassandra.dht.ExcludingBounds;
+import org.apache.cassandra.dht.IPartitioner;
+import org.apache.cassandra.dht.IncludingExcludingBounds;
+import org.apache.cassandra.dht.RandomPartitioner;
+import org.apache.cassandra.dht.Range;
+import org.apache.cassandra.service.ClientState;
+import org.apache.cassandra.service.StorageProxy;
+import org.apache.cassandra.service.StorageService;
+import org.apache.cassandra.thrift.Column;
+import org.apache.cassandra.thrift.ConsistencyLevel;
+import org.apache.cassandra.thrift.CqlMetadata;
+import org.apache.cassandra.thrift.CqlResult;
+import org.apache.cassandra.thrift.CqlResultType;
+import org.apache.cassandra.thrift.CqlRow;
+import org.apache.cassandra.thrift.IndexExpression;
+import org.apache.cassandra.thrift.IndexOperator;
+import org.apache.cassandra.thrift.InvalidRequestException;
+import org.apache.cassandra.thrift.RequestType;
+import org.apache.cassandra.thrift.SlicePredicate;
+import org.apache.cassandra.thrift.SliceRange;
+import org.apache.cassandra.thrift.ThriftValidation;
+import org.apache.cassandra.thrift.TimedOutException;
+import org.apache.cassandra.thrift.UnavailableException;
+import org.apache.cassandra.utils.ByteBufferUtil;
+import org.apache.cassandra.utils.Pair;
+
+/**
+ * Encapsulates a completely parsed SELECT query, including the target
+ * column family, expression, result count, and ordering clause.
+ *
+ */
+public class SelectStatement extends CQLStatement
+{
+    private static final Logger logger = LoggerFactory.getLogger(SelectStatement.class);
+    private final static ByteBuffer countColumn = ByteBufferUtil.bytes("count");
+
+    public final CFDefinition cfDef;
+    public final Parameters parameters;
+    private final List<Pair<CFDefinition.Name, ColumnIdentifier>> selectedNames = new ArrayList<Pair<CFDefinition.Name, ColumnIdentifier>>(); // empty => wildcard
+    private final Map<ColumnIdentifier, Restriction> restrictions = new HashMap<ColumnIdentifier, Restriction>();
+    private boolean hasIndexedExpression;
+
+    public SelectStatement(CFDefinition cfDef, Parameters parameters)
+    {
+        this.cfDef = cfDef;
+        this.parameters = parameters;
+    }
+
+    public void checkAccess(ClientState state) throws InvalidRequestException
+    {
+        state.hasColumnFamilyAccess(keyspace(), columnFamily(), Permission.READ);
+    }
+
+    public CqlResult execute(ClientState state, List<ByteBuffer> variables) throws InvalidRequestException, UnavailableException, TimedOutException
+    {
+        List<Row> rows;
+        if (isKeyRange())
+        {
+            rows = multiRangeSlice(variables);
+        }
+        else
+        {
+            rows = getSlice(variables);
+        }
+
+        CqlResult result = new CqlResult();
+        result.type = CqlResultType.ROWS;
+
+        // count resultset is a single column named "count"
+        if (parameters.isCount)
+        {
+            result.schema = new CqlMetadata(Collections.<ByteBuffer, String>emptyMap(),
+                                            Collections.<ByteBuffer, String>emptyMap(),
+                                            "AsciiType",
+                                            "LongType");
+            List<Column> columns = Collections.singletonList(new Column(countColumn).setValue(ByteBufferUtil.bytes((long) rows.size())));
+            result.rows = Collections.singletonList(new CqlRow(countColumn, columns));
+            return result;
+        }
+        else
+        {
+            // otherwise create resultset from query results
+            result.schema = new CqlMetadata(new HashMap<ByteBuffer, String>(),
+                    new HashMap<ByteBuffer, String>(),
+                    TypeParser.getShortName(cfDef.cfm.comparator),
+                    TypeParser.getShortName(cfDef.cfm.getDefaultValidator()));
+            result.rows = process(rows, result.schema);
+            return result;
+        }
+    }
+
+    public String keyspace()
+    {
+        return cfDef.cfm.ksName;
+    }
+
+    public String columnFamily()
+    {
+        return cfDef.cfm.cfName;
+    }
+
+    private List<Row> getSlice(List<ByteBuffer> variables) throws InvalidRequestException, TimedOutException, UnavailableException
+    {
+        QueryPath queryPath = new QueryPath(columnFamily());
+        List<ReadCommand> commands = new ArrayList<ReadCommand>();
+
+        // ...a range (slice) of column names
+        if (isColumnRange())
+        {
+            ByteBuffer start =getRequestedBound(true, variables);
+            ByteBuffer finish = getRequestedBound(false, variables);
+
+            // Note that we use the total limit for every key. This is
+            // potentially inefficient, but then again, IN + LIMIT is not a
+            // very sensible choice
+            for (ByteBuffer key : getKeys(variables))
+            {
+                QueryProcessor.validateKey(key);
+                QueryProcessor.validateSliceRange(cfDef.cfm, start, finish, parameters.isColumnsReversed);
+                commands.add(new SliceFromReadCommand(keyspace(),
+                                                      key,
+                                                      queryPath,
+                                                      start,
+                                                      finish,
+                                                      parameters.isColumnsReversed,
+                                                      getLimit()));
+            }
+        }
+        // ...of a list of column names
+        else
+        {
+            Collection<ByteBuffer> columnNames = getRequestedColumns(variables);
+            QueryProcessor.validateColumnNames(columnNames);
+
+            for (ByteBuffer key: getKeys(variables))
+            {
+                QueryProcessor.validateKey(key);
+                commands.add(new SliceByNamesReadCommand(keyspace(), key, queryPath, columnNames));
+            }
+        }
+
+        try
+        {
+            return StorageProxy.read(commands, parameters.consistencyLevel);
+        }
+        catch (TimeoutException e)
+        {
+            throw new TimedOutException();
+        }
+        catch (IOException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private List<Row> multiRangeSlice(List<ByteBuffer> variables) throws InvalidRequestException, TimedOutException, UnavailableException
+    {
+        List<Row> rows;
+        IPartitioner<?> p = StorageService.getPartitioner();
+
+        ByteBuffer startKeyBytes = getKeyStart(variables);
+        ByteBuffer finishKeyBytes = getKeyFinish(variables);
+
+        RowPosition startKey = RowPosition.forKey(startKeyBytes, p);
+        RowPosition finishKey = RowPosition.forKey(finishKeyBytes, p);
+        if (startKey.compareTo(finishKey) > 0 && !finishKey.isMinimum(p))
+        {
+            if (p instanceof RandomPartitioner)
+                throw new InvalidRequestException("Start key sorts after end key. This is not allowed; you probably should not specify end key at all, under RandomPartitioner");
+            else
+                throw new InvalidRequestException("Start key must sort before (or equal to) finish key in your partitioner!");
+        }
+        AbstractBounds<RowPosition> bounds;
+        if (includeStartKey())
+        {
+            bounds = includeFinishKey()
+                   ? new Bounds<RowPosition>(startKey, finishKey)
+                   : new IncludingExcludingBounds<RowPosition>(startKey, finishKey);
+        }
+        else
+        {
+            bounds = includeFinishKey()
+                   ? new Range<RowPosition>(startKey, finishKey)
+                   : new ExcludingBounds<RowPosition>(startKey, finishKey);
+        }
+
+        // XXX: Our use of Thrift structs internally makes me Sad. :(
+        SlicePredicate thriftSlicePredicate = makeSlicePredicate(variables);
+        QueryProcessor.validateSlicePredicate(cfDef.cfm, thriftSlicePredicate);
+
+        List<IndexExpression> expressions = getIndexExpressions(variables);
+
+        try
+        {
+            rows = StorageProxy.getRangeSlice(new RangeSliceCommand(keyspace(),
+                                                                    columnFamily(),
+                                                                    null,
+                                                                    thriftSlicePredicate,
+                                                                    bounds,
+                                                                    expressions,
+                                                                    getLimit(),
+                                                                    true), // limit by columns, not keys
+                                              parameters.consistencyLevel);
+        }
+        catch (IOException e)
+        {
+            throw new RuntimeException(e);
+        }
+        catch (TimeoutException e)
+        {
+            throw new TimedOutException();
+        }
+        return rows;
+    }
+
+    private SlicePredicate makeSlicePredicate(List<ByteBuffer> variables)
+    throws InvalidRequestException
+    {
+        SlicePredicate thriftSlicePredicate = new SlicePredicate();
+
+        if (isColumnRange())
+        {
+            SliceRange sliceRange = new SliceRange();
+            sliceRange.start = getRequestedBound(true, variables);
+            sliceRange.finish = getRequestedBound(false, variables);
+            sliceRange.reversed = parameters.isColumnsReversed;
+            sliceRange.count = -1; // We use this for range slices, where the count is ignored in favor of the global column count
+            thriftSlicePredicate.slice_range = sliceRange;
+        }
+        else
+        {
+            thriftSlicePredicate.column_names = getRequestedColumns(variables);
+        }
+        return thriftSlicePredicate;
+    }
+
+    private int getLimit()
+    {
+        // For sparse, we'll end up merging all defined colums into the same CqlRow. Thus we should query up
+        // to 'defined columns' * 'asked limit' to be sure to have enough columns. We'll trim after query if
+        // this end being too much.
+        return cfDef.isCompact ? parameters.limit : cfDef.metadata.size() * parameters.limit;
+    }
+
+    private boolean isKeyRange()
+    {
+        if (hasIndexedExpression)
+            return true;
+
+        Restriction r = restrictions.get(cfDef.key.name);
+        return r == null || !r.isEquality();
+    }
+
+    private Collection<ByteBuffer> getKeys(final List<ByteBuffer> variables) throws InvalidRequestException
+    {
+        final Restriction r = restrictions.get(cfDef.key.name);
+        if (r == null || !r.isEquality())
+            throw new IllegalStateException();
+
+        List<ByteBuffer> keys = new ArrayList<ByteBuffer>(r.eqValues.size());
+        for (Term t : r.eqValues)
+            keys.add(t.getByteBuffer(cfDef.key.type, variables));
+        return keys;
+    }
+
+    private ByteBuffer getKeyStart(List<ByteBuffer> variables) throws InvalidRequestException
+    {
+        Restriction r = restrictions.get(cfDef.key.name);
+        if (r == null)
+        {
+            return null;
+        }
+        else if (r.isEquality())
+        {
+            assert r.eqValues.size() == 1;
+            return r.eqValues.get(0).getByteBuffer(cfDef.key.type, variables);
+        }
+        else
+        {
+            return r.start == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : r.start.getByteBuffer(cfDef.key.type, variables);
+        }
+    }
+
+    private boolean includeStartKey()
+    {
+        Restriction r = restrictions.get(cfDef.key.name);
+        if (r == null || r.isEquality())
+            return true;
+        else
+            return r.startInclusive;
+    }
+
+    private ByteBuffer getKeyFinish(List<ByteBuffer> variables) throws InvalidRequestException
+    {
+        Restriction r = restrictions.get(cfDef.key.name);
+        if (r == null)
+        {
+            return null;
+        }
+        else if (r.isEquality())
+        {
+            assert r.eqValues.size() == 1;
+            return r.eqValues.get(0).getByteBuffer(cfDef.key.type, variables);
+        }
+        else
+        {
+            return r.end == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : r.end.getByteBuffer(cfDef.key.type, variables);
+        }
+    }
+
+    private boolean includeFinishKey()
+    {
+        Restriction r = restrictions.get(cfDef.key.name);
+        if (r == null || r.isEquality())
+            return true;
+        else
+            return r.endInclusive;
+    }
+
+    private boolean isColumnRange()
+    {
+        // Static CF never entails a column slice
+        if (!cfDef.isCompact && !cfDef.isComposite)
+            return false;
+
+        // Otherwise, it is a range query if it has at least one the column alias
+        // for which no relation is defined or is not EQ.
+        for (CFDefinition.Name name : cfDef.columns.values())
+        {
+            Restriction r = restrictions.get(name.name);
+            if (r == null || !r.isEquality())
+                return true;
+        }
+        return false;
+    }
+
+    private boolean isWildcard()
+    {
+        return selectedNames.isEmpty();
+    }
+
+    private List<ByteBuffer> getRequestedColumns(List<ByteBuffer> variables) throws InvalidRequestException
+    {
+        assert !isColumnRange();
+
+        ColumnNameBuilder builder = cfDef.getColumnNameBuilder();
+        for (CFDefinition.Name name : cfDef.columns.values())
+        {
+            Restriction r = restrictions.get(name.name);
+            assert r != null && r.isEquality() && r.eqValues.size() == 1;
+            builder.add(r.eqValues.get(0), Relation.Type.EQ, variables);
+        }
+
+        if (cfDef.isCompact)
+        {
+            return Collections.singletonList(builder.build());
+        }
+        else
+        {
+            List<ByteBuffer> columns = new ArrayList<ByteBuffer>();
+            // Adds all (requested) columns
+            Iterator<Pair<CFDefinition.Name, ColumnIdentifier>> iter = getExpandedSelection().iterator();
+            while (iter.hasNext())
+            {
+                CFDefinition.Name name = iter.next().left;
+                // Skip everything that is not a 'metadata' column
+                if (name.kind != CFDefinition.Name.Kind.COLUMN_METADATA)
+                    continue;
+                ColumnNameBuilder b = iter.hasNext() ? builder.copy() : builder;
+                ByteBuffer cname = b.add(name.name.key).build();
+                columns.add(cname);
+            }
+            return columns;
+        }
+    }
+
+    private ByteBuffer getRequestedBound(boolean isStart, List<ByteBuffer> variables) throws InvalidRequestException
+    {
+        assert isColumnRange();
+
+        ColumnNameBuilder builder = cfDef.getColumnNameBuilder();
+        for (CFDefinition.Name name : cfDef.columns.values())
+        {
+            Restriction r = restrictions.get(name.name);
+            if (r == null)
+            {
+                // There wasn't any non EQ relation on that key, we select all records having the preceding component as prefix.
+                // For composites, if there was preceding component and we're computing the end, we must change the last component
+                // End-Of-Component, otherwise we would be selecting only one record.
+                if (builder.componentCount() > 0 && !isStart)
+                    return builder.buildAsEndOfRange();
+                else
+                    return builder.build();
+            }
+
+            if (r.isEquality())
+            {
+                assert r.eqValues.size() == 1;
+                builder.add(r.eqValues.get(0), Relation.Type.EQ, variables);
+            }
+            else
+            {
+                Term t = isStart ? r.start : r.end;
+                Relation.Type op = isStart
+                    ? (r.startInclusive ? Relation.Type.GTE : Relation.Type.GT)
+                    : (r.endInclusive ? Relation.Type.LTE : Relation.Type.LT);
+                if (t == null)
+                    return ByteBufferUtil.EMPTY_BYTE_BUFFER;
+                else
+                    return builder.add(t, op, variables).build();
+            }
+        }
+        // Means no relation at all or everything was an equal
+        return builder.build();
+    }
+
+    private List<IndexExpression> getIndexExpressions(List<ByteBuffer> variables) throws InvalidRequestException
+    {
+        if (!hasIndexedExpression)
+            return Collections.<IndexExpression>emptyList();
+
+        List<IndexExpression> expressions = new ArrayList<IndexExpression>();
+        for (CFDefinition.Name name : cfDef.metadata.values())
+        {
+            Restriction restriction = restrictions.get(name.name);
+            if (restriction == null)
+                continue;
+
+            if (restriction.isEquality())
+            {
+                for (Term t : restriction.eqValues)
+                {
+                    ByteBuffer value = t.getByteBuffer(name.type, variables);
+                    expressions.add(new IndexExpression(name.name.key, IndexOperator.EQ, value));
+                }
+            }
+            else
+            {
+                if (restriction.start != null)
+                {
+                    ByteBuffer value = restriction.start.getByteBuffer(name.type, variables);
+                    expressions.add(new IndexExpression(name.name.key, restriction.startInclusive ? IndexOperator.GTE : IndexOperator.GT, value));
+                }
+                if (restriction.end != null)
+                {
+                    ByteBuffer value = restriction.end.getByteBuffer(name.type, variables);
+                    expressions.add(new IndexExpression(name.name.key, restriction.endInclusive ? IndexOperator.GTE : IndexOperator.GT, value));
+                }
+            }
+        }
+        return expressions;
+    }
+
+    private List<Pair<CFDefinition.Name, ColumnIdentifier>> getExpandedSelection()
+    {
+        if (selectedNames.isEmpty())
+        {
+            List<Pair<CFDefinition.Name, ColumnIdentifier>> selection = new ArrayList<Pair<CFDefinition.Name, ColumnIdentifier>>();
+            for (CFDefinition.Name name : cfDef)
+                selection.add(Pair.create(name, name.name));
+            return selection;
+        }
+        else
+        {
+            return selectedNames;
+        }
+    }
+
+    private ByteBuffer value(IColumn c)
+    {
+        return (c instanceof CounterColumn)
+             ? ByteBufferUtil.bytes(CounterContext.instance().total(c.value()))
+             : c.value();
+    }
+
+    private void addToSchema(CqlMetadata schema, Pair<CFDefinition.Name, ColumnIdentifier> p)
+    {
+        ByteBuffer nameAsRequested = p.right.key;
+        schema.name_types.put(nameAsRequested, TypeParser.getShortName(cfDef.cfm.comparator));
+        schema.value_types.put(nameAsRequested, TypeParser.getShortName(p.left.type));
+    }
+
+    private List<CqlRow> process(List<Row> rows, CqlMetadata schema)
+    {
+        List<CqlRow> cqlRows = new ArrayList<CqlRow>();
+        List<Pair<CFDefinition.Name, ColumnIdentifier>> selection = getExpandedSelection();
+        List<Column> thriftColumns = null;
+
+        for (org.apache.cassandra.db.Row row : rows)
+        {
+            if (cfDef.isCompact)
+            {
+                // One cqlRow per column
+                if (row.cf == null)
+                    continue;
+
+                for (IColumn c : row.cf.getSortedColumns())
+                {
+                    if (c.isMarkedForDelete())
+                        continue;
+
+                    thriftColumns = new ArrayList<Column>();
+
+                    ByteBuffer[] components = cfDef.isComposite
+                                            ? ((CompositeType)cfDef.cfm.comparator).split(c.name())
+                                            : null;
+
+                    // Respect selection order
+                    for (Pair<CFDefinition.Name, ColumnIdentifier> p : selection)
+                    {
+                        CFDefinition.Name name = p.left;
+                        ByteBuffer nameAsRequested = p.right.key;
+
+                        addToSchema(schema, p);
+                        Column col = new Column(nameAsRequested);
+                        switch (name.kind)
+                        {
+                            case KEY_ALIAS:
+                                col.setValue(row.key.key).setTimestamp(-1L);
+                                break;
+                            case COLUMN_ALIAS:
+                                col.setTimestamp(c.timestamp());
+                                if (cfDef.isComposite)
+                                {
+                                    if (name.compositePosition < components.length)
+                                        col.setValue(components[name.compositePosition]);
+                                    else
+                                        col.setValue(ByteBufferUtil.EMPTY_BYTE_BUFFER);
+                                }
+                                else
+                                {
+                                    col.setValue(c.name());
+                                }
+                                break;
+                            case VALUE_ALIAS:
+                                col.setValue(value(c)).setTimestamp(c.timestamp());
+                                break;
+                            case COLUMN_METADATA:
+                                // This should not happen for compact CF
+                                throw new AssertionError();
+                        }
+                        thriftColumns.add(col);
+                    }
+                    cqlRows.add(new CqlRow(row.key.key, thriftColumns));
+                }
+            }
+            else if (cfDef.isComposite)
+            {
+                // Sparse case: group column in cqlRow when composite prefix is equal
+                if (row.cf == null)
+                    continue;
+
+                CompositeType composite = (CompositeType)cfDef.cfm.comparator;
+                int last = composite.types.size() - 1;
+
+                ByteBuffer[] previous = null;
+                Map<ByteBuffer, IColumn> group = new HashMap<ByteBuffer, IColumn>();
+                for (IColumn c : row.cf)
+                {
+                    if (c.isMarkedForDelete())
+                        continue;
+
+                    ByteBuffer[] current = composite.split(c.name());
+                    // If current differs from previous, we've just finished a group
+                    if (previous != null && !isSameRow(previous, current))
+                    {
+                        cqlRows.add(handleGroup(selection, row.key.key, previous, group, schema));
+                        group = new HashMap<ByteBuffer, IColumn>();
+                    }
+
+                    // Accumulate the current column
+                    group.put(current[last], c);
+                    previous = current;
+                }
+                // Handle the last group
+                if (previous != null)
+                    cqlRows.add(handleGroup(selection, row.key.key, previous, group, schema));
+            }
+            else
+            {
+                // Static case: One cqlRow for all columns
+                thriftColumns = new ArrayList<Column>();
+                // Respect selection order
+                for (Pair<CFDefinition.Name, ColumnIdentifier> p : selection)
+                {
+                    CFDefinition.Name name = p.left;
+                    ByteBuffer nameAsRequested = p.right.key;
+
+                    if (name.kind == CFDefinition.Name.Kind.KEY_ALIAS)
+                    {
+                        addToSchema(schema, p);
+                        thriftColumns.add(new Column(nameAsRequested).setValue(row.key.key).setTimestamp(-1L));
+                        continue;
+                    }
+
+                    if (row.cf == null)
+                        continue;
+
+                    addToSchema(schema, p);
+                    IColumn c = row.cf.getColumn(name.name.key);
+                    Column col = new Column(name.name.key);
+                    if (c != null && !c.isMarkedForDelete())
+                        col.setValue(value(c)).setTimestamp(c.timestamp());
+                    thriftColumns.add(col);
+                }
+                cqlRows.add(new CqlRow(row.key.key, thriftColumns));
+            }
+        }
+        // We don't allow reversed on range scan, but we do on multiget (IN (...)), so let's reverse the rows there too.
+        if (parameters.isColumnsReversed)
+            Collections.reverse(cqlRows);
+
+        cqlRows = cqlRows.size() > parameters.limit ? cqlRows.subList(0, parameters.limit) : cqlRows;
+
+        // Trim result if needed to respect the limit
+        return cqlRows;
+    }
+
+    /**
+     * For sparse composite, returns wheter two columns belong to the same
+     * cqlRow base on the full list of component in the name.
+     * Two columns do belong together if they differ only by the last
+     * component.
+     */
+    private static boolean isSameRow(ByteBuffer[] c1, ByteBuffer[] c2)
+    {
+        // Cql don't allow to insert columns who doesn't have all component of
+        // the composite set for sparse composite. Someone coming from thrift
+        // could hit that though. But since we have no way to handle this
+        // correctly, better fail here and tell whomever may hit that (if
+        // someone ever do) to change the definition to a dense composite
+        assert c1.length == c2.length : "Sparse composite should not have partial column names";
+        for (int i = 0; i < c1.length - 1; i++)
+        {
+            if (!c1[i].equals(c2[i]))
+                return false;
+        }
+        return true;
+    }
+
+    private CqlRow handleGroup(List<Pair<CFDefinition.Name, ColumnIdentifier>> selection, ByteBuffer key, ByteBuffer[] components, Map<ByteBuffer, IColumn> columns, CqlMetadata schema)
+    {
+        List<Column> thriftColumns = new ArrayList<Column>();
+
+        // Respect requested order
+        for (Pair<CFDefinition.Name, ColumnIdentifier> p : selection)
+        {
+            CFDefinition.Name name = p.left;
+            ByteBuffer nameAsRequested = p.right.key;
+
+            addToSchema(schema, p);
+            Column col = new Column(nameAsRequested);
+            switch (name.kind)
+            {
+                case KEY_ALIAS:
+                    col.setValue(key).setTimestamp(-1L);
+                    break;
+                case COLUMN_ALIAS:
+                    col.setValue(components[name.compositePosition]);
+                    col.setTimestamp(-1L);
+                    break;
+                case VALUE_ALIAS:
+                    // This should not happen for SPARSE
+                    throw new AssertionError();
+                case COLUMN_METADATA:
+                    IColumn c = columns.get(name.name.key);
+                    if (c != null && !c.isMarkedForDelete())
+                        col.setValue(value(c)).setTimestamp(c.timestamp());
+                    break;
+            }
+            thriftColumns.add(col);
+        }
+        return new CqlRow(key, thriftColumns);
+    }
+
+    public static class RawStatement extends CFStatement implements Preprocessable
+    {
+        private final Parameters parameters;
+        private final List<ColumnIdentifier> selectClause;
+        private final List<Relation> whereClause;
+
+        public RawStatement(CFName cfName, Parameters parameters, List<ColumnIdentifier> selectClause, List<Relation> whereClause)
+        {
+            super(cfName);
+            this.parameters = parameters;
+            this.selectClause = selectClause;
+            this.whereClause = whereClause == null ? Collections.<Relation>emptyList() : whereClause;
+        }
+
+        public SelectStatement preprocess() throws InvalidRequestException
+        {
+            CFMetaData cfm = ThriftValidation.validateColumnFamily(keyspace(), columnFamily());
+            ThriftValidation.validateConsistencyLevel(keyspace(), parameters.consistencyLevel, RequestType.READ);
+
+            if (parameters.limit <= 0)
+                throw new InvalidRequestException("LIMIT must be strictly positive");
+
+            CFDefinition cfDef = cfm.getCfDef();
+            SelectStatement stmt = new SelectStatement(cfDef, parameters);
+            stmt.setBoundTerms(getBoundsTerms());
+
+            // Select clause
+            if (parameters.isCount)
+            {
+                if (selectClause.size() != 1 || (!selectClause.get(0).equals("*") && !selectClause.get(0).equals("1")))
+                    throw new InvalidRequestException("Only COUNT(*) and COUNT(1) operations are currently supported.");
+            }
+            else
+            {
+                for (ColumnIdentifier t : selectClause)
+                {
+                    CFDefinition.Name name = cfDef.get(t);
+                    if (name == null)
+                        throw new InvalidRequestException(String.format("Undefined name %s in selection clause", t));
+                    // Keeping the case (as in 'case sensitive') of the input name for the resultSet
+                    stmt.selectedNames.add(Pair.create(name, t));
+                }
+            }
+
+            /*
+             * WHERE clause. For a given entity, rules are:
+             *   - EQ relation conflicts with anything else (including a 2nd EQ)
+             *   - Can't have more than one LT(E) relation (resp. GT(E) relation)
+             *   - IN relation are restricted to row keys (for now) and conflics with anything else
+             *     (we could allow two IN for the same entity but that doesn't seem very useful)
+             *   - The value_alias cannot be restricted in any way (we don't support wide rows with indexed value in CQL so far)
+             */
+            for (Relation rel : whereClause)
+            {
+                CFDefinition.Name name = cfDef.get(rel.getEntity());
+                if (name == null)
+                    throw new InvalidRequestException(String.format("Undefined name %s in where clause ('%s')", rel.getEntity(), rel));
+
+                if (name.kind == CFDefinition.Name.Kind.VALUE_ALIAS)
+                    throw new InvalidRequestException(String.format("Restricting the value of a compact CF (%s) is not supported", name.name));
+
+                Restriction restriction = stmt.restrictions.get(name.name);
+                switch (rel.operator())
+                {
+                    case EQ:
+                        if (restriction != null)
+                            throw new InvalidRequestException(String.format("%s cannot be restricted by more than one relation if it includes an Equal", name));
+                        stmt.restrictions.put(name.name, new Restriction(Collections.singletonList(rel.getValue())));
+                        break;
+                    case GT:
+                    case GTE:
+                        if (name.kind == CFDefinition.Name.Kind.KEY_ALIAS && !StorageService.getPartitioner().preservesOrder())
+                            throw new InvalidRequestException("Only EQ and IN relation are supported on first component of the PRIMARY KEY for RandomPartitioner");
+                        if (restriction == null)
+                        {
+                            restriction = new Restriction();
+                            stmt.restrictions.put(name.name, restriction);
+                        }
+                        if (restriction.start != null)
+                            throw new InvalidRequestException(String.format("%s cannot be restricted by more than one Greater-Than relation", name));
+                        restriction.start = rel.getValue();
+                        if (rel.operator() == Relation.Type.GTE)
+                            restriction.startInclusive = true;
+                        break;
+                    case LT:
+                    case LTE:
+                        if (name.kind == CFDefinition.Name.Kind.KEY_ALIAS && !StorageService.getPartitioner().preservesOrder())
+                            throw new InvalidRequestException("Only EQ and IN relation are supported on first component of the PRIMARY KEY for RandomPartitioner");
+                        if (restriction == null)
+                        {
+                            restriction = new Restriction();
+                            stmt.restrictions.put(name.name, restriction);
+                        }
+                        if (restriction.end != null)
+                            throw new InvalidRequestException(String.format("%s cannot be restricted by more than one Lesser-Than relation", name));
+                        restriction.end = rel.getValue();
+                        if (rel.operator() == Relation.Type.LTE)
+                            restriction.endInclusive = true;
+                        break;
+                    case IN:
+                        if (restriction != null)
+                            throw new InvalidRequestException(String.format("%s cannot be restricted by more than one reation if it includes a IN", name));
+                        if (name.kind != CFDefinition.Name.Kind.KEY_ALIAS)
+                            throw new InvalidRequestException("IN relation can only be applied to the first component of the PRIMARY KEY");
+                        stmt.restrictions.put(name.name, new Restriction(rel.getInValues()));
+                        break;
+                }
+            }
+
+            /*
+             * At this point, the select statement if fully constructed, but we still have a few things to validate
+             */
+
+            // If a component of the PRIMARY KEY is restricted by a non-EQ relation, all preceding
+            // components must have a EQ, and all following must have no restriction
+            boolean shouldBeDone = false;
+            CFDefinition.Name previous = null;
+            for (CFDefinition.Name cname : cfDef.columns.values())
+            {
+                Restriction restriction = stmt.restrictions.get(cname.name);
+                if (restriction == null)
+                    shouldBeDone = true;
+                else if (shouldBeDone)
+                    throw new InvalidRequestException(String.format("PRIMARY KEY part %s cannot be restricted (preceding part %s is either not restricted or by a non-EQ relation)", cname, previous));
+                else if (!restriction.isEquality())
+                    shouldBeDone = true;
+                // We could support IN for the last name, we don't yet
+                else if (restriction.eqValues.size() > 1)
+                    throw new InvalidRequestException(String.format("PRIMARY KEY part %s cannot be restricted by IN relation", cname));
+
+                previous = cname;
+            }
+
+            // Deal with indexed columns
+            if (!cfDef.metadata.values().isEmpty())
+            {
+                boolean hasEq = false;
+                Set<ByteBuffer> indexed = Table.open(keyspace()).getColumnFamilyStore(columnFamily()).indexManager.getIndexedColumns();
+
+                for (CFDefinition.Name name : cfDef.metadata.values())
+                {
+                    Restriction restriction = stmt.restrictions.get(name.name);
+                    if (restriction == null)
+                        continue;
+
+                    stmt.hasIndexedExpression = true;
+                    if (restriction.isEquality() && indexed.contains(name.name.key))
+                    {
+                        hasEq = true;
+                        break;
+                    }
+                }
+
+                if (stmt.hasIndexedExpression && !hasEq)
+                    throw new InvalidRequestException("No indexed columns present in by-columns clause with Equal operator");
+
+                // If we have indexed columns and the key = X clause, we transform it into a key >= X AND key <= X clause.
+                // If it's a IN relation however, we reject it.
+                Restriction r = stmt.restrictions.get(cfDef.key.name);
+                if (r != null && r.isEquality())
+                {
+                    if (r.eqValues.size() > 1)
+                        throw new InvalidRequestException("Select on indexed columns and with IN clause for the PRIMARY KEY are not supported");
+
+                    r.start = r.eqValues.get(0);
+                    r.startInclusive = true;
+                    r.end = r.eqValues.get(0);
+                    r.endInclusive = true;
+                    r.eqValues = null;
+                }
+            }
+
+            // Only allow reversed if the row key restriction is an equality,
+            // since we don't know how to reverse otherwise
+            if (stmt.parameters.isColumnsReversed)
+            {
+                Restriction r = stmt.restrictions.get(cfDef.key.name);
+                if (r == null || !r.isEquality())
+                    throw new InvalidRequestException("Descending order is only supported is the first part of the PRIMARY KEY is restricted by an Equal or a IN");
+            }
+            return stmt;
+        }
+
+        @Override
+        public String toString()
+        {
+            return String.format("SelectRawStatement[name=%s, selectClause=%s, whereClause=%s, isCount=%s, cLevel=%s, limit=%s]",
+                    cfName,
+                    selectClause,
+                    whereClause,
+                    parameters.isCount,
+                    parameters.consistencyLevel,
+                    parameters.limit);
+        }
+
+        public void checkAccess(ClientState state)
+        {
+            throw new UnsupportedOperationException();
+        }
+
+        public CqlResult execute(ClientState state, List<ByteBuffer> variables)
+        {
+            throw new UnsupportedOperationException();
+        }
+    }
+
+    // A rather raw class that simplify validation and query for select
+    // Don't made public as this can be easily badly used
+    private static class Restriction
+    {
+        // for equality
+        List<Term> eqValues; // if null, it's a restriction by bounds
+
+        Restriction(List<Term> values)
+        {
+            this.eqValues = values;
+        }
+
+        // for bounds
+        Term start;
+        boolean startInclusive;
+        Term end;
+        boolean endInclusive;
+
+        Restriction()
+        {
+            this(null);
+        }
+
+        boolean isEquality()
+        {
+            return eqValues != null;
+        }
+    }
+
+    public static class Parameters
+    {
+        private final int limit;
+        private final ConsistencyLevel consistencyLevel;
+        private final boolean isColumnsReversed;
+        private final boolean isCount;
+
+        public Parameters(ConsistencyLevel consistency, int limit, boolean reversed, boolean isCount)
+        {
+            this.consistencyLevel = consistency;
+            this.limit = limit;
+            this.isColumnsReversed = reversed;
+            this.isCount = isCount;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/cassandra/blob/655ccc3a/src/java/org/apache/cassandra/cql3/statements/TruncateStatement.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/statements/TruncateStatement.java b/src/java/org/apache/cassandra/cql3/statements/TruncateStatement.java
new file mode 100644
index 0000000..d4c19c7
--- /dev/null
+++ b/src/java/org/apache/cassandra/cql3/statements/TruncateStatement.java
@@ -0,0 +1,68 @@
+/*
+ * 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.cassandra.cql3.statements;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.List;
+import java.util.concurrent.TimeoutException;
+
+import org.apache.cassandra.auth.Permission;
+import org.apache.cassandra.cql3.*;
+import org.apache.cassandra.service.ClientState;
+import org.apache.cassandra.service.StorageProxy;
+import org.apache.cassandra.thrift.CqlResult;
+import org.apache.cassandra.thrift.InvalidRequestException;
+import org.apache.cassandra.thrift.ThriftValidation;
+import org.apache.cassandra.thrift.UnavailableException;
+
+public class TruncateStatement extends CFStatement
+{
+    public TruncateStatement(CFName name)
+    {
+        super(name);
+    }
+
+    public void checkAccess(ClientState state) throws InvalidRequestException
+    {
+        state.hasColumnFamilyAccess(keyspace(), columnFamily(), Permission.WRITE);
+    }
+
+    public void validate(ClientState state) throws InvalidRequestException
+    {
+        ThriftValidation.validateColumnFamily(keyspace(), columnFamily());
+    }
+
+    public CqlResult execute(ClientState state, List<ByteBuffer> variables) throws InvalidRequestException, UnavailableException
+    {
+        try
+        {
+            StorageProxy.truncateBlocking(keyspace(), columnFamily());
+        }
+        catch (TimeoutException e)
+        {
+            throw (UnavailableException) new UnavailableException().initCause(e);
+        }
+        catch (IOException e)
+        {
+            throw (UnavailableException) new UnavailableException().initCause(e);
+        }
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cassandra/blob/655ccc3a/src/java/org/apache/cassandra/cql3/statements/UpdateStatement.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/statements/UpdateStatement.java b/src/java/org/apache/cassandra/cql3/statements/UpdateStatement.java
new file mode 100644
index 0000000..14fed1b
--- /dev/null
+++ b/src/java/org/apache/cassandra/cql3/statements/UpdateStatement.java
@@ -0,0 +1,359 @@
+/*
+ * 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.cassandra.cql3.statements;
+
+import java.nio.ByteBuffer;
+import java.util.*;
+
+import org.apache.cassandra.cql3.*;
+import org.apache.cassandra.config.CFMetaData;
+import org.apache.cassandra.db.CounterMutation;
+import org.apache.cassandra.db.IMutation;
+import org.apache.cassandra.db.RowMutation;
+import org.apache.cassandra.db.filter.QueryPath;
+import org.apache.cassandra.db.marshal.LongType;
+import org.apache.cassandra.service.ClientState;
+import org.apache.cassandra.thrift.InvalidRequestException;
+import org.apache.cassandra.utils.ByteBufferUtil;
+
+import static org.apache.cassandra.cql.QueryProcessor.validateColumnName;
+
+import static org.apache.cassandra.thrift.ThriftValidation.validateColumnFamily;
+import static org.apache.cassandra.thrift.ThriftValidation.validateCommutativeForWrite;
+
+/**
+ * An <code>UPDATE</code> statement parsed from a CQL query statement.
+ *
+ */
+public class UpdateStatement extends ModificationStatement
+{
+    private final Map<ColumnIdentifier, Operation> columns;
+    private final List<ColumnIdentifier> columnNames;
+    private final List<Term> columnValues;
+    private final List<Relation> whereClause;
+
+    private final Map<ColumnIdentifier, Operation> processedColumns = new HashMap<ColumnIdentifier, Operation>();
+    private final Map<ColumnIdentifier, List<Term>> processedKeys = new HashMap<ColumnIdentifier, List<Term>>();
+
+    /**
+     * Creates a new UpdateStatement from a column family name, columns map, consistency
+     * level, and key term.
+     *
+     * @param keyspace Keyspace (optional)
+     * @param columnFamily column family name
+     * @param columns a map of column name/values pairs
+     * @param whereClause the where clause
+     * @param attrs additional attributes for statement (CL, timestamp, timeToLive)
+     */
+    public UpdateStatement(CFName name,
+                           Map<ColumnIdentifier, Operation> columns,
+                           List<Relation> whereClause,
+                           Attributes attrs)
+    {
+        super(name, attrs);
+
+        this.columns = columns;
+        this.whereClause = whereClause;
+        this.columnNames = null;
+        this.columnValues = null;
+    }
+
+    /**
+     * Creates a new UpdateStatement from a column family name, a consistency level,
+     * key, and lists of column names and values.  It is intended for use with the
+     * alternate update format, <code>INSERT</code>.
+     *
+     * @param keyspace Keyspace (optional)
+     * @param columnFamily column family name
+     * @param columnNames list of column names
+     * @param columnValues list of column values (corresponds to names)
+     * @param attrs additional attributes for statement (CL, timestamp, timeToLive)
+     */
+    public UpdateStatement(CFName name,
+                           List<ColumnIdentifier> columnNames,
+                           List<Term> columnValues,
+                           Attributes attrs)
+    {
+        super(name, attrs);
+
+        this.columnNames = columnNames;
+        this.columnValues = columnValues;
+        this.whereClause = null;
+        this.columns = null;
+    }
+
+    /** {@inheritDoc} */
+    public List<IMutation> getMutations(ClientState clientState, List<ByteBuffer> variables) throws InvalidRequestException
+    {
+        boolean hasCommutativeOperation = false;
+
+        if (columns != null)
+        {
+            for (Map.Entry<ColumnIdentifier, Operation> column : columns.entrySet())
+            {
+                if (!column.getValue().isUnary())
+                    hasCommutativeOperation = true;
+
+                if (hasCommutativeOperation && column.getValue().isUnary())
+                    throw new InvalidRequestException("Mix of commutative and non-commutative operations is not allowed.");
+            }
+
+        }
+
+        // Deal here with the keyspace overwrite thingy to avoid mistake
+        CFMetaData metadata = validateColumnFamily(keyspace(), columnFamily(), hasCommutativeOperation);
+        if (hasCommutativeOperation)
+            validateCommutativeForWrite(metadata, cLevel);
+
+        CFDefinition cfDef = metadata.getCfDef();
+        preprocess(cfDef);
+
+        // Check key
+        List<Term> keys = processedKeys.get(cfDef.key.name);
+        if (keys == null || keys.isEmpty())
+            throw new InvalidRequestException(String.format("Missing mandatory PRIMARY KEY part %s", cfDef.key));
+
+        ColumnNameBuilder builder = cfDef.getColumnNameBuilder();
+        CFDefinition.Name firstEmpty = null;
+        for (CFDefinition.Name name : cfDef.columns.values())
+        {
+            List<Term> values = processedKeys.get(name.name);
+            if (values == null || values.isEmpty())
+            {
+                firstEmpty = name;
+                // For sparse, we must have all components
+                if (cfDef.isComposite && !cfDef.isCompact)
+                    throw new InvalidRequestException(String.format("Missing mandatory PRIMARY KEY part %s", name));
+            }
+            else if (firstEmpty != null)
+            {
+                throw new InvalidRequestException(String.format("Missing PRIMARY KEY part %s since %s is set", firstEmpty.name, name.name));
+            }
+            else
+            {
+                assert values.size() == 1; // We only allow IN for row keys so far
+                builder.add(values.get(0), Relation.Type.EQ, variables);
+            }
+        }
+
+        List<IMutation> rowMutations = new LinkedList<IMutation>();
+
+        for (Term key: keys)
+        {
+            ByteBuffer rawKey = key.getByteBuffer(cfDef.key.type, variables);
+            rowMutations.add(mutationForKey(cfDef, clientState, rawKey, builder, variables));
+        }
+
+        return rowMutations;
+    }
+
+    /**
+     * Compute a row mutation for a single key
+     *
+     *
+     * @param keyspace working keyspace
+     * @param key key to change
+     * @param metadata information about CF
+     *
+     * @param clientState
+     * @return row mutation
+     *
+     * @throws InvalidRequestException on the wrong request
+     */
+    private IMutation mutationForKey(CFDefinition cfDef, ClientState clientState, ByteBuffer key, ColumnNameBuilder builder, List<ByteBuffer> variables)
+    throws InvalidRequestException
+    {
+        // if true we need to wrap RowMutation into CounterMutation
+        boolean hasCounterColumn = false;
+        RowMutation rm = new RowMutation(cfDef.cfm.ksName, key);
+
+        if (cfDef.isCompact)
+        {
+            if (builder.componentCount() == 0)
+                throw new InvalidRequestException(String.format("Missing PRIMARY KEY part %s", cfDef.columns.values().iterator().next()));
+
+            Operation value = processedColumns.get(cfDef.value.name);
+            if (value == null)
+                throw new InvalidRequestException(String.format("Missing mandatory column %s", cfDef.value));
+            hasCounterColumn = addToMutation(clientState, rm, builder.build(), cfDef.value, value, variables);
+        }
+        else
+        {
+            for (CFDefinition.Name name : cfDef.metadata.values())
+            {
+                Operation value = processedColumns.get(name.name);
+                if (value == null)
+                    continue;
+
+                ByteBuffer colName = builder.copy().add(name.name.key).build();
+                hasCounterColumn |= addToMutation(clientState, rm, colName, name, value, variables);
+            }
+        }
+
+        return (hasCounterColumn) ? new CounterMutation(rm, getConsistencyLevel()) : rm;
+    }
+
+    private boolean addToMutation(ClientState clientState,
+                                  RowMutation rm,
+                                  ByteBuffer colName,
+                                  CFDefinition.Name valueDef,
+                                  Operation value,
+                                  List<ByteBuffer> variables) throws InvalidRequestException
+    {
+        if (value.isUnary())
+        {
+            ByteBuffer valueBytes = value.value.getByteBuffer(valueDef.type, variables);
+            validateColumnName(colName);
+            rm.add(new QueryPath(columnFamily(), null, colName),
+                   valueBytes,
+                   getTimestamp(clientState),
+                   getTimeToLive());
+            return false;
+        }
+        else
+        {
+            if (!valueDef.name.equals(value.ident))
+                throw new InvalidRequestException("Only expressions like X = X + <long> are supported.");
+
+            long val;
+            try
+            {
+                val = ByteBufferUtil.toLong(value.value.getByteBuffer(LongType.instance, variables));
+            }
+            catch (NumberFormatException e)
+            {
+                throw new InvalidRequestException(String.format("'%s' is an invalid value, should be a long.",
+                            value.value.getText()));
+            }
+
+            if (value.type == Operation.Type.MINUS)
+            {
+                if (val == Long.MIN_VALUE)
+                    throw new InvalidRequestException("The negation of " + val + " overflows supported integer precision (signed 8 bytes integer)");
+                else
+                    val = -val;
+            }
+            rm.addCounter(new QueryPath(columnFamily(), null, colName), val);
+            return true;
+        }
+    }
+
+    private void preprocess(CFDefinition cfDef) throws InvalidRequestException
+    {
+        if (columns == null)
+        {
+            // Created from an INSERT
+            // Don't hate, validate.
+            if (columnNames.size() != columnValues.size())
+                throw new InvalidRequestException("unmatched column names/values");
+            if (columnNames.size() < 1)
+                throw new InvalidRequestException("no columns specified for INSERT");
+
+            for (int i = 0; i < columnNames.size(); i++)
+            {
+                CFDefinition.Name name = cfDef.get(columnNames.get(i));
+                if (name == null)
+                    throw new InvalidRequestException(String.format("Unknown identifier %s", columnNames.get(i)));
+
+                switch (name.kind)
+                {
+                    case KEY_ALIAS:
+                    case COLUMN_ALIAS:
+                        if (processedKeys.containsKey(name.name))
+                            throw new InvalidRequestException(String.format("Multiple definition found for PRIMARY KEY part %s", name));
+                        processedKeys.put(name.name, Collections.singletonList(columnValues.get(i)));
+                        break;
+                    case VALUE_ALIAS:
+                    case COLUMN_METADATA:
+                        if (processedColumns.containsKey(name.name))
+                            throw new InvalidRequestException(String.format("Multiple definition found for column %s", name));
+                        processedColumns.put(name.name, new Operation(columnValues.get(i)));
+                        break;
+                }
+            }
+        }
+        else
+        {
+            // Created from an UPDATE
+            for (Map.Entry<ColumnIdentifier, Operation> entry : columns.entrySet())
+            {
+                CFDefinition.Name name = cfDef.get(entry.getKey());
+                if (name == null)
+                    throw new InvalidRequestException(String.format("Unknown identifier %s", entry.getKey()));
+
+                switch (name.kind)
+                {
+                    case KEY_ALIAS:
+                    case COLUMN_ALIAS:
+                        throw new InvalidRequestException(String.format("PRIMARY KEY part %s found in SET part", entry.getKey()));
+                    case VALUE_ALIAS:
+                    case COLUMN_METADATA:
+                        if (processedColumns.containsKey(name.name))
+                            throw new InvalidRequestException(String.format("Multiple definition found for column %s", name));
+                        processedColumns.put(name.name, entry.getValue());
+                        break;
+                }
+            }
+            processKeys(cfDef, whereClause, processedKeys);
+        }
+    }
+
+    // Reused by DeleteStatement
+    static void processKeys(CFDefinition cfDef, List<Relation> keys, Map<ColumnIdentifier, List<Term>> processed) throws InvalidRequestException
+    {
+        for (Relation rel : keys)
+        {
+            CFDefinition.Name name = cfDef.get(rel.getEntity());
+            if (name == null)
+                throw new InvalidRequestException(String.format("Unknown key identifier %s", rel.getEntity()));
+
+            switch (name.kind)
+            {
+                case KEY_ALIAS:
+                case COLUMN_ALIAS:
+                    List<Term> values;
+                    if (rel.operator() == Relation.Type.EQ)
+                        values = Collections.singletonList(rel.getValue());
+                    else if (name.kind == CFDefinition.Name.Kind.KEY_ALIAS && rel.operator() == Relation.Type.IN)
+                        values = rel.getInValues();
+                    else
+                        throw new InvalidRequestException(String.format("Invalid operator %s for key %s", rel.operator(), rel.getEntity()));
+
+                    if (processed.containsKey(name.name))
+                        throw new InvalidRequestException(String.format("Multiple definition found for PRIMARY KEY part %s", name));
+                    processed.put(name.name, values);
+                    break;
+                case VALUE_ALIAS:
+                case COLUMN_METADATA:
+                    throw new InvalidRequestException(String.format("PRIMARY KEY part %s found in SET part", rel.getEntity()));
+            }
+        }
+    }
+
+    public String toString()
+    {
+        return String.format("UpdateStatement(name=%s, keys=%s, columns=%s, consistency=%s, timestamp=%s, timeToLive=%s)",
+                             cfName,
+                             whereClause,
+                             columns,
+                             getConsistencyLevel(),
+                             timestamp,
+                             timeToLive);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cassandra/blob/655ccc3a/src/java/org/apache/cassandra/cql3/statements/UseStatement.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/statements/UseStatement.java b/src/java/org/apache/cassandra/cql3/statements/UseStatement.java
new file mode 100644
index 0000000..6a63c2a
--- /dev/null
+++ b/src/java/org/apache/cassandra/cql3/statements/UseStatement.java
@@ -0,0 +1,48 @@
+/*
+ * 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.cassandra.cql3.statements;
+
+import java.nio.ByteBuffer;
+import java.util.List;
+
+import org.apache.cassandra.cql3.CQLStatement;
+import org.apache.cassandra.service.ClientState;
+import org.apache.cassandra.thrift.CqlResult;
+import org.apache.cassandra.thrift.InvalidRequestException;
+
+public class UseStatement extends CQLStatement
+{
+    private final String keyspace;
+
+    public UseStatement(String keyspace)
+    {
+        this.keyspace = keyspace;
+    }
+
+    public void checkAccess(ClientState state)
+    {
+        // No specific access
+    }
+
+    public CqlResult execute(ClientState state, List<ByteBuffer> variables) throws InvalidRequestException
+    {
+        state.setKeyspace(keyspace);
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cassandra/blob/655ccc3a/src/java/org/apache/cassandra/db/CounterMutation.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/CounterMutation.java b/src/java/org/apache/cassandra/db/CounterMutation.java
index 324c0b6..541dfc0 100644
--- a/src/java/org/apache/cassandra/db/CounterMutation.java
+++ b/src/java/org/apache/cassandra/db/CounterMutation.java
@@ -147,6 +147,15 @@ public class CounterMutation implements IMutation
         rm.apply();
     }
 
+    public void addAll(IMutation m)
+    {
+        if (!(m instanceof CounterMutation))
+            throw new IllegalArgumentException();
+
+        CounterMutation cm = (CounterMutation)m;
+        rowMutation.addAll(cm.rowMutation);
+    }
+
     @Override
     public String toString()
     {

http://git-wip-us.apache.org/repos/asf/cassandra/blob/655ccc3a/src/java/org/apache/cassandra/db/IMutation.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/IMutation.java b/src/java/org/apache/cassandra/db/IMutation.java
index 3650f51..eeb92ea 100644
--- a/src/java/org/apache/cassandra/db/IMutation.java
+++ b/src/java/org/apache/cassandra/db/IMutation.java
@@ -29,4 +29,5 @@ public interface IMutation
     public ByteBuffer key();
     public void apply() throws IOException;
     public String toString(boolean shallow);
+    public void addAll(IMutation m);
 }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/655ccc3a/src/java/org/apache/cassandra/db/RowMutation.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/RowMutation.java b/src/java/org/apache/cassandra/db/RowMutation.java
index 4a58ad7..773877b 100644
--- a/src/java/org/apache/cassandra/db/RowMutation.java
+++ b/src/java/org/apache/cassandra/db/RowMutation.java
@@ -245,6 +245,25 @@ public class RowMutation implements IMutation, MessageProducer
         }
     }
 
+    public void addAll(IMutation m)
+    {
+        if (!(m instanceof RowMutation))
+            throw new IllegalArgumentException();
+
+        RowMutation rm = (RowMutation)m;
+        if (!table_.equals(rm.table_) || !key_.equals(rm.key_))
+            throw new IllegalArgumentException();
+
+        for (Map.Entry<Integer, ColumnFamily> entry : rm.modifications_.entrySet())
+        {
+            // It's slighty faster to assume the key wasn't present and fix if
+            // not in the case where it wasn't there indeed.
+            ColumnFamily cf = modifications_.put(entry.getKey(), entry.getValue());
+            if (cf != null)
+                entry.getValue().resolve(cf);
+        }
+    }
+
     /*
      * This is equivalent to calling commit. Applies the changes to
      * to the table that is obtained by calling Table.open().

http://git-wip-us.apache.org/repos/asf/cassandra/blob/655ccc3a/src/java/org/apache/cassandra/db/SystemTable.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/SystemTable.java b/src/java/org/apache/cassandra/db/SystemTable.java
index ef16eb0..99c343b 100644
--- a/src/java/org/apache/cassandra/db/SystemTable.java
+++ b/src/java/org/apache/cassandra/db/SystemTable.java
@@ -94,7 +94,7 @@ public class SystemTable
 
         rm = new RowMutation(Table.SYSTEM_TABLE, ByteBufferUtil.bytes("cql"));
         cf = ColumnFamily.create(Table.SYSTEM_TABLE, VERSION_CF);
-        cf.addColumn(new Column(ByteBufferUtil.bytes("version"), ByteBufferUtil.bytes(QueryProcessor.CQL_VERSION)));
+        cf.addColumn(new Column(ByteBufferUtil.bytes("version"), ByteBufferUtil.bytes(QueryProcessor.CQL_VERSION.toString())));
         rm.add(cf);
         rm.apply();
 

http://git-wip-us.apache.org/repos/asf/cassandra/blob/655ccc3a/src/java/org/apache/cassandra/db/marshal/AbstractCompositeType.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/marshal/AbstractCompositeType.java b/src/java/org/apache/cassandra/db/marshal/AbstractCompositeType.java
index d3f9c1a..cfc3b2e 100644
--- a/src/java/org/apache/cassandra/db/marshal/AbstractCompositeType.java
+++ b/src/java/org/apache/cassandra/db/marshal/AbstractCompositeType.java
@@ -108,6 +108,23 @@ public abstract class AbstractCompositeType extends AbstractType<ByteBuffer>
         return 1;
     }
 
+    /**
+     * Split a composite column names into it's components.
+     */
+    public ByteBuffer[] split(ByteBuffer name)
+    {
+        List<ByteBuffer> l = new ArrayList<ByteBuffer>();
+        ByteBuffer bb = name.duplicate();
+        int i = 0;
+        while (bb.remaining() > 0)
+        {
+            getNextComparator(i++, bb);
+            l.add(getWithShortLength(bb));
+            bb.get(); // skip end-of-component
+        }
+        return l.toArray(new ByteBuffer[l.size()]);
+    }
+
     public String getString(ByteBuffer bytes)
     {
         StringBuilder sb = new StringBuilder();

http://git-wip-us.apache.org/repos/asf/cassandra/blob/655ccc3a/src/java/org/apache/cassandra/db/marshal/CompositeType.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/marshal/CompositeType.java b/src/java/org/apache/cassandra/db/marshal/CompositeType.java
index 8343f27..3b71000 100644
--- a/src/java/org/apache/cassandra/db/marshal/CompositeType.java
+++ b/src/java/org/apache/cassandra/db/marshal/CompositeType.java
@@ -18,12 +18,17 @@
  */
 package org.apache.cassandra.db.marshal;
 
+import java.io.*;
 import java.nio.ByteBuffer;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
+import org.apache.cassandra.cql3.ColumnNameBuilder;
+import org.apache.cassandra.cql3.Relation;
+import org.apache.cassandra.cql3.Term;
 import org.apache.cassandra.config.ConfigurationException;
+import org.apache.cassandra.io.util.FastByteArrayOutputStream;
+import org.apache.cassandra.utils.ByteBufferUtil;
+import org.apache.cassandra.thrift.InvalidRequestException;
 
 /*
  * The encoding of a CompositeType column name should be:
@@ -47,8 +52,7 @@ import org.apache.cassandra.config.ConfigurationException;
  */
 public class CompositeType extends AbstractCompositeType
 {
-    // package protected for unit tests sake
-    final List<AbstractType<?>> types;
+    public final List<AbstractType<?>> types;
 
     // interning instances
     private static final Map<List<AbstractType<?>>, CompositeType> instances = new HashMap<List<AbstractType<?>>, CompositeType>();
@@ -138,4 +142,118 @@ public class CompositeType extends AbstractCompositeType
     {
         return getClass().getName() + TypeParser.stringifyTypeParameters(types);
     }
+
+    public static class Builder implements ColumnNameBuilder
+    {
+        private final CompositeType composite;
+        private int current;
+
+        private final FastByteArrayOutputStream baos = new FastByteArrayOutputStream();
+        private final DataOutput out = new DataOutputStream(baos);
+
+        public Builder(CompositeType composite)
+        {
+            this.composite = composite;
+        }
+
+        private Builder(Builder b)
+        {
+            this(b.composite);
+            this.current = b.current;
+            try
+            {
+                out.write(b.baos.toByteArray());
+            }
+            catch (IOException e)
+            {
+                throw new RuntimeException(e);
+            }
+        }
+
+        public Builder add(Term t, Relation.Type op, List<ByteBuffer> variables) throws InvalidRequestException
+        {
+            if (current >= composite.types.size())
+                throw new IllegalStateException("Composite column is already fully constructed");
+
+            AbstractType currentType = composite.types.get(current++);
+            ByteBuffer buffer = t.getByteBuffer(currentType, variables);
+            try
+            {
+                ByteBufferUtil.writeWithShortLength(buffer, out);
+
+                /*
+                 * Given the rules for eoc (end-of-component, see AbstractCompositeType.compare()),
+                 * We can select:
+                 *   - = 'a' by using <'a'><0>
+                 *   - < 'a' by using <'a'><-1>
+                 *   - <= 'a' by using <'a'><1>
+                 *   - > 'a' by using <'a'><1>
+                 *   - >= 'a' by using <'a'><0>
+                 */
+                switch (op)
+                {
+                    case LT:
+                        out.write((byte) -1);
+                        break;
+                    case GT:
+                    case LTE:
+                        out.write((byte) 1);
+                        break;
+                    default:
+                        out.write((byte) 0);
+                        break;
+                }
+                return this;
+            }
+            catch (IOException e)
+            {
+                throw new RuntimeException(e);
+            }
+        }
+
+        public Builder add(ByteBuffer bb)
+        {
+            if (current >= composite.types.size())
+                throw new IllegalStateException("Composite column is already fully constructed");
+
+            try
+            {
+                ByteBufferUtil.writeWithShortLength(bb, out);
+                out.write((byte) 0);
+                return this;
+            }
+            catch (IOException e)
+            {
+                throw new RuntimeException(e);
+            }
+        }
+
+        public int componentCount()
+        {
+            return current;
+        }
+
+        public ByteBuffer build()
+        {
+            return ByteBuffer.wrap(baos.toByteArray());
+        }
+
+        public ByteBuffer buildAsEndOfRange()
+        {
+            if (current >= composite.types.size())
+                throw new IllegalStateException("Composite column is already fully constructed");
+
+            if (current == 0)
+                return ByteBufferUtil.EMPTY_BYTE_BUFFER;
+
+            ByteBuffer bb = build();
+            bb.put(bb.remaining() - 1, (byte)1);
+            return bb;
+        }
+
+        public Builder copy()
+        {
+            return new Builder(this);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/655ccc3a/src/java/org/apache/cassandra/service/ClientState.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/service/ClientState.java b/src/java/org/apache/cassandra/service/ClientState.java
index d2df8a9..c66d99f 100644
--- a/src/java/org/apache/cassandra/service/ClientState.java
+++ b/src/java/org/apache/cassandra/service/ClientState.java
@@ -20,6 +20,8 @@ package org.apache.cassandra.service;
 
 import java.util.*;
 
+import com.google.common.collect.Sets;
+import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -32,6 +34,7 @@ import org.apache.cassandra.cql.CQLStatement;
 import org.apache.cassandra.db.Table;
 import org.apache.cassandra.thrift.AuthenticationException;
 import org.apache.cassandra.thrift.InvalidRequestException;
+import org.apache.cassandra.utils.SemanticVersion;
 
 /**
  * A container for per-client, thread-local state that Avro/Thrift threads must hold.
@@ -41,12 +44,14 @@ public class ClientState
 {
     private static final int MAX_CACHE_PREPARED = 10000;    // Enough to keep buggy clients from OOM'ing us
     private static Logger logger = LoggerFactory.getLogger(ClientState.class);
+    public static final SemanticVersion DEFAULT_CQL_VERSION = org.apache.cassandra.cql.QueryProcessor.CQL_VERSION;
 
     // Current user for the session
     private AuthenticatedUser user;
     private String keyspace;
     // Reusable array for authorization
     private final List<Object> resource = new ArrayList<Object>();
+    private SemanticVersion cqlVersion = DEFAULT_CQL_VERSION;
 
     // An LRU map of prepared statements
     private Map<Integer, CQLStatement> prepared = new LinkedHashMap<Integer, CQLStatement>(16, 0.75f, true) {
@@ -55,6 +60,12 @@ public class ClientState
         }
     };
 
+    private Map<Integer, org.apache.cassandra.cql3.CQLStatement> cql3Prepared = new LinkedHashMap<Integer, org.apache.cassandra.cql3.CQLStatement>(16, 0.75f, true) {
+        protected boolean removeEldestEntry(Map.Entry<Integer, org.apache.cassandra.cql3.CQLStatement> eldest) {
+            return size() > MAX_CACHE_PREPARED;
+        }
+    };
+
     private long clock;
 
     /**
@@ -69,7 +80,12 @@ public class ClientState
     {
         return prepared;
     }
-    
+
+    public Map<Integer, org.apache.cassandra.cql3.CQLStatement> getCQL3Prepared()
+    {
+        return cql3Prepared;
+    }
+
     public String getRawKeyspace()
     {
         return keyspace;
@@ -129,6 +145,7 @@ public class ClientState
         keyspace = null;
         resourceClear();
         prepared.clear();
+        cql3Prepared.clear();
     }
 
     /**
@@ -219,4 +236,42 @@ public class ClientState
         clock = clock >= current ? clock + 1 : current;
         return clock;
     }
+
+    public void setCQLVersion(String str) throws InvalidRequestException
+    {
+        SemanticVersion version;
+        try
+        {
+            version = new SemanticVersion(str);
+        }
+        catch (IllegalArgumentException e)
+        {
+            throw new InvalidRequestException(e.getMessage());
+        }
+
+        SemanticVersion cql = org.apache.cassandra.cql.QueryProcessor.CQL_VERSION;
+        SemanticVersion cql3 = org.apache.cassandra.cql3.QueryProcessor.CQL_VERSION;
+
+        if (version.isSupportedBy(cql))
+            cqlVersion = cql;
+        else if (version.isSupportedBy(cql3))
+            cqlVersion = cql3;
+        else
+            throw new InvalidRequestException(String.format("Provided version %s is not supported by this server (supported: %s)",
+                                                            version,
+                                                            StringUtils.join(getCQLSupportedVersion(), ", ")));
+    }
+
+    public SemanticVersion getCQLVersion()
+    {
+        return cqlVersion;
+    }
+
+    public static SemanticVersion[] getCQLSupportedVersion()
+    {
+        SemanticVersion cql = org.apache.cassandra.cql.QueryProcessor.CQL_VERSION;
+        SemanticVersion cql3 = org.apache.cassandra.cql3.QueryProcessor.CQL_VERSION;
+
+        return new SemanticVersion[]{ cql, cql3 };
+    }
 }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/655ccc3a/src/java/org/apache/cassandra/service/StorageService.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/service/StorageService.java b/src/java/org/apache/cassandra/service/StorageService.java
index c6d03b1..975b729 100644
--- a/src/java/org/apache/cassandra/service/StorageService.java
+++ b/src/java/org/apache/cassandra/service/StorageService.java
@@ -413,6 +413,7 @@ public class StorageService implements IEndpointStateChangeSubscriber, StorageSe
     {
         logger_.info("Cassandra version: " + FBUtilities.getReleaseVersionString());
         logger_.info("Thrift API version: " + Constants.VERSION);
+        logger_.info("CQL supported versions: " + StringUtils.join(ClientState.getCQLSupportedVersion(), ",") + " (default: " + ClientState.DEFAULT_CQL_VERSION + ")");
 
         if (initialized)
         {

http://git-wip-us.apache.org/repos/asf/cassandra/blob/655ccc3a/src/java/org/apache/cassandra/thrift/CassandraServer.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/thrift/CassandraServer.java b/src/java/org/apache/cassandra/thrift/CassandraServer.java
index 8611307..b84c3ce 100644
--- a/src/java/org/apache/cassandra/thrift/CassandraServer.java
+++ b/src/java/org/apache/cassandra/thrift/CassandraServer.java
@@ -1182,7 +1182,10 @@ public class CassandraServer implements Cassandra.Iface
                 
         try
         {
-            return QueryProcessor.process(queryString, state());
+            if (state().getCQLVersion().major == 2)
+                return QueryProcessor.process(queryString, state());
+            else
+                return org.apache.cassandra.cql3.QueryProcessor.process(queryString, state());
         }
         catch (RecognitionException e)
         {
@@ -1201,7 +1204,10 @@ public class CassandraServer implements Cassandra.Iface
         
         try
         {
-            return QueryProcessor.prepare(queryString, state());
+            if (state().getCQLVersion().major == 2)
+                return QueryProcessor.prepare(queryString, state());
+            else
+                return org.apache.cassandra.cql3.QueryProcessor.prepare(queryString, state());
         }
         catch (RecognitionException e)
         {
@@ -1215,14 +1221,34 @@ public class CassandraServer implements Cassandra.Iface
     throws InvalidRequestException, UnavailableException, TimedOutException, SchemaDisagreementException, TException
     {
         if (logger.isDebugEnabled()) logger.debug("execute_prepared_cql_query");
-        
-        CQLStatement statement = state().getPrepared().get(itemId);
 
-        if (statement == null)
-            throw new InvalidRequestException(String.format("Prepared query with ID %d not found", itemId));
-        logger.trace("Retrieved prepared statement #{} with {} bind markers", itemId, state().getPrepared().size());
+        if (state().getCQLVersion().major == 2)
+        {
+            CQLStatement statement = state().getPrepared().get(itemId);
+
+            if (statement == null)
+                throw new InvalidRequestException(String.format("Prepared query with ID %d not found", itemId));
+            logger.trace("Retrieved prepared statement #{} with {} bind markers", itemId, statement.boundTerms);
+
+            return QueryProcessor.processPrepared(statement, state(), bindVariables);
+        }
+        else
+        {
+            org.apache.cassandra.cql3.CQLStatement statement = state().getCQL3Prepared().get(itemId);
+
+            if (statement == null)
+                throw new InvalidRequestException(String.format("Prepared query with ID %d not found", itemId));
+            logger.trace("Retrieved prepared statement #{} with {} bind markers", itemId, statement.getBoundsTerms());
+
+            return org.apache.cassandra.cql3.QueryProcessor.processPrepared(statement, state(), bindVariables);
+        }
+    }
+
+    public void set_cql_version(String version) throws InvalidRequestException
+    {
+        logger.debug("set_cql_version: " + version);
 
-        return QueryProcessor.processPrepared(statement, state(), bindVariables);
+        state().setCQLVersion(version);
     }
 
     // main method moved to CassandraDaemon

http://git-wip-us.apache.org/repos/asf/cassandra/blob/655ccc3a/src/java/org/apache/cassandra/thrift/ThriftValidation.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/thrift/ThriftValidation.java b/src/java/org/apache/cassandra/thrift/ThriftValidation.java
index c09aeca..a4e32b1 100644
--- a/src/java/org/apache/cassandra/thrift/ThriftValidation.java
+++ b/src/java/org/apache/cassandra/thrift/ThriftValidation.java
@@ -617,10 +617,6 @@ public class ThriftValidation
             if (cf_def.column_metadata == null)
                 return;
 
-            AbstractType<?> comparator = cfType == ColumnFamilyType.Standard
-                                    ? TypeParser.parse(cf_def.comparator_type)
-                                    : TypeParser.parse(cf_def.subcomparator_type);
-
             if (cf_def.key_alias != null)
             {
                 // check if any of the columns has name equal to the cf.key_alias
@@ -642,6 +638,8 @@ public class ThriftValidation
                         indexNames.add(cd.getIndexName());
             }
 
+            AbstractType<?> comparator = CFMetaData.getColumnDefinitionComparator(cf_def);
+
             for (ColumnDef c : cf_def.column_metadata)
             {
                 TypeParser.parse(c.validation_class);
@@ -653,7 +651,7 @@ public class ThriftValidation
                 catch (MarshalException e)
                 {
                     throw new InvalidRequestException(String.format("Column name %s is not valid for comparator %s",
-                                                                    ByteBufferUtil.bytesToHex(c.name), cf_def.comparator_type));
+                                                                    ByteBufferUtil.bytesToHex(c.name), comparator));
                 }
 
                 if (c.index_type == null)