Skip to content

Commit d59b639

Browse files
committed
[CALCITE-2259] Allow Java 8 syntax
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)
1 parent 5bbc501 commit d59b639

File tree

525 files changed

+7983
-10608
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

525 files changed

+7983
-10608
lines changed

cassandra/src/main/java/org/apache/calcite/adapter/cassandra/CassandraRules.java

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,14 @@
3838
import org.apache.calcite.rex.RexInputRef;
3939
import org.apache.calcite.rex.RexNode;
4040
import org.apache.calcite.rex.RexVisitorImpl;
41-
import org.apache.calcite.runtime.PredicateImpl;
4241
import org.apache.calcite.sql.SqlKind;
4342
import org.apache.calcite.sql.validate.SqlValidatorUtil;
4443
import org.apache.calcite.util.Pair;
4544

46-
import com.google.common.base.Predicate;
47-
import com.google.common.base.Predicates;
48-
4945
import java.util.HashSet;
5046
import java.util.List;
5147
import java.util.Set;
48+
import java.util.function.Predicate;
5249

5350
/**
5451
* Rules and relational operators for
@@ -95,7 +92,7 @@ abstract static class CassandraConverterRule extends ConverterRule {
9592

9693
CassandraConverterRule(Class<? extends RelNode> clazz,
9794
String description) {
98-
this(clazz, Predicates.<RelNode>alwaysTrue(), description);
95+
this(clazz, r -> true, description);
9996
}
10097

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

124117
private static final CassandraFilterRule INSTANCE = new CassandraFilterRule();
125118

@@ -268,28 +261,21 @@ public RelNode convert(RelNode rel) {
268261
* {@link CassandraSort}.
269262
*/
270263
private static class CassandraSortRule extends RelOptRule {
271-
private static final Predicate<Sort> SORT_PREDICATE =
272-
new PredicateImpl<Sort>() {
273-
public boolean test(Sort input) {
274-
// Limits are handled by CassandraLimit
275-
return input.offset == null && input.fetch == null;
276-
}
277-
};
278-
private static final Predicate<CassandraFilter> FILTER_PREDICATE =
279-
new PredicateImpl<CassandraFilter>() {
280-
public boolean test(CassandraFilter input) {
281-
// We can only use implicit sorting within a single partition
282-
return input.isSinglePartition();
283-
}
284-
};
264+
285265
private static final RelOptRuleOperand CASSANDRA_OP =
286266
operand(CassandraToEnumerableConverter.class,
287-
operand(CassandraFilter.class, null, FILTER_PREDICATE, any()));
267+
operandJ(CassandraFilter.class, null,
268+
// We can only use implicit sorting within a single partition
269+
CassandraFilter::isSinglePartition, any()));
288270

289271
private static final CassandraSortRule INSTANCE = new CassandraSortRule();
290272

291273
private CassandraSortRule() {
292-
super(operand(Sort.class, null, SORT_PREDICATE, CASSANDRA_OP), "CassandraSortRule");
274+
super(
275+
operandJ(Sort.class, null,
276+
// Limits are handled by CassandraLimit
277+
sort -> sort.offset == null && sort.fetch == null, CASSANDRA_OP),
278+
"CassandraSortRule");
293279
}
294280

295281
public RelNode convert(Sort sort, CassandraFilter filter) {

cassandra/src/main/java/org/apache/calcite/adapter/cassandra/CassandraSchema.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.apache.calcite.avatica.util.Casing;
2020
import org.apache.calcite.jdbc.CalciteSchema;
2121
import org.apache.calcite.rel.RelFieldCollation;
22-
import org.apache.calcite.rel.RelNode;
2322
import org.apache.calcite.rel.type.RelDataTypeFactory;
2423
import org.apache.calcite.rel.type.RelDataTypeImpl;
2524
import org.apache.calcite.rel.type.RelDataTypeSystem;
@@ -50,7 +49,6 @@
5049
import com.datastax.driver.core.MaterializedViewMetadata;
5150
import com.datastax.driver.core.Session;
5251
import com.datastax.driver.core.TableMetadata;
53-
import com.google.common.base.Function;
5452
import com.google.common.collect.ImmutableList;
5553
import com.google.common.collect.ImmutableMap;
5654

@@ -113,11 +111,8 @@ public CassandraSchema(String host, String keyspace, String username, String pas
113111
this.parentSchema = parentSchema;
114112
this.name = name;
115113

116-
this.hook = Hook.TRIMMED.add(new Function<RelNode, Void>() {
117-
public Void apply(RelNode node) {
118-
CassandraSchema.this.addMaterializedViews();
119-
return null;
120-
}
114+
this.hook = Hook.TRIMMED.add(node -> {
115+
CassandraSchema.this.addMaterializedViews();
121116
});
122117
}
123118

@@ -177,19 +172,19 @@ Pair<List<String>, List<String>> getKeyFields(String columnFamily, boolean view)
177172
}
178173

179174
List<ColumnMetadata> partitionKey = table.getPartitionKey();
180-
List<String> pKeyFields = new ArrayList<String>();
175+
List<String> pKeyFields = new ArrayList<>();
181176
for (ColumnMetadata column : partitionKey) {
182177
pKeyFields.add(column.getName());
183178
}
184179

185180
List<ColumnMetadata> clusteringKey = table.getClusteringColumns();
186-
List<String> cKeyFields = new ArrayList<String>();
181+
List<String> cKeyFields = new ArrayList<>();
187182
for (ColumnMetadata column : clusteringKey) {
188183
cKeyFields.add(column.getName());
189184
}
190185

191-
return Pair.of((List<String>) ImmutableList.copyOf(pKeyFields),
192-
(List<String>) ImmutableList.copyOf(cKeyFields));
186+
return Pair.of(ImmutableList.copyOf(pKeyFields),
187+
ImmutableList.copyOf(cKeyFields));
193188
}
194189

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

207202
List<ClusteringOrder> clusteringOrder = table.getClusteringOrder();
208-
List<RelFieldCollation> keyCollations = new ArrayList<RelFieldCollation>();
203+
List<RelFieldCollation> keyCollations = new ArrayList<>();
209204

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

239234
// Add all the selected columns to the query
240-
List<String> columnNames = new ArrayList<String>();
235+
List<String> columnNames = new ArrayList<>();
241236
for (ColumnMetadata column : view.getColumns()) {
242237
columnNames.add("\"" + column.getName() + "\"");
243238
}

cassandra/src/main/java/org/apache/calcite/adapter/cassandra/CassandraTable.java

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242

4343
import com.datastax.driver.core.ResultSet;
4444
import com.datastax.driver.core.Session;
45+
import com.google.common.collect.ImmutableList;
4546

46-
import java.util.Collections;
4747
import java.util.Iterator;
4848
import java.util.List;
4949
import java.util.Map;
@@ -97,9 +97,8 @@ public List<RelFieldCollation> getClusteringOrder() {
9797
}
9898

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

105104
/** Executes a CQL query on the underlying table.
@@ -118,12 +117,12 @@ public Enumerable<Object> query(final Session session, List<Map.Entry<String, Cl
118117
final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
119118
final RelDataType rowType = getRowType(typeFactory);
120119

121-
Function1<String, Void> addField = new Function1<String, Void>() {
122-
public Void apply(String fieldName) {
123-
SqlTypeName typeName = rowType.getField(fieldName, true, false).getType().getSqlTypeName();
124-
fieldInfo.add(fieldName, typeFactory.createSqlType(typeName)).nullable(true);
125-
return null;
126-
}
120+
Function1<String, Void> addField = fieldName -> {
121+
SqlTypeName typeName =
122+
rowType.getField(fieldName, true, false).getType().getSqlTypeName();
123+
fieldInfo.add(fieldName, typeFactory.createSqlType(typeName))
124+
.nullable(true);
125+
return null;
127126
};
128127

129128
if (selectFields.isEmpty()) {
@@ -143,26 +142,24 @@ public Void apply(String fieldName) {
143142
if (selectFields.isEmpty()) {
144143
selectString = "*";
145144
} else {
146-
selectString = Util.toString(new Iterable<String>() {
147-
public Iterator<String> iterator() {
148-
final Iterator<Map.Entry<String, String>> selectIterator =
149-
selectFields.iterator();
145+
selectString = Util.toString(() -> {
146+
final Iterator<Map.Entry<String, String>> selectIterator =
147+
selectFields.iterator();
150148

151-
return new Iterator<String>() {
152-
@Override public boolean hasNext() {
153-
return selectIterator.hasNext();
154-
}
149+
return new Iterator<String>() {
150+
@Override public boolean hasNext() {
151+
return selectIterator.hasNext();
152+
}
155153

156-
@Override public String next() {
157-
Map.Entry<String, String> entry = selectIterator.next();
158-
return entry.getKey() + " AS " + entry.getValue();
159-
}
154+
@Override public String next() {
155+
Map.Entry<String, String> entry = selectIterator.next();
156+
return entry.getKey() + " AS " + entry.getValue();
157+
}
160158

161-
@Override public void remove() {
162-
throw new UnsupportedOperationException();
163-
}
164-
};
165-
}
159+
@Override public void remove() {
160+
throw new UnsupportedOperationException();
161+
}
162+
};
166163
}, "", ", ", "");
167164
}
168165

cassandra/src/main/java/org/apache/calcite/adapter/cassandra/CassandraToEnumerableConverter.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.apache.calcite.util.BuiltInMethod;
4040
import org.apache.calcite.util.Pair;
4141

42-
import com.google.common.base.Function;
4342
import com.google.common.collect.Lists;
4443

4544
import java.util.AbstractList;
@@ -144,12 +143,7 @@ private static <T> MethodCallExpression constantArrayList(List<T> values,
144143
/** E.g. {@code constantList("x", "y")} returns
145144
* {@code {ConstantExpression("x"), ConstantExpression("y")}}. */
146145
private static <T> List<Expression> constantList(List<T> values) {
147-
return Lists.transform(values,
148-
new Function<T, Expression>() {
149-
public Expression apply(T a0) {
150-
return Expressions.constant(a0);
151-
}
152-
});
146+
return Lists.transform(values, Expressions::constant);
153147
}
154148
}
155149

cassandra/src/main/java/org/apache/calcite/adapter/cassandra/CassandraToEnumerableConverterRule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.apache.calcite.rel.core.RelFactories;
2424
import org.apache.calcite.tools.RelBuilderFactory;
2525

26-
import com.google.common.base.Predicates;
26+
import java.util.function.Predicate;
2727

2828
/**
2929
* Rule to convert a relational expression from
@@ -40,7 +40,7 @@ public class CassandraToEnumerableConverterRule extends ConverterRule {
4040
*/
4141
public CassandraToEnumerableConverterRule(
4242
RelBuilderFactory relBuilderFactory) {
43-
super(RelNode.class, Predicates.<RelNode>alwaysTrue(),
43+
super(RelNode.class, (Predicate<RelNode>) r -> true,
4444
CassandraRel.CONVENTION, EnumerableConvention.INSTANCE,
4545
relBuilderFactory, "CassandraToEnumerableConverterRule");
4646
}

core/src/main/codegen/templates/Parser.jj

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ import org.apache.calcite.util.Pair;
106106
import org.apache.calcite.util.Util;
107107
import org.apache.calcite.util.trace.CalciteTrace;
108108

109-
import com.google.common.collect.Lists;
110-
111109
import org.slf4j.Logger;
112110

113111
import java.io.Reader;
@@ -1010,7 +1008,7 @@ SqlNode SqlStmtEof() :
10101008
*/
10111009
SqlSelect SqlSelect() :
10121010
{
1013-
final List<SqlLiteral> keywords = Lists.newArrayList();
1011+
final List<SqlLiteral> keywords = new ArrayList<SqlLiteral>();
10141012
final SqlNodeList keywordList;
10151013
List<SqlNode> selectList;
10161014
final SqlNode fromClause;
@@ -1261,7 +1259,7 @@ SqlNode NamedRoutineCall(
12611259
ExprContext exprContext) :
12621260
{
12631261
SqlIdentifier name;
1264-
final List<SqlNode> list = Lists.newArrayList();
1262+
final List<SqlNode> list = new ArrayList<SqlNode>();
12651263
final Span s;
12661264
}
12671265
{
@@ -1290,7 +1288,7 @@ SqlNode NamedRoutineCall(
12901288
*/
12911289
SqlNode SqlInsert() :
12921290
{
1293-
final List<SqlLiteral> keywords = Lists.newArrayList();
1291+
final List<SqlLiteral> keywords = new ArrayList<SqlLiteral>();
12941292
final SqlNodeList keywordList;
12951293
SqlNode table;
12961294
SqlNodeList extendList = null;
@@ -1498,7 +1496,7 @@ SqlUpdate WhenMatchedClause(SqlNode table, SqlIdentifier alias) :
14981496
SqlInsert WhenNotMatchedClause(SqlNode table) :
14991497
{
15001498
final Span insertSpan, valuesSpan;
1501-
final List<SqlLiteral> keywords = Lists.newArrayList();
1499+
final List<SqlLiteral> keywords = new ArrayList<SqlLiteral>();
15021500
final SqlNodeList keywordList;
15031501
SqlNodeList insertColumnList = null;
15041502
SqlNode rowConstructor;
@@ -1981,7 +1979,7 @@ SqlNode TableRef2(boolean lateral) :
19811979
SqlNodeList ExtendList() :
19821980
{
19831981
final Span s;
1984-
List<SqlNode> list = Lists.newArrayList();
1982+
List<SqlNode> list = new ArrayList<SqlNode>();
19851983
}
19861984
{
19871985
<LPAREN> { s = span(); }
@@ -2205,7 +2203,7 @@ SqlNode WhereOpt() :
22052203
*/
22062204
SqlNodeList GroupByOpt() :
22072205
{
2208-
List<SqlNode> list = Lists.newArrayList();
2206+
List<SqlNode> list = new ArrayList<SqlNode>();
22092207
final Span s;
22102208
}
22112209
{
@@ -2221,7 +2219,7 @@ SqlNodeList GroupByOpt() :
22212219

22222220
List<SqlNode> GroupingElementList() :
22232221
{
2224-
List<SqlNode> list = Lists.newArrayList();
2222+
List<SqlNode> list = new ArrayList<SqlNode>();
22252223
SqlNode e;
22262224
}
22272225
{

core/src/main/java/org/apache/calcite/adapter/clone/ArrayTable.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040

4141
import com.google.common.base.Supplier;
4242
import com.google.common.collect.ImmutableList;
43-
import com.google.common.collect.Lists;
4443

4544
import java.lang.reflect.Array;
4645
import java.lang.reflect.Type;
4746
import java.util.AbstractList;
47+
import java.util.ArrayList;
4848
import java.util.Arrays;
4949
import java.util.Collections;
5050
import java.util.List;
@@ -72,7 +72,7 @@ public RelDataType getRowType(RelDataTypeFactory typeFactory) {
7272
}
7373

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

286286
public Object permute(Object dataSet, int[] sources) {
@@ -816,7 +816,7 @@ public static class Content {
816816
this(columns, size,
817817
sortField >= 0
818818
? RelCollations.createSingleton(sortField)
819-
: ImmutableList.<RelCollation>of());
819+
: ImmutableList.of());
820820
}
821821

822822
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)