forked from apache/calcite
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CALCITE-6724] MockTable support for multiple (individual & composite…
…) keys MockTable currently allows only one key to be specified; the key can be either simple or composite. In real scenarios though a table can have multiple keys.
- Loading branch information
Showing
3 changed files
with
145 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
testkit/src/test/java/org/apache/calcite/test/MockTableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* 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.calcite.test; | ||
|
||
import org.apache.calcite.rel.type.RelDataTypeSystem; | ||
import org.apache.calcite.sql.type.SqlTypeFactoryImpl; | ||
import org.apache.calcite.sql.type.SqlTypeName; | ||
import org.apache.calcite.sql2rel.NullInitializerExpressionFactory; | ||
import org.apache.calcite.test.catalog.MockCatalogReader; | ||
import org.apache.calcite.util.ImmutableBitSet; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.hasToString; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
/** | ||
* Tests for {@link org.apache.calcite.test.catalog.MockCatalogReader.MockTable}. | ||
*/ | ||
public class MockTableTest { | ||
private static final SqlTypeFactoryImpl TYPE_FACTORY = | ||
new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT); | ||
|
||
@Test void testAddColumnCreatesIndividualKeys() { | ||
MockCatalogReader.MockTable t = newTable(); | ||
t.addColumn("k1", TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER), true); | ||
t.addColumn("k2", TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER), true); | ||
assertThat(t.getKeys(), hasToString("[{0}, {1}]")); | ||
} | ||
|
||
@Test void testAddKeyWithOneEntryCreatesSimpleKey() { | ||
MockCatalogReader.MockTable t = newTable(); | ||
t.addColumn("k1", TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER)); | ||
t.addColumn("k2", TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER)); | ||
t.addKey("k1"); | ||
assertThat(t.getKeys(), hasToString("[{0}]")); | ||
} | ||
|
||
@Test void testAddKeyWithMultipleEntriesCreatesCompositeKey() { | ||
MockCatalogReader.MockTable t = newTable(); | ||
t.addColumn("k1", TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER)); | ||
t.addColumn("k2", TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER)); | ||
t.addKey("k1", "k2"); | ||
assertThat(t.getKeys(), hasToString("[{0, 1}]")); | ||
} | ||
|
||
@Test void testAddKeyWithMissingColumnNameThrowsException() { | ||
MockCatalogReader.MockTable t = newTable(); | ||
t.addColumn("k1", TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER)); | ||
t.addColumn("k2", TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER)); | ||
assertThrows(IllegalArgumentException.class, () -> t.addKey("k1", "k3")); | ||
} | ||
|
||
@Test void testAddKeyUsingColumnIndex() { | ||
MockCatalogReader.MockTable t = newTable(); | ||
t.addColumn("k1", TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER)); | ||
t.addColumn("k2", TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER)); | ||
t.addKey(ImmutableBitSet.of(0, 1)); | ||
assertThat(t.getKeys(), hasToString("[{0, 1}]")); | ||
} | ||
|
||
@Test void testAddKeyUsingWrongIndexThrowsException() { | ||
MockCatalogReader.MockTable t = newTable(); | ||
t.addColumn("k1", TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER)); | ||
t.addColumn("k2", TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER)); | ||
assertThrows(IllegalArgumentException.class, () -> t.addKey(ImmutableBitSet.of(0, 2))); | ||
} | ||
|
||
@Test void testAddKeyMultipleTimes() { | ||
MockCatalogReader.MockTable t = newTable(); | ||
t.addColumn("k1", TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER)); | ||
t.addColumn("k2", TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER)); | ||
t.addColumn("k3", TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER)); | ||
t.addKey("k1"); | ||
t.addKey("k2", "k3"); | ||
assertThat(t.getKeys(), hasToString("[{0}, {1, 2}]")); | ||
} | ||
|
||
private static MockCatalogReader.MockTable newTable() { | ||
|
||
MockCatalogReader catalogReader = new MockCatalogReader(TYPE_FACTORY, false) { | ||
@Override public MockCatalogReader init() { | ||
return this; | ||
} | ||
}; | ||
return new MockCatalogReader.MockTable(catalogReader, "catalog", "schema", "table", false, | ||
false, 0.0, null, NullInitializerExpressionFactory.INSTANCE); | ||
} | ||
} |