Skip to content

Commit

Permalink
Core: View core parser implementations
Browse files Browse the repository at this point in the history
Co-authored-by: John Zhuge <[email protected]>
  • Loading branch information
amogh-jahagirdar and jzhuge committed Apr 8, 2023
1 parent d485cc8 commit 1be161b
Show file tree
Hide file tree
Showing 16 changed files with 1,341 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.apache.iceberg.view;

import java.util.List;
import javax.annotation.Nullable;
import org.apache.iceberg.catalog.Namespace;
import org.immutables.value.Value;

Expand All @@ -45,12 +44,8 @@ default String type() {
@Nullable
Namespace defaultNamespace();

/**
* The query output schema ID at version create time, without aliases or null if no schema is
* defined
*/
@Nullable
Integer schemaId();
/** The query output schema id at version create time, without aliases. */
int schemaId();

/** The view field comments. */
List<String> fieldComments();
Expand Down
58 changes: 58 additions & 0 deletions core/src/main/java/org/apache/iceberg/util/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -283,6 +284,63 @@ public static void writeLongFieldIf(
}
}

@FunctionalInterface
public interface JsonWriter<T> {
void write(T object, JsonGenerator generator) throws IOException;
}

public static <T> void writeObjectList(
String property, Iterable<T> objectList, JsonWriter<T> writer, JsonGenerator generator)
throws IOException {
generator.writeArrayFieldStart(property);
for (T object : objectList) {
writer.write(object, generator);
}
generator.writeEndArray();
}

public static void writeStringList(
String property, List<String> stringList, JsonGenerator generator) throws IOException {
generator.writeArrayFieldStart(property);
for (String s : stringList) {
generator.writeString(s);
}
generator.writeEndArray();
}

@FunctionalInterface
public interface JsonReader<T> {
T read(JsonNode node);
}

public static <T> T getObject(String property, JsonNode node, JsonReader<T> reader) {
Preconditions.checkArgument(node.has(property), "Cannot parse missing object %s", property);
JsonNode pNode = node.get(property);
Preconditions.checkArgument(
pNode.isObject(), "Cannot parse %s from non-object value: %s", property, pNode);
return reader.read(pNode);
}

public static <T> List<T> getObjectList(
String property, JsonNode node, Function<JsonNode, T> reader) {
Preconditions.checkArgument(node.has(property), "Cannot parse missing list %s", property);
return ImmutableList.<T>builder().addAll(objectArrayIterator(property, node, reader)).build();
}

static <T> Iterator<T> objectArrayIterator(
String property, JsonNode node, Function<JsonNode, T> reader) {
return new JsonArrayIterator<T>(property, node) {
protected T convert(JsonNode element) {
return reader.apply(element);
}

protected void validate(JsonNode element) {
Preconditions.checkArgument(
element.isObject(), "Cannot parse %s from non-object value: %s", property, element);
}
};
}

abstract static class JsonArrayIterator<T> implements Iterator<T> {

private final Iterator<JsonNode> elements;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.iceberg.view;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.apache.iceberg.catalog.Namespace;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;

public class BaseSQLViewRepresentation implements SQLViewRepresentation {

private final String query;
private final String dialect;
private final String defaultCatalog;
private final int schemaId;
private final Namespace defaultNamespace;
private final List<String> fieldAliases;
private final List<String> fieldComments;

public static Builder builder() {
return new Builder();
}

public static Builder buildFrom(SQLViewRepresentation representation) {
return builder()
.query(representation.query())
.schemaId(representation.schemaId())
.dialect(representation.dialect())
.defaultCatalog(representation.defaultCatalog())
.defaultNamespace(representation.defaultNamespace())
.fieldAliases(representation.fieldAliases())
.fieldComments(representation.fieldComments());
}

private BaseSQLViewRepresentation(
String query,
String dialect,
String defaultCatalog,
Namespace defaultNamespace,
int schemaId,
List<String> fieldAliases,
List<String> fieldComments) {
this.query = Preconditions.checkNotNull(query, "sql should not be null");
this.dialect = Preconditions.checkNotNull(dialect, "dialect should not be null");
this.defaultCatalog =
Preconditions.checkNotNull(defaultCatalog, "default catalog should not null");
this.defaultNamespace =
Preconditions.checkNotNull(defaultNamespace, "default namespace should not be null");
this.schemaId = Preconditions.checkNotNull(schemaId, "schema should not be null");
this.fieldAliases =
Preconditions.checkNotNull(fieldAliases, "field aliases should not be null");
this.fieldComments =
Preconditions.checkNotNull(fieldComments, "field comments should not be null");
}

@Override
public String query() {
return query;
}

@Override
public String dialect() {
return dialect;
}

@Override
public String defaultCatalog() {
return defaultCatalog;
}

@Override
public Namespace defaultNamespace() {
return defaultNamespace;
}

@Override
public int schemaId() {
return schemaId;
}

@Override
public List<String> fieldComments() {
return fieldComments;
}

@Override
public List<String> fieldAliases() {
return fieldAliases;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BaseSQLViewRepresentation that = (BaseSQLViewRepresentation) o;
return Objects.equals(query, that.query)
&& Objects.equals(dialect, that.dialect)
&& Objects.equals(schemaId, that.schemaId)
&& Objects.equals(defaultCatalog, that.defaultCatalog)
&& Objects.equals(defaultNamespace, that.defaultNamespace)
&& Objects.equals(fieldAliases, that.fieldAliases)
&& Objects.equals(fieldComments, that.fieldComments);
}

@Override
public int hashCode() {
return Objects.hash(
query, dialect, schemaId, defaultCatalog, defaultNamespace, fieldAliases, fieldComments);
}

@Override
public String toString() {
return "BaseViewDefinition{"
+ "query='"
+ query
+ '\''
+ ", dialect="
+ dialect
+ ", schema="
+ schemaId
+ ", defaultCatalog='"
+ defaultCatalog
+ '\''
+ ", defaultNamespace="
+ defaultNamespace
+ ", fieldAliases="
+ fieldAliases
+ ", fieldComments="
+ fieldComments
+ '}';
}

public static final class Builder {

private String sql;
private int schemaId;
private String dialect = "";
private String defaultCatalog = "";
private Namespace defaultNamespace = Namespace.empty();
private List<String> fieldAliases = Collections.emptyList();
private List<String> fieldComments = Collections.emptyList();

private Builder() {}

public Builder query(String value) {
sql = value;
return this;
}

public Builder dialect(String value) {
dialect = value;
return this;
}

public Builder schemaId(int value) {
schemaId = value;
return this;
}

public Builder defaultCatalog(String value) {
defaultCatalog = value;
return this;
}

public Builder defaultNamespace(Namespace value) {
defaultNamespace = value;
return this;
}

public Builder fieldAliases(List<String> value) {
fieldAliases = value;
return this;
}

public Builder fieldComments(List<String> value) {
fieldComments = value;
return this;
}

public BaseSQLViewRepresentation build() {
return new BaseSQLViewRepresentation(
sql, dialect, defaultCatalog, defaultNamespace, schemaId, fieldAliases, fieldComments);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.iceberg.view;

import java.util.Objects;
import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;

public class BaseViewHistoryEntry implements ViewHistoryEntry {
private final long timestampMillis;
private final int versionId;

static ViewHistoryEntry of(long timestampMillis, int versionId) {
return new BaseViewHistoryEntry(timestampMillis, versionId);
}

private BaseViewHistoryEntry(long timestampMillis, int versionId) {
this.timestampMillis = timestampMillis;
this.versionId = versionId;
}

@Override
public long timestampMillis() {
return timestampMillis;
}

@Override
public int versionId() {
return versionId;
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}
BaseViewHistoryEntry that = (BaseViewHistoryEntry) other;
return timestampMillis == that.timestampMillis && versionId == that.versionId;
}

@Override
public int hashCode() {
return Objects.hash(timestampMillis, versionId);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("timestampMillis", timestampMillis)
.add("versionId", versionId)
.toString();
}
}
Loading

0 comments on commit 1be161b

Please sign in to comment.