Search in sources :

Example 1 with FunctionSignature

use of com.datastax.oss.driver.api.core.metadata.schema.FunctionSignature in project java-driver by datastax.

the class AggregateParserTest method should_parse_modern_table.

@Test
public void should_parse_modern_table() {
    AggregateParser parser = new AggregateParser(new DataTypeCqlNameParser(), context);
    AggregateMetadata aggregate = parser.parseAggregate(SUM_AND_TO_STRING_ROW_3_0, KEYSPACE_ID, Collections.emptyMap());
    assertThat(aggregate.getKeyspace().asInternal()).isEqualTo("ks");
    assertThat(aggregate.getSignature().getName().asInternal()).isEqualTo("sum_and_to_string");
    assertThat(aggregate.getSignature().getParameterTypes()).containsExactly(DataTypes.INT);
    FunctionSignature stateFuncSignature = aggregate.getStateFuncSignature();
    assertThat(stateFuncSignature.getName().asInternal()).isEqualTo("plus");
    assertThat(stateFuncSignature.getParameterTypes()).containsExactly(DataTypes.INT, DataTypes.INT);
    assertThat(aggregate.getStateType()).isEqualTo(DataTypes.INT);
    Optional<FunctionSignature> finalFuncSignature = aggregate.getFinalFuncSignature();
    assertThat(finalFuncSignature).isPresent();
    assertThat(finalFuncSignature).hasValueSatisfying(signature -> {
        assertThat(signature.getName().asInternal()).isEqualTo("to_string");
        assertThat(signature.getParameterTypes()).containsExactly(DataTypes.INT);
    });
    assertThat(aggregate.getReturnType()).isEqualTo(DataTypes.TEXT);
    assertThat(aggregate.getInitCond().get()).isInstanceOf(Integer.class).isEqualTo(0);
}
Also used : AggregateMetadata(com.datastax.oss.driver.api.core.metadata.schema.AggregateMetadata) FunctionSignature(com.datastax.oss.driver.api.core.metadata.schema.FunctionSignature) Test(org.junit.Test)

Example 2 with FunctionSignature

use of com.datastax.oss.driver.api.core.metadata.schema.FunctionSignature in project java-driver by datastax.

the class AggregateParserTest method should_parse_legacy_table.

@Test
public void should_parse_legacy_table() {
    AggregateParser parser = new AggregateParser(new DataTypeClassNameParser(), context);
    AggregateMetadata aggregate = parser.parseAggregate(SUM_AND_TO_STRING_ROW_2_2, KEYSPACE_ID, Collections.emptyMap());
    assertThat(aggregate.getKeyspace().asInternal()).isEqualTo("ks");
    assertThat(aggregate.getSignature().getName().asInternal()).isEqualTo("sum_and_to_string");
    assertThat(aggregate.getSignature().getParameterTypes()).containsExactly(DataTypes.INT);
    FunctionSignature stateFuncSignature = aggregate.getStateFuncSignature();
    assertThat(stateFuncSignature.getName().asInternal()).isEqualTo("plus");
    assertThat(stateFuncSignature.getParameterTypes()).containsExactly(DataTypes.INT, DataTypes.INT);
    assertThat(aggregate.getStateType()).isEqualTo(DataTypes.INT);
    Optional<FunctionSignature> finalFuncSignature = aggregate.getFinalFuncSignature();
    assertThat(finalFuncSignature).isPresent();
    assertThat(finalFuncSignature).hasValueSatisfying(signature -> {
        assertThat(signature.getName().asInternal()).isEqualTo("to_string");
        assertThat(signature.getParameterTypes()).containsExactly(DataTypes.INT);
    });
    assertThat(aggregate.getReturnType()).isEqualTo(DataTypes.TEXT);
    assertThat(aggregate.getInitCond().get()).isInstanceOf(Integer.class).isEqualTo(0);
}
Also used : AggregateMetadata(com.datastax.oss.driver.api.core.metadata.schema.AggregateMetadata) FunctionSignature(com.datastax.oss.driver.api.core.metadata.schema.FunctionSignature) Test(org.junit.Test)

Example 3 with FunctionSignature

use of com.datastax.oss.driver.api.core.metadata.schema.FunctionSignature in project java-driver by datastax.

the class SchemaParserTest method should_parse_keyspace_with_all_children.

@Test
public void should_parse_keyspace_with_all_children() {
    // Needed to parse the aggregate
    when(context.getCodecRegistry()).thenReturn(CodecRegistry.DEFAULT);
    SchemaRefresh refresh = (SchemaRefresh) parse(rows -> rows.withKeyspaces(ImmutableList.of(mockModernKeyspaceRow("ks"))).withTypes(ImmutableList.of(mockTypeRow("ks", "t", ImmutableList.of("i"), ImmutableList.of("int")))).withTables(ImmutableList.of(TableParserTest.TABLE_ROW_3_0)).withColumns(TableParserTest.COLUMN_ROWS_3_0).withIndexes(TableParserTest.INDEX_ROWS_3_0).withViews(ImmutableList.of(ViewParserTest.VIEW_ROW_3_0)).withColumns(ViewParserTest.COLUMN_ROWS_3_0).withFunctions(ImmutableList.of(FunctionParserTest.ID_ROW_3_0)).withAggregates(ImmutableList.of(AggregateParserTest.SUM_AND_TO_STRING_ROW_3_0)));
    assertThat(refresh.newKeyspaces).hasSize(1);
    KeyspaceMetadata keyspace = refresh.newKeyspaces.values().iterator().next();
    checkKeyspace(keyspace);
    assertThat(keyspace.getUserDefinedTypes()).hasSize(1).containsKey(CqlIdentifier.fromInternal("t"));
    assertThat(keyspace.getTables()).hasSize(1).containsKey(CqlIdentifier.fromInternal("foo"));
    assertThat(keyspace.getViews()).hasSize(1).containsKey(CqlIdentifier.fromInternal("alltimehigh"));
    assertThat(keyspace.getFunctions()).hasSize(1).containsKey(new FunctionSignature(CqlIdentifier.fromInternal("id"), DataTypes.INT));
    assertThat(keyspace.getAggregates()).hasSize(1).containsKey(new FunctionSignature(CqlIdentifier.fromInternal("sum_and_to_string"), DataTypes.INT));
}
Also used : CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) SchemaRefresh(com.datastax.oss.driver.internal.core.metadata.schema.refresh.SchemaRefresh) MetadataRefresh(com.datastax.oss.driver.internal.core.metadata.MetadataRefresh) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) CassandraSchemaRows(com.datastax.oss.driver.internal.core.metadata.schema.queries.CassandraSchemaRows) Consumer(java.util.function.Consumer) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) DataTypes(com.datastax.oss.driver.api.core.type.DataTypes) SchemaRows(com.datastax.oss.driver.internal.core.metadata.schema.queries.SchemaRows) FunctionSignature(com.datastax.oss.driver.api.core.metadata.schema.FunctionSignature) Map(java.util.Map) Assertions.assertThat(com.datastax.oss.driver.Assertions.assertThat) SchemaRefresh(com.datastax.oss.driver.internal.core.metadata.schema.refresh.SchemaRefresh) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) FunctionSignature(com.datastax.oss.driver.api.core.metadata.schema.FunctionSignature) Test(org.junit.Test)

Example 4 with FunctionSignature

use of com.datastax.oss.driver.api.core.metadata.schema.FunctionSignature in project java-driver by datastax.

the class AggregateParser method parseAggregate.

public AggregateMetadata parseAggregate(AdminRow row, CqlIdentifier keyspaceId, Map<CqlIdentifier, UserDefinedType> userDefinedTypes) {
    // Cassandra < 3.0:
    // CREATE TABLE system.schema_aggregates (
    // keyspace_name text,
    // aggregate_name text,
    // signature frozen<list<text>>,
    // argument_types list<text>,
    // final_func text,
    // initcond blob,
    // return_type text,
    // state_func text,
    // state_type text,
    // PRIMARY KEY (keyspace_name, aggregate_name, signature)
    // ) WITH CLUSTERING ORDER BY (aggregate_name ASC, signature ASC)
    // 
    // Cassandra >= 3.0:
    // CREATE TABLE system.schema_aggregates (
    // keyspace_name text,
    // aggregate_name text,
    // argument_types frozen<list<text>>,
    // final_func text,
    // initcond text,
    // return_type text,
    // state_func text,
    // state_type text,
    // PRIMARY KEY (keyspace_name, aggregate_name, argument_types)
    // ) WITH CLUSTERING ORDER BY (aggregate_name ASC, argument_types ASC)
    String simpleName = row.getString("aggregate_name");
    List<String> argumentTypes = row.getListOfString("argument_types");
    FunctionSignature signature = new FunctionSignature(CqlIdentifier.fromInternal(simpleName), dataTypeParser.parse(keyspaceId, argumentTypes, userDefinedTypes, context));
    DataType stateType = dataTypeParser.parse(keyspaceId, row.getString("state_type"), userDefinedTypes, context);
    TypeCodec<Object> stateTypeCodec = context.getCodecRegistry().codecFor(stateType);
    String stateFuncSimpleName = row.getString("state_func");
    FunctionSignature stateFuncSignature = new FunctionSignature(CqlIdentifier.fromInternal(stateFuncSimpleName), ImmutableList.<DataType>builder().add(stateType).addAll(signature.getParameterTypes()).build());
    String finalFuncSimpleName = row.getString("final_func");
    FunctionSignature finalFuncSignature = (finalFuncSimpleName == null) ? null : new FunctionSignature(CqlIdentifier.fromInternal(finalFuncSimpleName), stateType);
    DataType returnType = dataTypeParser.parse(keyspaceId, row.getString("return_type"), userDefinedTypes, context);
    Object initCond;
    if (row.isString("initcond")) {
        // Cassandra 3
        String initCondString = row.getString("initcond");
        initCond = (initCondString == null) ? null : stateTypeCodec.parse(initCondString);
    } else {
        // Cassandra 2.2
        ByteBuffer initCondBlob = row.getByteBuffer("initcond");
        initCond = (initCondBlob == null) ? null : stateTypeCodec.decode(initCondBlob, context.getProtocolVersion());
    }
    return new DefaultAggregateMetadata(keyspaceId, signature, finalFuncSignature, initCond, returnType, stateFuncSignature, stateType, stateTypeCodec);
}
Also used : DefaultAggregateMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultAggregateMetadata) DataType(com.datastax.oss.driver.api.core.type.DataType) FunctionSignature(com.datastax.oss.driver.api.core.metadata.schema.FunctionSignature) ByteBuffer(java.nio.ByteBuffer)

Example 5 with FunctionSignature

use of com.datastax.oss.driver.api.core.metadata.schema.FunctionSignature in project java-driver by datastax.

the class FunctionParser method parseFunction.

public FunctionMetadata parseFunction(AdminRow row, CqlIdentifier keyspaceId, Map<CqlIdentifier, UserDefinedType> userDefinedTypes) {
    // Cassandra < 3.0:
    // CREATE TABLE system.schema_functions (
    // keyspace_name text,
    // function_name text,
    // signature frozen<list<text>>,
    // argument_names list<text>,
    // argument_types list<text>,
    // body text,
    // called_on_null_input boolean,
    // language text,
    // return_type text,
    // PRIMARY KEY (keyspace_name, function_name, signature)
    // ) WITH CLUSTERING ORDER BY (function_name ASC, signature ASC)
    // 
    // Cassandra >= 3.0:
    // CREATE TABLE system_schema.functions (
    // keyspace_name text,
    // function_name text,
    // argument_names frozen<list<text>>,
    // argument_types frozen<list<text>>,
    // body text,
    // called_on_null_input boolean,
    // language text,
    // return_type text,
    // PRIMARY KEY (keyspace_name, function_name, argument_types)
    // ) WITH CLUSTERING ORDER BY (function_name ASC, argument_types ASC)
    String simpleName = row.getString("function_name");
    List<CqlIdentifier> argumentNames = ImmutableList.copyOf(Lists.transform(row.getListOfString("argument_names"), CqlIdentifier::fromInternal));
    List<String> argumentTypes = row.getListOfString("argument_types");
    if (argumentNames.size() != argumentTypes.size()) {
        LOG.warn("[{}] Error parsing system row for function {}.{}, " + "number of argument names and types don't match (got {} and {}).", logPrefix, keyspaceId.asInternal(), simpleName, argumentNames.size(), argumentTypes.size());
        return null;
    }
    FunctionSignature signature = new FunctionSignature(CqlIdentifier.fromInternal(simpleName), dataTypeParser.parse(keyspaceId, argumentTypes, userDefinedTypes, context));
    String body = row.getString("body");
    Boolean calledOnNullInput = row.getBoolean("called_on_null_input");
    String language = row.getString("language");
    DataType returnType = dataTypeParser.parse(keyspaceId, row.getString("return_type"), userDefinedTypes, context);
    return new DefaultFunctionMetadata(keyspaceId, signature, argumentNames, body, calledOnNullInput, language, returnType);
}
Also used : DefaultFunctionMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultFunctionMetadata) DataType(com.datastax.oss.driver.api.core.type.DataType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) FunctionSignature(com.datastax.oss.driver.api.core.metadata.schema.FunctionSignature)

Aggregations

FunctionSignature (com.datastax.oss.driver.api.core.metadata.schema.FunctionSignature)5 Test (org.junit.Test)3 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)2 AggregateMetadata (com.datastax.oss.driver.api.core.metadata.schema.AggregateMetadata)2 DataType (com.datastax.oss.driver.api.core.type.DataType)2 Assertions.assertThat (com.datastax.oss.driver.Assertions.assertThat)1 KeyspaceMetadata (com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata)1 DataTypes (com.datastax.oss.driver.api.core.type.DataTypes)1 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)1 MetadataRefresh (com.datastax.oss.driver.internal.core.metadata.MetadataRefresh)1 DefaultAggregateMetadata (com.datastax.oss.driver.internal.core.metadata.schema.DefaultAggregateMetadata)1 DefaultFunctionMetadata (com.datastax.oss.driver.internal.core.metadata.schema.DefaultFunctionMetadata)1 CassandraSchemaRows (com.datastax.oss.driver.internal.core.metadata.schema.queries.CassandraSchemaRows)1 SchemaRows (com.datastax.oss.driver.internal.core.metadata.schema.queries.SchemaRows)1 SchemaRefresh (com.datastax.oss.driver.internal.core.metadata.schema.refresh.SchemaRefresh)1 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)1 ByteBuffer (java.nio.ByteBuffer)1 Map (java.util.Map)1 Consumer (java.util.function.Consumer)1 Mockito.when (org.mockito.Mockito.when)1