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);
}
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);
}
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));
}
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);
}
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);
}
Aggregations