Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

Misc cleanup trying to get a handle on rules

Kevin Risdenb31ea98bfaec
diff --git a/solr/core/src/java/org/apache/solr/handler/sql/LimitStream.java b/solr/core/src/java/org/apache/solr/handler/sql/LimitStream.java
new file mode 100644
index 00000000000..403ec451d8f
--- /dev/null
+++ b/solr/core/src/java/org/apache/solr/handler/sql/LimitStream.java
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.handler.sql;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExplanation;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+class LimitStream extends TupleStream {
+
+ private TupleStream stream;
+ private int limit;
+ private int count;
+
+ LimitStream(TupleStream stream, int limit) {
+ this.stream = stream;
+ this.limit = limit;
+ }
+
+ public void open() throws IOException {
+ this.stream.open();
+ }
+
+ public void close() throws IOException {
+ this.stream.close();
+ }
+
+ public List<TupleStream> children() {
+ List<TupleStream> children = new ArrayList<>();
+ children.add(stream);
+ return children;
+ }
+
+ public StreamComparator getStreamSort(){
+ return stream.getStreamSort();
+ }
+
+ public void setStreamContext(StreamContext context) {
+ stream.setStreamContext(context);
+ }
+
+ @Override
+ public Explanation toExplanation(StreamFactory factory) throws IOException {
+
+ return new StreamExplanation(getStreamNodeId().toString())
+ .withChildren(new Explanation[]{
+ stream.toExplanation(factory)
+ })
+ .withFunctionName("SQL LIMIT")
+ .withExpression("--non-expressible--")
+ .withImplementingClass(this.getClass().getName())
+ .withExpressionType(Explanation.ExpressionType.STREAM_DECORATOR);
+ }
+
+ public Tuple read() throws IOException {
+ ++count;
+ if(count > limit) {
+ Map<String, String> fields = new HashMap<>();
+ fields.put("EOF", "true");
+ return new Tuple(fields);
+ }
+
+ return stream.read();
+ }
+}
\ No newline at end of file
diff --git a/solr/core/src/java/org/apache/solr/handler/sql/SolrFilter.java b/solr/core/src/java/org/apache/solr/handler/sql/SolrFilter.java
index 1828ae532db..096c6e5b3ac 100644
--- a/solr/core/src/java/org/apache/solr/handler/sql/SolrFilter.java
+++ b/solr/core/src/java/org/apache/solr/handler/sql/SolrFilter.java
@@ -57,11 +57,11 @@ public class SolrFilter extends Filter implements SolrRel {
public void implement(Implementor implementor) {
implementor.visitChild(0, getInput());
Translator translator = new Translator(SolrRules.solrFieldNames(getRowType()));
- List<String> fqs = translator.translateMatch(condition);
- implementor.add(null, fqs);
+ String query = translator.translateMatch(condition);
+ implementor.addQuery(query);
}
- /** Translates {@link RexNode} expressions into Solr fq strings. */
+ /** Translates {@link RexNode} expressions into Solr query strings. */
private static class Translator {
private final List<String> fieldNames;
@@ -69,20 +69,18 @@ public class SolrFilter extends Filter implements SolrRel {
this.fieldNames = fieldNames;
}
- private List<String> translateMatch(RexNode condition) {
+ private String translateMatch(RexNode condition) {
return translateOr(condition);
}
- private List<String> translateOr(RexNode condition) {
- List<String> list = new ArrayList<>();
+ private String translateOr(RexNode condition) {
+ List<String> ors = new ArrayList<>();
for (RexNode node : RelOptUtil.disjunctions(condition)) {
- list.add(translateAnd(node));
+ ors.add(translateAnd(node));
}
- return list;
+ return String.join(" OR ", ors);
}
- /** Translates a condition that may be an AND of other conditions. Gathers
- * together conditions that apply to the same field. */
private String translateAnd(RexNode node0) {
List<String> ands = new ArrayList<>();
for (RexNode node : RelOptUtil.conjunctions(node0)) {
@@ -95,13 +93,15 @@ public class SolrFilter extends Filter implements SolrRel {
private String translateMatch2(RexNode node) {
switch (node.getKind()) {
case EQUALS:
- return translateBinary(null, null, (RexCall) node);
+ return translateBinary("", "", (RexCall) node);
+// case NOT_EQUALS:
+// return null;
// case LESS_THAN:
// return translateBinary("$lt", "$gt", (RexCall) node);
// case LESS_THAN_OR_EQUAL:
// return translateBinary("$lte", "$gte", (RexCall) node);
-// case NOT_EQUALS:
-// return translateBinary("$ne", "$ne", (RexCall) node);
+ case NOT:
+ return translateBinary("-", "-", (RexCall) node);
// case GREATER_THAN:
// return translateBinary("$gt", "$lt", (RexCall) node);
// case GREATER_THAN_OR_EQUAL:
@@ -113,6 +113,9 @@ public class SolrFilter extends Filter implements SolrRel {
/** Translates a call to a binary operator, reversing arguments if necessary. */
private String translateBinary(String op, String rop, RexCall call) {
+ if(call.operands.size() != 2) {
+ throw new AssertionError("hello");
+ }
final RexNode left = call.operands.get(0);
final RexNode right = call.operands.get(1);
String b = translateBinary2(op, left, right);
@@ -142,12 +145,11 @@ public class SolrFilter extends Filter implements SolrRel {
return translateOp2(op, name, rightLiteral);
case CAST:
return translateBinary2(op, ((RexCall) left).operands.get(0), right);
- case OTHER_FUNCTION:
+// case OTHER_FUNCTION:
// String itemName = SolrRules.isItem((RexCall) left);
// if (itemName != null) {
// return translateOp2(op, itemName, rightLiteral);
// }
- // fall through
default:
return null;
}
@@ -155,15 +157,9 @@ public class SolrFilter extends Filter implements SolrRel {
private String translateOp2(String op, String name, RexLiteral right) {
if (op == null) {
- // E.g.: {deptno: 100}
- return name + ":" + right.getValue2();
- } else {
-// // E.g. {deptno: {$lt: 100}}
-// // which may later be combined with other conditions:
-// // E.g. {deptno: [$lt: 100, $gt: 50]}
-// multimap.put(name, Pair.of(op, right));
- return null;
+ op = "";
+ }
+ return op + name + ":" + right.getValue2();
}
- }
}
}
diff --git a/solr/core/src/java/org/apache/solr/handler/sql/SolrMethod.java b/solr/core/src/java/org/apache/solr/handler/sql/SolrMethod.java
index 323f1353443..bce569a74d4 100644
--- a/solr/core/src/java/org/apache/solr/handler/sql/SolrMethod.java
+++ b/solr/core/src/java/org/apache/solr/handler/sql/SolrMethod.java
@@ -26,7 +26,7 @@ import org.apache.calcite.linq4j.tree.Types;
* Builtin methods in the Solr adapter.
*/
public enum SolrMethod {
- SOLR_QUERYABLE_QUERY(SolrTable.SolrQueryable.class, "query", List.class, List.class, List.class, String.class);
+ SOLR_QUERYABLE_QUERY(SolrTable.SolrQueryable.class, "query", List.class, String.class, List.class, String.class);
public final Method method;
diff --git a/solr/core/src/java/org/apache/solr/handler/sql/SolrProject.java b/solr/core/src/java/org/apache/solr/handler/sql/SolrProject.java
index 26d9ee0dde1..7f8b38b8ba9 100644
--- a/solr/core/src/java/org/apache/solr/handler/sql/SolrProject.java
+++ b/solr/core/src/java/org/apache/solr/handler/sql/SolrProject.java
@@ -63,6 +63,6 @@ public class SolrProject extends Project implements SolrRel {
final String expr = pair.left.accept(translator);
fieldMappings.put(name, expr);
}
- implementor.add(fieldMappings, null);
+ implementor.addFieldMappings(fieldMappings);
}
}
diff --git a/solr/core/src/java/org/apache/solr/handler/sql/SolrRel.java b/solr/core/src/java/org/apache/solr/handler/sql/SolrRel.java
index a61cc3f5619..fdeb4d01562 100644
--- a/solr/core/src/java/org/apache/solr/handler/sql/SolrRel.java
+++ b/solr/core/src/java/org/apache/solr/handler/sql/SolrRel.java
@@ -37,25 +37,21 @@ public interface SolrRel extends RelNode {
/** Callback for the implementation process that converts a tree of {@link SolrRel} nodes into a Solr query. */
class Implementor {
final Map<String, String> fieldMappings = new HashMap<>();
- final List<String> filterQueries = new ArrayList<>();
+ String query = null;
String limitValue = null;
final List<String> order = new ArrayList<>();
RelOptTable table;
SolrTable solrTable;
- /** Adds newly projected fields and restricted filterQueries.
- *
- * @param fieldMappings New fields to be projected from a query
- * @param filterQueries New filterQueries to be applied to the query
- */
- public void add(Map<String, String> fieldMappings, List<String> filterQueries) {
+ public void addFieldMappings(Map<String, String> fieldMappings) {
if (fieldMappings != null) {
this.fieldMappings.putAll(fieldMappings);
}
- if (filterQueries != null) {
- this.filterQueries.addAll(filterQueries);
- }
+ }
+
+ public void addQuery(String query) {
+ this.query = query;
}
public void addOrder(List<String> newOrder) {
diff --git a/solr/core/src/java/org/apache/solr/handler/sql/SolrRules.java b/solr/core/src/java/org/apache/solr/handler/sql/SolrRules.java
index 9cefc55d605..df85c94e39d 100644
--- a/solr/core/src/java/org/apache/solr/handler/sql/SolrRules.java
+++ b/solr/core/src/java/org/apache/solr/handler/sql/SolrRules.java
@@ -25,8 +25,10 @@ import org.apache.calcite.adapter.java.JavaTypeFactory;
import org.apache.calcite.plan.Convention;
import org.apache.calcite.plan.RelOptRule;
import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelCollations;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.convert.ConverterRule;
+import org.apache.calcite.rel.core.Sort;
import org.apache.calcite.rel.logical.LogicalFilter;
import org.apache.calcite.rel.logical.LogicalProject;
import org.apache.calcite.rel.type.RelDataType;
@@ -129,117 +131,34 @@ public class SolrRules {
public RelNode convert(RelNode rel) {
final LogicalProject project = (LogicalProject) rel;
final RelTraitSet traitSet = project.getTraitSet().replace(out);
- return new SolrProject(project.getCluster(), traitSet,
- convert(project.getInput(), out), project.getProjects(), project.getRowType());
+ return new SolrProject(
+ rel.getCluster(),
+ traitSet,
+ convert(project.getInput(), out),
+ project.getProjects(),
+ project.getRowType());
}
}
/**
* Rule to convert a {@link org.apache.calcite.rel.core.Sort} to a {@link SolrSort}.
*/
-// private static class SolrSortRule extends RelOptRule {
-// private static final com.google.common.base.Predicate<Sort> SORT_PREDICATE =
-// input -> {
-// // CQL has no support for offsets
-// return input.offset == null;
-// };
-// private static final com.google.common.base.Predicate<SolrFilter> FILTER_PREDICATE =
-// input -> {
-// // We can only use implicit sorting within a single partition
-// return input.isSinglePartition();
-// };
-// private static final RelOptRuleOperand SOLR_OP =
-// operand(SolrToEnumerableConverter.class,
-// operand(SolrFilter.class, null, FILTER_PREDICATE, any()));
-//
-// private static final SolrSortRule INSTANCE = new SolrSortRule();
-//
-// private SolrSortRule() {
-// super(operand(Sort.class, null, SORT_PREDICATE, SOLR_OP), "SolrSortRule");
-// }
-//
-// public RelNode convert(Sort sort, SolrFilter filter) {
-// final RelTraitSet traitSet =
-// sort.getTraitSet().replace(SolrRel.CONVENTION)
-// .replace(sort.getCollation());
-// return new SolrSort(sort.getCluster(), traitSet,
-// convert(sort.getInput(), traitSet.replace(RelCollations.EMPTY)),
-// sort.getCollation(), filter.getImplicitCollation(), sort.fetch);
-// }
-//
-// public boolean matches(RelOptRuleCall call) {
-// final Sort sort = call.rel(0);
-// final SolrFilter filter = call.rel(2);
-// return collationsCompatible(sort.getCollation(), filter.getImplicitCollation());
-// }
-//
-// /** Check if it is possible to exploit native CQL sorting for a given collation.
-// *
-// * @return True if it is possible to achieve this sort in Solr
-// */
-// private boolean collationsCompatible(RelCollation sortCollation, RelCollation implicitCollation) {
-// List<RelFieldCollation> sortFieldCollations = sortCollation.getFieldCollations();
-// List<RelFieldCollation> implicitFieldCollations = implicitCollation.getFieldCollations();
-//
-// if (sortFieldCollations.size() > implicitFieldCollations.size()) {
-// return false;
-// }
-// if (sortFieldCollations.size() == 0) {
-// return true;
-// }
-//
-// // Check if we need to reverse the order of the implicit collation
-// boolean reversed = reverseDirection(sortFieldCollations.get(0).getDirection())
-// == implicitFieldCollations.get(0).getDirection();
-//
-// for (int i = 0; i < sortFieldCollations.size(); i++) {
-// RelFieldCollation sorted = sortFieldCollations.get(i);
-// RelFieldCollation implied = implicitFieldCollations.get(i);
-//
-// // Check that the fields being sorted match
-// if (sorted.getFieldIndex() != implied.getFieldIndex()) {
-// return false;
-// }
-//
-// // Either all fields must be sorted in the same direction
-// // or the opposite direction based on whether we decided
-// // if the sort direction should be reversed above
-// RelFieldCollation.Direction sortDirection = sorted.getDirection();
-// RelFieldCollation.Direction implicitDirection = implied.getDirection();
-// if ((!reversed && sortDirection != implicitDirection)
-// || (reversed && reverseDirection(sortDirection) != implicitDirection)) {
-// return false;
-// }
-// }
-//
-// return true;
-// }
-//
-// /** Find the reverse of a given collation direction.
-// *
-// * @return Reverse of the input direction
-// */
-// private RelFieldCollation.Direction reverseDirection(RelFieldCollation.Direction direction) {
-// switch(direction) {
-// case ASCENDING:
-// case STRICTLY_ASCENDING:
-// return RelFieldCollation.Direction.DESCENDING;
-// case DESCENDING:
-// case STRICTLY_DESCENDING:
-// return RelFieldCollation.Direction.ASCENDING;
-// default:
-// return null;
-// }
-// }
-//
-// /** @see org.apache.calcite.rel.convert.ConverterRule */
-// public void onMatch(RelOptRuleCall call) {
-// final Sort sort = call.rel(0);
-// SolrFilter filter = call.rel(2);
-// final RelNode converted = convert(sort, filter);
-// if (converted != null) {
-// call.transformTo(converted);
-// }
-// }
-// }
+ private static class SolrSortRule extends SolrConverterRule {
+ public static final SolrSortRule INSTANCE = new SolrSortRule();
+
+ private SolrSortRule() {
+ super(Sort.class, "SolrSortRule");
+ }
+
+ public RelNode convert(RelNode rel) {
+ final Sort sort = (Sort) rel;
+ final RelTraitSet traitSet = sort.getTraitSet().replace(out).replace(sort.getCollation());
+ return new SolrSort(
+ rel.getCluster(),
+ traitSet,
+ convert(sort.getInput(), traitSet.replace(RelCollations.EMPTY)),
+ sort.getCollation(),
+ sort.fetch);
+ }
+ }
}
diff --git a/solr/core/src/java/org/apache/solr/handler/sql/SolrSort.java b/solr/core/src/java/org/apache/solr/handler/sql/SolrSort.java
index 465986b96f1..602375de9a7 100644
--- a/solr/core/src/java/org/apache/solr/handler/sql/SolrSort.java
+++ b/solr/core/src/java/org/apache/solr/handler/sql/SolrSort.java
@@ -36,14 +36,10 @@ import org.apache.calcite.rex.RexNode;
* Implementation of {@link org.apache.calcite.rel.core.Sort} relational expression in Solr.
*/
public class SolrSort extends Sort implements SolrRel {
- private final RelCollation implicitCollation;
- public SolrSort(RelOptCluster cluster, RelTraitSet traitSet, RelNode child, RelCollation collation,
- RelCollation implicitCollation, RexNode fetch) {
+ public SolrSort(RelOptCluster cluster, RelTraitSet traitSet, RelNode child, RelCollation collation, RexNode fetch) {
super(cluster, traitSet, child, collation, null, fetch);
- this.implicitCollation = implicitCollation;
-
assert getConvention() == SolrRel.CONVENTION;
assert getConvention() == child.getConvention();
}
@@ -55,7 +51,7 @@ public class SolrSort extends Sort implements SolrRel {
@Override
public Sort copy(RelTraitSet traitSet, RelNode input, RelCollation newCollation, RexNode offset, RexNode fetch) {
- return new SolrSort(getCluster(), traitSet, input, collation, implicitCollation, fetch);
+ return new SolrSort(getCluster(), traitSet, input, collation, fetch);
}
public void implement(Implementor implementor) {
@@ -68,16 +64,17 @@ public class SolrSort extends Sort implements SolrRel {
final List<RelDataTypeField> fields = getRowType().getFieldList();
for (RelFieldCollation fieldCollation : sortCollations) {
final String name = fields.get(fieldCollation.getFieldIndex()).getName();
- String direction = "ASC";
+ String direction = "asc";
if (fieldCollation.getDirection().equals(RelFieldCollation.Direction.DESCENDING)) {
- direction = "DESC";
+ direction = "desc";
}
fieldOrder.add(name + " " + direction);
}
implementor.addOrder(fieldOrder);
}
- if (fetch != null) {
+
+ if(fetch != null) {
implementor.setLimit(((RexLiteral) fetch).getValue().toString());
}
}
diff --git a/solr/core/src/java/org/apache/solr/handler/sql/SolrTable.java b/solr/core/src/java/org/apache/solr/handler/sql/SolrTable.java
index 1e5113c4f41..1f704fe7a2c 100644
--- a/solr/core/src/java/org/apache/solr/handler/sql/SolrTable.java
+++ b/solr/core/src/java/org/apache/solr/handler/sql/SolrTable.java
@@ -70,18 +70,18 @@ public class SolrTable extends AbstractQueryableTable implements TranslatableTab
}
public Enumerable<Object> query(final Properties properties) {
- return query(properties, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), null);
+ return query(properties, Collections.emptyList(), null, Collections.emptyList(), null);
}
/** Executes a Solr query on the underlying table.
*
* @param properties Connections properties
* @param fields List of fields to project
- * @param filterQueries A list of filterQueries which should be used in the query
+ * @param query A string for the query
* @return Enumerator of results
*/
public Enumerable<Object> query(final Properties properties, List<String> fields,
- List<String> filterQueries, List<String> order, String limit) {
+ String query, List<String> order, String limit) {
Map<String, String> solrParams = new HashMap<>();
//solrParams.put(CommonParams.OMIT_HEADER, "true");
solrParams.put(CommonParams.Q, "*:*");
@@ -93,11 +93,11 @@ public class SolrTable extends AbstractQueryableTable implements TranslatableTab
solrParams.put(CommonParams.FL, String.join(",", fields));
}
- if (filterQueries.isEmpty()) {
+ if (query == null) {
solrParams.put(CommonParams.FQ, "*:*");
} else {
// SolrParams should be a ModifiableParams instead of a map so we could add multiple FQs
- solrParams.put(CommonParams.FQ, String.join(" OR ", filterQueries));
+ solrParams.put(CommonParams.FQ, query);
}
// Build and issue the query and return an Enumerator over the results
@@ -113,17 +113,23 @@ public class SolrTable extends AbstractQueryableTable implements TranslatableTab
solrParams.put(CommonParams.SORT, String.join(",", order));
}
+ TupleStream tupleStream;
+ try {
+ String zk = properties.getProperty("zk");
+ tupleStream = new CloudSolrStream(zk, collection, solrParams);
+ if(limit != null) {
+ tupleStream = new LimitStream(tupleStream, Integer.parseInt(limit));
+ }
+
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+
+ final TupleStream finalStream = tupleStream;
+
return new AbstractEnumerable<Object>() {
public Enumerator<Object> enumerator() {
- TupleStream tupleStream;
- try {
- String zk = properties.getProperty("zk");
- tupleStream = new CloudSolrStream(zk, collection, solrParams);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
-
- return new SolrEnumerator(tupleStream, fields);
+ return new SolrEnumerator(finalStream, fields);
}
};
}
@@ -161,9 +167,8 @@ public class SolrTable extends AbstractQueryableTable implements TranslatableTab
* @see SolrMethod#SOLR_QUERYABLE_QUERY
*/
@SuppressWarnings("UnusedDeclaration")
- public Enumerable<Object> query(List<String> fields, List<String> filterQueries,
- List<String> order, String limit) {
- return getTable().query(getProperties(), fields, filterQueries, order, limit);
+ public Enumerable<Object> query(List<String> fields, String query, List<String> order, String limit) {
+ return getTable().query(getProperties(), fields, query, order, limit);
}
}
}
diff --git a/solr/core/src/java/org/apache/solr/handler/sql/SolrToEnumerableConverter.java b/solr/core/src/java/org/apache/solr/handler/sql/SolrToEnumerableConverter.java
index 76558558e81..d8b67ad76d3 100644
--- a/solr/core/src/java/org/apache/solr/handler/sql/SolrToEnumerableConverter.java
+++ b/solr/core/src/java/org/apache/solr/handler/sql/SolrToEnumerableConverter.java
@@ -71,7 +71,7 @@ public class SolrToEnumerableConverter extends ConverterImpl implements Enumerab
final Expression table = list.append("table", solrImplementor.table.getExpression(SolrTable.SolrQueryable.class));
final Expression fields = list.append("fields",
constantArrayList(generateFields(SolrRules.solrFieldNames(rowType), solrImplementor.fieldMappings), String.class));
- final Expression filterQueries = list.append("filterQueries", constantArrayList(solrImplementor.filterQueries, String.class));
+ final Expression filterQueries = list.append("query", Expressions.constant(solrImplementor.query, String.class));
final Expression order = list.append("order", constantArrayList(solrImplementor.order, String.class));
final Expression limit = list.append("limit", Expressions.constant(solrImplementor.limitValue));
Expression enumerable = list.append("enumerable", Expressions.call(table, SolrMethod.SOLR_QUERYABLE_QUERY.method,