use of com.datastax.oss.dsbulk.mapping.CQLLiteral 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.dsbulk.mapping.CQLLiteral in project dsbulk by datastax.
the class SchemaSettings method inferUpdateCounterQuery.
private String inferUpdateCounterQuery(ImmutableMultimap<MappingField, CQLFragment> fieldsToVariables) {
StringBuilder sb = new StringBuilder("UPDATE ");
sb.append(keyspaceName.render(VARIABLE)).append('.').append(tableName.render(VARIABLE));
// Note: TTL and timestamp are not allowed in counter queries;
// a test is made inside the following method for fixed TTL and timestamps;
// function-style TTL and timestamps will be tested below and forbidden as well
appendWriteTimeAndTTL(sb, null, null);
sb.append(" SET ");
Set<CQLFragment> cols = maybeSortCols(fieldsToVariables);
Iterator<CQLFragment> colsIterator = cols.iterator();
boolean isFirst = true;
List<CQLWord> pks = primaryKeyColumns();
while (colsIterator.hasNext()) {
CQLFragment col = colsIterator.next();
if (col instanceof CQLWord && pks.contains(col)) {
continue;
}
// forbid writetime and TTL right-hand function calls when updating a counter table
if (col instanceof FunctionCall) {
throw new IllegalArgumentException("Invalid mapping: function calls are not allowed when updating a counter table.");
}
// for update queries there can be only one field mapped to a given column
MappingField field = fieldsToVariables.inverse().get(col).iterator().next();
if (field instanceof FunctionCall) {
throw new IllegalArgumentException("Invalid mapping: function calls are not allowed when updating a counter table.");
} else if (field instanceof CQLLiteral) {
throw new IllegalArgumentException("Invalid mapping: constant expressions are not allowed when updating a counter table.");
}
if (!isFirst) {
sb.append(", ");
}
isFirst = false;
sb.append(col.render(VARIABLE)).append(" = ").append(col.render(VARIABLE)).append(" + ").append(col.render(NAMED_ASSIGNMENT));
}
sb.append(" WHERE ");
Iterator<CQLWord> pksIterator = pks.iterator();
while (pksIterator.hasNext()) {
CQLFragment col = pksIterator.next();
sb.append(col.render(VARIABLE)).append(" = ").append(col.render(NAMED_ASSIGNMENT));
if (pksIterator.hasNext()) {
sb.append(" AND ");
}
}
return sb.toString();
}
use of com.datastax.oss.dsbulk.mapping.CQLLiteral 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.dsbulk.mapping.CQLLiteral in project dsbulk by datastax.
the class SchemaSettings method appendWriteTimeAndTTL.
private void appendWriteTimeAndTTL(StringBuilder sb, @Nullable CQLFragment writetime, @Nullable CQLFragment ttl) {
boolean hasTtl = ttlSeconds != -1 || ttl != null;
boolean hasTimestamp = timestampMicros != -1 || writetime != null;
if (hasTtl || hasTimestamp) {
if (isCounterTable()) {
throw new IllegalArgumentException("Cannot set TTL or timestamp when updating a counter table.");
}
sb.append(" USING ");
if (hasTtl) {
sb.append("TTL ");
if (ttlSeconds != -1) {
sb.append(ttlSeconds);
} else {
assert ttl != null;
sb.append(ttl.render(NAMED_ASSIGNMENT));
}
if (hasTimestamp) {
sb.append(" AND ");
}
}
if (hasTimestamp) {
sb.append("TIMESTAMP ");
if (timestampMicros != -1) {
sb.append(timestampMicros);
} else {
assert writetime != null;
if (writetime instanceof TypedCQLLiteral) {
DataType dataType = ((TypedCQLLiteral) writetime).getDataType();
if (dataType == DataTypes.TIMESTAMP) {
ConvertingCodec<String, Instant> codec = codecFactory.createConvertingCodec(DataTypes.TIMESTAMP, GenericType.STRING, true);
String literal = ((CQLLiteral) writetime).getLiteral();
Instant i = codec.externalToInternal(codec.parse(literal));
long micros = instantToNumber(i, MICROSECONDS, EPOCH);
writetime = new TypedCQLLiteral(Long.toString(micros), DataTypes.BIGINT);
}
}
sb.append(writetime.render(NAMED_ASSIGNMENT));
}
}
}
}
Aggregations