use of io.crate.metadata.doc.DocTableInfo in project crate by crate.
the class CopyToPlan method bind.
@VisibleForTesting
public static BoundCopyTo bind(AnalyzedCopyTo copyTo, CoordinatorTxnCtx txnCtx, NodeContext nodeCtx, Row parameters, SubQueryResults subQueryResults) {
Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(txnCtx, nodeCtx, x, parameters, subQueryResults);
DocTableInfo table = (DocTableInfo) copyTo.tableInfo();
List<String> partitions = resolvePartitions(Lists2.map(copyTo.table().partitionProperties(), x -> x.map(eval)), table);
List<Symbol> outputs = new ArrayList<>();
Map<ColumnIdent, Symbol> overwrites = null;
boolean columnsDefined = false;
final List<String> outputNames = new ArrayList<>(copyTo.columns().size());
if (!copyTo.columns().isEmpty()) {
// TODO: remove outputNames?
for (Symbol symbol : copyTo.columns()) {
assert symbol instanceof Reference : "Only references are expected here";
RefVisitor.visitRefs(symbol, r -> outputNames.add(r.column().sqlFqn()));
outputs.add(DocReferences.toSourceLookup(symbol));
}
columnsDefined = true;
} else {
Reference sourceRef;
if (table.isPartitioned() && partitions.isEmpty()) {
// table is partitioned, insert partitioned columns into the output
overwrites = new HashMap<>();
for (Reference reference : table.partitionedByColumns()) {
if (!(reference instanceof GeneratedReference)) {
overwrites.put(reference.column(), reference);
}
}
if (overwrites.size() > 0) {
sourceRef = table.getReference(DocSysColumns.DOC);
} else {
sourceRef = table.getReference(DocSysColumns.RAW);
}
} else {
sourceRef = table.getReference(DocSysColumns.RAW);
}
outputs = List.of(sourceRef);
}
Settings settings = genericPropertiesToSettings(copyTo.properties().map(eval));
WriterProjection.CompressionType compressionType = settingAsEnum(WriterProjection.CompressionType.class, COMPRESSION_SETTING.get(settings));
WriterProjection.OutputFormat outputFormat = settingAsEnum(WriterProjection.OutputFormat.class, OUTPUT_FORMAT_SETTING.get(settings));
if (!columnsDefined && outputFormat == WriterProjection.OutputFormat.JSON_ARRAY) {
throw new UnsupportedFeatureException("Output format not supported without specifying columns.");
}
WhereClause whereClause = new WhereClause(copyTo.whereClause(), partitions, Collections.emptySet());
return new BoundCopyTo(outputs, table, whereClause, Literal.of(DataTypes.STRING.sanitizeValue(eval.apply(copyTo.uri()))), compressionType, outputFormat, outputNames.isEmpty() ? null : outputNames, columnsDefined, overwrites, settings);
}
use of io.crate.metadata.doc.DocTableInfo in project crate by crate.
the class GeneratedColumnsTest method testSubscriptExpressionThatReturnsAnArray.
@Test
public void testSubscriptExpressionThatReturnsAnArray() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table t (obj object as (arr array(integer)), arr as obj['arr'])").build();
QueriedSelectRelation query = e.analyze("select obj, arr from t");
DocTableInfo table = ((DocTableRelation) query.from().get(0)).tableInfo();
GeneratedColumns<Doc> generatedColumns = new GeneratedColumns<>(new InputFactory(e.nodeCtx), CoordinatorTxnCtx.systemTransactionContext(), GeneratedColumns.Validation.NONE, new DocRefResolver(Collections.emptyList()), Collections.emptyList(), table.generatedColumns());
BytesReference bytes = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startObject("obj").startArray("arr").value(10).value(20).endArray().endObject().endObject());
generatedColumns.setNextRow(new Doc(1, table.concreteIndices()[0], "1", 1, 1, 1, XContentHelper.convertToMap(bytes, false, XContentType.JSON).v2(), bytes::utf8ToString));
Map.Entry<Reference, Input<?>> generatedColumn = generatedColumns.generatedToInject().iterator().next();
assertThat((List<Object>) generatedColumn.getValue().value(), contains(10, 20));
}
use of io.crate.metadata.doc.DocTableInfo in project crate by crate.
the class UpdateSourceGenTest method testSetXBasedOnXAndPartitionedColumn.
@Test
public void testSetXBasedOnXAndPartitionedColumn() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addPartitionedTable("create table t (x int, p int) partitioned by (p)", new PartitionName(new RelationName("doc", "t"), Collections.singletonList("1")).asIndexName()).build();
AnalyzedUpdateStatement update = e.analyze("update t set x = x + p");
Assignments assignments = Assignments.convert(update.assignmentByTargetCol(), e.nodeCtx);
DocTableInfo table = (DocTableInfo) update.table().tableInfo();
UpdateSourceGen updateSourceGen = new UpdateSourceGen(txnCtx, e.nodeCtx, table, assignments.targetNames());
Map<String, Object> source = singletonMap("x", 1);
Map<String, Object> updatedSource = updateSourceGen.generateSource(new Doc(1, table.concreteIndices()[0], "1", 1, 1, 1, source, () -> {
try {
return Strings.toString(XContentFactory.jsonBuilder().map(source));
} catch (IOException e1) {
throw new RuntimeException(e1);
}
}), assignments.sources(), new Object[0]);
assertThat(updatedSource, is(Map.of("x", 2)));
}
use of io.crate.metadata.doc.DocTableInfo in project crate by crate.
the class UpdateSourceGenTest method testNestedGeneratedColumnIsGeneratedValidateValueIfGivenByUser.
@Test
public void testNestedGeneratedColumnIsGeneratedValidateValueIfGivenByUser() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table t (x int, obj object as (y as 'foo'))").build();
AnalyzedUpdateStatement update = e.analyze("update t set x = 4, obj = {y='foo'}");
Assignments assignments = Assignments.convert(update.assignmentByTargetCol(), e.nodeCtx);
DocTableInfo table = (DocTableInfo) update.table().tableInfo();
UpdateSourceGen updateSourceGen = new UpdateSourceGen(txnCtx, e.nodeCtx, table, assignments.targetNames());
Map<String, Object> updatedSource = updateSourceGen.generateSource(new Doc(1, table.concreteIndices()[0], "1", 1, 1, 1, emptyMap(), () -> "{}"), assignments.sources(), new Object[0]);
assertThat(updatedSource, is(Map.of("obj", Map.of("y", "foo"), "x", 4)));
}
use of io.crate.metadata.doc.DocTableInfo in project crate by crate.
the class UpdateSourceGenTest method testGeneratedColumnUsingFunctionDependingOnActiveTransaction.
@Test
public void testGeneratedColumnUsingFunctionDependingOnActiveTransaction() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table t (x int, gen as current_schema)").build();
AnalyzedUpdateStatement update = e.analyze("update t set x = 1");
Assignments assignments = Assignments.convert(update.assignmentByTargetCol(), e.nodeCtx);
DocTableInfo table = (DocTableInfo) update.table().tableInfo();
UpdateSourceGen sourceGen = new UpdateSourceGen(TransactionContext.of(DUMMY_SESSION_INFO), e.nodeCtx, table, assignments.targetNames());
Map<String, Object> source = sourceGen.generateSource(new Doc(1, table.concreteIndices()[0], "1", 1, 1, 1, emptyMap(), () -> "{}"), assignments.sources(), new Object[0]);
assertThat(source, is(Map.of("gen", "dummySchema", "x", 1)));
}
Aggregations