-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathPlannerHints.java
More file actions
94 lines (77 loc) · 2.99 KB
/
Copy pathPlannerHints.java
File metadata and controls
94 lines (77 loc) · 2.99 KB
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
/*
* Copyright © 2021 DataSQRL (contact@datasqrl.com)
*
* Licensed 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 com.datasqrl.planner.hint;
import com.datasqrl.error.ErrorCollector;
import com.datasqrl.error.ErrorLabel;
import com.datasqrl.planner.parser.SqrlComments;
import com.datasqrl.planner.parser.StatementParserException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.apache.calcite.rel.type.RelDataTypeField;
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class PlannerHints {
public static final PlannerHints EMPTY = new PlannerHints(List.of());
private final List<PlannerHint> hints;
public static PlannerHints from(
SqrlComments comments, Optional<PlannerHints> inheritedHints, ErrorCollector errors) {
var hints = new ArrayList<PlannerHint>();
inheritedHints.ifPresent(inherited -> hints.addAll(inherited.hints));
comments.hints().stream()
.map(c -> PlannerHint.from(c, errors))
.flatMap(Optional::stream)
.forEach(hints::add);
return new PlannerHints(hints);
}
public Optional<ColumnNamesHint> getQueryByHint() {
var queryBy = hints.stream().filter(QueryByHint.class::isInstance).toList();
if (queryBy.size() > 1) {
throw new StatementParserException(
ErrorLabel.GENERIC,
queryBy.get(1).getSource().getFileLocation(),
"Cannot have more than one `query_by_*` or `no_query` hint per table");
}
return queryBy.stream().map(ColumnNamesHint.class::cast).findFirst();
}
public boolean isEmpty() {
return hints.isEmpty();
}
public boolean isTest() {
return getHint(TestHint.class).isPresent();
}
public boolean isNoRowsTest() {
return getHint(TestHint.class).map(TestHint::isNoRows).orElse(false);
}
public boolean isWorkload() {
return getHint(WorkloadHint.class).isPresent();
}
public boolean isNoSource() {
return getHint(NoSourceHint.class).isPresent();
}
public <H> Optional<H> getHint(Class<H> hintClass) {
return getHints(hintClass).findFirst();
}
public <H> Stream<H> getHints(Class<H> hintClass) {
return hints.stream().filter(hintClass::isInstance).map(hintClass::cast);
}
public void updateColumnNamesHints(Function<String, RelDataTypeField> fieldByIndex) {
getHints(ColumnNamesHint.class).forEach(hint -> hint.validateAndUpdate(fieldByIndex));
}
}