Java Code Examples for org.apache.cassandra.cql3.QueryProcessor#parseStatement()

The following examples show how to use org.apache.cassandra.cql3.QueryProcessor#parseStatement() . 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: CFMetaData.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
public static CFMetaData compile(String cql, String keyspace)
{
    try
    {
        CFStatement parsed = (CFStatement)QueryProcessor.parseStatement(cql);
        parsed.prepareKeyspace(keyspace);
        CreateTableStatement statement = (CreateTableStatement) parsed.prepare().statement;
        CFMetaData cfm = newSystemMetadata(keyspace, statement.columnFamily(), "", statement.comparator);
        statement.applyPropertiesTo(cfm);
        return cfm.rebuild();
    }
    catch (RequestValidationException e)
    {
        throw new RuntimeException(e);
    }
}
 
Example 3
Source File: CassandraUtils.java    From sstable-tools with Apache License 2.0 5 votes vote down vote up
public static CFMetaData tableFromCQL(InputStream source, UUID cfid) throws IOException {
    String schema = CharStreams.toString(new InputStreamReader(source, "UTF-8"));
    logger.trace("Loading Schema" + schema);
    CFStatement statement = (CFStatement) QueryProcessor.parseStatement(schema);
    String keyspace = "";
    try {
        keyspace = statement.keyspace() == null ? "turtles" : statement.keyspace();
    } catch (AssertionError e) { // if -ea added we should provide lots of warnings that things probably wont work
        logger.warn("Remove '-ea' JVM option when using sstable-tools library");
        keyspace = "turtles";
    }
    statement.prepareKeyspace(keyspace);
    if(Schema.instance.getKSMetaData(keyspace) == null) {
        Schema.instance.setKeyspaceMetadata(KeyspaceMetadata.create(keyspace, KeyspaceParams.local(), Tables.none(),
                Views.none(), getTypes(), Functions.none()));
    }
    CFMetaData cfm;
    if(cfid != null) {
        cfm = ((CreateTableStatement) statement.prepare().statement).metadataBuilder().withId(cfid).build();
        KeyspaceMetadata prev = Schema.instance.getKSMetaData(keyspace);
        List<CFMetaData> tables = Lists.newArrayList(prev.tablesAndViews());
        tables.add(cfm);
        Schema.instance.setKeyspaceMetadata(prev.withSwapped(Tables.of(tables)));
        Schema.instance.load(cfm);
    } else {
        cfm = ((CreateTableStatement) statement.prepare().statement).getCFMetaData();
    }
    return cfm;
}
 
Example 4
Source File: Cqlsh.java    From sstable-tools with Apache License 2.0 5 votes vote down vote up
public void doSchema(String command) throws Exception {
    String path = command.substring(6).trim().replaceAll("\"", "");

    if (path.equalsIgnoreCase("off")) {
        System.out.println(DISABLING_SCHEMA);
        CassandraUtils.cqlOverride = null;
    } else if (Strings.isNullOrEmpty(path)) {
        if (!Strings.isNullOrEmpty(CassandraUtils.cqlOverride)) {
            System.out.printf(USER_DEFINED_SCHEMA, CassandraUtils.cqlOverride);
        } else {
            System.out.println(NO_USER_DEFINED_SCHEMA);
        }
    } else {
        File schemaFile = new File(path);
        if (!schemaFile.exists()) {
            System.err.printf(CANNOT_FIND_FILE, schemaFile.getAbsolutePath());
        } else {
            String cql = new String(Files.readAllBytes(schemaFile.toPath()));
            try {
                ParsedStatement statement = QueryProcessor.parseStatement(cql);
                if (statement instanceof CreateTableStatement.RawStatement) {
                    CassandraUtils.cqlOverride = cql;
                    System.out.printf(IMPORTED_SCHEMA, schemaFile.getAbsolutePath());
                } else {
                    System.err.printf(FAILED_TO_IMPORT_SCHEMA, schemaFile.getAbsoluteFile(), "Wrong type of statement, " + statement.getClass());
                }
            } catch (SyntaxException se) {
                System.err.printf(FAILED_TO_IMPORT_SCHEMA, schemaFile.getAbsoluteFile(), se.getMessage());
            }
        }
    }

}
 
Example 5
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 6
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;
    }
}