use of org.apache.calcite.linq4j.function.Function1 in project calcite by apache.
the class CassandraTable method query.
/**
* Executes a CQL query on the underlying table.
*
* @param session Cassandra session
* @param fields List of fields to project
* @param predicates A list of predicates which should be used in the query
* @return Enumerator of results
*/
public Enumerable<Object> query(final Session session, List<Map.Entry<String, Class>> fields, final List<Map.Entry<String, String>> selectFields, List<String> predicates, List<String> order, final Integer offset, final Integer fetch) {
// Build the type of the resulting row based on the provided fields
final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
final RelDataType rowType = getRowType(typeFactory);
Function1<String, Void> addField = new Function1<String, Void>() {
public Void apply(String fieldName) {
SqlTypeName typeName = rowType.getField(fieldName, true, false).getType().getSqlTypeName();
fieldInfo.add(fieldName, typeFactory.createSqlType(typeName)).nullable(true);
return null;
}
};
if (selectFields.isEmpty()) {
for (Map.Entry<String, Class> field : fields) {
addField.apply(field.getKey());
}
} else {
for (Map.Entry<String, String> field : selectFields) {
addField.apply(field.getKey());
}
}
final RelProtoDataType resultRowType = RelDataTypeImpl.proto(fieldInfo.build());
// Construct the list of fields to project
final String selectString;
if (selectFields.isEmpty()) {
selectString = "*";
} else {
selectString = Util.toString(new Iterable<String>() {
public Iterator<String> iterator() {
final Iterator<Map.Entry<String, String>> selectIterator = selectFields.iterator();
return new Iterator<String>() {
@Override
public boolean hasNext() {
return selectIterator.hasNext();
}
@Override
public String next() {
Map.Entry<String, String> entry = selectIterator.next();
return entry.getKey() + " AS " + entry.getValue();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}, "", ", ", "");
}
// Combine all predicates conjunctively
String whereClause = "";
if (!predicates.isEmpty()) {
whereClause = " WHERE ";
whereClause += Util.toString(predicates, "", " AND ", "");
}
// Build and issue the query and return an Enumerator over the results
StringBuilder queryBuilder = new StringBuilder("SELECT ");
queryBuilder.append(selectString);
queryBuilder.append(" FROM \"" + columnFamily + "\"");
queryBuilder.append(whereClause);
if (!order.isEmpty()) {
queryBuilder.append(Util.toString(order, " ORDER BY ", ", ", ""));
}
int limit = offset;
if (fetch >= 0) {
limit += fetch;
}
if (limit > 0) {
queryBuilder.append(" LIMIT " + limit);
}
queryBuilder.append(" ALLOW FILTERING");
final String query = queryBuilder.toString();
return new AbstractEnumerable<Object>() {
public Enumerator<Object> enumerator() {
final ResultSet results = session.execute(query);
// Skip results until we get to the right offset
int skip = 0;
Enumerator<Object> enumerator = new CassandraEnumerator(results, resultRowType);
while (skip < offset && enumerator.moveNext()) {
skip++;
}
return enumerator;
}
};
}
use of org.apache.calcite.linq4j.function.Function1 in project calcite by apache.
the class TableScanNode method createEnumerable.
private static TableScanNode createEnumerable(Compiler compiler, TableScan rel, Enumerable<Row> enumerable, final ImmutableIntList acceptedProjects, List<RexNode> rejectedFilters, final ImmutableIntList rejectedProjects) {
if (!rejectedFilters.isEmpty()) {
final RexNode filter = RexUtil.composeConjunction(rel.getCluster().getRexBuilder(), rejectedFilters, false);
assert filter != null;
// Re-map filter for the projects that have been applied already
final RexNode filter2;
final RelDataType inputRowType;
if (acceptedProjects == null) {
filter2 = filter;
inputRowType = rel.getRowType();
} else {
final Mapping mapping = Mappings.target(acceptedProjects, rel.getTable().getRowType().getFieldCount());
filter2 = RexUtil.apply(mapping, filter);
final RelDataTypeFactory.Builder builder = rel.getCluster().getTypeFactory().builder();
final List<RelDataTypeField> fieldList = rel.getTable().getRowType().getFieldList();
for (int acceptedProject : acceptedProjects) {
builder.add(fieldList.get(acceptedProject));
}
inputRowType = builder.build();
}
final Scalar condition = compiler.compile(ImmutableList.of(filter2), inputRowType);
final Context context = compiler.createContext();
enumerable = enumerable.where(new Predicate1<Row>() {
@Override
public boolean apply(Row row) {
context.values = row.getValues();
Boolean b = (Boolean) condition.execute(context);
return b != null && b;
}
});
}
if (rejectedProjects != null) {
enumerable = enumerable.select(new Function1<Row, Row>() {
final Object[] values = new Object[rejectedProjects.size()];
@Override
public Row apply(Row row) {
final Object[] inValues = row.getValues();
for (int i = 0; i < rejectedProjects.size(); i++) {
values[i] = inValues[rejectedProjects.get(i)];
}
return Row.asCopy(values);
}
});
}
return new TableScanNode(compiler, rel, enumerable);
}
use of org.apache.calcite.linq4j.function.Function1 in project calcite by apache.
the class JaninoRexCompiler method compile.
public Scalar compile(List<RexNode> nodes, RelDataType inputRowType) {
final RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
for (RexNode node : nodes) {
programBuilder.addProject(node, null);
}
final RexProgram program = programBuilder.getProgram();
final BlockBuilder builder = new BlockBuilder();
final ParameterExpression context_ = Expressions.parameter(Context.class, "context");
final ParameterExpression outputValues_ = Expressions.parameter(Object[].class, "outputValues");
final JavaTypeFactoryImpl javaTypeFactory = new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
// public void execute(Context, Object[] outputValues)
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 baz(context_, outputValues_, builder.toBlock());
}
use of org.apache.calcite.linq4j.function.Function1 in project calcite by apache.
the class ResultSetEnumerable method primitiveRowBuilderFactory.
private static Function1<ResultSet, Function0<Object>> primitiveRowBuilderFactory(final Primitive[] primitives) {
return new Function1<ResultSet, Function0<Object>>() {
public Function0<Object> apply(final ResultSet resultSet) {
final ResultSetMetaData metaData;
final int columnCount;
try {
metaData = resultSet.getMetaData();
columnCount = metaData.getColumnCount();
} catch (SQLException e) {
throw new RuntimeException(e);
}
assert columnCount == primitives.length;
if (columnCount == 1) {
return new Function0<Object>() {
public Object apply() {
try {
return resultSet.getObject(1);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
};
}
// noinspection unchecked
return (Function0) new Function0<Object[]>() {
public Object[] apply() {
try {
final List<Object> list = new ArrayList<Object>();
for (int i = 0; i < columnCount; i++) {
list.add(primitives[i].jdbcGet(resultSet, i + 1));
}
return list.toArray();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
};
}
};
}
use of org.apache.calcite.linq4j.function.Function1 in project calcite by apache.
the class PhysTypeImpl method generateCollationKey.
public Pair<Expression, Expression> generateCollationKey(final List<RelFieldCollation> collations) {
final Expression selector;
if (collations.size() == 1) {
RelFieldCollation collation = collations.get(0);
ParameterExpression parameter = Expressions.parameter(javaRowClass, "v");
selector = Expressions.lambda(Function1.class, fieldReference(parameter, collation.getFieldIndex()), parameter);
return Pair.<Expression, Expression>of(selector, Expressions.call(BuiltInMethod.NULLS_COMPARATOR.method, Expressions.constant(collation.nullDirection == RelFieldCollation.NullDirection.FIRST), Expressions.constant(collation.getDirection() == RelFieldCollation.Direction.DESCENDING)));
}
selector = Expressions.call(BuiltInMethod.IDENTITY_SELECTOR.method);
// int c;
// c = Utilities.compare(v0, v1);
// if (c != 0) return c; // or -c if descending
// ...
// return 0;
BlockBuilder body = new BlockBuilder();
final ParameterExpression parameterV0 = Expressions.parameter(javaRowClass, "v0");
final ParameterExpression parameterV1 = Expressions.parameter(javaRowClass, "v1");
final ParameterExpression parameterC = Expressions.parameter(int.class, "c");
final int mod = collations.size() == 1 ? Modifier.FINAL : 0;
body.add(Expressions.declare(mod, parameterC, null));
for (RelFieldCollation collation : collations) {
final int index = collation.getFieldIndex();
Expression arg0 = fieldReference(parameterV0, index);
Expression arg1 = fieldReference(parameterV1, index);
switch(Primitive.flavor(fieldClass(index))) {
case OBJECT:
arg0 = Types.castIfNecessary(Comparable.class, arg0);
arg1 = Types.castIfNecessary(Comparable.class, arg1);
}
final boolean nullsFirst = collation.nullDirection == RelFieldCollation.NullDirection.FIRST;
final boolean descending = collation.getDirection() == RelFieldCollation.Direction.DESCENDING;
final Method method = (fieldNullable(index) ? (nullsFirst ^ descending ? BuiltInMethod.COMPARE_NULLS_FIRST : BuiltInMethod.COMPARE_NULLS_LAST) : BuiltInMethod.COMPARE).method;
body.add(Expressions.statement(Expressions.assign(parameterC, Expressions.call(method.getDeclaringClass(), method.getName(), arg0, arg1))));
body.add(Expressions.ifThen(Expressions.notEqual(parameterC, Expressions.constant(0)), Expressions.return_(null, descending ? Expressions.negate(parameterC) : parameterC)));
}
body.add(Expressions.return_(null, Expressions.constant(0)));
final List<MemberDeclaration> memberDeclarations = Expressions.<MemberDeclaration>list(Expressions.methodDecl(Modifier.PUBLIC, int.class, "compare", ImmutableList.of(parameterV0, parameterV1), body.toBlock()));
if (EnumerableRules.BRIDGE_METHODS) {
final ParameterExpression parameterO0 = Expressions.parameter(Object.class, "o0");
final ParameterExpression parameterO1 = Expressions.parameter(Object.class, "o1");
BlockBuilder bridgeBody = new BlockBuilder();
bridgeBody.add(Expressions.return_(null, Expressions.call(Expressions.parameter(Comparable.class, "this"), BuiltInMethod.COMPARATOR_COMPARE.method, Expressions.convert_(parameterO0, javaRowClass), Expressions.convert_(parameterO1, javaRowClass))));
memberDeclarations.add(overridingMethodDecl(BuiltInMethod.COMPARATOR_COMPARE.method, ImmutableList.of(parameterO0, parameterO1), bridgeBody.toBlock()));
}
return Pair.<Expression, Expression>of(selector, Expressions.new_(Comparator.class, Collections.<Expression>emptyList(), memberDeclarations));
}
Aggregations