Search in sources :

Example 6 with org.apache.cassandra.cql3.functions

use of org.apache.cassandra.cql3.functions in project jOOQ by jOOQ.

the class FirebirdDatabase method getRoutines0.

@Override
protected List<RoutineDefinition> getRoutines0() throws SQLException {
    Rdb$procedures p = RDB$PROCEDURES.as("p");
    Rdb$functions fu = RDB$FUNCTIONS.as("fu");
    Rdb$functionArguments fa = RDB$FUNCTION_ARGUMENTS.as("fa");
    Rdb$fields fi = RDB$FIELDS.as("fi");
    return create().select(p.RDB$PROCEDURE_NAME.trim(), inline(null, VARCHAR).as("t"), inline(null, SMALLINT).as("p"), inline(null, SMALLINT).as("s")).from(p).where(p.RDB$PROCEDURE_TYPE.eq((short) 2)).union(is30() ? select(fu.RDB$FUNCTION_NAME.trim(), FIELD_TYPE(fi).as("t"), coalesce(CHARACTER_LENGTH(fi), fi.RDB$FIELD_PRECISION).as("p"), FIELD_SCALE(fi).as("s")).from(fu).leftAntiJoin(p).on(fu.RDB$FUNCTION_NAME.eq(p.RDB$PROCEDURE_NAME)).join(fa).on(fu.RDB$FUNCTION_NAME.eq(fa.RDB$FUNCTION_NAME)).leftOuterJoin(fi).on(fa.RDB$FIELD_SOURCE.eq(fi.RDB$FIELD_NAME)).where(fa.RDB$ARGUMENT_POSITION.eq(inline((short) 0))) : select(inline(""), inline(""), inline((short) 0), inline((short) 0)).where(falseCondition())).orderBy(1).collect(mapping(r -> new FirebirdRoutineDefinition(getSchemata().get(0), r.get(0, String.class), r.get(1, String.class), r.get(2, Integer.class), r.get(3, Integer.class)), Collectors.<RoutineDefinition>toList()));
}
Also used : Arrays(java.util.Arrays) DSL(org.jooq.impl.DSL) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) FIELD_SCALE(org.jooq.meta.firebird.FirebirdDatabase.FIELD_SCALE) DefaultDomainDefinition(org.jooq.meta.DefaultDomainDefinition) Rdb$functions(org.jooq.meta.firebird.rdb.tables.Rdb$functions) DefaultSequenceDefinition(org.jooq.meta.DefaultSequenceDefinition) DSL.when(org.jooq.impl.DSL.when) DataTypeDefinition(org.jooq.meta.DataTypeDefinition) Record4(org.jooq.Record4) BigDecimal(java.math.BigDecimal) RoutineDefinition(org.jooq.meta.RoutineDefinition) PackageDefinition(org.jooq.meta.PackageDefinition) RDB$FUNCTIONS(org.jooq.meta.firebird.rdb.Tables.RDB$FUNCTIONS) RDB$PROCEDURES(org.jooq.meta.firebird.rdb.Tables.RDB$PROCEDURES) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) DSL.trim(org.jooq.impl.DSL.trim) Rdb$indices(org.jooq.meta.firebird.rdb.tables.Rdb$indices) Arrays.asList(java.util.Arrays.asList) RDB$REF_CONSTRAINTS(org.jooq.meta.firebird.rdb.Tables.RDB$REF_CONSTRAINTS) Map(java.util.Map) ResultQuery(org.jooq.ResultQuery) DSLContext(org.jooq.DSLContext) DSL.coalesce(org.jooq.impl.DSL.coalesce) SQLDialect(org.jooq.SQLDialect) SortOrder(org.jooq.SortOrder) Record6(org.jooq.Record6) SMALLINT(org.jooq.impl.SQLDataType.SMALLINT) RDB$TRIGGERS(org.jooq.meta.firebird.rdb.Tables.RDB$TRIGGERS) Rdb$checkConstraints(org.jooq.meta.firebird.rdb.tables.Rdb$checkConstraints) DSL.falseCondition(org.jooq.impl.DSL.falseCondition) Field(org.jooq.Field) DSL.select(org.jooq.impl.DSL.select) Result(org.jooq.Result) DSL.choose(org.jooq.impl.DSL.choose) Collectors(java.util.stream.Collectors) TableType(org.jooq.TableOptions.TableType) BOOLEAN(org.jooq.impl.SQLDataType.BOOLEAN) DomainDefinition(org.jooq.meta.DomainDefinition) List(java.util.List) INTEGER(org.jooq.impl.SQLDataType.INTEGER) SQLDataType(org.jooq.impl.SQLDataType) Rdb$triggers(org.jooq.meta.firebird.rdb.tables.Rdb$triggers) DSL.noCondition(org.jooq.impl.DSL.noCondition) Entry(java.util.Map.Entry) SchemaMappingType(org.jooq.meta.jaxb.SchemaMappingType) FirebirdDataType(org.jooq.util.firebird.FirebirdDataType) DefaultCheckConstraintDefinition(org.jooq.meta.DefaultCheckConstraintDefinition) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) DefaultRelations(org.jooq.meta.DefaultRelations) RDB$INDEX_SEGMENTS(org.jooq.meta.firebird.rdb.Tables.RDB$INDEX_SEGMENTS) SequenceDefinition(org.jooq.meta.SequenceDefinition) RDB$RELATION_CONSTRAINTS(org.jooq.meta.firebird.rdb.Tables.RDB$RELATION_CONSTRAINTS) Rdb$procedures(org.jooq.meta.firebird.rdb.tables.Rdb$procedures) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) Rdb$refConstraints(org.jooq.meta.firebird.rdb.tables.Rdb$refConstraints) DSL.decode(org.jooq.impl.DSL.decode) DSL.nullif(org.jooq.impl.DSL.nullif) RDB$RELATION_FIELDS(org.jooq.meta.firebird.rdb.Tables.RDB$RELATION_FIELDS) TableDefinition(org.jooq.meta.TableDefinition) IndexDefinition(org.jooq.meta.IndexDefinition) RDB$CHECK_CONSTRAINTS(org.jooq.meta.firebird.rdb.Tables.RDB$CHECK_CONSTRAINTS) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) BIGINT(org.jooq.impl.SQLDataType.BIGINT) NUMERIC(org.jooq.impl.SQLDataType.NUMERIC) Collectors.mapping(java.util.stream.Collectors.mapping) VARCHAR(org.jooq.impl.SQLDataType.VARCHAR) ArrayDefinition(org.jooq.meta.ArrayDefinition) UDTDefinition(org.jooq.meta.UDTDefinition) DSL.inline(org.jooq.impl.DSL.inline) Record(org.jooq.Record) CHARACTER_LENGTH(org.jooq.meta.firebird.FirebirdDatabase.CHARACTER_LENGTH) RDB$RELATIONS(org.jooq.meta.firebird.rdb.Tables.RDB$RELATIONS) Rdb$indexSegments(org.jooq.meta.firebird.rdb.tables.Rdb$indexSegments) Rdb$relationConstraints(org.jooq.meta.firebird.rdb.tables.Rdb$relationConstraints) FIELD_TYPE(org.jooq.meta.firebird.FirebirdDatabase.FIELD_TYPE) Rdb$functionArguments(org.jooq.meta.firebird.rdb.tables.Rdb$functionArguments) RDB$FIELDS(org.jooq.meta.firebird.rdb.Tables.RDB$FIELDS) FIREBIRD(org.jooq.SQLDialect.FIREBIRD) StringUtils(org.jooq.tools.StringUtils) Rdb$fields(org.jooq.meta.firebird.rdb.tables.Rdb$fields) Record12(org.jooq.Record12) SchemaDefinition(org.jooq.meta.SchemaDefinition) DSL.max(org.jooq.impl.DSL.max) Tables(org.jooq.meta.firebird.rdb.Tables) DSL.any(org.jooq.impl.DSL.any) EnumDefinition(org.jooq.meta.EnumDefinition) RDB$INDICES(org.jooq.meta.firebird.rdb.Tables.RDB$INDICES) Collections(java.util.Collections) ResultQueryDatabase(org.jooq.meta.ResultQueryDatabase) AbstractDatabase(org.jooq.meta.AbstractDatabase) CatalogDefinition(org.jooq.meta.CatalogDefinition) RoutineDefinition(org.jooq.meta.RoutineDefinition) Rdb$functionArguments(org.jooq.meta.firebird.rdb.tables.Rdb$functionArguments) Rdb$fields(org.jooq.meta.firebird.rdb.tables.Rdb$fields) Rdb$functions(org.jooq.meta.firebird.rdb.tables.Rdb$functions) Rdb$procedures(org.jooq.meta.firebird.rdb.tables.Rdb$procedures)

Example 7 with org.apache.cassandra.cql3.functions

use of org.apache.cassandra.cql3.functions in project cassandra by apache.

the class BytesConversionFcts method all.

public static Collection<Function> all() {
    Collection<Function> functions = new ArrayList<>();
    // for varchar, so we special case it below. We also skip blob for obvious reasons.
    for (CQL3Type type : CQL3Type.Native.values()) {
        if (type != CQL3Type.Native.VARCHAR && type != CQL3Type.Native.BLOB) {
            functions.add(makeToBlobFunction(type.getType()));
            functions.add(makeFromBlobFunction(type.getType()));
        }
    }
    functions.add(VarcharAsBlobFct);
    functions.add(BlobAsVarcharFct);
    return functions;
}
Also used : CQL3Type(org.apache.cassandra.cql3.CQL3Type) ArrayList(java.util.ArrayList)

Example 8 with org.apache.cassandra.cql3.functions

use of org.apache.cassandra.cql3.functions in project cassandra by apache.

the class DropTypeStatement method apply.

// TODO: expand types into tuples in all dropped columns of all tables
public Keyspaces apply(Keyspaces schema) {
    ByteBuffer name = bytes(typeName);
    KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);
    UserType type = null == keyspace ? null : keyspace.types.getNullable(name);
    if (null == type) {
        if (ifExists)
            return schema;
        throw ire("Type '%s.%s' doesn't exist", keyspaceName, typeName);
    }
    /*
         * We don't want to drop a type unless it's not used anymore (mainly because
         * if someone drops a type and recreates one with the same name but different
         * definition with the previous name still in use, things can get messy).
         * We have three places to check:
         * 1) UDFs and UDAs using the type
         * 2) other user type that can nest the one we drop and
         * 3) existing tables referencing the type (maybe in a nested way).
         */
    Iterable<Function> functions = keyspace.functions.referencingUserType(name);
    if (!isEmpty(functions)) {
        throw ire("Cannot drop user type '%s.%s' as it is still used by functions %s", keyspaceName, typeName, join(", ", transform(functions, f -> f.name().toString())));
    }
    Iterable<UserType> types = keyspace.types.referencingUserType(name);
    if (!isEmpty(types)) {
        throw ire("Cannot drop user type '%s.%s' as it is still used by user types %s", keyspaceName, typeName, join(", ", transform(types, UserType::getNameAsString)));
    }
    Iterable<TableMetadata> tables = keyspace.tables.referencingUserType(name);
    if (!isEmpty(tables)) {
        throw ire("Cannot drop user type '%s.%s' as it is still used by tables %s", keyspaceName, typeName, join(", ", transform(tables, t -> t.name)));
    }
    return schema.withAddedOrUpdated(keyspace.withSwapped(keyspace.types.without(type)));
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) Function(org.apache.cassandra.cql3.functions.Function) ByteBuffer(java.nio.ByteBuffer) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) UserType(org.apache.cassandra.db.marshal.UserType)

Example 9 with org.apache.cassandra.cql3.functions

use of org.apache.cassandra.cql3.functions 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();
        }

        @Override
        public boolean areAllFetchedColumnsKnown() {
            return Iterables.all(factories, f -> f.areAllFetchedColumnsKnown());
        }

        @Override
        public void addFetchedColumns(ColumnFilter.Builder builder) {
            for (Selector.Factory factory : factories) factory.addFetchedColumns(builder);
        }
    };
}
Also used : ColumnSpecification(org.apache.cassandra.cql3.ColumnSpecification) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) StrBuilder(org.apache.commons.lang3.text.StrBuilder) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) List(java.util.List) QueryOptions(org.apache.cassandra.cql3.QueryOptions)

Example 10 with org.apache.cassandra.cql3.functions

use of org.apache.cassandra.cql3.functions in project cassandra by apache.

the class UFAuthTest method testBatchStatement.

@Test
public void testBatchStatement() throws Throwable {
    List<ModificationStatement> statements = new ArrayList<>();
    List<String> functions = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        String functionName = createSimpleFunction();
        ModificationStatement stmt = (ModificationStatement) getStatement(String.format("INSERT INTO %s (k, v1, v2) " + "VALUES (%s, %s, %s)", KEYSPACE + "." + currentTable(), i, i, functionCall(functionName)));
        functions.add(functionName);
        statements.add(stmt);
    }
    BatchStatement batch = new BatchStatement(BatchStatement.Type.LOGGED, VariableSpecifications.empty(), statements, Attributes.none());
    assertUnauthorized(batch, functions);
    grantExecuteOnFunction(functions.get(0));
    assertUnauthorized(batch, functions.subList(1, functions.size()));
    grantExecuteOnFunction(functions.get(1));
    assertUnauthorized(batch, functions.subList(2, functions.size()));
    grantExecuteOnFunction(functions.get(2));
    batch.authorize(clientState);
}
Also used : ModificationStatement(org.apache.cassandra.cql3.statements.ModificationStatement) BatchStatement(org.apache.cassandra.cql3.statements.BatchStatement) Test(org.junit.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)4 java.util (java.util)3 ArrayList (java.util.ArrayList)3 Function (org.apache.cassandra.cql3.functions.Function)3 org.apache.cassandra.db.marshal (org.apache.cassandra.db.marshal)3 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)3 ProtocolVersion (org.apache.cassandra.transport.ProtocolVersion)3 Maps (com.google.common.collect.Maps)2 String.format (java.lang.String.format)2 CharacterCodingException (java.nio.charset.CharacterCodingException)2 List (java.util.List)2 TimeUnit (java.util.concurrent.TimeUnit)2 Collectors (java.util.stream.Collectors)2 Collectors.toList (java.util.stream.Collectors.toList)2 Collectors.toSet (java.util.stream.Collectors.toSet)2 org.apache.cassandra.config (org.apache.cassandra.config)2 org.apache.cassandra.cql3 (org.apache.cassandra.cql3)2 QueryProcessor.executeInternal (org.apache.cassandra.cql3.QueryProcessor.executeInternal)2 QueryProcessor.executeOnceInternal (org.apache.cassandra.cql3.QueryProcessor.executeOnceInternal)2 org.apache.cassandra.cql3.functions (org.apache.cassandra.cql3.functions)2