|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iceberg.view; |
| 21 | + |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Objects; |
| 25 | +import org.apache.iceberg.Schema; |
| 26 | +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; |
| 27 | + |
| 28 | +/** |
| 29 | + * SQL definition for a view |
| 30 | + */ |
| 31 | +class BaseViewDefinition implements ViewDefinition { |
| 32 | + private final String sql; |
| 33 | + private final String dialect; |
| 34 | + private final Schema schema; |
| 35 | + private final String defaultCatalog; |
| 36 | + private final List<String> defaultNamespace; |
| 37 | + private final List<String> fieldAliases; |
| 38 | + private final List<String> fieldComments; |
| 39 | + |
| 40 | + public static Builder builder() { |
| 41 | + return new Builder(); |
| 42 | + } |
| 43 | + |
| 44 | + public static Builder buildFrom(ViewDefinition that) { |
| 45 | + return builder() |
| 46 | + .sql(that.sql()) |
| 47 | + .dialect(that.dialect()) |
| 48 | + .schema(that.schema()) |
| 49 | + .defaultCatalog(that.defaultCatalog()) |
| 50 | + .defaultNamespace(that.defaultNamespace()) |
| 51 | + .fieldAliases(that.fieldAliases()) |
| 52 | + .fieldComments(that.fieldComments()); |
| 53 | + } |
| 54 | + |
| 55 | + private BaseViewDefinition( |
| 56 | + String sql, String dialect, Schema schema, String defaultCatalog, List<String> defaultNamespace, |
| 57 | + List<String> fieldAliases, List<String> fieldComments) { |
| 58 | + this.sql = Preconditions.checkNotNull(sql, "sql should not be null"); |
| 59 | + this.dialect = Preconditions.checkNotNull(dialect, "dialect should not be null"); |
| 60 | + this.schema = schema; |
| 61 | + this.defaultCatalog = Preconditions.checkNotNull(defaultCatalog, "default catalog should not null"); |
| 62 | + this.defaultNamespace = Preconditions.checkNotNull(defaultNamespace, "default namespace should not be null"); |
| 63 | + this.fieldAliases = Preconditions.checkNotNull(fieldAliases, "field aliases should not be null"); |
| 64 | + this.fieldComments = Preconditions.checkNotNull(fieldComments, "field comments should not be null"); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public String sql() { |
| 69 | + return sql; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public String dialect() { |
| 74 | + return dialect; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public Schema schema() { |
| 79 | + return schema; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public String defaultCatalog() { |
| 84 | + return defaultCatalog; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public List<String> defaultNamespace() { |
| 89 | + return defaultNamespace; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public List<String> fieldAliases() { |
| 94 | + return fieldAliases; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public List<String> fieldComments() { |
| 99 | + return fieldComments; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public boolean equals(Object o) { |
| 104 | + if (this == o) { |
| 105 | + return true; |
| 106 | + } |
| 107 | + if (o == null || getClass() != o.getClass()) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + BaseViewDefinition that = (BaseViewDefinition) o; |
| 111 | + return Objects.equals(sql, that.sql) && |
| 112 | + Objects.equals(dialect, that.dialect) && |
| 113 | + Objects.equals(schema.asStruct(), that.schema.asStruct()) && |
| 114 | + Objects.equals(defaultCatalog, that.defaultCatalog) && |
| 115 | + Objects.equals(defaultNamespace, that.defaultNamespace) && |
| 116 | + Objects.equals(fieldAliases, that.fieldAliases) && |
| 117 | + Objects.equals(fieldComments, that.fieldComments); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public int hashCode() { |
| 122 | + return Objects.hash(sql, dialect, schema.asStruct(), defaultCatalog, defaultNamespace, fieldAliases, fieldComments); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public String toString() { |
| 127 | + return "BaseViewDefinition{" + |
| 128 | + "sql='" + sql + '\'' + |
| 129 | + ", dialect=" + dialect + |
| 130 | + ", schema=" + schema + |
| 131 | + ", defaultCatalog='" + defaultCatalog + '\'' + |
| 132 | + ", defaultNamespace=" + defaultNamespace + |
| 133 | + ", fieldAliases=" + fieldAliases + |
| 134 | + ", fieldComments=" + fieldComments + |
| 135 | + '}'; |
| 136 | + } |
| 137 | + |
| 138 | + public static final class Builder { |
| 139 | + |
| 140 | + private String sql; |
| 141 | + private String dialect = ""; |
| 142 | + private Schema schema = new Schema(); |
| 143 | + private String defaultCatalog = ""; |
| 144 | + private List<String> defaultNamespace = Collections.emptyList(); |
| 145 | + private List<String> fieldAliases = Collections.emptyList(); |
| 146 | + private List<String> fieldComments = Collections.emptyList(); |
| 147 | + |
| 148 | + private Builder() { |
| 149 | + } |
| 150 | + |
| 151 | + public Builder sql(String value) { |
| 152 | + sql = value; |
| 153 | + return this; |
| 154 | + } |
| 155 | + |
| 156 | + public Builder dialect(String value) { |
| 157 | + dialect = value; |
| 158 | + return this; |
| 159 | + } |
| 160 | + |
| 161 | + public Builder schema(Schema value) { |
| 162 | + schema = value; |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + public Builder defaultCatalog(String value) { |
| 167 | + defaultCatalog = value; |
| 168 | + return this; |
| 169 | + } |
| 170 | + |
| 171 | + public Builder defaultNamespace(List<String> value) { |
| 172 | + defaultNamespace = value; |
| 173 | + return this; |
| 174 | + } |
| 175 | + |
| 176 | + public Builder fieldAliases(List<String> value) { |
| 177 | + fieldAliases = value; |
| 178 | + return this; |
| 179 | + } |
| 180 | + |
| 181 | + public Builder fieldComments(List<String> value) { |
| 182 | + fieldComments = value; |
| 183 | + return this; |
| 184 | + } |
| 185 | + |
| 186 | + public BaseViewDefinition build() { |
| 187 | + return new BaseViewDefinition( |
| 188 | + sql, |
| 189 | + dialect, |
| 190 | + schema, |
| 191 | + defaultCatalog, |
| 192 | + defaultNamespace, |
| 193 | + fieldAliases, |
| 194 | + fieldComments); |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments