use of io.crate.testing.SQLExecutor in project crate by crate.
the class FetchRewriteTest method test_fetchrewrite_on_rename_puts_fetch_marker_into_alias_scope.
@Test
public void test_fetchrewrite_on_rename_puts_fetch_marker_into_alias_scope() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table tbl (x int)").build();
DocTableInfo tableInfo = e.resolveTableInfo("tbl");
Reference x = (Reference) e.asSymbol("x");
var relation = new DocTableRelation(tableInfo);
var alias = new AliasedAnalyzedRelation(relation, new RelationName(null, "t1"));
var collect = new Collect(relation, List.of(x), WhereClause.MATCH_ALL, 1L, DataTypes.INTEGER.fixedSize());
Symbol t1X = alias.getField(x.column(), Operation.READ, true);
assertThat(t1X, Matchers.notNullValue());
var rename = new Rename(List.of(t1X), alias.relationName(), alias, collect);
FetchRewrite fetchRewrite = rename.rewriteToFetch(new TableStats(), List.of());
assertThat(fetchRewrite, Matchers.notNullValue());
LogicalPlan newRename = fetchRewrite.newPlan();
assertThat(newRename, isPlan("Rename[t1._fetchid] AS t1\n" + " └ Collect[doc.tbl | [_fetchid] | true]"));
assertThat("fetchRewrite replacedOutputs.keySet() must always match the outputs of the operator prior to the rewrite", List.copyOf(fetchRewrite.replacedOutputs().keySet()), is(rename.outputs()));
assertThat(fetchRewrite.replacedOutputs(), Matchers.hasEntry(isField("x", alias.relationName()), isFetchStub("_doc['x']")));
assertThat(newRename.outputs(), contains(isFetchMarker(alias.relationName(), contains(isReference("_doc['x']")))));
FetchStub fetchStub = (FetchStub) fetchRewrite.replacedOutputs().entrySet().iterator().next().getValue();
assertThat("FetchStub fetchMarker must be changed to the aliased marker", fetchStub.fetchMarker(), Matchers.sameInstance(newRename.outputs().get(0)));
}
use of io.crate.testing.SQLExecutor in project crate by crate.
the class MapBackedSymbolReplacerTest method test_convert_symbol_by_traversing.
@Test
public void test_convert_symbol_by_traversing() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table tbl (x int)").build();
Symbol x = e.asSymbol("x");
Symbol x_source_lookup = e.asSymbol("_doc['x']");
Symbol alias = new AliasSymbol("a_alias", x);
Symbol alias_source_lookup = new AliasSymbol("a_alias", x_source_lookup);
// Mapping does not contain the requested symbol, but a child of it. Fallback to symbol traversing.
Map<Symbol, Symbol> mapping = Map.of(x, x_source_lookup);
assertThat(MapBackedSymbolReplacer.convert(alias, mapping), is(alias_source_lookup));
}
use of io.crate.testing.SQLExecutor in project crate by crate.
the class SelectivityFunctionsCalculationTest method test_group_operator_adapt_expected_row_count_based_on_column_stats.
@Test
public void test_group_operator_adapt_expected_row_count_based_on_column_stats() throws Throwable {
var samples = IntStream.concat(IntStream.generate(() -> 10).limit(50), IntStream.generate(() -> 20).limit(50)).boxed().collect(Collectors.toList());
long numDocs = 2_000L;
Stats stats = new Stats(numDocs, DataTypes.INTEGER.fixedSize(), Map.of(new ColumnIdent("x"), ColumnStats.fromSortedValues(samples, DataTypes.INTEGER, 0, numDocs)));
TableStats tableStats = new TableStats();
tableStats.updateTableStats(Map.of(new RelationName("doc", "tbl"), stats));
SQLExecutor e = SQLExecutor.builder(clusterService).setTableStats(tableStats).addTable("create table doc.tbl (x int)").build();
LogicalPlan plan = e.logicalPlan("select x, count(*) from doc.tbl group by x");
assertThat(plan.numExpectedRows(), Matchers.is(2L));
}
use of io.crate.testing.SQLExecutor in project crate by crate.
the class OptimizerTest method test_cast_is_not_swapped_when_column_explicitly_casted.
@Test
public void test_cast_is_not_swapped_when_column_explicitly_casted() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table tbl (strCol string, intCol int)").build();
Symbol symbol = Optimizer.optimizeCasts(e.asSymbol("strCol::bigint > 3"), e.getPlannerContext(clusterService.state()));
assertThat(symbol, SymbolMatchers.isFunction(GtOperator.NAME, SymbolMatchers.isFunction("cast"), SymbolMatchers.isLiteral(3L)));
}
use of io.crate.testing.SQLExecutor in project crate by crate.
the class SymbolToColumnDefinitionConverterTest method testScopedNameToColumnDefinition.
@Test
public void testScopedNameToColumnDefinition() throws IOException {
String createTableStmt = "create table tbl (" + " col_default_object object as (" + " col_nested_integer integer," + " col_nested_object object as (" + " col_nested_timestamp_with_time_zone timestamp with time zone" + " )" + " )" + ")";
SQLExecutor e = SQLExecutor.builder(clusterService).addTable(createTableStmt).build();
String selectStmt = "select A.col_default_object['col_nested_integer'], " + " A.col_default_object['col_nested_object']['col_nested_timestamp_with_time_zone'], " + " A.col_default_object['col_nested_object'] " + "from " + " (select " + " col_default_object['col_nested_integer'], " + " col_default_object['col_nested_object']['col_nested_timestamp_with_time_zone'], " + " col_default_object['col_nested_object']" + " from tbl) as A";
var analyzedRelation = e.analyze(selectStmt);
var actual = Lists2.map(analyzedRelation.outputs(), Symbols::toColumnDefinition);
assertThat(actual, containsInAnyOrder(isColumnDefinition("col_default_object['col_nested_integer']", isColumnType(DataTypes.INTEGER.getName())), isColumnDefinition("col_default_object['col_nested_object']['col_nested_timestamp_with_time_zone']", isColumnType(DataTypes.TIMESTAMPZ.getName())), isColumnDefinition("col_default_object['col_nested_object']", isObjectColumnType(DataTypes.UNTYPED_OBJECT.getName(), isColumnPolicy(OBJECT_TYPE_DEFAULT_COLUMN_POLICY), contains(isColumnDefinition("col_nested_timestamp_with_time_zone", isColumnType(DataTypes.TIMESTAMPZ.getName())))))));
}
Aggregations