Skip to content

Commit

Permalink
New IR -- WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiafi committed Dec 13, 2024
1 parent 67c941f commit 6ea030e
Show file tree
Hide file tree
Showing 29 changed files with 2,301 additions and 1 deletion.
53 changes: 53 additions & 0 deletions core/trino-main/src/main/java/io/trino/sql/newir/Attribute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 io.trino.sql.newir;

import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.Immutable;
import io.trino.spi.type.IrType;

import java.util.List;

import static java.util.Objects.requireNonNull;

@Immutable
public interface Attribute
{}

record ConstantResult<T extends IrType>(T type, Object value)
implements Attribute // TODO rnn
{}

record OutputNames(List<String> names)
implements Attribute
{
OutputNames(List<String> names)
{
this.names = ImmutableList.copyOf(requireNonNull(names, "names is null"));
}
}

record FieldName(String name)
implements Attribute
{
FieldName
{
requireNonNull(name, "name is null");
}
}

record Cardinality(int cardinality)
implements Attribute
{
}
129 changes: 129 additions & 0 deletions core/trino-main/src/main/java/io/trino/sql/newir/Block.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* 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 io.trino.sql.newir;

import com.google.common.collect.ImmutableList;
import io.trino.spi.type.IrType;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static io.trino.sql.newir.PrinterOptions.INDENT;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;

public record Block(Optional<String> name, List<Argument> arguments, List<Operation> operations)
implements Node
{
public record Argument(String name, IrType type) // TODO validate name
// TODO accept "name" at construction, and add "^"??
implements Value
{
@Override
public Block source(Program program)
{
return program.getBlock(this);
}
}

public Block(Optional<String> name, List<Argument> arguments, List<Operation> operations)
{
this.name = requireNonNull(name, "name is null"); // TODO validate name
this.arguments = ImmutableList.copyOf(requireNonNull(arguments, "argumentNames is null"));
this.operations = ImmutableList.copyOf(requireNonNull(operations, "operations is null"));
// TODO verify list not emtpy

// TODO verify that ends with a terminal operation
}

@Override
public String print(int indentLevel)
{
StringBuilder builder = new StringBuilder();
String indent = INDENT.repeat(indentLevel);

builder.append(indent)
.append(name().orElse(""));

if (!arguments().isEmpty()) {
builder.append(arguments().stream()
.map(argument -> argument.name() + " : " + argument.type())
.collect(joining(", ", " (", ")")));
}

builder.append(operations().stream()
.map(operation -> operation.print(indentLevel + 1))
.collect(joining("\n", "\n", "")));

return builder.toString();
}

@Override
public String prettyPrint(int indentLevel)
{
return "pretty block";
}

public int getIndex(Argument argument)
{
int index = arguments.indexOf(argument);
checkState(index >= 0, "no chyba nie"); // TODO error
return index;
}

public Operation getTerminalOperation()
{
return operations.getLast();
}

public IrType getReturnedType()
{
return getTerminalOperation().result().type();
}

public static class Builder
{
private final Optional<String> name;
private final List<Argument> arguments;
private final ImmutableList.Builder<Operation> operations = ImmutableList.builder();
private Optional<Operation> recentOperation = Optional.empty();

public Builder(Optional<String> name, List<Argument> arguments)
{
this.name = name;
this.arguments = arguments;
}

public Builder addOperation(Operation operation)
{
operations.add(operation); // TODO will throw on null.
recentOperation = Optional.of(operation);
return this;
}

// access to the recently added operation allows the caller to insert a return operation or a navigating operation (in the future)
public Operation recentOperation()
{
return recentOperation.orElseThrow(() -> new RuntimeException("no operations added yet")); // TODO error
}

public Block build()
{
return new Block(name, arguments, operations.build());
}
}
}
92 changes: 92 additions & 0 deletions core/trino-main/src/main/java/io/trino/sql/newir/Constant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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 io.trino.sql.newir;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import io.trino.spi.type.IrType;

import java.util.List;
import java.util.Objects;
import java.util.Set;

import static java.util.Objects.requireNonNull;

public final class Constant
implements Operation
{
private static final String NAME = "constant";

private final Result result;
private final Set<Attribute> attributes;

public Constant(String resultName, IrType type, Object value)
{
requireNonNull(resultName, "resultName is null");

this.result = new Result(resultName, type);

this.attributes = ImmutableSet.of(new ConstantResult<>(type, value)); // TODO have another attr with the actual value unwrapped
}

@Override
public String name()
{
return NAME;
}

@Override
public Result result() {
return result;
}

@Override
public List<Value> arguments()
{
return ImmutableList.of();
}

@Override
public List<Region> regions()
{
return ImmutableList.of();
}

@Override
public Set<Attribute> attributes() {
return attributes;
}

@Override
public String prettyPrint(int indentLevel)
{
return "pretty constant";
}

@Override
public boolean equals(Object obj)
{
if (obj == this) {return true;}
if (obj == null || obj.getClass() != this.getClass()) {return false;}
var that = (Constant) obj;
return Objects.equals(this.result, that.result) &&
Objects.equals(this.attributes, that.attributes);
}

@Override
public int hashCode()
{
return Objects.hash(result, attributes);
}
}
62 changes: 62 additions & 0 deletions core/trino-main/src/main/java/io/trino/sql/newir/Context.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 io.trino.sql.newir;

import com.google.common.collect.ImmutableMap;
import io.trino.sql.planner.Symbol;

import java.util.Map;

import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static java.util.HashMap.newHashMap;
import static java.util.Objects.requireNonNull;

public record Context(Block.Builder block, Map<Symbol, RowField> symbolMapping)
{
public Context(Block.Builder block)
{
this(block, Map.of());
}

public Context(Block.Builder block, Map<Symbol, RowField> symbolMapping)
{
this.block = requireNonNull(block, "block is null");
this.symbolMapping = ImmutableMap.copyOf(requireNonNull(symbolMapping, "symbolMapping is null"));
}

public static Map<Symbol, RowField> argumentMapping(Block.Argument argument, Map<Symbol, String> symbolMapping)
{
return symbolMapping.entrySet().stream()
.collect(toImmutableMap(
Map.Entry::getKey,
entry -> new RowField(argument, entry.getValue())));
}

public static Map<Symbol, RowField> composedMapping(Context context, Map<Symbol, RowField> newMapping)
{
Map<Symbol, RowField> composed = newHashMap(context.symbolMapping().size() + newMapping.size());
composed.putAll(context.symbolMapping());
composed.putAll(newMapping);
return composed;
}

public record RowField(Block.Argument row, String field)
{
public RowField
{
requireNonNull(row, "row is null");
requireNonNull(field, "field is null");
}
}
}
Loading

0 comments on commit 6ea030e

Please sign in to comment.