|
| 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.spi.type.Type; |
| 19 | +import io.trino.sql.ir.Logical; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import static java.util.Objects.requireNonNull; |
| 25 | + |
| 26 | +public class Attributes |
| 27 | +{ |
| 28 | + public static final AttributeMetadata<Long> CARDINALITY = new AttributeMetadata<>("cardinality", Long.class, true); |
| 29 | + public static final AttributeMetadata<ConstantResult> CONSTANT_RESULT = new AttributeMetadata<>("constant_result", ConstantResult.class, true); |
| 30 | + public static final AttributeMetadata<String> FIELD_NAME = new AttributeMetadata<>("field_name", String.class, false); |
| 31 | + public static final AttributeMetadata<JoinType> JOIN_TYPE = new AttributeMetadata<>("join_type", JoinType.class, false); |
| 32 | + public static final AttributeMetadata<LogicalOperator> LOGICAL_OPERATOR = new AttributeMetadata<>("logical_operator", LogicalOperator.class, false); |
| 33 | + public static final AttributeMetadata<OutputNames> OUTPUT_NAMES = new AttributeMetadata<>("output_names", OutputNames.class, false); |
| 34 | + |
| 35 | + // TODO define attributes for deeply nested fields, not just top level or column level |
| 36 | + |
| 37 | + private Attributes() {} |
| 38 | + |
| 39 | + public static class AttributeMetadata<T> |
| 40 | + { |
| 41 | + private final String name; |
| 42 | + private final Class<T> type; |
| 43 | + private final boolean external; |
| 44 | + |
| 45 | + private AttributeMetadata(String name, Class<T> type, boolean external) |
| 46 | + { |
| 47 | + this.name = requireNonNull(name, "name is null"); |
| 48 | + this.type = requireNonNull(type, "type is null"); |
| 49 | + this.external = external; |
| 50 | + } |
| 51 | + |
| 52 | + public T getAttribute(Map<String, Object> map) |
| 53 | + { |
| 54 | + return this.type.cast(map.get(this.name)); |
| 55 | + } |
| 56 | + |
| 57 | + public T putAttribute(Map<String, Object> map, T attribute) |
| 58 | + { |
| 59 | + return this.type.cast(map.put(name, attribute)); |
| 60 | + } |
| 61 | + |
| 62 | + public Map<String, Object> asMap(T attribute) |
| 63 | + { |
| 64 | + return ImmutableMap.of(name, attribute); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public record ConstantResult(Type type, Object value) |
| 69 | + { |
| 70 | + public ConstantResult |
| 71 | + { |
| 72 | + requireNonNull(type, "type is null"); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String toString() |
| 77 | + { |
| 78 | + return value.toString() + ":" + type.toString(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public enum JoinType |
| 83 | + { |
| 84 | + INNER, |
| 85 | + LEFT, |
| 86 | + RIGHT, |
| 87 | + FULL; |
| 88 | + |
| 89 | + public static JoinType of(io.trino.sql.planner.plan.JoinType joinType) |
| 90 | + { |
| 91 | + return switch (joinType) { |
| 92 | + case INNER -> INNER; |
| 93 | + case LEFT -> LEFT; |
| 94 | + case RIGHT -> RIGHT; |
| 95 | + case FULL -> FULL; |
| 96 | + }; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public record OutputNames(List<String> outputNames) |
| 101 | + { |
| 102 | + public OutputNames(List<String> outputNames) |
| 103 | + { |
| 104 | + this.outputNames = ImmutableList.copyOf(requireNonNull(outputNames, "outputNames is null")); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public String toString() |
| 109 | + { |
| 110 | + return outputNames.toString(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public enum LogicalOperator |
| 115 | + { |
| 116 | + AND, |
| 117 | + OR; |
| 118 | + |
| 119 | + public static LogicalOperator of(Logical.Operator operator) |
| 120 | + { |
| 121 | + return switch (operator) { |
| 122 | + case AND -> AND; |
| 123 | + case OR -> OR; |
| 124 | + }; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments