Skip to content

Commit 8f19d4a

Browse files
committed
New IR -- WIP
1 parent 67c941f commit 8f19d4a

Some content is hidden

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

44 files changed

+5337
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.trino.sql.dialect.ir;
15+
16+
import com.google.common.collect.ImmutableMap;
17+
import io.trino.sql.newir.Operation.AttributeKey;
18+
19+
import java.util.Map;
20+
21+
public class Dialect
22+
{
23+
// dialect name
24+
public static final String IR = "ir";
25+
26+
public static final String TERMINAL = "terminal";
27+
28+
private Dialect() {}
29+
30+
public static void terminalOperation(ImmutableMap.Builder<AttributeKey, Object> builder)
31+
{
32+
builder.put(new AttributeKey(IR, TERMINAL), true);
33+
}
34+
35+
public static Map<AttributeKey, Object> terminalOperation()
36+
{
37+
return ImmutableMap.of(new AttributeKey(IR, TERMINAL), true);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.trino.sql.dialect.trino;
15+
16+
import com.google.common.collect.ImmutableList;
17+
import com.google.common.collect.ImmutableMap;
18+
import io.trino.metadata.ResolvedFunction;
19+
import io.trino.spi.type.Type;
20+
import io.trino.sql.ir.Comparison;
21+
import io.trino.sql.ir.Logical;
22+
import io.trino.sql.newir.Operation.AttributeKey;
23+
24+
import java.util.List;
25+
import java.util.Map;
26+
27+
import static io.trino.sql.dialect.trino.Dialect.TRINO;
28+
import static java.util.Objects.requireNonNull;
29+
30+
public class Attributes
31+
{
32+
public static final AttributeMetadata<Long> CARDINALITY = new AttributeMetadata<>("cardinality", Long.class, true);
33+
public static final AttributeMetadata<ComparisonOperator> COMPARISON_OPERATOR = new AttributeMetadata<>("comparison_operator", ComparisonOperator.class, false);
34+
public static final AttributeMetadata<ConstantResult> CONSTANT_RESULT = new AttributeMetadata<>("constant_result", ConstantResult.class, true);
35+
public static final AttributeMetadata<Integer> FIELD_INDEX = new AttributeMetadata<>("field_index", Integer.class, false);
36+
public static final AttributeMetadata<String> FIELD_NAME = new AttributeMetadata<>("field_name", String.class, false);
37+
public static final AttributeMetadata<JoinType> JOIN_TYPE = new AttributeMetadata<>("join_type", JoinType.class, false);
38+
public static final AttributeMetadata<LogicalOperator> LOGICAL_OPERATOR = new AttributeMetadata<>("logical_operator", LogicalOperator.class, false);
39+
public static final AttributeMetadata<OutputNames> OUTPUT_NAMES = new AttributeMetadata<>("output_names", OutputNames.class, false);
40+
public static final AttributeMetadata<ResolvedFunction> RESOLVED_FUNCTION = new AttributeMetadata<>("resolved_function", ResolvedFunction.class, false);
41+
42+
// TODO define attributes for deeply nested fields, not just top level or column level
43+
44+
private Attributes() {}
45+
46+
public static class AttributeMetadata<T>
47+
{
48+
private final String name;
49+
private final Class<T> type;
50+
private final boolean external;
51+
52+
private AttributeMetadata(String name, Class<T> type, boolean external)
53+
{
54+
this.name = requireNonNull(name, "name is null");
55+
this.type = requireNonNull(type, "type is null");
56+
this.external = external;
57+
}
58+
59+
public T getAttribute(Map<AttributeKey, Object> map)
60+
{
61+
return this.type.cast(map.get(new AttributeKey(TRINO, name)));
62+
}
63+
64+
public void putAttribute(ImmutableMap.Builder<AttributeKey, Object> builder, T attribute)
65+
{
66+
builder.put(new AttributeKey(TRINO, name), attribute);
67+
}
68+
69+
public T putAttribute(Map<AttributeKey, Object> map, T attribute)
70+
{
71+
return this.type.cast(map.put(new AttributeKey(TRINO, name), attribute));
72+
}
73+
74+
public Map<AttributeKey, Object> asMap(T attribute)
75+
{
76+
return ImmutableMap.of(new AttributeKey(TRINO, name), attribute);
77+
}
78+
}
79+
80+
public record ConstantResult(Type type, Object value)
81+
{
82+
public ConstantResult
83+
{
84+
requireNonNull(type, "type is null");
85+
}
86+
87+
@Override
88+
public String toString()
89+
{
90+
return value.toString() + ":" + type.toString();
91+
}
92+
}
93+
94+
public enum JoinType
95+
{
96+
INNER,
97+
LEFT,
98+
RIGHT,
99+
FULL;
100+
101+
public static JoinType of(io.trino.sql.planner.plan.JoinType joinType)
102+
{
103+
return switch (joinType) {
104+
case INNER -> INNER;
105+
case LEFT -> LEFT;
106+
case RIGHT -> RIGHT;
107+
case FULL -> FULL;
108+
};
109+
}
110+
}
111+
112+
public record OutputNames(List<String> outputNames)
113+
{
114+
public OutputNames(List<String> outputNames)
115+
{
116+
this.outputNames = ImmutableList.copyOf(requireNonNull(outputNames, "outputNames is null"));
117+
}
118+
119+
@Override
120+
public String toString()
121+
{
122+
return outputNames.toString();
123+
}
124+
}
125+
126+
public enum LogicalOperator
127+
{
128+
AND,
129+
OR;
130+
131+
public static LogicalOperator of(Logical.Operator operator)
132+
{
133+
return switch (operator) {
134+
case AND -> AND;
135+
case OR -> OR;
136+
};
137+
}
138+
}
139+
140+
public enum ComparisonOperator
141+
{
142+
EQUAL("="),
143+
NOT_EQUAL("<>"),
144+
LESS_THAN("<"),
145+
LESS_THAN_OR_EQUAL("<="),
146+
GREATER_THAN(">"),
147+
GREATER_THAN_OR_EQUAL(">="),
148+
IDENTICAL("≡"); // not distinct
149+
150+
private final String value;
151+
152+
ComparisonOperator(String value)
153+
{
154+
this.value = value;
155+
}
156+
157+
public String getValue()
158+
{
159+
return value;
160+
}
161+
162+
public static ComparisonOperator of(Comparison.Operator operator)
163+
{
164+
return switch (operator) {
165+
case EQUAL -> EQUAL;
166+
case NOT_EQUAL -> NOT_EQUAL;
167+
case LESS_THAN -> LESS_THAN;
168+
case LESS_THAN_OR_EQUAL -> LESS_THAN_OR_EQUAL;
169+
case GREATER_THAN -> GREATER_THAN;
170+
case GREATER_THAN_OR_EQUAL -> GREATER_THAN_OR_EQUAL;
171+
case IDENTICAL -> IDENTICAL;
172+
};
173+
}
174+
}
175+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.trino.sql.dialect.trino;
15+
16+
import com.google.common.collect.ImmutableList;
17+
import com.google.common.collect.ImmutableMap;
18+
import io.trino.sql.newir.Block;
19+
import io.trino.sql.planner.Symbol;
20+
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
import static com.google.common.collect.ImmutableMap.toImmutableMap;
25+
import static java.util.HashMap.newHashMap;
26+
import static java.util.Objects.requireNonNull;
27+
28+
public record Context(Block.Builder block, Map<Symbol, RowField> symbolMapping)
29+
{
30+
public Context(Block.Builder block)
31+
{
32+
this(block, Map.of());
33+
}
34+
35+
public Context(Block.Builder block, Map<Symbol, RowField> symbolMapping)
36+
{
37+
this.block = requireNonNull(block, "block is null");
38+
this.symbolMapping = ImmutableMap.copyOf(requireNonNull(symbolMapping, "symbolMapping is null"));
39+
}
40+
41+
public static Map<Symbol, RowField> argumentMapping(Block.Parameter parameter, Map<Symbol, String> symbolMapping)
42+
{
43+
return symbolMapping.entrySet().stream()
44+
.collect(toImmutableMap(
45+
Map.Entry::getKey,
46+
entry -> new RowField(parameter, entry.getValue())));
47+
}
48+
49+
public static Map<Symbol, RowField> composedMapping(Context context, Map<Symbol, RowField> newMapping)
50+
{
51+
return composedMapping(context, ImmutableList.of(newMapping));
52+
}
53+
54+
/**
55+
* Compose the correlated mapping from the context with symbol mappings for the current block parameters.
56+
*
57+
* @param context rewrite context containing symbol mapping from all levels of correlation
58+
* @param newMappings list of symbol mappings for current block parameters
59+
* @return composed symbol mapping to rewrite the current block
60+
*/
61+
public static Map<Symbol, RowField> composedMapping(Context context, List<Map<Symbol, RowField>> newMappings)
62+
{
63+
Map<Symbol, RowField> composed = newHashMap(context.symbolMapping().size() + newMappings.stream().mapToInt(Map::size).sum());
64+
composed.putAll(context.symbolMapping());
65+
newMappings.stream().forEach(composed::putAll);
66+
return composed;
67+
}
68+
69+
public record RowField(Block.Parameter row, String field)
70+
{
71+
public RowField
72+
{
73+
requireNonNull(row, "row is null");
74+
requireNonNull(field, "field is null");
75+
}
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.trino.sql.dialect.trino;
15+
16+
import io.trino.spi.TrinoException;
17+
import io.trino.sql.newir.Type;
18+
19+
import static io.trino.spi.StandardErrorCode.IR_ERROR;
20+
import static java.lang.String.format;
21+
22+
public class Dialect
23+
{
24+
// dialect name
25+
public static final String TRINO = "trino";
26+
27+
private Dialect() {}
28+
29+
public static io.trino.spi.type.Type trinoType(Type type)
30+
{
31+
if (!type.dialect().equals(TRINO)) {
32+
throw new TrinoException(IR_ERROR, format("expected a type of the %s dialect, actual dialect: %s", TRINO, type.dialect()));
33+
}
34+
35+
if (type.dialectType() instanceof io.trino.spi.type.Type trinoType) {
36+
return trinoType;
37+
}
38+
39+
throw new TrinoException(IR_ERROR, "expected a trino type, actual: " + type.dialectType().getClass().getSimpleName());
40+
}
41+
42+
public static Type irType(io.trino.spi.type.Type type)
43+
{
44+
return new Type(TRINO, type);
45+
}
46+
}

0 commit comments

Comments
 (0)