forked from opendistro-for-elasticsearch/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElasticsearchIndex.java
162 lines (144 loc) · 6.81 KB
/
ElasticsearchIndex.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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 com.amazon.opendistroforelasticsearch.sql.elasticsearch.storage;
import com.amazon.opendistroforelasticsearch.sql.common.setting.Settings;
import com.amazon.opendistroforelasticsearch.sql.common.utils.StringUtils;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.client.ElasticsearchClient;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.data.value.ElasticsearchExprValueFactory;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.planner.logical.ElasticsearchLogicalIndexAgg;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.planner.logical.ElasticsearchLogicalIndexScan;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.planner.logical.ElasticsearchLogicalPlanOptimizerFactory;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.request.system.ElasticsearchDescribeIndexRequest;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.storage.script.aggregation.AggregationQueryBuilder;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.storage.script.filter.FilterQueryBuilder;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.storage.script.sort.SortQueryBuilder;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.storage.serialization.DefaultExpressionSerializer;
import com.amazon.opendistroforelasticsearch.sql.planner.DefaultImplementor;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalPlan;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalRelation;
import com.amazon.opendistroforelasticsearch.sql.planner.physical.PhysicalPlan;
import com.amazon.opendistroforelasticsearch.sql.storage.Table;
import com.google.common.annotations.VisibleForTesting;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;
/** Elasticsearch table (index) implementation. */
@RequiredArgsConstructor
public class ElasticsearchIndex implements Table {
/** Elasticsearch client connection. */
private final ElasticsearchClient client;
private final Settings settings;
/** Current Elasticsearch index name. */
private final String indexName;
/*
* TODO: Assume indexName doesn't have wildcard.
* Need to either handle field name conflicts
* or lazy evaluate when query engine pulls field type.
*/
@Override
public Map<String, ExprType> getFieldTypes() {
return new ElasticsearchDescribeIndexRequest(client, indexName).getFieldTypes();
}
/**
* TODO: Push down operations to index scan operator as much as possible in future.
*/
@Override
public PhysicalPlan implement(LogicalPlan plan) {
ElasticsearchIndexScan indexScan = new ElasticsearchIndexScan(client, settings, indexName,
new ElasticsearchExprValueFactory(getFieldTypes()));
/*
* Visit logical plan with index scan as context so logical operators visited, such as
* aggregation, filter, will accumulate (push down) Elasticsearch query and aggregation DSL on
* index scan.
*/
return plan.accept(new ElasticsearchDefaultImplementor(indexScan), indexScan);
}
@Override
public LogicalPlan optimize(LogicalPlan plan) {
return ElasticsearchLogicalPlanOptimizerFactory.create().optimize(plan);
}
@VisibleForTesting
@RequiredArgsConstructor
public static class ElasticsearchDefaultImplementor
extends DefaultImplementor<ElasticsearchIndexScan> {
private final ElasticsearchIndexScan indexScan;
@Override
public PhysicalPlan visitNode(LogicalPlan plan, ElasticsearchIndexScan context) {
if (plan instanceof ElasticsearchLogicalIndexScan) {
return visitIndexScan((ElasticsearchLogicalIndexScan) plan, context);
} else if (plan instanceof ElasticsearchLogicalIndexAgg) {
return visitIndexAggregation((ElasticsearchLogicalIndexAgg) plan, context);
} else {
throw new IllegalStateException(StringUtils.format("unexpected plan node type %s",
plan.getClass()));
}
}
/**
* Implement ElasticsearchLogicalIndexScan.
*/
public PhysicalPlan visitIndexScan(ElasticsearchLogicalIndexScan node,
ElasticsearchIndexScan context) {
if (null != node.getSortList()) {
final SortQueryBuilder builder = new SortQueryBuilder();
context.pushDownSort(node.getSortList().stream()
.map(sort -> builder.build(sort.getValue(), sort.getKey()))
.collect(Collectors.toList()));
}
if (null != node.getFilter()) {
FilterQueryBuilder queryBuilder = new FilterQueryBuilder(new DefaultExpressionSerializer());
QueryBuilder query = queryBuilder.build(node.getFilter());
context.pushDown(query);
}
if (node.getLimit() != null) {
context.pushDownLimit(node.getLimit(), node.getOffset());
}
if (node.hasProjects()) {
context.pushDownProjects(node.getProjectList());
}
return indexScan;
}
/**
* Implement ElasticsearchLogicalIndexAgg.
*/
public PhysicalPlan visitIndexAggregation(ElasticsearchLogicalIndexAgg node,
ElasticsearchIndexScan context) {
if (node.getFilter() != null) {
FilterQueryBuilder queryBuilder = new FilterQueryBuilder(
new DefaultExpressionSerializer());
QueryBuilder query = queryBuilder.build(node.getFilter());
context.pushDown(query);
}
AggregationQueryBuilder builder =
new AggregationQueryBuilder(new DefaultExpressionSerializer());
List<AggregationBuilder> aggregationBuilder =
builder.buildAggregationBuilder(node.getAggregatorList(),
node.getGroupByList(), node.getSortList());
context.pushDownAggregation(aggregationBuilder);
context.pushTypeMapping(
builder.buildTypeMapping(node.getAggregatorList(),
node.getGroupByList()));
return indexScan;
}
@Override
public PhysicalPlan visitRelation(LogicalRelation node, ElasticsearchIndexScan context) {
return indexScan;
}
}
}