Skip to content

Commit ed4c70f

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

27 files changed

+2089
-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,101 @@
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+
22+
import static com.google.common.base.Preconditions.checkArgument;
23+
import static com.google.common.base.Preconditions.checkState;
24+
import static java.util.Objects.requireNonNull;
25+
26+
public record Block(Optional<String> name, List<Argument> arguments, List<Operation> operations)
27+
implements Node
28+
{
29+
public record Argument(String name, IrType type) // TODO validate name
30+
// TODO accept "name" at construction, and add "^"??
31+
implements Value
32+
{
33+
@Override
34+
public Block source(Program program)
35+
{
36+
return program.getBlock(this);
37+
}
38+
}
39+
40+
public Block(Optional<String> name, List<Argument> arguments, List<Operation> operations)
41+
{
42+
this.name = requireNonNull(name, "name is null"); // TODO validate name
43+
this.arguments = ImmutableList.copyOf(requireNonNull(arguments, "argumentNames is null"));
44+
this.operations = ImmutableList.copyOf(requireNonNull(operations, "operations is null"));
45+
46+
// TODO verify that ends with a terminal operation
47+
}
48+
49+
@Override
50+
public String print(int indentLevel)
51+
{
52+
return "block";
53+
}
54+
55+
@Override
56+
public String prettyPrint(int indentLevel)
57+
{
58+
return "pretty block";
59+
}
60+
61+
public int getIndex(Argument argument)
62+
{
63+
int index = arguments.indexOf(argument);
64+
checkState(index >= 0, "no chyba nie"); // TODO error
65+
return index;
66+
}
67+
68+
public Operation getTerminalOperation()
69+
{
70+
return operations.getLast();
71+
}
72+
73+
public IrType getReturnedType()
74+
{
75+
return getTerminalOperation().result().type();
76+
}
77+
78+
public static class Builder
79+
{
80+
private final Optional<String> name;
81+
private final List<Argument> arguments;
82+
private final ImmutableList.Builder<Operation> operations = ImmutableList.builder();
83+
84+
public Builder(Optional<String> name, List<Argument> arguments)
85+
{
86+
this.name = name;
87+
this.arguments = arguments;
88+
}
89+
90+
public Builder addOperation(Operation operation)
91+
{
92+
operations.add(operation); // TODO will throw on null.
93+
return this;
94+
}
95+
96+
public Block build()
97+
{
98+
return new Block(name, arguments, operations.build());
99+
}
100+
}
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 final Result result;
30+
private final Set<Attribute> attributes;
31+
32+
public Constant(String resultName, IrType type, Object value)
33+
{
34+
requireNonNull(resultName, "resultName is null");
35+
36+
this.result = new Result(resultName, type);
37+
38+
this.attributes = ImmutableSet.of(new ConstantResult<>(type, value));
39+
}
40+
41+
@Override
42+
public Result result() {
43+
return result;
44+
}
45+
46+
@Override
47+
public List<Value> arguments()
48+
{
49+
return ImmutableList.of();
50+
}
51+
52+
@Override
53+
public List<Region> regions()
54+
{
55+
return ImmutableList.of();
56+
}
57+
58+
@Override
59+
public Set<Attribute> attributes() {
60+
return attributes;
61+
}
62+
63+
@Override
64+
public String print(int indentLevel)
65+
{
66+
return "constant";
67+
}
68+
69+
@Override
70+
public String prettyPrint(int indentLevel)
71+
{
72+
return "pretty constant";
73+
}
74+
75+
@Override
76+
public boolean equals(Object obj)
77+
{
78+
if (obj == this) {return true;}
79+
if (obj == null || obj.getClass() != this.getClass()) {return false;}
80+
var that = (Constant) obj;
81+
return Objects.equals(this.result, that.result) &&
82+
Objects.equals(this.attributes, that.attributes);
83+
}
84+
85+
@Override
86+
public int hashCode()
87+
{
88+
return Objects.hash(result, attributes);
89+
}
90+
}
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)