Skip to content

Commit

Permalink
[CALCITE-2259] Allow Java 8 syntax
Browse files Browse the repository at this point in the history
In summary: use lambdas where possible, switch from Guava function
types to Java function types or lambdas, but continue to use Guava
components (such as immutable collections and cache) that have no
equivalent in the Java runtime.

1. Change single-abstract-method (SAM) classes to lambdas. Preserve
formatting wherever possible.

2. Change AssertQuery.returns argument type from Guava Function to Java
Consumer. If you are using a lambda and see 'returns is deprecated',
remove the 'return null;' line, and the lambda will become a Consumer
(whose return is void).

3. Change RelOptRuleOperand and RelOptRule.operand methods to take Java
Predicate rather than Guava Predicate.

4. Change the argument of Hook.add and .addThread from Guava Function to
Java Consumer.

5. Change 'list.toArray(new T[list.size()])' to 'list.toArray(new T[0])'
because the latter is simpler, and just as efficient on recent Java
versions.

6. Resource references; change "try (Closeable ignore = foo())" to "try
  (foo())", especially uses of TryThreadLocal and Hook.Closeable.

7. Convert linq4j Function1 to java Function, Function2 to java BiFunction

8. Fix occurrences of Intellij's "Explicit type can be replaced with
<>" inspection. (Occurs for "List<String> list = new
ArrayList<String>();".)

9. Change Guava Preconditions.checkNotNull to Java
Objects.requireNonNull. (Kevin Risden)

10. Break out anonymous classes and fix dependency problems.

11. Use CacheLoader.of(Function) where possible.

12. Replace sub-classes of ThreadLocal with ThreadLocal.withInitial().

13. Replace Guava collection methods with calls to Java collection types,
for example replace Lists.newArrayList() with new ArrayList<>(),
Maps.newHashSet() with new HashSet<>(), similarly Sets.

14. Replace Guava Joiner with String.join.

15. Replace Collections.emptyList() with ImmutableList.of() in a few
places.

For backwards compatibility, we preserved (and deprecated) the old
methods that used Guava types. In a few cases where new and old have
the same signature (after erasure), we could not add a method with the
same name, so we gave the new method a "J" suffix. Examples include
Hook.property and .propertyJ, RelOptRule.operand and .operandJ.

In test code, we have not slavishly ensured backwards compatibility.

We do not intend to remove uses of Guava's immutable collections.

We have ignored Intellij's "Pseudo functional style code" inspection
most of the time, but in a few cases have converted Lists.transform(),
Iterables.transform(), and Iterables.filter() into Java streams. Use
the Util.toImmutableList() collector if the result is to be an
immutable list. Use Util.transform() rather than Lists.transform()
if you have a Java function rather than a Guava function or lambda.

Not covered in this change (might be done in future):
* Convert Collections.sort(list) to list.sort.
* Review uses of 'for (Map.Entry<K, V> e : map.entrySet())' and see
  whether it makes sense to convert to 'map.forEach((k, v) ->
  ...)'. Intellij inspection is called 'Replace with Map.forEach'.

Breaking changes:
* LatticeStatisticProvider.Factory, result of RexUtil.notFun(), and
  arguments to Mappings.target() are Function (was Guava, now Java)
* Argument to SqlSpecialOperator.TokenSequence.parser() is Predicate
  (was Guava, now Java)
* AggregateNode.AccumulatorFactory extends Supplier (was Guava, now Java)
  • Loading branch information
julianhyde committed Jul 8, 2018
1 parent 5bbc501 commit d59b639
Show file tree
Hide file tree
Showing 525 changed files with 7,983 additions and 10,608 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,14 @@
import org.apache.calcite.rex.RexInputRef;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.rex.RexVisitorImpl;
import org.apache.calcite.runtime.PredicateImpl;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.validate.SqlValidatorUtil;
import org.apache.calcite.util.Pair;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;

/**
* Rules and relational operators for
Expand Down Expand Up @@ -95,7 +92,7 @@ abstract static class CassandraConverterRule extends ConverterRule {

CassandraConverterRule(Class<? extends RelNode> clazz,
String description) {
this(clazz, Predicates.<RelNode>alwaysTrue(), description);
this(clazz, r -> true, description);
}

<R extends RelNode> CassandraConverterRule(Class<R> clazz,
Expand All @@ -113,13 +110,9 @@ <R extends RelNode> CassandraConverterRule(Class<R> clazz,
*/
private static class CassandraFilterRule extends RelOptRule {
private static final Predicate<LogicalFilter> PREDICATE =
new PredicateImpl<LogicalFilter>() {
public boolean test(LogicalFilter input) {
// TODO: Check for an equality predicate on the partition key
// Right now this just checks if we have a single top-level AND
return RelOptUtil.disjunctions(input.getCondition()).size() == 1;
}
};
// TODO: Check for an equality predicate on the partition key
// Right now this just checks if we have a single top-level AND
filter -> RelOptUtil.disjunctions(filter.getCondition()).size() == 1;

private static final CassandraFilterRule INSTANCE = new CassandraFilterRule();

Expand Down Expand Up @@ -268,28 +261,21 @@ public RelNode convert(RelNode rel) {
* {@link CassandraSort}.
*/
private static class CassandraSortRule extends RelOptRule {
private static final Predicate<Sort> SORT_PREDICATE =
new PredicateImpl<Sort>() {
public boolean test(Sort input) {
// Limits are handled by CassandraLimit
return input.offset == null && input.fetch == null;
}
};
private static final Predicate<CassandraFilter> FILTER_PREDICATE =
new PredicateImpl<CassandraFilter>() {
public boolean test(CassandraFilter input) {
// We can only use implicit sorting within a single partition
return input.isSinglePartition();
}
};

private static final RelOptRuleOperand CASSANDRA_OP =
operand(CassandraToEnumerableConverter.class,
operand(CassandraFilter.class, null, FILTER_PREDICATE, any()));
operandJ(CassandraFilter.class, null,
// We can only use implicit sorting within a single partition
CassandraFilter::isSinglePartition, any()));

private static final CassandraSortRule INSTANCE = new CassandraSortRule();

private CassandraSortRule() {
super(operand(Sort.class, null, SORT_PREDICATE, CASSANDRA_OP), "CassandraSortRule");
super(
operandJ(Sort.class, null,
// Limits are handled by CassandraLimit
sort -> sort.offset == null && sort.fetch == null, CASSANDRA_OP),
"CassandraSortRule");
}

public RelNode convert(Sort sort, CassandraFilter filter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.apache.calcite.avatica.util.Casing;
import org.apache.calcite.jdbc.CalciteSchema;
import org.apache.calcite.rel.RelFieldCollation;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rel.type.RelDataTypeImpl;
import org.apache.calcite.rel.type.RelDataTypeSystem;
Expand Down Expand Up @@ -50,7 +49,6 @@
import com.datastax.driver.core.MaterializedViewMetadata;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.TableMetadata;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

Expand Down Expand Up @@ -113,11 +111,8 @@ public CassandraSchema(String host, String keyspace, String username, String pas
this.parentSchema = parentSchema;
this.name = name;

this.hook = Hook.TRIMMED.add(new Function<RelNode, Void>() {
public Void apply(RelNode node) {
CassandraSchema.this.addMaterializedViews();
return null;
}
this.hook = Hook.TRIMMED.add(node -> {
CassandraSchema.this.addMaterializedViews();
});
}

Expand Down Expand Up @@ -177,19 +172,19 @@ Pair<List<String>, List<String>> getKeyFields(String columnFamily, boolean view)
}

List<ColumnMetadata> partitionKey = table.getPartitionKey();
List<String> pKeyFields = new ArrayList<String>();
List<String> pKeyFields = new ArrayList<>();
for (ColumnMetadata column : partitionKey) {
pKeyFields.add(column.getName());
}

List<ColumnMetadata> clusteringKey = table.getClusteringColumns();
List<String> cKeyFields = new ArrayList<String>();
List<String> cKeyFields = new ArrayList<>();
for (ColumnMetadata column : clusteringKey) {
cKeyFields.add(column.getName());
}

return Pair.of((List<String>) ImmutableList.copyOf(pKeyFields),
(List<String>) ImmutableList.copyOf(cKeyFields));
return Pair.of(ImmutableList.copyOf(pKeyFields),
ImmutableList.copyOf(cKeyFields));
}

/** Get the collation of all clustering key columns.
Expand All @@ -205,7 +200,7 @@ public List<RelFieldCollation> getClusteringOrder(String columnFamily, boolean v
}

List<ClusteringOrder> clusteringOrder = table.getClusteringOrder();
List<RelFieldCollation> keyCollations = new ArrayList<RelFieldCollation>();
List<RelFieldCollation> keyCollations = new ArrayList<>();

int i = 0;
for (ClusteringOrder order : clusteringOrder) {
Expand Down Expand Up @@ -237,7 +232,7 @@ private void addMaterializedViews() {
StringBuilder queryBuilder = new StringBuilder("SELECT ");

// Add all the selected columns to the query
List<String> columnNames = new ArrayList<String>();
List<String> columnNames = new ArrayList<>();
for (ColumnMetadata column : view.getColumns()) {
columnNames.add("\"" + column.getName() + "\"");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
import com.google.common.collect.ImmutableList;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -97,9 +97,8 @@ public List<RelFieldCollation> getClusteringOrder() {
}

public Enumerable<Object> query(final Session session) {
return query(session, Collections.<Map.Entry<String, Class>>emptyList(),
Collections.<Map.Entry<String, String>>emptyList(),
Collections.<String>emptyList(), Collections.<String>emptyList(), 0, -1);
return query(session, ImmutableList.of(), ImmutableList.of(),
ImmutableList.of(), ImmutableList.of(), 0, -1);
}

/** Executes a CQL query on the underlying table.
Expand All @@ -118,12 +117,12 @@ public Enumerable<Object> query(final Session session, List<Map.Entry<String, Cl
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;
}
Function1<String, Void> addField = fieldName -> {
SqlTypeName typeName =
rowType.getField(fieldName, true, false).getType().getSqlTypeName();
fieldInfo.add(fieldName, typeFactory.createSqlType(typeName))
.nullable(true);
return null;
};

if (selectFields.isEmpty()) {
Expand All @@ -143,26 +142,24 @@ public Void apply(String fieldName) {
if (selectFields.isEmpty()) {
selectString = "*";
} else {
selectString = Util.toString(new Iterable<String>() {
public Iterator<String> iterator() {
final Iterator<Map.Entry<String, String>> selectIterator =
selectFields.iterator();
selectString = Util.toString(() -> {
final Iterator<Map.Entry<String, String>> selectIterator =
selectFields.iterator();

return new Iterator<String>() {
@Override public boolean hasNext() {
return selectIterator.hasNext();
}
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 String next() {
Map.Entry<String, String> entry = selectIterator.next();
return entry.getKey() + " AS " + entry.getValue();
}

@Override public void remove() {
throw new UnsupportedOperationException();
}
};
}
@Override public void remove() {
throw new UnsupportedOperationException();
}
};
}, "", ", ", "");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.apache.calcite.util.BuiltInMethod;
import org.apache.calcite.util.Pair;

import com.google.common.base.Function;
import com.google.common.collect.Lists;

import java.util.AbstractList;
Expand Down Expand Up @@ -144,12 +143,7 @@ private static <T> MethodCallExpression constantArrayList(List<T> values,
/** E.g. {@code constantList("x", "y")} returns
* {@code {ConstantExpression("x"), ConstantExpression("y")}}. */
private static <T> List<Expression> constantList(List<T> values) {
return Lists.transform(values,
new Function<T, Expression>() {
public Expression apply(T a0) {
return Expressions.constant(a0);
}
});
return Lists.transform(values, Expressions::constant);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.calcite.rel.core.RelFactories;
import org.apache.calcite.tools.RelBuilderFactory;

import com.google.common.base.Predicates;
import java.util.function.Predicate;

/**
* Rule to convert a relational expression from
Expand All @@ -40,7 +40,7 @@ public class CassandraToEnumerableConverterRule extends ConverterRule {
*/
public CassandraToEnumerableConverterRule(
RelBuilderFactory relBuilderFactory) {
super(RelNode.class, Predicates.<RelNode>alwaysTrue(),
super(RelNode.class, (Predicate<RelNode>) r -> true,
CassandraRel.CONVENTION, EnumerableConvention.INSTANCE,
relBuilderFactory, "CassandraToEnumerableConverterRule");
}
Expand Down
16 changes: 7 additions & 9 deletions core/src/main/codegen/templates/Parser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ import org.apache.calcite.util.Pair;
import org.apache.calcite.util.Util;
import org.apache.calcite.util.trace.CalciteTrace;

import com.google.common.collect.Lists;

import org.slf4j.Logger;

import java.io.Reader;
Expand Down Expand Up @@ -1010,7 +1008,7 @@ SqlNode SqlStmtEof() :
*/
SqlSelect SqlSelect() :
{
final List<SqlLiteral> keywords = Lists.newArrayList();
final List<SqlLiteral> keywords = new ArrayList<SqlLiteral>();
final SqlNodeList keywordList;
List<SqlNode> selectList;
final SqlNode fromClause;
Expand Down Expand Up @@ -1261,7 +1259,7 @@ SqlNode NamedRoutineCall(
ExprContext exprContext) :
{
SqlIdentifier name;
final List<SqlNode> list = Lists.newArrayList();
final List<SqlNode> list = new ArrayList<SqlNode>();
final Span s;
}
{
Expand Down Expand Up @@ -1290,7 +1288,7 @@ SqlNode NamedRoutineCall(
*/
SqlNode SqlInsert() :
{
final List<SqlLiteral> keywords = Lists.newArrayList();
final List<SqlLiteral> keywords = new ArrayList<SqlLiteral>();
final SqlNodeList keywordList;
SqlNode table;
SqlNodeList extendList = null;
Expand Down Expand Up @@ -1498,7 +1496,7 @@ SqlUpdate WhenMatchedClause(SqlNode table, SqlIdentifier alias) :
SqlInsert WhenNotMatchedClause(SqlNode table) :
{
final Span insertSpan, valuesSpan;
final List<SqlLiteral> keywords = Lists.newArrayList();
final List<SqlLiteral> keywords = new ArrayList<SqlLiteral>();
final SqlNodeList keywordList;
SqlNodeList insertColumnList = null;
SqlNode rowConstructor;
Expand Down Expand Up @@ -1981,7 +1979,7 @@ SqlNode TableRef2(boolean lateral) :
SqlNodeList ExtendList() :
{
final Span s;
List<SqlNode> list = Lists.newArrayList();
List<SqlNode> list = new ArrayList<SqlNode>();
}
{
<LPAREN> { s = span(); }
Expand Down Expand Up @@ -2205,7 +2203,7 @@ SqlNode WhereOpt() :
*/
SqlNodeList GroupByOpt() :
{
List<SqlNode> list = Lists.newArrayList();
List<SqlNode> list = new ArrayList<SqlNode>();
final Span s;
}
{
Expand All @@ -2221,7 +2219,7 @@ SqlNodeList GroupByOpt() :

List<SqlNode> GroupingElementList() :
{
List<SqlNode> list = Lists.newArrayList();
List<SqlNode> list = new ArrayList<SqlNode>();
SqlNode e;
}
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@

import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

import java.lang.reflect.Array;
import java.lang.reflect.Type;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -72,7 +72,7 @@ public RelDataType getRowType(RelDataTypeFactory typeFactory) {
}

public Statistic getStatistic() {
final List<ImmutableBitSet> keys = Lists.newArrayList();
final List<ImmutableBitSet> keys = new ArrayList<>();
final Content content = supplier.get();
for (Ord<Column> ord : Ord.zip(content.columns)) {
if (ord.e.cardinality == content.size) {
Expand Down Expand Up @@ -280,7 +280,7 @@ public RepresentationType getType() {
public Object freeze(ColumnLoader.ValueSet valueSet, int[] sources) {
// We assume the values have been canonized.
final List<Comparable> list = permuteList(valueSet.values, sources);
return list.toArray(new Comparable[list.size()]);
return list.toArray(new Comparable[0]);
}

public Object permute(Object dataSet, int[] sources) {
Expand Down Expand Up @@ -816,7 +816,7 @@ public static class Content {
this(columns, size,
sortField >= 0
? RelCollations.createSingleton(sortField)
: ImmutableList.<RelCollation>of());
: ImmutableList.of());
}

@SuppressWarnings("unchecked")
Expand Down
Loading

0 comments on commit d59b639

Please sign in to comment.