|
| 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 | +} |
0 commit comments