use of com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMultimap in project dsbulk by datastax.
the class SchemaSettings method inferBatchInsertQuery.
private String inferBatchInsertQuery(ImmutableMultimap<MappingField, CQLFragment> fieldsToVariables) {
List<CQLWord> pks = primaryKeyColumns();
Set<CQLFragment> allSpecificVariables = new LinkedHashSet<>();
Map<CQLWord, WriteTimeAndTTL> specificWriteTimesAndTTLs = new LinkedHashMap<>();
boolean hasGlobalWritetime = false;
boolean hasGlobalTTL = false;
for (CQLFragment variable : fieldsToVariables.values()) {
if (variable instanceof FunctionCall) {
FunctionCall functionCall = (FunctionCall) variable;
if (functionCall.getFunctionName().equals(WRITETIME)) {
for (CQLFragment arg : functionCall.getArgs()) {
if (arg.equals(STAR)) {
if (preserveTimestamp) {
throw new IllegalStateException("Invalid mapping: writetime(*) is not allowed when schema.preserveTimestamp is true.");
}
hasGlobalWritetime = true;
} else {
CQLWord col = (CQLWord) arg;
if (pks.contains(col)) {
throw new IllegalStateException("Invalid mapping: writetime() function arg must be either '*' or a non-primary key column name.");
}
if (fieldsToVariables.containsValue(col)) {
allSpecificVariables.add(col);
allSpecificVariables.add(functionCall);
specificWriteTimesAndTTLs.compute(col, (k, v) -> {
if (v == null) {
v = new WriteTimeAndTTL();
MappingField colField = fieldsToVariables.inverse().get(col).iterator().next();
v.value = colField instanceof CQLFragment ? (CQLFragment) colField : col;
}
MappingField writetimeField = fieldsToVariables.inverse().get(functionCall).iterator().next();
v.writetime = writetimeField instanceof CQLLiteral ? (CQLLiteral) writetimeField : CQLWord.fromInternal(functionCall.render(INTERNAL));
return v;
});
} else {
throw new IllegalStateException(String.format("Invalid mapping: target column %s must be present if %s is also present.", col.render(VARIABLE), functionCall.render(INTERNAL)));
}
}
}
} else if (functionCall.getFunctionName().equals(TTL)) {
for (CQLFragment arg : functionCall.getArgs()) {
if (arg.equals(STAR)) {
if (preserveTtl) {
throw new IllegalStateException("Invalid mapping: ttl(*) is not allowed when schema.preserveTtl is true.");
}
hasGlobalTTL = true;
} else {
CQLWord col = (CQLWord) arg;
if (pks.contains(col)) {
throw new IllegalStateException("Invalid mapping: ttl() function arg must be either '*' or a non-primary key column name.");
}
if (fieldsToVariables.containsValue(col)) {
allSpecificVariables.add(col);
allSpecificVariables.add(functionCall);
specificWriteTimesAndTTLs.compute((CQLWord) arg, (k, v) -> {
if (v == null) {
v = new WriteTimeAndTTL();
MappingField colField = fieldsToVariables.inverse().get(col).iterator().next();
v.value = colField instanceof CQLFragment ? (CQLFragment) colField : col;
}
MappingField ttlField = fieldsToVariables.inverse().get(functionCall).iterator().next();
v.ttl = ttlField instanceof CQLLiteral ? (CQLLiteral) ttlField : CQLWord.fromInternal(functionCall.render(INTERNAL));
return v;
});
} else {
throw new IllegalStateException(String.format("Invalid mapping: target column %s must be present if %s is also present.", col.render(VARIABLE), functionCall.render(INTERNAL)));
}
}
}
}
}
}
ImmutableMultimap.Builder<MappingField, CQLFragment> defaultFieldsToVariablesBuilder = ImmutableMultimap.builder();
for (Entry<MappingField, CQLFragment> entry : fieldsToVariables.entries()) {
CQLFragment variable = entry.getValue();
if (!allSpecificVariables.contains(variable)) {
defaultFieldsToVariablesBuilder.put(entry);
}
}
ImmutableMultimap<MappingField, CQLFragment> defaultFieldsToVariables = defaultFieldsToVariablesBuilder.build();
boolean hasRegularColumnsWithoutSpecificWritetimeAndTTL = defaultFieldsToVariables.values().stream().filter(CQLWord.class::isInstance).map(CQLWord.class::cast).anyMatch(variable -> !pks.contains(variable));
if (!hasRegularColumnsWithoutSpecificWritetimeAndTTL) {
if (hasGlobalWritetime) {
throw new IllegalStateException("Invalid mapping: writetime(*) function has no target column.");
}
if (hasGlobalTTL) {
throw new IllegalStateException("Invalid mapping: ttl(*) function has no target column.");
}
}
StringBuilder sb = new StringBuilder();
if (!hasRegularColumnsWithoutSpecificWritetimeAndTTL && specificWriteTimesAndTTLs.size() == 1) {
// edge case: there is only one regular column in the table,
// and it has specific writetime or tll: no need for a BATCH as there is only one child
// statement.
Entry<CQLWord, WriteTimeAndTTL> entry = specificWriteTimesAndTTLs.entrySet().iterator().next();
appendBatchChildQuery(sb, entry.getKey(), entry.getValue().value, entry.getValue().writetime, entry.getValue().ttl, pks);
} else {
sb.append("BEGIN UNLOGGED BATCH ");
// generate a first INSERT INTO child query similar to the ones generated for simple INSERTs.
if (hasRegularColumnsWithoutSpecificWritetimeAndTTL) {
sb.append(inferInsertQuery(defaultFieldsToVariables)).append("; ");
}
// generate a specific INSERT INTO query for that variable only + its TTL and/or writetime.
for (Entry<CQLWord, WriteTimeAndTTL> entry : specificWriteTimesAndTTLs.entrySet()) {
appendBatchChildQuery(sb, entry.getKey(), entry.getValue().value, entry.getValue().writetime, entry.getValue().ttl, pks);
sb.append("; ");
}
sb.append("APPLY BATCH");
}
return sb.toString();
}
use of com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMultimap in project dsbulk by datastax.
the class SchemaSettings method inferInsertQuery.
private String inferInsertQuery(ImmutableMultimap<MappingField, CQLFragment> fieldsToVariables) {
ImmutableMultimap.Builder<MappingField, CQLFragment> regularFieldsToVariablesBuilder = ImmutableMultimap.builder();
CQLFragment writetime = null;
CQLFragment ttl = null;
for (Entry<MappingField, CQLFragment> entry : fieldsToVariables.entries()) {
if (entry.getValue() instanceof FunctionCall) {
FunctionCall functionCall = (FunctionCall) entry.getValue();
if (functionCall.getFunctionName().equals(WRITETIME)) {
assert writetime == null;
if (entry.getKey() instanceof CQLLiteral) {
writetime = (CQLLiteral) entry.getKey();
} else {
writetime = CQLWord.fromInternal(functionCall.render(INTERNAL));
}
} else if (functionCall.getFunctionName().equals(TTL)) {
assert ttl == null;
if (entry.getKey() instanceof CQLLiteral) {
ttl = (CQLLiteral) entry.getKey();
} else {
ttl = CQLWord.fromInternal(functionCall.render(INTERNAL));
}
}
} else {
regularFieldsToVariablesBuilder.put(entry);
}
}
ImmutableMultimap<MappingField, CQLFragment> regularFieldsToVariables = regularFieldsToVariablesBuilder.build();
StringBuilder sb = new StringBuilder("INSERT INTO ");
sb.append(keyspaceName.render(VARIABLE)).append('.').append(tableName.render(VARIABLE)).append(" (");
appendColumnNames(regularFieldsToVariables, sb, VARIABLE);
sb.append(") VALUES (");
Set<CQLFragment> cols = maybeSortCols(regularFieldsToVariables);
Iterator<CQLFragment> it = cols.iterator();
while (it.hasNext()) {
CQLFragment col = it.next();
// for insert queries there can be only one field mapped to a given column
MappingField field = fieldsToVariables.inverse().get(col).iterator().next();
if (field instanceof CQLFragment) {
sb.append(((CQLFragment) field).render(NAMED_ASSIGNMENT));
} else {
sb.append(col.render(NAMED_ASSIGNMENT));
}
if (it.hasNext()) {
sb.append(", ");
}
}
sb.append(')');
appendWriteTimeAndTTL(sb, writetime, ttl);
return sb.toString();
}
use of com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMultimap in project dsbulk by datastax.
the class SchemaSettings method prepareStatementAndCreateMapping.
@NonNull
private Mapping prepareStatementAndCreateMapping(CqlSession session, boolean batchingEnabled, EnumSet<StatisticsMode> modes) {
ImmutableMultimap<MappingField, CQLFragment> fieldsToVariables = null;
if (!config.hasPath(QUERY)) {
// in the absence of user-provided queries, create the mapping *before* query generation and
// preparation
List<CQLFragment> columns = table.getColumns().values().stream().filter(col -> !isDSESearchPseudoColumn(col)).flatMap(column -> {
CQLWord colName = CQLWord.fromCqlIdentifier(column.getName());
List<CQLFragment> cols = Lists.newArrayList(colName);
if (schemaGenerationStrategy.isMapping()) {
if (preserveTimestamp && checkWritetimeTtlSupported(column, WRITETIME)) {
cols.add(new FunctionCall(null, WRITETIME, colName));
}
if (preserveTtl && checkWritetimeTtlSupported(column, TTL)) {
cols.add(new FunctionCall(null, TTL, colName));
}
}
return cols.stream();
}).collect(Collectors.toList());
fieldsToVariables = createFieldsToVariablesMap(columns);
// query generation
if (schemaGenerationStrategy.isWriting()) {
if (isCounterTable()) {
query = inferUpdateCounterQuery(fieldsToVariables);
} else if (requiresBatchInsertQuery(fieldsToVariables)) {
query = inferBatchInsertQuery(fieldsToVariables);
} else {
query = inferInsertQuery(fieldsToVariables);
}
} else if (schemaGenerationStrategy.isReading() && schemaGenerationStrategy.isMapping()) {
query = inferReadQuery(fieldsToVariables);
} else if (schemaGenerationStrategy.isReading() && schemaGenerationStrategy.isCounting()) {
query = inferCountQuery(modes);
} else {
throw new IllegalStateException("Unsupported schema generation strategy: " + schemaGenerationStrategy);
}
LOGGER.debug("Inferred query: {}", query);
queryInspector = new QueryInspector(query);
// validate generated query
if (schemaGenerationStrategy.isWriting()) {
validatePrimaryKeyPresent(fieldsToVariables);
}
}
assert query != null;
assert queryInspector != null;
if (!queryInspector.getKeyspaceName().isPresent()) {
session.execute("USE " + keyspaceName);
}
// Transform user-provided queries before preparation
if (config.hasPath(QUERY)) {
if (schemaGenerationStrategy.isReading() && queryInspector.isParallelizable()) {
int whereClauseIndex = queryInspector.getFromClauseEndIndex() + 1;
StringBuilder sb = new StringBuilder(query.substring(0, whereClauseIndex));
appendTokenRangeRestriction(sb);
query = sb.append(query.substring(whereClauseIndex)).toString();
}
if (schemaGenerationStrategy.isCounting()) {
if (modes.contains(StatisticsMode.partitions) || modes.contains(StatisticsMode.ranges) || modes.contains(StatisticsMode.hosts)) {
throw new IllegalArgumentException(String.format("Cannot count with stats.modes = %s when schema.query is provided; " + "only stats.modes = [global] is allowed", modes));
}
// reduce row size by only selecting one column
StringBuilder sb = new StringBuilder("SELECT ");
sb.append(getGlobalCountSelector());
query = sb.append(' ').append(query.substring(queryInspector.getFromClauseStartIndex())).toString();
}
queryInspector = new QueryInspector(query);
}
if (batchingEnabled && queryInspector.isBatch()) {
preparedStatements = unwrapAndPrepareBatchChildStatements(session);
} else {
preparedStatements = Collections.singletonList(session.prepare(query));
}
if (config.hasPath(QUERY)) {
// in the presence of user-provided queries, create the mapping *after* query preparation
Stream<ColumnDefinitions> variables = getVariables();
fieldsToVariables = createFieldsToVariablesMap(variables.flatMap(defs -> StreamSupport.stream(defs.spliterator(), false)).map(def -> def.getName().asInternal()).map(CQLWord::fromInternal).collect(Collectors.toList()));
// validate user-provided query
if (schemaGenerationStrategy.isWriting()) {
if (mutatesOnlyStaticColumns()) {
// DAT-414: mutations that only affect static columns are allowed
// to skip the clustering columns, only the partition key should be present.
validatePartitionKeyPresent(fieldsToVariables);
} else {
validatePrimaryKeyPresent(fieldsToVariables);
}
}
}
assert fieldsToVariables != null;
return new DefaultMapping(transformFieldsToVariables(fieldsToVariables), codecFactory, transformWriteTimeVariables(queryInspector.getWriteTimeVariables()));
}
use of com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMultimap 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();
}
Aggregations