Apache Lucene open-source search software
Merge branch 'master' into jira/solr-8593
Kevin Risdenaae6f2703fb8
diff --cc solr/core/src/java/org/apache/solr/handler/SQLHandler.javaindex 6b3192ca127,b8df7f2dc90..9e76726dfe3--- a/solr/core/src/java/org/apache/solr/handler/SQLHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/SQLHandler.java@@@ -42,12 -50,18 +42,11 @@@ import org.apache.solr.client.solrj.io.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;-import org.apache.solr.client.solrj.io.stream.expr.Explanation.ExpressionType;-import org.apache.solr.client.solrj.io.stream.metrics.*;import org.apache.solr.common.SolrException;-import org.apache.solr.common.cloud.DocCollection;-import org.apache.solr.common.cloud.ZkStateReader;-import org.apache.solr.common.params.CommonParams;import org.apache.solr.common.params.ModifiableSolrParams;--import org.apache.solr.common.params.SolrParams;import org.apache.solr.core.CoreContainer;import org.apache.solr.core.SolrCore;+import org.apache.solr.handler.sql.CalciteSolrDriver;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.response.SolrQueryResponse;import org.apache.solr.security.AuthorizationContext;@@@ -252,7 -1291,390 +251,6 @@@ public class SQLHandler extends Request}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(ExpressionType.STREAM_DECORATOR);- }-- public Tuple read() throws IOException {- ++count;- if(count > limit) {- Map fields = new HashMap();- fields.put("EOF", "true");- return new Tuple(fields);- }-- Tuple tuple = stream.read();- return tuple;- }- }-- public static enum AggregationMode {-- MAP_REDUCE,- FACET;-- public static AggregationMode getMode(String mode) throws IOException{- if(mode.equalsIgnoreCase("facet")) {- return FACET;- } else if(mode.equalsIgnoreCase("map_reduce")) {- return MAP_REDUCE;- } else {- throw new IOException("Invalid aggregation mode:"+mode);- }- }- }-- private static class HavingStream extends TupleStream {-- private TupleStream stream;- private HavingVisitor havingVisitor;- private Expression havingExpression;-- public HavingStream(TupleStream stream, Expression havingExpression, Map<String, String> reverseAliasMap) {- this.stream = stream;- this.havingVisitor = new HavingVisitor(reverseAliasMap);- this.havingExpression = havingExpression;- }-- public void open() throws IOException {- this.stream.open();- }-- public void close() throws IOException {- this.stream.close();- }-- public StreamComparator getStreamSort(){- return stream.getStreamSort();- }-- public List<TupleStream> children() {- List<TupleStream> children = new ArrayList();- children.add(stream);- return children;- }-- @Override- public Explanation toExplanation(StreamFactory factory) throws IOException {-- return new StreamExplanation(getStreamNodeId().toString())- .withChildren(new Explanation[]{- stream.toExplanation(factory)- })- .withFunctionName("SQL HAVING")- .withExpression("--non-expressible--")- .withImplementingClass(this.getClass().getName())- .withExpressionType(ExpressionType.STREAM_DECORATOR);- }-- public void setStreamContext(StreamContext context) {- stream.setStreamContext(context);- }-- public Tuple read() throws IOException {- while (true) {- Tuple tuple = stream.read();- if (tuple.EOF) {- return tuple;- }-- if (havingVisitor.process(havingExpression, tuple)) {- return tuple;- }- }- }- }-- private static class CatalogsStream extends TupleStream {- private final String zkHost;- private StreamContext context;- private int currentIndex = 0;- private List<String> catalogs;-- CatalogsStream(String zkHost) {- this.zkHost = zkHost;- }-- public List<TupleStream> children() {- return new ArrayList<>();- }-- public void open() throws IOException {- this.catalogs = new ArrayList<>();- this.catalogs.add(this.zkHost);- }-- @Override- public Explanation toExplanation(StreamFactory factory) throws IOException {-- return new StreamExplanation(getStreamNodeId().toString())- .withFunctionName("SQL CATALOG")- .withExpression("--non-expressible--")- .withImplementingClass(this.getClass().getName())- .withExpressionType(ExpressionType.STREAM_DECORATOR);- }-- public Tuple read() throws IOException {- Map<String, String> fields = new HashMap<>();- if (this.currentIndex < this.catalogs.size()) {- fields.put("TABLE_CAT", this.catalogs.get(this.currentIndex));- this.currentIndex += 1;- } else {- fields.put("EOF", "true");- }- return new Tuple(fields);- }-- public StreamComparator getStreamSort() {- return null;- }-- public void close() throws IOException {-- }-- public void setStreamContext(StreamContext context) {- this.context = context;- }- }-- private static class SchemasStream extends TupleStream {- private final String zkHost;- private StreamContext context;-- SchemasStream(String zkHost) {- this.zkHost = zkHost;- }-- public List<TupleStream> children() {- return new ArrayList<>();- }-- public void open() throws IOException {-- }-- @Override- public Explanation toExplanation(StreamFactory factory) throws IOException {-- return new StreamExplanation(getStreamNodeId().toString())- .withFunctionName("SQL SCHEMA")- .withExpression("--non-expressible--")- .withImplementingClass(this.getClass().getName())- .withExpressionType(ExpressionType.STREAM_DECORATOR);- }-- public Tuple read() throws IOException {- Map<String, String> fields = new HashMap<>();- fields.put("EOF", "true");- return new Tuple(fields);- }-- public StreamComparator getStreamSort() {- return null;- }-- public void close() throws IOException {-- }-- public void setStreamContext(StreamContext context) {- this.context = context;- }- }-- private static class TableStream extends TupleStream {- private final String zkHost;- private StreamContext context;- private int currentIndex = 0;- private List<String> tables;-- TableStream(String zkHost) {- this.zkHost = zkHost;- }-- public List<TupleStream> children() {- return new ArrayList<>();- }-- public void open() throws IOException {- this.tables = new ArrayList<>();-- CloudSolrClient cloudSolrClient = this.context.getSolrClientCache().getCloudSolrClient(this.zkHost);- cloudSolrClient.connect();- ZkStateReader zkStateReader = cloudSolrClient.getZkStateReader();- Map<String, DocCollection> collections = zkStateReader.getClusterState().getCollectionsMap();- if (collections.size() != 0) {- this.tables.addAll(collections.keySet());- }- Collections.sort(this.tables);- }-- @Override- public Explanation toExplanation(StreamFactory factory) throws IOException {-- return new StreamExplanation(getStreamNodeId().toString())- .withFunctionName("SQL TABLE")- .withExpression("--non-expressible--")- .withImplementingClass(this.getClass().getName())- .withExpressionType(ExpressionType.STREAM_DECORATOR);- }-- public Tuple read() throws IOException {- Map<String, String> fields = new HashMap<>();- if (this.currentIndex < this.tables.size()) {- fields.put("TABLE_CAT", this.zkHost);- fields.put("TABLE_SCHEM", null);- fields.put("TABLE_NAME", this.tables.get(this.currentIndex));- fields.put("TABLE_TYPE", "TABLE");- fields.put("REMARKS", null);- this.currentIndex += 1;- } else {- fields.put("EOF", "true");- }- return new Tuple(fields);- }-- public StreamComparator getStreamSort() {- return null;- }-- public void close() throws IOException {-- }-- public void setStreamContext(StreamContext context) {- this.context = context;- }- }-- private static class MetadataStream extends TupleStream {-- private final TupleStream stream;- private final SQLVisitor sqlVisitor;- private boolean firstTuple = true;-- public MetadataStream(TupleStream stream, SQLVisitor sqlVistor) {- this.stream = stream;- this.sqlVisitor = sqlVistor;- }-- public List<TupleStream> children() {- return this.stream.children();- }-- public void open() throws IOException {- this.stream.open();- }-- @Override- public Explanation toExplanation(StreamFactory factory) throws IOException {-- return new StreamExplanation(getStreamNodeId().toString())- .withChildren(new Explanation[]{- stream.toExplanation(factory)- })- .withFunctionName("SQL METADATA")- .withExpression("--non-expressible--")- .withImplementingClass(this.getClass().getName())- .withExpressionType(ExpressionType.STREAM_DECORATOR);- }-- // Return a metadata tuple as the first tuple and then pass through to the underlying stream.- public Tuple read() throws IOException {- if(firstTuple) {- firstTuple = false;-- Map fields = new HashMap<>();- fields.put("isMetadata", true);- fields.put("fields", sqlVisitor.fields);- fields.put("aliases", sqlVisitor.columnAliases);- return new Tuple(fields);- }-- return this.stream.read();- }-- public StreamComparator getStreamSort() {- return this.stream.getStreamSort();- }-- public void close() throws IOException {- this.stream.close();- }-- public void setStreamContext(StreamContext context) {- this.stream.setStreamContext(context);- }- }-- private static class HavingVisitor extends AstVisitor<Boolean, Tuple> {-- private Map<String,String> reverseAliasMap;-- public HavingVisitor(Map<String, String> reverseAliasMap) {- this.reverseAliasMap = reverseAliasMap;- }-- protected Boolean visitLogicalBinaryExpression(LogicalBinaryExpression node, Tuple tuple) {-- Boolean b = process(node.getLeft(), tuple);- if(node.getType() == LogicalBinaryExpression.Type.AND) {- if(!b) {- //Short circuit- return false;- } else {- return process(node.getRight(), tuple);- }- } else {- if(b) {- //Short circuit- return true;- } else {- return process(node.getRight(), tuple);- }- }- }-- protected Boolean visitComparisonExpression(ComparisonExpression node, Tuple tuple) {- String field = getHavingField(node.getLeft());-- if(reverseAliasMap.containsKey(field)) {- field = reverseAliasMap.get(field);- }-- double d = Double.parseDouble(node.getRight().toString());- double td = tuple.getDouble(field);- ComparisonExpression.Type t = node.getType();--- switch(t) {- case LESS_THAN:- return td < d;- case LESS_THAN_OR_EQUAL:- return td <= d;- case NOT_EQUAL:- return td != d;- case EQUAL:- return td == d;- case GREATER_THAN:- return td > d;- case GREATER_THAN_OR_EQUAL:- return td >= d;- default:- return false;- }}}- }+}diff --cc solr/solrj/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.javaindex bdd9a0ad771,1829c70f54a..284192270ea--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.java@@@ -489,17 -478,21 +489,17 @@@ public class JdbcTest extends SolrCloud}List<String> collections = new ArrayList<>();- collections.addAll(cluster.getSolrClient().getZkStateReader().getClusterState().getCollections());+ collections.addAll(cluster.getSolrClient().getZkStateReader().getClusterState().getCollectionsMap().keySet());Collections.sort(collections);- try(ResultSet rs = databaseMetaData.getSchemas()) {- assertFalse(rs.next());- }-- try(ResultSet rs = databaseMetaData.getTables(zkHost, null, "%", null)) {+ try(ResultSet rs = databaseMetaData.getTables(null, zkHost, "%", null)) {for(String acollection : collections) {assertTrue(rs.next());- assertEquals(zkHost, rs.getString("TABLE_CAT"));- assertNull(rs.getString("TABLE_SCHEM"));- assertEquals(acollection, rs.getString("TABLE_NAME"));- assertEquals("TABLE", rs.getString("TABLE_TYPE"));- assertNull(rs.getString("REMARKS"));+ assertNull(rs.getString("tableCat"));+ assertEquals(zkHost, rs.getString("tableSchem"));+ assertEquals(acollection, rs.getString("tableName"));+ assertEquals("TABLE", rs.getString("tableType"));+ assertNull(rs.getString("remarks"));}assertFalse(rs.next());}