use of com.datastax.oss.dsbulk.mapping.CQLWord in project dsbulk by datastax.
the class SchemaSettings method validateKeyPresent.
private void validateKeyPresent(ImmutableMultimap<MappingField, CQLFragment> fieldsToVariables, List<ColumnMetadata> columns) {
Collection<CQLFragment> mappingVariables = fieldsToVariables.values();
Map<CQLWord, CQLFragment> queryVariables = queryInspector.getAssignments();
for (ColumnMetadata pk : columns) {
CQLWord pkVariable = CQLWord.fromCqlIdentifier(pk.getName());
CQLFragment queryVariable = queryVariables.get(pkVariable);
// the provided query did not contain such column
if (queryVariable == null) {
throw new IllegalArgumentException("Missing required primary key column " + pkVariable.render(VARIABLE) + " from schema.mapping or schema.query");
}
// if the PK is mapped to a function or a literal in the query (DAT-326)
if (queryVariable instanceof CQLWord) {
// the mapping did not contain such column
if (!mappingVariables.contains(queryVariable)) {
throw new IllegalArgumentException("Missing required primary key column " + pkVariable.render(VARIABLE) + " from schema.mapping");
}
}
}
}
use of com.datastax.oss.dsbulk.mapping.CQLWord in project dsbulk by datastax.
the class SchemaSettings method columnsToVariables.
@NonNull
private Set<CQLWord> columnsToVariables(Collection<ColumnMetadata> columns) {
Map<CQLWord, CQLFragment> boundVariables = queryInspector.getAssignments();
Set<CQLWord> variables = new HashSet<>(columns.size());
for (ColumnMetadata column : columns) {
CQLFragment variable = boundVariables.get(CQLWord.fromCqlIdentifier(column.getName()));
if (variable instanceof CQLWord) {
variables.add((CQLWord) variable);
}
}
return variables;
}
use of com.datastax.oss.dsbulk.mapping.CQLWord in project dsbulk by datastax.
the class SchemaSettings method inferFieldsToVariablesMap.
private ImmutableMultimap<MappingField, CQLFragment> inferFieldsToVariablesMap(Collection<CQLFragment> columns) {
// use a builder to preserve iteration order
ImmutableMultimap.Builder<MappingField, CQLFragment> fieldsToVariables = ImmutableMultimap.builder();
List<CQLFragment> excludedVariables = new ArrayList<>(mapping.getExcludedVariables());
if (!isCounterTable() && schemaGenerationStrategy.isMapping()) {
for (CQLWord variable : mapping.getExcludedVariables()) {
if (preserveTimestamp) {
excludedVariables.add(new FunctionCall(null, WRITETIME, variable));
}
if (preserveTtl) {
excludedVariables.add(new FunctionCall(null, TTL, variable));
}
}
}
int i = 0;
for (CQLFragment colName : columns) {
if (!excludedVariables.contains(colName)) {
if (mappingPreference == INDEXED_ONLY) {
fieldsToVariables.put(new IndexedMappingField(i), colName);
} else {
fieldsToVariables.put(new MappedMappingField(colName.render(INTERNAL)), colName);
}
}
i++;
}
return fieldsToVariables.build();
}
use of com.datastax.oss.dsbulk.mapping.CQLWord in project dsbulk by datastax.
the class QueryInspectorTest method should_detect_function_select.
@Test
void should_detect_function_select() {
QueryInspector inspector = new QueryInspector("SELECT myFunction(col1) as f1, \"MyFunction\"(\"My Col 2\") FROM ks.table1");
CQLWord name1 = CQLWord.fromInternal("myfunction");
CQLWord name2 = CQLWord.fromInternal("MyFunction");
FunctionCall f1 = new FunctionCall(null, name1, COL_1);
FunctionCall f2 = new FunctionCall(null, name2, MY_COL_2);
assertThat(inspector.getResultSetVariables()).hasSize(2).containsKeys(f1, f2).containsValues(CQLWord.fromInternal("f1"), f2);
}
use of com.datastax.oss.dsbulk.mapping.CQLWord in project dsbulk by datastax.
the class QueryInspector method visitNormalInsertStatement.
@Override
public CQLFragment visitNormalInsertStatement(NormalInsertStatementContext ctx) {
if (ctx.cident().size() != ctx.term().size()) {
throw new IllegalArgumentException(String.format("Invalid query: the number of columns to insert (%d) does not match the number of terms (%d): %s.", ctx.cident().size(), ctx.term().size(), query));
}
for (int i = 0; i < ctx.cident().size(); i++) {
CQLWord column = visitCident(ctx.cident().get(i));
CQLFragment variable = visitTerm(ctx.term().get(i));
assignmentsBuilder.put(column, variable == QUESTION_MARK ? column : variable);
}
if (ctx.usingClause() != null) {
visitUsingClause(ctx.usingClause());
}
return null;
}
Aggregations