amogh-jahagirdar commented on code in PR #6598: URL: https://github.com/apache/iceberg/pull/6598#discussion_r1070738898
########## core/src/test/java/org/apache/iceberg/view/TestViewRepresentationParser.java: ########## @@ -0,0 +1,177 @@ +/* + * 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 com.fasterxml.jackson.databind.JsonNode; +import java.util.List; +import org.apache.iceberg.Schema; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.assertj.core.api.Assertions; +import org.junit.Assert; +import org.junit.Test; + +public class TestViewRepresentationParser { + + @Test + public void testCantParseNonSqlViewRepresentation() { + String json = "{\"type\":\"non-sql\"}"; + Assertions.assertThatThrownBy(() -> ViewRepresentationParser.fromJson(json, ImmutableList.of())) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Cannot deserialize unknown view representation type non-sql"); + } + + @Test + public void testParseSqlViewRepresentation() { + String requiredFields = + "{\"type\":\"sql\", \"sql\": \"select * from foo\", \"schema-id\": 1, \"dialect\": \"spark-sql\"}"; + List<Schema> schemas = ImmutableList.of(new Schema(1)); + SQLViewRepresentation viewRepresentation = + ImmutableSQLViewRepresentation.builder() + .query("select * from foo") + .schema(new Schema(1)) + .dialect("spark-sql") + .build(); + validateSqlRepresentation( + viewRepresentation, ViewRepresentationParser.fromJson(requiredFields, schemas)); + + String requiredAndOptionalFields = + "{\"type\":\"sql\", \"sql\": \"select * from foo\", \"schema-id\": 1, \"dialect\": \"spark-sql\", " + + "\"default-catalog\":\"cat\", " + + "\"default-namespace\":[\"part1\",\"part2\"], " + + "\"field-aliases\":[\"col1\", \"col2\"], " + + "\"field-comments\":[\"Comment col1\", \"Comment col2\"]}"; + + SQLViewRepresentation viewWithOptionalFields = + ImmutableSQLViewRepresentation.builder() + .query("select * from foo") + .schema(new Schema(1)) + .dialect("spark-sql") + .defaultCatalog("cat") + .fieldAliases(ImmutableList.of("col1", "col2")) + .fieldComments(ImmutableList.of("Comment col1", "Comment col2")) + .defaultNamespace(Namespace.of("part1", "part2")) + .build(); + + validateSqlRepresentation( + viewWithOptionalFields, + ViewRepresentationParser.fromJson(requiredAndOptionalFields, schemas)); + } + + private void validateSqlRepresentation( + SQLViewRepresentation expected, ViewRepresentation actual) { + Assert.assertEquals("Expected type sql", expected.type(), actual.type()); + + SQLViewRepresentation sqlViewRepresentation = (SQLViewRepresentation) actual; + Assert.assertEquals( + "Expected same query string", expected.query(), sqlViewRepresentation.query()); + Assert.assertEquals( + "Expected same dialect", expected.dialect(), sqlViewRepresentation.dialect()); + Assert.assertTrue(expected.schema().sameSchema(sqlViewRepresentation.schema())); Review Comment: This is one awkward part if we maintain just schema, since schema doesn't override .equals() we compare using sameSchema which just ignores schemaId and compared the columns/types. I don't think this should be the case. We should always compare equality on the basis of schema ID. To do this I could extend SQLViewRepresentation with a custom equalsTo but that would add more complexity. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org