use of io.crate.analyze.relations.DocTableRelation in project crate by crate.
the class CopyAnalyzer method convertCopyTo.
CopyToAnalyzedStatement convertCopyTo(CopyTo node, Analysis analysis) {
if (!node.directoryUri()) {
throw new UnsupportedOperationException("Using COPY TO without specifying a DIRECTORY is not supported");
}
TableInfo tableInfo = schemas.getTableInfo(TableIdent.of(node.table(), analysis.sessionContext().defaultSchema()));
if (!(tableInfo instanceof DocTableInfo)) {
throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "Cannot COPY %s TO. COPY TO only supports user tables", tableInfo.ident()));
}
Operation.blockedRaiseException(tableInfo, Operation.READ);
DocTableRelation tableRelation = new DocTableRelation((DocTableInfo) tableInfo);
EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.MUTATE, null, tableRelation);
ExpressionAnalyzer expressionAnalyzer = createExpressionAnalyzer(analysis, tableRelation);
ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
Settings settings = GenericPropertiesConverter.settingsFromProperties(node.genericProperties(), analysis.parameterContext(), SETTINGS_APPLIERS).build();
WriterProjection.CompressionType compressionType = settingAsEnum(WriterProjection.CompressionType.class, settings.get(COMPRESSION_SETTINGS.name()));
WriterProjection.OutputFormat outputFormat = settingAsEnum(WriterProjection.OutputFormat.class, settings.get(OUTPUT_FORMAT_SETTINGS.name()));
Symbol uri = expressionAnalyzer.convert(node.targetUri(), expressionAnalysisContext);
uri = normalizer.normalize(uri, analysis.transactionContext());
List<String> partitions = resolvePartitions(node, analysis, tableRelation);
List<Symbol> outputs = new ArrayList<>();
QuerySpec querySpec = new QuerySpec();
WhereClause whereClause = createWhereClause(node.whereClause(), tableRelation, partitions, normalizer, expressionAnalyzer, expressionAnalysisContext, analysis.transactionContext());
querySpec.where(whereClause);
Map<ColumnIdent, Symbol> overwrites = null;
boolean columnsDefined = false;
List<String> outputNames = null;
if (!node.columns().isEmpty()) {
outputNames = new ArrayList<>(node.columns().size());
for (Expression expression : node.columns()) {
Symbol symbol = expressionAnalyzer.convert(expression, expressionAnalysisContext);
symbol = normalizer.normalize(symbol, analysis.transactionContext());
outputNames.add(SymbolPrinter.INSTANCE.printSimple(symbol));
outputs.add(DocReferenceConverter.convertIf(symbol));
}
columnsDefined = true;
} else {
Reference sourceRef;
if (tableRelation.tableInfo().isPartitioned() && partitions.isEmpty()) {
// table is partitioned, insert partitioned columns into the output
overwrites = new HashMap<>();
for (Reference reference : tableRelation.tableInfo().partitionedByColumns()) {
if (!(reference instanceof GeneratedReference)) {
overwrites.put(reference.ident().columnIdent(), reference);
}
}
if (overwrites.size() > 0) {
sourceRef = tableRelation.tableInfo().getReference(DocSysColumns.DOC);
} else {
sourceRef = tableRelation.tableInfo().getReference(DocSysColumns.RAW);
}
} else {
sourceRef = tableRelation.tableInfo().getReference(DocSysColumns.RAW);
}
outputs = ImmutableList.<Symbol>of(sourceRef);
}
querySpec.outputs(outputs);
if (!columnsDefined && outputFormat == WriterProjection.OutputFormat.JSON_ARRAY) {
throw new UnsupportedFeatureException("Output format not supported without specifying columns.");
}
QueriedDocTable subRelation = new QueriedDocTable(tableRelation, querySpec);
return new CopyToAnalyzedStatement(subRelation, settings, uri, compressionType, outputFormat, outputNames, columnsDefined, overwrites);
}
use of io.crate.analyze.relations.DocTableRelation in project crate by crate.
the class MultiSourceFetchPushDown method process.
void process() {
remainingOutputs = statement.querySpec().outputs();
statement.querySpec().outputs(new ArrayList<>());
HashMap<Symbol, Symbol> topLevelOutputMap = new HashMap<>(statement.canBeFetched().size());
HashMap<Symbol, Symbol> mssOutputMap = new HashMap<>(statement.querySpec().outputs().size() + 2);
ArrayList<Symbol> mssOutputs = new ArrayList<>(statement.sources().size() + statement.requiredForQuery().size());
for (Map.Entry<QualifiedName, RelationSource> entry : statement.sources().entrySet()) {
RelationSource source = entry.getValue();
if (!(source.relation() instanceof DocTableRelation)) {
int index = 0;
for (Symbol output : source.querySpec().outputs()) {
RelationColumn rc = new RelationColumn(entry.getKey(), index++, output.valueType());
mssOutputs.add(rc);
mssOutputMap.put(output, rc);
topLevelOutputMap.put(output, new InputColumn(mssOutputs.size() - 1, output.valueType()));
}
continue;
}
DocTableRelation rel = (DocTableRelation) source.relation();
HashSet<Field> canBeFetched = filterByRelation(statement.canBeFetched(), rel);
if (!canBeFetched.isEmpty()) {
RelationColumn fetchIdColumn = new RelationColumn(entry.getKey(), 0, DataTypes.LONG);
mssOutputs.add(fetchIdColumn);
InputColumn fetchIdInput = new InputColumn(mssOutputs.size() - 1);
ArrayList<Symbol> qtOutputs = new ArrayList<>(source.querySpec().outputs().size() - canBeFetched.size() + 1);
Reference fetchId = rel.tableInfo().getReference(DocSysColumns.FETCHID);
qtOutputs.add(fetchId);
for (Symbol output : source.querySpec().outputs()) {
if (!canBeFetched.contains(output)) {
qtOutputs.add(output);
RelationColumn rc = new RelationColumn(entry.getKey(), qtOutputs.size() - 1, output.valueType());
mssOutputs.add(rc);
mssOutputMap.put(output, rc);
topLevelOutputMap.put(output, new InputColumn(mssOutputs.size() - 1, output.valueType()));
}
}
for (Field field : canBeFetched) {
FetchReference fr = new FetchReference(fetchIdInput, DocReferenceConverter.toSourceLookup(rel.resolveField(field)));
allocateFetchedReference(fr, rel);
topLevelOutputMap.put(field, fr);
}
source.querySpec().outputs(qtOutputs);
} else {
int index = 0;
for (Symbol output : source.querySpec().outputs()) {
RelationColumn rc = new RelationColumn(entry.getKey(), index++, output.valueType());
mssOutputs.add(rc);
mssOutputMap.put(output, rc);
topLevelOutputMap.put(output, new InputColumn(mssOutputs.size() - 1, output.valueType()));
}
}
}
statement.querySpec().outputs(mssOutputs);
MappingSymbolVisitor.inPlace().processInplace(remainingOutputs, topLevelOutputMap);
if (statement.querySpec().orderBy().isPresent()) {
MappingSymbolVisitor.inPlace().processInplace(statement.querySpec().orderBy().get().orderBySymbols(), mssOutputMap);
}
}
use of io.crate.analyze.relations.DocTableRelation in project crate by crate.
the class DeleteStatementPlanner method planDelete.
public static Plan planDelete(DeleteAnalyzedStatement analyzedStatement, Planner.Context context) {
DocTableRelation tableRelation = analyzedStatement.analyzedRelation();
List<WhereClause> whereClauses = new ArrayList<>(analyzedStatement.whereClauses().size());
List<DocKeys.DocKey> docKeys = new ArrayList<>(analyzedStatement.whereClauses().size());
Map<Integer, Integer> itemToBulkIdx = new HashMap<>();
int bulkIdx = -1;
int itemIdx = 0;
for (WhereClause whereClause : analyzedStatement.whereClauses()) {
bulkIdx++;
if (whereClause.noMatch()) {
continue;
}
if (whereClause.docKeys().isPresent() && whereClause.docKeys().get().size() == 1) {
DocKeys.DocKey docKey = whereClause.docKeys().get().getOnlyKey();
if (docKey.id() != null) {
docKeys.add(docKey);
itemToBulkIdx.put(itemIdx, bulkIdx);
itemIdx++;
}
} else if (!whereClause.noMatch()) {
whereClauses.add(whereClause);
}
}
if (!docKeys.isEmpty()) {
return new ESDelete(context.jobId(), context.nextExecutionPhaseId(), tableRelation.tableInfo(), docKeys, itemToBulkIdx, analyzedStatement.whereClauses().size());
} else if (!whereClauses.isEmpty()) {
return deleteByQuery(tableRelation.tableInfo(), whereClauses, context);
}
return new NoopPlan(context.jobId());
}
use of io.crate.analyze.relations.DocTableRelation in project crate by crate.
the class DeleteAnalyzerTest method testDeleteWhere.
@Test
public void testDeleteWhere() throws Exception {
AnalyzedDeleteStatement delete = e.analyze("delete from users where name='Trillian'");
DocTableRelation tableRelation = delete.relation();
TableInfo tableInfo = tableRelation.tableInfo();
assertThat(USER_TABLE_IDENT, equalTo(tableInfo.ident()));
assertThat(tableInfo.rowGranularity(), is(RowGranularity.DOC));
assertThat(delete.query(), isFunction(EqOperator.NAME, isReference("name"), isLiteral("Trillian")));
}
use of io.crate.analyze.relations.DocTableRelation in project crate by crate.
the class AbstractTableFunctionsTest method prepareFunctions.
@Before
public void prepareFunctions() {
DocTableRelation relation = mock(DocTableRelation.class);
RelationName relationName = new RelationName(null, "t");
when(relation.getField(any(ColumnIdent.class), any(Operation.class), any(Boolean.class))).thenReturn(new Reference(new ReferenceIdent(relationName, "name"), RowGranularity.NODE, DataTypes.STRING, 0, null));
sqlExpressions = new SqlExpressions(Map.of(relationName, relation));
}
Aggregations