use of io.crate.analyze.relations.QueriedDocTable 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.QueriedDocTable in project crate by crate.
the class SubSelectAnalyzerTest method testSimpleSubSelect.
@Test
public void testSimpleSubSelect() throws Exception {
SelectAnalyzedStatement statement = analyze("select aliased_sub.x / aliased_sub.i from (select x, i from t1) as aliased_sub");
QueriedDocTable relation = (QueriedDocTable) statement.relation();
assertThat(relation.fields().size(), is(1));
assertThat(relation.fields().get(0), isField("(x / i)"));
assertThat(relation.tableRelation().tableInfo(), is(T1_INFO));
}
use of io.crate.analyze.relations.QueriedDocTable in project crate by crate.
the class QueriedDocTableFetchPushDownTest method testScoreGetsPushedDown.
@Test
public void testScoreGetsPushedDown() throws Exception {
QuerySpec qs = new QuerySpec();
qs.outputs(Lists.newArrayList(REF_A, REF_I, REF_SCORE));
qs.orderBy(new OrderBy(Lists.newArrayList(REF_I), new boolean[] { true }, new Boolean[] { false }));
QueriedDocTableFetchPushDown pd = new QueriedDocTableFetchPushDown(new QueriedDocTable(TABLE_REL, qs));
QueriedDocTable sub = pd.pushDown();
assertThat(qs, isSQL("SELECT FETCH(INPUT(0), s.t._doc['a']), INPUT(1), INPUT(2) ORDER BY INPUT(1) DESC NULLS LAST"));
assertThat(sub.querySpec(), isSQL("SELECT s.t._fetchid, s.t.i, s.t._score ORDER BY s.t.i DESC NULLS LAST"));
}
use of io.crate.analyze.relations.QueriedDocTable in project crate by crate.
the class QueriedDocTableFetchPushDownTest method testPushDownOrderRefUsedInFunction.
@Test
public void testPushDownOrderRefUsedInFunction() throws Exception {
QuerySpec qs = new QuerySpec();
Function funcOfI = abs(REF_I);
qs.outputs(Lists.newArrayList(REF_A, REF_I, funcOfI));
qs.orderBy(new OrderBy(Lists.newArrayList(REF_I), new boolean[] { true }, new Boolean[] { false }));
QueriedDocTableFetchPushDown pd = new QueriedDocTableFetchPushDown(new QueriedDocTable(TABLE_REL, qs));
QueriedDocTable sub = pd.pushDown();
assertThat(qs, isSQL("SELECT FETCH(INPUT(0), s.t._doc['a']), INPUT(1), abs(INPUT(1)) ORDER BY INPUT(1) DESC NULLS LAST"));
assertThat(sub.querySpec(), isSQL("SELECT s.t._fetchid, s.t.i ORDER BY s.t.i DESC NULLS LAST"));
}
use of io.crate.analyze.relations.QueriedDocTable in project crate by crate.
the class QueriedDocTableFetchPushDownTest method testPushDownWithNestedOrder.
@Test
public void testPushDownWithNestedOrder() throws Exception {
QuerySpec qs = new QuerySpec();
qs.outputs(Lists.newArrayList(REF_A, REF_I));
qs.orderBy(new OrderBy(Lists.newArrayList(abs(REF_I)), new boolean[] { true }, new Boolean[] { false }));
QueriedDocTableFetchPushDown pd = new QueriedDocTableFetchPushDown(new QueriedDocTable(TABLE_REL, qs));
QueriedDocTable sub = pd.pushDown();
assertThat(qs, isSQL("SELECT FETCH(INPUT(0), s.t._doc['a']), FETCH(INPUT(0), s.t._doc['i']) ORDER BY INPUT(1) DESC NULLS LAST"));
assertThat(sub.querySpec(), isSQL("SELECT s.t._fetchid, abs(s.t.i) ORDER BY abs(s.t.i) DESC NULLS LAST"));
}
Aggregations