use of io.crate.analyze.symbol.Function in project crate by crate.
the class QueriedDocTableFetchPushDownTest method testPushDownWithNestedOrderInOutput.
@Test
public void testPushDownWithNestedOrderInOutput() 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(funcOfI), 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']), INPUT(1) 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"));
}
use of io.crate.analyze.symbol.Function in project crate by crate.
the class PlannerTest method testAllocateRouting.
@Test
public void testAllocateRouting() throws Exception {
TableIdent custom = new TableIdent("custom", "t1");
TableInfo tableInfo1 = TestingTableInfo.builder(custom, shardRouting("t1")).add("id", DataTypes.INTEGER, null).build();
TableInfo tableInfo2 = TestingTableInfo.builder(custom, shardRoutingForReplicas("t1")).add("id", DataTypes.INTEGER, null).build();
Planner.Context plannerContext = new Planner.Context(e.planner, clusterService, UUID.randomUUID(), null, normalizer, new TransactionContext(SessionContext.SYSTEM_SESSION), 0, 0);
WhereClause whereClause = new WhereClause(new Function(new FunctionInfo(new FunctionIdent(EqOperator.NAME, Arrays.<DataType>asList(DataTypes.INTEGER, DataTypes.INTEGER)), DataTypes.BOOLEAN), Arrays.asList(tableInfo1.getReference(new ColumnIdent("id")), Literal.of(2))));
plannerContext.allocateRouting(tableInfo1, WhereClause.MATCH_ALL, null);
plannerContext.allocateRouting(tableInfo2, whereClause, null);
// 2 routing allocations with different where clause must result in 2 allocated routings
java.lang.reflect.Field tableRoutings = Planner.Context.class.getDeclaredField("tableRoutings");
tableRoutings.setAccessible(true);
Multimap<TableIdent, Planner.TableRouting> routing = (Multimap<TableIdent, Planner.TableRouting>) tableRoutings.get(plannerContext);
assertThat(routing.size(), is(2));
// The routings must be the same after merging the locations
Iterator<Planner.TableRouting> iterator = routing.values().iterator();
Routing routing1 = iterator.next().routing;
Routing routing2 = iterator.next().routing;
assertThat(routing1, is(routing2));
}
use of io.crate.analyze.symbol.Function in project crate by crate.
the class RelationAnalyzer method visitTableFunction.
@Override
public AnalyzedRelation visitTableFunction(TableFunction node, StatementAnalysisContext statementContext) {
RelationAnalysisContext context = statementContext.currentRelationContext();
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, statementContext.sessionContext(), statementContext.convertParamFunction(), new FieldProvider() {
@Override
public Symbol resolveField(QualifiedName qualifiedName, Operation operation) {
throw new UnsupportedOperationException("Can only resolve literals");
}
@Override
public Symbol resolveField(QualifiedName qualifiedName, @Nullable List path, Operation operation) {
throw new UnsupportedOperationException("Can only resolve literals");
}
}, null);
Function function = (Function) expressionAnalyzer.convert(node.functionCall(), context.expressionAnalysisContext());
FunctionImplementation functionImplementation = functions.getSafe(function.info().ident());
if (functionImplementation.info().type() != FunctionInfo.Type.TABLE) {
throw new UnsupportedFeatureException("Non table function " + function.info().ident().name() + " is not supported in from clause");
}
TableFunctionImplementation tableFunction = (TableFunctionImplementation) functionImplementation;
TableInfo tableInfo = tableFunction.createTableInfo(clusterService);
Operation.blockedRaiseException(tableInfo, statementContext.currentOperation());
TableRelation tableRelation = new TableFunctionRelation(tableInfo, tableFunction, function);
context.addSourceRelation(node.name(), tableRelation);
return tableRelation;
}
use of io.crate.analyze.symbol.Function in project crate by crate.
the class InsertFromSubQueryAnalyzerTest method testImplicitTypeCasting.
@Test
public void testImplicitTypeCasting() throws Exception {
InsertFromSubQueryAnalyzedStatement statement = e.analyze("insert into users (id, name, shape) (" + " select id, other_id, name from users " + " where name = 'Trillian'" + ")");
List<Symbol> outputSymbols = statement.subQueryRelation().querySpec().outputs();
assertThat(statement.columns().size(), is(outputSymbols.size()));
assertThat(outputSymbols.get(1), instanceOf(Function.class));
Function castFunction = (Function) outputSymbols.get(1);
assertThat(castFunction, isFunction(CastFunctionResolver.FunctionNames.TO_STRING));
Function geoCastFunction = (Function) outputSymbols.get(2);
assertThat(geoCastFunction, isFunction(CastFunctionResolver.FunctionNames.TO_GEO_SHAPE));
}
use of io.crate.analyze.symbol.Function in project crate by crate.
the class DeleteAnalyzerTest method testDeleteWhere.
@Test
public void testDeleteWhere() throws Exception {
DeleteAnalyzedStatement statement = e.analyze("delete from users where name='Trillian'");
DocTableRelation tableRelation = statement.analyzedRelation;
TableInfo tableInfo = tableRelation.tableInfo();
assertThat(USER_TABLE_IDENT, equalTo(tableInfo.ident()));
assertThat(tableInfo.rowGranularity(), is(RowGranularity.DOC));
Function whereClause = (Function) statement.whereClauses.get(0).query();
assertEquals(EqOperator.NAME, whereClause.info().ident().name());
assertFalse(whereClause.info().type() == FunctionInfo.Type.AGGREGATE);
assertThat(whereClause.arguments().get(0), isReference("name"));
assertThat(whereClause.arguments().get(1), isLiteral("Trillian"));
}
Aggregations