org.apache.cassandra.cql3.statements.SelectStatement Java Examples

The following examples show how to use org.apache.cassandra.cql3.statements.SelectStatement. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Cqlsh.java    From sstable-tools with Apache License 2.0 6 votes vote down vote up
public Query getQuery(String command) throws Exception {
    SelectStatement.RawStatement statement = (SelectStatement.RawStatement) QueryProcessor.parseStatement(command);
    if (statement.columnFamily().matches("sstables?")) {
        if (sstables.isEmpty()) {
            return null;
        }
        metadata = CassandraUtils.tableFromBestSource(sstables.iterator().next());
        return new Query(command, sstables, metadata);
    } else {
        File path = new File(statement.columnFamily());
        if (!path.exists()) {
            throw new FileNotFoundException(path.getAbsolutePath());
        }
        metadata = CassandraUtils.tableFromBestSource(path);
        return new Query(command, Collections.singleton(path), metadata);
    }
}
 
Example #2
Source File: SSTableAttachedSecondaryIndexTest.java    From sasi with Apache License 2.0 6 votes vote down vote up
private Set<String> executeCQL(String rawStatement) throws Exception
{
    SelectStatement statement = (SelectStatement) QueryProcessor.parseStatement(rawStatement).prepare().statement;
    ResultMessage.Rows cqlRows = statement.executeInternal(QueryState.forInternalCalls(), new QueryOptions(ConsistencyLevel.LOCAL_ONE, Collections.<ByteBuffer>emptyList()));

    Set<String> results = new TreeSet<>();
    for (CqlRow row : cqlRows.toThriftResult().getRows())
    {
        for (org.apache.cassandra.thrift.Column col : row.columns)
        {
            String columnName = UTF8Type.instance.getString(col.bufferForName());
            if (columnName.equals("key"))
                results.add(AsciiType.instance.getString(col.bufferForValue()));
        }
    }

    return results;
}
 
Example #3
Source File: SSTableAttachedSecondaryIndexTest.java    From sasi with Apache License 2.0 5 votes vote down vote up
private static IndexExpression[] getExpressions(String cqlQuery) throws Exception
{
    ParsedStatement parsedStatement = QueryProcessor.parseStatement(String.format(cqlQuery, KS_NAME, CF_NAME));
    SelectStatement selectStatement = (SelectStatement) parsedStatement.prepare().statement;

    List<IndexExpression> expressions = selectStatement.getIndexExpressions(Collections.<ByteBuffer>emptyList());
    return expressions.toArray(new IndexExpression[expressions.size()]);
}
 
Example #4
Source File: UntypedResultSet.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private FromPager(SelectStatement select, QueryPager pager, int pageSize)
{
    this.select = select;
    this.pager = pager;
    this.pageSize = pageSize;
    this.metadata = select.getResultMetadata().names;
}
 
Example #5
Source File: Auth.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up Authenticator and Authorizer.
 */
public static void setup()
{
    if (DatabaseDescriptor.getAuthenticator() instanceof AllowAllAuthenticator)
        return;

    setupAuthKeyspace();
    setupTable(USERS_CF, USERS_CF_SCHEMA);

    DatabaseDescriptor.getAuthenticator().setup();
    DatabaseDescriptor.getAuthorizer().setup();

    // register a custom MigrationListener for permissions cleanup after dropped keyspaces/cfs.
    MigrationManager.instance.register(new AuthMigrationListener());

    // the delay is here to give the node some time to see its peers - to reduce
    // "Skipped default superuser setup: some nodes were not ready" log spam.
    // It's the only reason for the delay.
    ScheduledExecutors.nonPeriodicTasks.schedule(new Runnable()
    {
        public void run()
        {
            setupDefaultSuperuser();
        }
    }, SUPERUSER_SETUP_DELAY, TimeUnit.MILLISECONDS);

    try
    {
        String query = String.format("SELECT * FROM %s.%s WHERE name = ?", AUTH_KS, USERS_CF);
        selectUserStatement = (SelectStatement) QueryProcessor.parseStatement(query).prepare().statement;
    }
    catch (RequestValidationException e)
    {
        throw new AssertionError(e); // not supposed to happen
    }
}
 
Example #6
Source File: PasswordAuthenticator.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public void setup()
{
    Auth.setupTable(CREDENTIALS_CF, CREDENTIALS_CF_SCHEMA);

    // the delay is here to give the node some time to see its peers - to reduce
    // "skipped default user setup: some nodes are were not ready" log spam.
    // It's the only reason for the delay.
    ScheduledExecutors.nonPeriodicTasks.schedule(new Runnable()
    {
        public void run()
        {
          setupDefaultUser();
        }
    }, Auth.SUPERUSER_SETUP_DELAY, TimeUnit.MILLISECONDS);

    try
    {
        String query = String.format("SELECT %s FROM %s.%s WHERE username = ?",
                                     SALTED_HASH,
                                     Auth.AUTH_KS,
                                     CREDENTIALS_CF);
        authenticateStatement = (SelectStatement) QueryProcessor.parseStatement(query).prepare().statement;
    }
    catch (RequestValidationException e)
    {
        throw new AssertionError(e); // not supposed to happen
    }
}
 
Example #7
Source File: CassandraAuthorizer.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public void setup()
{
    Auth.setupTable(PERMISSIONS_CF, PERMISSIONS_CF_SCHEMA);

    try
    {
        String query = String.format("SELECT permissions FROM %s.%s WHERE username = ? AND resource = ?", Auth.AUTH_KS, PERMISSIONS_CF);
        authorizeStatement = (SelectStatement) QueryProcessor.parseStatement(query).prepare().statement;
    }
    catch (RequestValidationException e)
    {
        throw new AssertionError(e); // not supposed to happen
    }
}
 
Example #8
Source File: ResultMessage.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static ResultSet.Metadata extractResultMetadata(CQLStatement statement)
{
    if (!(statement instanceof SelectStatement))
        return ResultSet.Metadata.EMPTY;

    return ((SelectStatement)statement).getResultMetadata();
}
 
Example #9
Source File: Query.java    From sstable-tools with Apache License 2.0 4 votes vote down vote up
public Query(String query, Collection<File> path, CFMetaData cfm) throws IllegalAccessException, NoSuchFieldException, IOException {
    SelectStatement.RawStatement statement = (SelectStatement.RawStatement) QueryProcessor.parseStatement(query);
    if (!statement.parameters.orderings.isEmpty()) {
        throw new UnsupportedOperationException("ORDER BY not supported");
    }
    if (statement.parameters.isDistinct) {
        throw new UnsupportedOperationException("DISTINCT not supported");
    }
    this.path = path;

    VariableSpecifications boundNames = statement.getBoundVariables();

    Selection selection = statement.selectClause.isEmpty()
            ? Selection.wildcard(cfm)
            : Selection.fromSelectors(cfm, statement.selectClause, VariableSpecifications.empty(), !statement.parameters.groups.isEmpty());

    // yes its unfortunate, im sorry
    StatementType type = mock(StatementType.class);
    when(type.allowClusteringColumnSlices()).thenReturn(true);
    when(type.allowNonPrimaryKeyInWhereClause()).thenReturn(true);
    when(type.allowPartitionKeyRanges()).thenReturn(true);
    when(type.allowUseOfSecondaryIndices()).thenReturn(false);

    this.restrictions = new StatementRestrictions(
            type,
            cfm,
            statement.whereClause,
            boundNames,
            selection.containsOnlyStaticColumns(),
            selection.containsAComplexColumn(),
            true, true);

    this.statement = statement;
    this.cfm = cfm;
    this.selection = selection;

    ColumnFilter filter;
    if (selection.isWildcard()) {
        filter = ColumnFilter.all(cfm);
    } else {
        ColumnFilter.Builder builder = ColumnFilter.selectionBuilder();
        selection.getColumns().stream().filter(def -> !def.isPrimaryKeyColumn()).forEach(builder::add);
        filter = builder.build();
    }
    this.queriedColumns = filter;

    try {
        // Make getAggregationSpecification public and invoke it..
        Method getAggregationSpecification = SelectStatement.RawStatement.class.getDeclaredMethod("getAggregationSpecification", CFMetaData.class, Selection.class, StatementRestrictions.class, boolean.class);
        getAggregationSpecification.setAccessible(true);
        this.aggregationSpec = (AggregationSpecification) getAggregationSpecification.invoke(statement, cfm, selection, restrictions, statement.parameters.isDistinct);
    } catch (Exception e) {
        logger.error("Unable to get aggregationSpecification", e);
        this.aggregationSpec = null;
    }
}
 
Example #10
Source File: UntypedResultSet.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public static UntypedResultSet create(SelectStatement select, QueryPager pager, int pageSize)
{
    return new FromPager(select, pager, pageSize);
}