use of org.apache.cassandra.cql3.ColumnSpecification in project cassandra by apache.
the class ExecuteMessage method execute.
public Message.Response execute(QueryState state, long queryStartNanoTime) {
try {
QueryHandler handler = ClientState.getCQLQueryHandler();
ParsedStatement.Prepared prepared = handler.getPrepared(statementId);
if (prepared == null)
throw new PreparedQueryNotFoundException(statementId);
options.prepare(prepared.boundNames);
CQLStatement statement = prepared.statement;
if (options.getPageSize() == 0)
throw new ProtocolException("The page size cannot be 0");
UUID tracingId = null;
if (isTracingRequested()) {
tracingId = UUIDGen.getTimeUUID();
state.prepareTracingSession(tracingId);
}
if (state.traceNextQuery()) {
state.createTracingSession(getCustomPayload());
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
if (options.getPageSize() > 0)
builder.put("page_size", Integer.toString(options.getPageSize()));
if (options.getConsistency() != null)
builder.put("consistency_level", options.getConsistency().name());
if (options.getSerialConsistency() != null)
builder.put("serial_consistency_level", options.getSerialConsistency().name());
builder.put("query", prepared.rawCQLStatement);
for (int i = 0; i < prepared.boundNames.size(); i++) {
ColumnSpecification cs = prepared.boundNames.get(i);
String boundName = cs.name.toString();
String boundValue = cs.type.asCQL3Type().toCQLLiteral(options.getValues().get(i), options.getProtocolVersion());
if (boundValue.length() > 1000) {
boundValue = boundValue.substring(0, 1000) + "...'";
}
//Here we prefix boundName with the index to avoid possible collission in builder keys due to
//having multiple boundValues for the same variable
builder.put("bound_var_" + Integer.toString(i) + "_" + boundName, boundValue);
}
Tracing.instance.begin("Execute CQL3 prepared query", state.getClientAddress(), builder.build());
}
// Some custom QueryHandlers are interested by the bound names. We provide them this information
// by wrapping the QueryOptions.
QueryOptions queryOptions = QueryOptions.addColumnSpecifications(options, prepared.boundNames);
Message.Response response = handler.processPrepared(statement, state, queryOptions, getCustomPayload(), queryStartNanoTime);
if (options.skipMetadata() && response instanceof ResultMessage.Rows)
((ResultMessage.Rows) response).result.metadata.setSkipMetadata();
if (tracingId != null)
response.setTracingId(tracingId);
return response;
} catch (Exception e) {
JVMStabilityInspector.inspectThrowable(e);
return ErrorMessage.fromException(e);
} finally {
Tracing.instance.stopSession();
}
}
use of org.apache.cassandra.cql3.ColumnSpecification in project cassandra by apache.
the class AbstractFunctionSelector method newFactory.
public static Factory newFactory(final Function fun, final SelectorFactories factories) throws InvalidRequestException {
if (fun.isAggregate()) {
if (factories.doesAggregation())
throw new InvalidRequestException("aggregate functions cannot be used as arguments of aggregate functions");
}
return new Factory() {
protected String getColumnName() {
return fun.columnName(factories.getColumnNames());
}
protected AbstractType<?> getReturnType() {
return fun.returnType();
}
protected void addColumnMapping(SelectionColumnMapping mapping, ColumnSpecification resultsColumn) {
SelectionColumnMapping tmpMapping = SelectionColumnMapping.newMapping();
for (Factory factory : factories) factory.addColumnMapping(tmpMapping, resultsColumn);
if (tmpMapping.getMappings().get(resultsColumn).isEmpty())
// add a null mapping for cases where there are no
// further selectors, such as no-arg functions and count
mapping.addMapping(resultsColumn, (ColumnMetadata) null);
else
// collate the mapped columns from the child factories & add those
mapping.addMapping(resultsColumn, tmpMapping.getMappings().values());
}
public void addFunctionsTo(List<Function> functions) {
fun.addFunctionsTo(functions);
factories.addFunctionsTo(functions);
}
public Selector newInstance(QueryOptions options) throws InvalidRequestException {
return fun.isAggregate() ? new AggregateFunctionSelector(fun, factories.newInstances(options)) : new ScalarFunctionSelector(fun, factories.newInstances(options));
}
public boolean isWritetimeSelectorFactory() {
return factories.containsWritetimeSelectorFactory();
}
public boolean isTTLSelectorFactory() {
return factories.containsTTLSelectorFactory();
}
public boolean isAggregateSelectorFactory() {
return fun.isAggregate() || factories.doesAggregation();
}
};
}
use of org.apache.cassandra.cql3.ColumnSpecification in project cassandra by apache.
the class FunctionResolver method matchAguments.
private static AssignmentTestable.TestResult matchAguments(String keyspace, Function fun, List<? extends AssignmentTestable> providedArgs, String receiverKs, String receiverCf) {
if (providedArgs.size() != fun.argTypes().size())
return AssignmentTestable.TestResult.NOT_ASSIGNABLE;
// It's an exact match if all are exact match, but is not assignable as soon as any is non assignable.
AssignmentTestable.TestResult res = AssignmentTestable.TestResult.EXACT_MATCH;
for (int i = 0; i < providedArgs.size(); i++) {
AssignmentTestable provided = providedArgs.get(i);
if (provided == null) {
res = AssignmentTestable.TestResult.WEAKLY_ASSIGNABLE;
continue;
}
ColumnSpecification expected = makeArgSpec(receiverKs, receiverCf, fun, i);
AssignmentTestable.TestResult argRes = provided.testAssignment(keyspace, expected);
if (argRes == AssignmentTestable.TestResult.NOT_ASSIGNABLE)
return AssignmentTestable.TestResult.NOT_ASSIGNABLE;
if (argRes == AssignmentTestable.TestResult.WEAKLY_ASSIGNABLE)
res = AssignmentTestable.TestResult.WEAKLY_ASSIGNABLE;
}
return res;
}
use of org.apache.cassandra.cql3.ColumnSpecification in project cassandra by apache.
the class SingleColumnRelation method newINRestriction.
@Override
protected Restriction newINRestriction(TableMetadata table, VariableSpecifications boundNames) {
ColumnMetadata columnDef = entity.prepare(table);
List<? extends ColumnSpecification> receivers = toReceivers(columnDef);
List<Term> terms = toTerms(receivers, inValues, table.keyspace, boundNames);
if (terms == null) {
Term term = toTerm(receivers, value, table.keyspace, boundNames);
return new SingleColumnRestriction.InRestrictionWithMarker(columnDef, (Lists.Marker) term);
}
// An IN restrictions with only one element is the same than an EQ restriction
if (terms.size() == 1)
return new SingleColumnRestriction.EQRestriction(columnDef, terms.get(0));
return new SingleColumnRestriction.InRestrictionWithValues(columnDef, terms);
}
use of org.apache.cassandra.cql3.ColumnSpecification in project cassandra by apache.
the class CQLSSTableWriter method rawAddRow.
/**
* Adds a new row to the writer given already serialized values.
* <p>
* This is equivalent to the other rawAddRow methods, but takes a map whose
* keys are the names of the columns to add instead of taking a list of the
* values in the order of the insert statement used during construction of
* this write.
*
* @param values a map of colum name to column values representing the new
* row to add. Note that if a column is not part of the map, it's value will
* be {@code null}. If the map contains keys that does not correspond to one
* of the column of the insert statement used when creating this writer, the
* the corresponding value is ignored.
* @return this writer.
*/
public CQLSSTableWriter rawAddRow(Map<String, ByteBuffer> values) throws InvalidRequestException, IOException {
int size = Math.min(values.size(), boundNames.size());
List<ByteBuffer> rawValues = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
ColumnSpecification spec = boundNames.get(i);
rawValues.add(values.get(spec.name.toString()));
}
return rawAddRow(rawValues);
}
Aggregations