use of org.apache.cassandra.exceptions.InvalidRequestException in project cassandra by apache.
the class PermissionsManagementStatement method validate.
public void validate(ClientState state) throws RequestValidationException {
// validate login here before checkAccess to avoid leaking user existence to anonymous users.
state.ensureNotAnonymous();
if (!DatabaseDescriptor.getRoleManager().isExistingRole(grantee))
throw new InvalidRequestException(String.format("Role %s doesn't exist", grantee.getRoleName()));
// if a keyspace is omitted when GRANT/REVOKE ON TABLE <table>, we need to correct the resource.
// called both here and in checkAccess(), as in some cases we do not call the latter.
resource = maybeCorrectResource(resource, state);
// altering permissions on builtin functions is not supported
if (resource instanceof FunctionResource && SchemaConstants.SYSTEM_KEYSPACE_NAME.equals(((FunctionResource) resource).getKeyspace())) {
throw new InvalidRequestException("Altering permissions on builtin functions is not supported");
}
if (!resource.exists())
throw new InvalidRequestException(String.format("Resource %s doesn't exist", resource));
}
use of org.apache.cassandra.exceptions.InvalidRequestException in project cassandra by apache.
the class FunctionResolver method get.
/**
* @param keyspace the current keyspace
* @param name the name of the function
* @param providedArgs the arguments provided for the function call
* @param receiverKs the receiver's keyspace
* @param receiverCf the receiver's table
* @param receiverType if the receiver type is known (during inserts, for example), this should be the type of
* the receiver
* @throws InvalidRequestException
*/
public static Function get(String keyspace, FunctionName name, List<? extends AssignmentTestable> providedArgs, String receiverKs, String receiverCf, AbstractType<?> receiverType) throws InvalidRequestException {
if (name.equalsNativeFunction(TOKEN_FUNCTION_NAME))
return new TokenFct(Schema.instance.getTableMetadata(receiverKs, receiverCf));
// due to needing to know the argument types in advance).
if (name.equalsNativeFunction(ToJsonFct.NAME))
throw new InvalidRequestException("toJson() may only be used within the selection clause of SELECT statements");
// Similarly, we can only use fromJson when we know the receiver type (such as inserts)
if (name.equalsNativeFunction(FromJsonFct.NAME)) {
if (receiverType == null)
throw new InvalidRequestException("fromJson() cannot be used in the selection clause of a SELECT statement");
return FromJsonFct.getInstance(receiverType);
}
Collection<Function> candidates;
if (!name.hasKeyspace()) {
// function name not fully qualified
candidates = new ArrayList<>();
// add 'SYSTEM' (native) candidates
candidates.addAll(Schema.instance.getFunctions(name.asNativeFunction()));
// add 'current keyspace' candidates
candidates.addAll(Schema.instance.getFunctions(new FunctionName(keyspace, name.name)));
} else {
// function name is fully qualified (keyspace + name)
candidates = Schema.instance.getFunctions(name);
}
if (candidates.isEmpty())
return null;
// Fast path if there is only one choice
if (candidates.size() == 1) {
Function fun = candidates.iterator().next();
validateTypes(keyspace, fun, providedArgs, receiverKs, receiverCf);
return fun;
}
List<Function> compatibles = null;
for (Function toTest : candidates) {
if (matchReturnType(toTest, receiverType)) {
AssignmentTestable.TestResult r = matchAguments(keyspace, toTest, providedArgs, receiverKs, receiverCf);
switch(r) {
case EXACT_MATCH:
// We always favor exact matches
return toTest;
case WEAKLY_ASSIGNABLE:
if (compatibles == null)
compatibles = new ArrayList<>();
compatibles.add(toTest);
break;
}
}
}
if (compatibles == null) {
if (OperationFcts.isOperation(name))
throw invalidRequest("the '%s' operation is not supported between %s and %s", OperationFcts.getOperator(name), providedArgs.get(0), providedArgs.get(1));
throw invalidRequest("Invalid call to function %s, none of its type signatures match (known type signatures: %s)", name, format(candidates));
}
if (compatibles.size() > 1) {
if (OperationFcts.isOperation(name)) {
if (receiverType != null && !containsMarkers(providedArgs)) {
for (Function toTest : compatibles) {
List<AbstractType<?>> argTypes = toTest.argTypes();
if (receiverType.equals(argTypes.get(0)) && receiverType.equals(argTypes.get(1)))
return toTest;
}
}
throw invalidRequest("Ambiguous '%s' operation: use type casts to disambiguate", OperationFcts.getOperator(name), providedArgs.get(0), providedArgs.get(1));
}
if (OperationFcts.isNegation(name))
throw invalidRequest("Ambiguous negation: use type casts to disambiguate");
throw invalidRequest("Ambiguous call to function %s (can be matched by following signatures: %s): use type casts to disambiguate", name, format(compatibles));
}
return compatibles.get(0);
}
use of org.apache.cassandra.exceptions.InvalidRequestException in project cassandra by apache.
the class UserTypes method validateUserTypeAssignableTo.
public static <T extends AssignmentTestable> void validateUserTypeAssignableTo(ColumnSpecification receiver, Map<FieldIdentifier, T> entries) {
if (!receiver.type.isUDT())
throw new InvalidRequestException(String.format("Invalid user type literal for %s of type %s", receiver, receiver.type.asCQL3Type()));
UserType ut = (UserType) receiver.type;
for (int i = 0; i < ut.size(); i++) {
FieldIdentifier field = ut.fieldName(i);
T value = entries.get(field);
if (value == null)
continue;
ColumnSpecification fieldSpec = fieldSpecOf(receiver, i);
if (!value.testAssignment(receiver.ksName, fieldSpec).isAssignable()) {
throw new InvalidRequestException(String.format("Invalid user type literal for %s: field %s is not of type %s", receiver, field, fieldSpec.type.asCQL3Type()));
}
}
}
use of org.apache.cassandra.exceptions.InvalidRequestException in project cassandra by apache.
the class CQL3CasRequest method addExistsCondition.
private void addExistsCondition(Clustering clustering, RowCondition condition, boolean isNotExist) {
assert condition instanceof ExistCondition || condition instanceof NotExistCondition;
RowCondition previous = getConditionsForRow(clustering);
if (previous != null) {
if (previous.getClass().equals(condition.getClass())) {
// We shouldn't have a previous condition unless hasExists has been set already.
assert hasExists;
return;
} else {
// these should be prevented by the parser, but it doesn't hurt to check
throw (previous instanceof NotExistCondition || previous instanceof ExistCondition) ? new InvalidRequestException("Cannot mix IF EXISTS and IF NOT EXISTS conditions for the same row") : new InvalidRequestException("Cannot mix IF conditions and IF " + (isNotExist ? "NOT " : "") + "EXISTS for the same row");
}
}
setConditionsForRow(clustering, condition);
hasExists = true;
}
use of org.apache.cassandra.exceptions.InvalidRequestException in project cassandra by apache.
the class CreateViewStatement method getColumnIdentifier.
private static boolean getColumnIdentifier(TableMetadata cfm, Set<ColumnIdentifier> basePK, boolean hasNonPKColumn, ColumnMetadata.Raw raw, List<ColumnIdentifier> columns, StatementRestrictions restrictions) {
ColumnMetadata def = raw.prepare(cfm);
boolean isPk = basePK.contains(def.name);
if (!isPk && hasNonPKColumn)
throw new InvalidRequestException(String.format("Cannot include more than one non-primary key column '%s' in materialized view primary key", def.name));
// We don't need to include the "IS NOT NULL" filter on a non-composite partition key
// because we will never allow a single partition key to be NULL
boolean isSinglePartitionKey = def.isPartitionKey() && cfm.partitionKeyColumns().size() == 1;
if (!isSinglePartitionKey && !restrictions.isRestricted(def))
throw new InvalidRequestException(String.format("Primary key column '%s' is required to be filtered by 'IS NOT NULL'", def.name));
columns.add(def.name);
return !isPk;
}
Aggregations