mirror of
https://github.com/scylladb/scylladb.git
synced 2026-04-24 18:40:38 +00:00
cql3: Convert SingleColumnRelation
This commit is contained in:
@@ -15,59 +15,59 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.cassandra.cql3;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
/*
|
||||
* Copyright 2015 Cloudius Systems
|
||||
*
|
||||
* Modified by Cloudius Systems
|
||||
*/
|
||||
|
||||
import org.apache.cassandra.config.CFMetaData;
|
||||
import org.apache.cassandra.config.ColumnDefinition;
|
||||
import org.apache.cassandra.cql3.Term.Raw;
|
||||
import org.apache.cassandra.cql3.restrictions.Restriction;
|
||||
import org.apache.cassandra.cql3.restrictions.SingleColumnRestriction;
|
||||
import org.apache.cassandra.cql3.statements.Bound;
|
||||
import org.apache.cassandra.db.marshal.CollectionType;
|
||||
import org.apache.cassandra.db.marshal.ListType;
|
||||
import org.apache.cassandra.db.marshal.MapType;
|
||||
import org.apache.cassandra.exceptions.InvalidRequestException;
|
||||
#pragma once
|
||||
|
||||
import static org.apache.cassandra.cql3.statements.RequestValidations.checkFalse;
|
||||
import static org.apache.cassandra.cql3.statements.RequestValidations.checkTrue;
|
||||
#include <vector>
|
||||
|
||||
#include "core/shared_ptr.hh"
|
||||
#include "to_string.hh"
|
||||
|
||||
#include "cql3/relation.hh"
|
||||
#include "cql3/column_identifier.hh"
|
||||
#include "cql3/term.hh"
|
||||
|
||||
namespace cql3 {
|
||||
|
||||
/**
|
||||
* Relations encapsulate the relationship between an entity of some kind, and
|
||||
* a value (term). For example, <key> > "start" or "colname1" = "somevalue".
|
||||
*
|
||||
*/
|
||||
public final class SingleColumnRelation extends Relation
|
||||
{
|
||||
private final ColumnIdentifier.Raw entity;
|
||||
private final Term.Raw mapKey;
|
||||
private final Term.Raw value;
|
||||
private final List<Term.Raw> inValues;
|
||||
|
||||
private SingleColumnRelation(ColumnIdentifier.Raw entity, Term.Raw mapKey, Operator type, Term.Raw value, List<Term.Raw> inValues)
|
||||
{
|
||||
this.entity = entity;
|
||||
this.mapKey = mapKey;
|
||||
this.relationType = type;
|
||||
this.value = value;
|
||||
this.inValues = inValues;
|
||||
}
|
||||
|
||||
class single_column_relation final : public relation {
|
||||
private:
|
||||
::shared_ptr<column_identifier::raw> _entity;
|
||||
::shared_ptr<term::raw> _map_key;
|
||||
::shared_ptr<term::raw> _value;
|
||||
std::vector<::shared_ptr<term::raw>> _in_values;
|
||||
private:
|
||||
single_column_relation(::shared_ptr<column_identifier::raw> entity, ::shared_ptr<term::raw> map_key,
|
||||
const operator_type& type, ::shared_ptr<term::raw> value, std::vector<::shared_ptr<term::raw>> in_values)
|
||||
: relation(type)
|
||||
, _entity(std::move(entity))
|
||||
, _map_key(std::move(map_key))
|
||||
, _value(std::move(value))
|
||||
, _in_values(std::move(in_values))
|
||||
{ }
|
||||
public:
|
||||
/**
|
||||
* Creates a new relation.
|
||||
*
|
||||
* @param entity the kind of relation this is; what the term is being compared to.
|
||||
* @param mapKey the key into the entity identifying the value the term is being compared to.
|
||||
* @param map_key the key into the entity identifying the value the term is being compared to.
|
||||
* @param type the type that describes how this entity relates to the value.
|
||||
* @param value the value being compared.
|
||||
*/
|
||||
public SingleColumnRelation(ColumnIdentifier.Raw entity, Term.Raw mapKey, Operator type, Term.Raw value)
|
||||
{
|
||||
this(entity, mapKey, type, value, null);
|
||||
}
|
||||
single_column_relation(::shared_ptr<column_identifier::raw> entity, ::shared_ptr<term::raw> map_key,
|
||||
const operator_type& type, ::shared_ptr<term::raw> value)
|
||||
: single_column_relation(std::move(entity), std::move(map_key), type, std::move(value), {})
|
||||
{ }
|
||||
|
||||
/**
|
||||
* Creates a new relation.
|
||||
@@ -76,31 +76,32 @@ public final class SingleColumnRelation extends Relation
|
||||
* @param type the type that describes how this entity relates to the value.
|
||||
* @param value the value being compared.
|
||||
*/
|
||||
public SingleColumnRelation(ColumnIdentifier.Raw entity, Operator type, Term.Raw value)
|
||||
single_column_relation(::shared_ptr<column_identifier::raw> entity, const operator_type& type, ::shared_ptr<term::raw> value)
|
||||
: single_column_relation(std::move(entity), {}, std::move(type), std::move(value))
|
||||
{ }
|
||||
|
||||
#if 0
|
||||
public static SingleColumnRelation createInRelation(::shared_ptr<column_identifier::raw> entity, List<::shared_ptr<term::raw>> in_values)
|
||||
{
|
||||
this(entity, null, type, value);
|
||||
return new SingleColumnRelation(entity, null, operator_type::IN, null, in_values);
|
||||
}
|
||||
#endif
|
||||
|
||||
public:
|
||||
::shared_ptr<column_identifier::raw> get_entity() {
|
||||
return _entity;
|
||||
}
|
||||
|
||||
public static SingleColumnRelation createInRelation(ColumnIdentifier.Raw entity, List<Term.Raw> inValues)
|
||||
{
|
||||
return new SingleColumnRelation(entity, null, Operator.IN, null, inValues);
|
||||
}
|
||||
|
||||
public ColumnIdentifier.Raw getEntity()
|
||||
{
|
||||
return entity;
|
||||
}
|
||||
|
||||
public Term.Raw getMapKey()
|
||||
{
|
||||
return mapKey;
|
||||
::shared_ptr<term::raw> get_map_key() {
|
||||
return _map_key;
|
||||
}
|
||||
|
||||
#if 0
|
||||
@Override
|
||||
protected Term toTerm(List<? extends ColumnSpecification> receivers,
|
||||
Raw raw,
|
||||
String keyspace,
|
||||
VariableSpecifications boundNames)
|
||||
::shared_ptr<variable_specifications> boundNames)
|
||||
throws InvalidRequestException
|
||||
{
|
||||
assert receivers.size() == 1;
|
||||
@@ -114,86 +115,93 @@ public final class SingleColumnRelation extends Relation
|
||||
{
|
||||
switch (relationType)
|
||||
{
|
||||
case GT: return new SingleColumnRelation(entity, Operator.GTE, value);
|
||||
case LT: return new SingleColumnRelation(entity, Operator.LTE, value);
|
||||
case GT: return new SingleColumnRelation(entity, operator_type.GTE, value);
|
||||
case LT: return new SingleColumnRelation(entity, operator_type.LTE, value);
|
||||
default: return this;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String entityAsString = entity.toString();
|
||||
if (mapKey != null)
|
||||
entityAsString = String.format("%s[%s]", entityAsString, mapKey);
|
||||
virtual sstring to_string() override {
|
||||
auto entity_as_string = _entity->to_string();
|
||||
if (_map_key) {
|
||||
entity_as_string = sprint("%s[%s]", std::move(entity_as_string), _map_key->to_string());
|
||||
}
|
||||
|
||||
if (isIN())
|
||||
return String.format("%s IN %s", entityAsString, inValues);
|
||||
if (is_IN()) {
|
||||
return sprint("%s IN %s", entity_as_string, ::to_string(_in_values));
|
||||
}
|
||||
|
||||
return String.format("%s %s %s", entityAsString, relationType, value);
|
||||
return sprint("%s %s %s", entity_as_string, _relation_type, _value->to_string());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Restriction newEQRestriction(CFMetaData cfm,
|
||||
VariableSpecifications boundNames) throws InvalidRequestException
|
||||
{
|
||||
ColumnDefinition columnDef = toColumnDefinition(cfm, entity);
|
||||
if (mapKey == null)
|
||||
protected:
|
||||
virtual ::shared_ptr<restrictions::restriction> new_EQ_restriction(schema_ptr schema,
|
||||
::shared_ptr<variable_specifications> bound_names) override {
|
||||
throw std::runtime_error("not implemented");
|
||||
#if 0
|
||||
ColumnDefinition columnDef = toColumnDefinition(schema, entity);
|
||||
if (map_key == null)
|
||||
{
|
||||
Term term = toTerm(toReceivers(cfm, columnDef), value, cfm.ksName, boundNames);
|
||||
Term term = toTerm(toReceivers(schema, columnDef), value, schema.ksName, bound_names);
|
||||
return new SingleColumnRestriction.EQ(columnDef, term);
|
||||
}
|
||||
List<? extends ColumnSpecification> receivers = toReceivers(cfm, columnDef);
|
||||
Term entryKey = toTerm(Collections.singletonList(receivers.get(0)), mapKey, cfm.ksName, boundNames);
|
||||
Term entryValue = toTerm(Collections.singletonList(receivers.get(1)), value, cfm.ksName, boundNames);
|
||||
List<? extends ColumnSpecification> receivers = toReceivers(schema, columnDef);
|
||||
Term entryKey = toTerm(Collections.singletonList(receivers.get(0)), map_key, schema.ksName, bound_names);
|
||||
Term entryValue = toTerm(Collections.singletonList(receivers.get(1)), value, schema.ksName, bound_names);
|
||||
return new SingleColumnRestriction.Contains(columnDef, entryKey, entryValue);
|
||||
#endif
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Restriction newINRestriction(CFMetaData cfm,
|
||||
VariableSpecifications boundNames) throws InvalidRequestException
|
||||
{
|
||||
ColumnDefinition columnDef = cfm.getColumnDefinition(getEntity().prepare(cfm));
|
||||
List<? extends ColumnSpecification> receivers = toReceivers(cfm, columnDef);
|
||||
List<Term> terms = toTerms(receivers, inValues, cfm.ksName, boundNames);
|
||||
virtual ::shared_ptr<restrictions::restriction> new_IN_restriction(schema_ptr schema,
|
||||
::shared_ptr<variable_specifications> bound_names) override {
|
||||
throw std::runtime_error("not implemented");
|
||||
#if 0
|
||||
ColumnDefinition columnDef = schema.getColumnDefinition(getEntity().prepare(schema));
|
||||
List<? extends ColumnSpecification> receivers = toReceivers(schema, columnDef);
|
||||
List<Term> terms = toTerms(receivers, in_values, schema.ksName, bound_names);
|
||||
if (terms == null)
|
||||
{
|
||||
Term term = toTerm(receivers, value, cfm.ksName, boundNames);
|
||||
Term term = toTerm(receivers, value, schema.ksName, bound_names);
|
||||
return new SingleColumnRestriction.InWithMarker(columnDef, (Lists.Marker) term);
|
||||
}
|
||||
return new SingleColumnRestriction.InWithValues(columnDef, terms);
|
||||
#endif
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Restriction newSliceRestriction(CFMetaData cfm,
|
||||
VariableSpecifications boundNames,
|
||||
Bound bound,
|
||||
boolean inclusive) throws InvalidRequestException
|
||||
{
|
||||
ColumnDefinition columnDef = toColumnDefinition(cfm, entity);
|
||||
Term term = toTerm(toReceivers(cfm, columnDef), value, cfm.ksName, boundNames);
|
||||
virtual ::shared_ptr<restrictions::restriction> new_slice_restriction(schema_ptr schema,
|
||||
::shared_ptr<variable_specifications> bound_names,
|
||||
statements::bound bound,
|
||||
bool inclusive) override {
|
||||
throw std::runtime_error("not implemented");
|
||||
#if 0
|
||||
ColumnDefinition columnDef = toColumnDefinition(schema, entity);
|
||||
Term term = toTerm(toReceivers(schema, columnDef), value, schema.ksName, bound_names);
|
||||
return new SingleColumnRestriction.Slice(columnDef, bound, inclusive, term);
|
||||
#endif
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Restriction newContainsRestriction(CFMetaData cfm,
|
||||
VariableSpecifications boundNames,
|
||||
boolean isKey) throws InvalidRequestException
|
||||
{
|
||||
ColumnDefinition columnDef = toColumnDefinition(cfm, entity);
|
||||
Term term = toTerm(toReceivers(cfm, columnDef), value, cfm.ksName, boundNames);
|
||||
return new SingleColumnRestriction.Contains(columnDef, term, isKey);
|
||||
virtual shared_ptr<restrictions::restriction> new_contains_restriction(schema_ptr schema,
|
||||
::shared_ptr<variable_specifications> bound_names,
|
||||
bool is_key) override {
|
||||
throw std::runtime_error("not implemented");
|
||||
#if 0
|
||||
ColumnDefinition columnDef = toColumnDefinition(schema, entity);
|
||||
Term term = toTerm(toReceivers(schema, columnDef), value, schema.ksName, bound_names);
|
||||
return new SingleColumnRestriction.Contains(columnDef, term, is_key);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if 0
|
||||
/**
|
||||
* Returns the receivers for this relation.
|
||||
*
|
||||
* @param cfm the Column Family meta data
|
||||
* @param schema the Column Family meta data
|
||||
* @param columnDef the column definition
|
||||
* @return the receivers for the specified relation.
|
||||
* @throws InvalidRequestException if the relation is invalid
|
||||
*/
|
||||
private List<? extends ColumnSpecification> toReceivers(CFMetaData cfm, ColumnDefinition columnDef) throws InvalidRequestException
|
||||
private List<? extends ColumnSpecification> toReceivers(schema_ptr schema, ColumnDefinition columnDef) throws InvalidRequestException
|
||||
{
|
||||
ColumnSpecification receiver = columnDef;
|
||||
|
||||
@@ -204,7 +212,7 @@ public final class SingleColumnRelation extends Relation
|
||||
if (isIN())
|
||||
{
|
||||
// For partition keys we only support IN for the last name so far
|
||||
checkFalse(columnDef.isPartitionKey() && !isLastPartitionKey(cfm, columnDef),
|
||||
checkFalse(columnDef.isPartitionKey() && !isLastPartitionKey(schema, columnDef),
|
||||
"Partition KEY part %s cannot be restricted by IN relation (only the last part of the partition key can)",
|
||||
columnDef.name);
|
||||
|
||||
@@ -229,7 +237,7 @@ public final class SingleColumnRelation extends Relation
|
||||
|
||||
checkFalse(isContainsKey() && !(receiver.type instanceof MapType), "Cannot use CONTAINS KEY on non-map column %s", receiver.name);
|
||||
|
||||
if (mapKey != null)
|
||||
if (map_key != null)
|
||||
{
|
||||
checkFalse(receiver.type instanceof ListType, "Indexes on list entries (%s[index] = value) are not currently supported.", receiver.name);
|
||||
checkTrue(receiver.type instanceof MapType, "Column %s cannot be used as a map", receiver.name);
|
||||
@@ -244,13 +252,13 @@ public final class SingleColumnRelation extends Relation
|
||||
"Collection column '%s' (%s) cannot be restricted by a '%s' relation",
|
||||
receiver.name,
|
||||
receiver.type.asCQL3Type(),
|
||||
operator());
|
||||
get_operator());
|
||||
|
||||
if (isContainsKey() || isContains())
|
||||
{
|
||||
receiver = makeCollectionReceiver(receiver, isContainsKey());
|
||||
}
|
||||
else if (receiver.type.isMultiCell() && mapKey != null && isEQ())
|
||||
else if (receiver.type.isMultiCell() && map_key != null && isEQ())
|
||||
{
|
||||
List<ColumnSpecification> receivers = new ArrayList<>(2);
|
||||
receivers.add(makeCollectionReceiver(receiver, true));
|
||||
@@ -262,36 +270,39 @@ public final class SingleColumnRelation extends Relation
|
||||
return Collections.singletonList(receiver);
|
||||
}
|
||||
|
||||
private ColumnSpecification makeCollectionReceiver(ColumnSpecification receiver, boolean forKey)
|
||||
private ColumnSpecification makeCollectionReceiver(ColumnSpecification receiver, bool forKey)
|
||||
{
|
||||
return ((CollectionType<?>) receiver.type).makeCollectionReceiver(receiver, forKey);
|
||||
}
|
||||
|
||||
private boolean isLegalRelationForNonFrozenCollection()
|
||||
private bool isLegalRelationForNonFrozenCollection()
|
||||
{
|
||||
return isContainsKey() || isContains() || isMapEntryEquality();
|
||||
}
|
||||
|
||||
private boolean isMapEntryEquality()
|
||||
private bool isMapEntryEquality()
|
||||
{
|
||||
return mapKey != null && isEQ();
|
||||
return map_key != null && isEQ();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the specified column is the last column of the partition key.
|
||||
*
|
||||
* @param cfm the column family meta data
|
||||
* @param schema the column family meta data
|
||||
* @param columnDef the column to check
|
||||
* @return <code>true</code> if the specified column is the last column of the partition key, <code>false</code>
|
||||
* otherwise.
|
||||
*/
|
||||
private static boolean isLastPartitionKey(CFMetaData cfm, ColumnDefinition columnDef)
|
||||
private static bool isLastPartitionKey(schema_ptr schema, ColumnDefinition columnDef)
|
||||
{
|
||||
return columnDef.position() == cfm.partitionKeyColumns().size() - 1;
|
||||
return columnDef.position() == schema.partitionKeyColumns().size() - 1;
|
||||
}
|
||||
|
||||
private boolean canHaveOnlyOneValue()
|
||||
private bool canHaveOnlyOneValue()
|
||||
{
|
||||
return isEQ() || (isIN() && inValues != null && inValues.size() == 1);
|
||||
return isEQ() || (isIN() && in_values != null && in_values.size() == 1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user