use of org.apache.calcite.linq4j.function.Function1 in project calcite by apache.
the class EnumerableTableScan method toRows.
private Expression toRows(PhysType physType, Expression expression) {
if (physType.getFormat() == JavaRowFormat.SCALAR && Object[].class.isAssignableFrom(elementType) && getRowType().getFieldCount() == 1 && (table.unwrap(ScannableTable.class) != null || table.unwrap(FilterableTable.class) != null || table.unwrap(ProjectableFilterableTable.class) != null)) {
return Expressions.call(BuiltInMethod.SLICE0.method, expression);
}
JavaRowFormat oldFormat = format();
if (physType.getFormat() == oldFormat && !hasCollectionField(rowType)) {
return expression;
}
final ParameterExpression row_ = Expressions.parameter(elementType, "row");
final int fieldCount = table.getRowType().getFieldCount();
List<Expression> expressionList = new ArrayList<>(fieldCount);
for (int i = 0; i < fieldCount; i++) {
expressionList.add(fieldExpression(row_, i, physType, oldFormat));
}
return Expressions.call(expression, BuiltInMethod.SELECT.method, Expressions.lambda(Function1.class, physType.record(expressionList), row_));
}
use of org.apache.calcite.linq4j.function.Function1 in project calcite by apache.
the class CalcitePrepareImpl method prepare_.
<T> CalciteSignature<T> prepare_(Context context, Query<T> query, Type elementType, long maxRowCount) {
if (SIMPLE_SQLS.contains(query.sql)) {
return simplePrepare(context, query.sql);
}
final JavaTypeFactory typeFactory = context.getTypeFactory();
CalciteCatalogReader catalogReader = new CalciteCatalogReader(context.getRootSchema(), context.getDefaultSchemaPath(), typeFactory, context.config());
final List<Function1<Context, RelOptPlanner>> plannerFactories = createPlannerFactories();
if (plannerFactories.isEmpty()) {
throw new AssertionError("no planner factories");
}
RuntimeException exception = Util.FoundOne.NULL;
for (Function1<Context, RelOptPlanner> plannerFactory : plannerFactories) {
final RelOptPlanner planner = plannerFactory.apply(context);
if (planner == null) {
throw new AssertionError("factory returned null planner");
}
try {
return prepare2_(context, query, elementType, maxRowCount, catalogReader, planner);
} catch (RelOptPlanner.CannotPlanException e) {
exception = e;
}
}
throw exception;
}
use of org.apache.calcite.linq4j.function.Function1 in project calcite by apache.
the class SqlUtil method filterRoutinesByParameterType.
/**
* @see Glossary#SQL99 SQL:1999 Part 2 Section 10.4 Syntax Rule 6.b.iii.2.B
*/
private static Iterator<SqlOperator> filterRoutinesByParameterType(SqlSyntax syntax, final Iterator<SqlOperator> routines, final List<RelDataType> argTypes, final List<String> argNames) {
if (syntax != SqlSyntax.FUNCTION) {
return routines;
}
// noinspection unchecked
return (Iterator) Iterators.filter(Iterators.filter(routines, SqlFunction.class), new PredicateImpl<SqlFunction>() {
public boolean test(SqlFunction function) {
List<RelDataType> paramTypes = function.getParamTypes();
if (paramTypes == null) {
// no parameter information for builtins; keep for now
return true;
}
final List<RelDataType> permutedArgTypes;
if (argNames != null) {
// Arguments passed by name. Make sure that the function has
// parameters of all of these names.
final Map<Integer, Integer> map = new HashMap<>();
for (Ord<String> argName : Ord.zip(argNames)) {
final int i = function.getParamNames().indexOf(argName.e);
if (i < 0) {
return false;
}
map.put(i, argName.i);
}
permutedArgTypes = Functions.generate(paramTypes.size(), new Function1<Integer, RelDataType>() {
public RelDataType apply(Integer a0) {
if (map.containsKey(a0)) {
return argTypes.get(map.get(a0));
} else {
return null;
}
}
});
} else {
permutedArgTypes = Lists.newArrayList(argTypes);
while (permutedArgTypes.size() < argTypes.size()) {
paramTypes.add(null);
}
}
for (Pair<RelDataType, RelDataType> p : Pair.zip(paramTypes, permutedArgTypes)) {
final RelDataType argType = p.right;
final RelDataType paramType = p.left;
if (argType != null && !SqlTypeUtil.canCastFrom(paramType, argType, false)) {
return false;
}
}
return true;
}
});
}
use of org.apache.calcite.linq4j.function.Function1 in project streamline by hortonworks.
the class RexNodeToJavaCodeCompiler method compileToBlock.
private BlockBuilder compileToBlock(final RexProgram program, ParameterExpression context_, ParameterExpression outputValues_) {
RelDataType inputRowType = program.getInputRowType();
final BlockBuilder builder = new BlockBuilder();
final JavaTypeFactoryImpl javaTypeFactory = new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
final RexToLixTranslator.InputGetter inputGetter = new RexToLixTranslator.InputGetterImpl(ImmutableList.of(Pair.<Expression, PhysType>of(Expressions.field(context_, BuiltInMethod.CONTEXT_VALUES.field), PhysTypeImpl.of(javaTypeFactory, inputRowType, JavaRowFormat.ARRAY, false))));
final Function1<String, RexToLixTranslator.InputGetter> correlates = new Function1<String, RexToLixTranslator.InputGetter>() {
public RexToLixTranslator.InputGetter apply(String a0) {
throw new UnsupportedOperationException();
}
};
final Expression root = Expressions.field(context_, BuiltInMethod.CONTEXT_ROOT.field);
final List<Expression> list = RexToLixTranslator.translateProjects(program, javaTypeFactory, builder, null, root, inputGetter, correlates);
for (int i = 0; i < list.size(); i++) {
builder.add(Expressions.statement(Expressions.assign(Expressions.arrayIndex(outputValues_, Expressions.constant(i)), list.get(i))));
}
return builder;
}
use of org.apache.calcite.linq4j.function.Function1 in project storm by apache.
the class RexNodeToJavaCodeCompiler method compileToBlock.
private BlockBuilder compileToBlock(final RexProgram program, ParameterExpression context, ParameterExpression outputValues) {
RelDataType inputRowType = program.getInputRowType();
final BlockBuilder builder = new BlockBuilder();
final JavaTypeFactoryImpl javaTypeFactory = new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
final RexToLixTranslator.InputGetter inputGetter = new RexToLixTranslator.InputGetterImpl(ImmutableList.of(Pair.<Expression, PhysType>of(Expressions.field(context, BuiltInMethod.CONTEXT_VALUES.field), PhysTypeImpl.of(javaTypeFactory, inputRowType, JavaRowFormat.ARRAY, false))));
final Function1<String, RexToLixTranslator.InputGetter> correlates = new Function1<String, RexToLixTranslator.InputGetter>() {
@Override
public RexToLixTranslator.InputGetter apply(String a0) {
throw new UnsupportedOperationException();
}
};
final Expression root = Expressions.field(context, BuiltInMethod.CONTEXT_ROOT.field);
final List<Expression> list = RexToLixTranslator.translateProjects(program, javaTypeFactory, builder, null, root, inputGetter, correlates);
for (int i = 0; i < list.size(); i++) {
builder.add(Expressions.statement(Expressions.assign(Expressions.arrayIndex(outputValues, Expressions.constant(i)), list.get(i))));
}
return builder;
}
Aggregations