Skip to content

Commit 6ea030e

Browse files
committed
New IR -- WIP
1 parent 67c941f commit 6ea030e

29 files changed

+2301
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.newir;
15+
16+
import com.google.common.collect.ImmutableList;
17+
import com.google.errorprone.annotations.Immutable;
18+
import io.trino.spi.type.IrType;
19+
20+
import java.util.List;
21+
22+
import static java.util.Objects.requireNonNull;
23+
24+
@Immutable
25+
public interface Attribute
26+
{}
27+
28+
record ConstantResult<T extends IrType>(T type, Object value)
29+
implements Attribute // TODO rnn
30+
{}
31+
32+
record OutputNames(List<String> names)
33+
implements Attribute
34+
{
35+
OutputNames(List<String> names)
36+
{
37+
this.names = ImmutableList.copyOf(requireNonNull(names, "names is null"));
38+
}
39+
}
40+
41+
record FieldName(String name)
42+
implements Attribute
43+
{
44+
FieldName
45+
{
46+
requireNonNull(name, "name is null");
47+
}
48+
}
49+
50+
record Cardinality(int cardinality)
51+
implements Attribute
52+
{
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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.newir;
15+
16+
import com.google.common.collect.ImmutableList;
17+
import io.trino.spi.type.IrType;
18+
19+
import java.util.List;
20+
import java.util.Optional;
21+
import java.util.stream.Collectors;
22+
23+
import static com.google.common.base.Preconditions.checkArgument;
24+
import static com.google.common.base.Preconditions.checkState;
25+
import static io.trino.sql.newir.PrinterOptions.INDENT;
26+
import static java.util.Objects.requireNonNull;
27+
import static java.util.stream.Collectors.joining;
28+
29+
public record Block(Optional<String> name, List<Argument> arguments, List<Operation> operations)
30+
implements Node
31+
{
32+
public record Argument(String name, IrType type) // TODO validate name
33+
// TODO accept "name" at construction, and add "^"??
34+
implements Value
35+
{
36+
@Override
37+
public Block source(Program program)
38+
{
39+
return program.getBlock(this);
40+
}
41+
}
42+
43+
public Block(Optional<String> name, List<Argument> arguments, List<Operation> operations)
44+
{
45+
this.name = requireNonNull(name, "name is null"); // TODO validate name
46+
this.arguments = ImmutableList.copyOf(requireNonNull(arguments, "argumentNames is null"));
47+
this.operations = ImmutableList.copyOf(requireNonNull(operations, "operations is null"));
48+
// TODO verify list not emtpy
49+
50+
// TODO verify that ends with a terminal operation
51+
}
52+
53+
@Override
54+
public String print(int indentLevel)
55+
{
56+
StringBuilder builder = new StringBuilder();
57+
String indent = INDENT.repeat(indentLevel);
58+
59+
builder.append(indent)
60+
.append(name().orElse(""));
61+
62+
if (!arguments().isEmpty()) {
63+
builder.append(arguments().stream()
64+
.map(argument -> argument.name() + " : " + argument.type())
65+
.collect(joining(", ", " (", ")")));
66+
}
67+
68+
builder.append(operations().stream()
69+
.map(operation -> operation.print(indentLevel + 1))
70+
.collect(joining("\n", "\n", "")));
71+
72+
return builder.toString();
73+
}
74+
75+
@Override
76+
public String prettyPrint(int indentLevel)
77+
{
78+
return "pretty block";
79+
}
80+
81+
public int getIndex(Argument argument)
82+
{
83+
int index = arguments.indexOf(argument);
84+
checkState(index >= 0, "no chyba nie"); // TODO error
85+
return index;
86+
}
87+
88+
public Operation getTerminalOperation()
89+
{
90+
return operations.getLast();
91+
}
92+
93+
public IrType getReturnedType()
94+
{
95+
return getTerminalOperation().result().type();
96+
}
97+
98+
public static class Builder
99+
{
100+
private final Optional<String> name;
101+
private final List<Argument> arguments;
102+
private final ImmutableList.Builder<Operation> operations = ImmutableList.builder();
103+
private Optional<Operation> recentOperation = Optional.empty();
104+
105+
public Builder(Optional<String> name, List<Argument> arguments)
106+
{
107+
this.name = name;
108+
this.arguments = arguments;
109+
}
110+
111+
public Builder addOperation(Operation operation)
112+
{
113+
operations.add(operation); // TODO will throw on null.
114+
recentOperation = Optional.of(operation);
115+
return this;
116+
}
117+
118+
// access to the recently added operation allows the caller to insert a return operation or a navigating operation (in the future)
119+
public Operation recentOperation()
120+
{
121+
return recentOperation.orElseThrow(() -> new RuntimeException("no operations added yet")); // TODO error
122+
}
123+
124+
public Block build()
125+
{
126+
return new Block(name, arguments, operations.build());
127+
}
128+
}
129+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.newir;
15+
16+
import com.google.common.collect.ImmutableList;
17+
import com.google.common.collect.ImmutableSet;
18+
import io.trino.spi.type.IrType;
19+
20+
import java.util.List;
21+
import java.util.Objects;
22+
import java.util.Set;
23+
24+
import static java.util.Objects.requireNonNull;
25+
26+
public final class Constant
27+
implements Operation
28+
{
29+
private static final String NAME = "constant";
30+
31+
private final Result result;
32+
private final Set<Attribute> attributes;
33+
34+
public Constant(String resultName, IrType type, Object value)
35+
{
36+
requireNonNull(resultName, "resultName is null");
37+
38+
this.result = new Result(resultName, type);
39+
40+
this.attributes = ImmutableSet.of(new ConstantResult<>(type, value)); // TODO have another attr with the actual value unwrapped
41+
}
42+
43+
@Override
44+
public String name()
45+
{
46+
return NAME;
47+
}
48+
49+
@Override
50+
public Result result() {
51+
return result;
52+
}
53+
54+
@Override
55+
public List<Value> arguments()
56+
{
57+
return ImmutableList.of();
58+
}
59+
60+
@Override
61+
public List<Region> regions()
62+
{
63+
return ImmutableList.of();
64+
}
65+
66+
@Override
67+
public Set<Attribute> attributes() {
68+
return attributes;
69+
}
70+
71+
@Override
72+
public String prettyPrint(int indentLevel)
73+
{
74+
return "pretty constant";
75+
}
76+
77+
@Override
78+
public boolean equals(Object obj)
79+
{
80+
if (obj == this) {return true;}
81+
if (obj == null || obj.getClass() != this.getClass()) {return false;}
82+
var that = (Constant) obj;
83+
return Objects.equals(this.result, that.result) &&
84+
Objects.equals(this.attributes, that.attributes);
85+
}
86+
87+
@Override
88+
public int hashCode()
89+
{
90+
return Objects.hash(result, attributes);
91+
}
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.newir;
15+
16+
import com.google.common.collect.ImmutableMap;
17+
import io.trino.sql.planner.Symbol;
18+
19+
import java.util.Map;
20+
21+
import static com.google.common.collect.ImmutableMap.toImmutableMap;
22+
import static java.util.HashMap.newHashMap;
23+
import static java.util.Objects.requireNonNull;
24+
25+
public record Context(Block.Builder block, Map<Symbol, RowField> symbolMapping)
26+
{
27+
public Context(Block.Builder block)
28+
{
29+
this(block, Map.of());
30+
}
31+
32+
public Context(Block.Builder block, Map<Symbol, RowField> symbolMapping)
33+
{
34+
this.block = requireNonNull(block, "block is null");
35+
this.symbolMapping = ImmutableMap.copyOf(requireNonNull(symbolMapping, "symbolMapping is null"));
36+
}
37+
38+
public static Map<Symbol, RowField> argumentMapping(Block.Argument argument, Map<Symbol, String> symbolMapping)
39+
{
40+
return symbolMapping.entrySet().stream()
41+
.collect(toImmutableMap(
42+
Map.Entry::getKey,
43+
entry -> new RowField(argument, entry.getValue())));
44+
}
45+
46+
public static Map<Symbol, RowField> composedMapping(Context context, Map<Symbol, RowField> newMapping)
47+
{
48+
Map<Symbol, RowField> composed = newHashMap(context.symbolMapping().size() + newMapping.size());
49+
composed.putAll(context.symbolMapping());
50+
composed.putAll(newMapping);
51+
return composed;
52+
}
53+
54+
public record RowField(Block.Argument row, String field)
55+
{
56+
public RowField
57+
{
58+
requireNonNull(row, "row is null");
59+
requireNonNull(field, "field is null");
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)