use of org.apache.calcite.rel.metadata.MetadataDef in project calcite by apache.
the class MockCatalogReaderExtended method init.
@Override
public MockCatalogReaderExtended init() {
super.init();
MockSchema salesSchema = new MockSchema("SALES");
// Same as "EMP_20" except it uses ModifiableViewTable which populates
// constrained columns with default values on INSERT and has a single constraint on DEPTNO.
List<String> empModifiableViewNames = ImmutableList.of(salesSchema.getCatalogName(), salesSchema.getName(), "EMP_MODIFIABLEVIEW");
TableMacro empModifiableViewMacro = MockModifiableViewRelOptTable.viewMacro(rootSchema, "select EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, SLACKER from EMPDEFAULTS" + " where DEPTNO = 20", empModifiableViewNames.subList(0, 2), ImmutableList.of(empModifiableViewNames.get(2)), true);
TranslatableTable empModifiableView = empModifiableViewMacro.apply(ImmutableList.of());
MockModifiableViewRelOptTable mockEmpViewTable = MockModifiableViewRelOptTable.create((MockModifiableViewRelOptTable.MockModifiableViewTable) empModifiableView, this, empModifiableViewNames.get(0), empModifiableViewNames.get(1), empModifiableViewNames.get(2), false, 20, null);
registerTable(mockEmpViewTable);
// Same as "EMP_MODIFIABLEVIEW" except that all columns are in the view, columns are reordered,
// and there is an `extra` extended column.
List<String> empModifiableViewNames2 = ImmutableList.of(salesSchema.getCatalogName(), salesSchema.getName(), "EMP_MODIFIABLEVIEW2");
TableMacro empModifiableViewMacro2 = MockModifiableViewRelOptTable.viewMacro(rootSchema, "select ENAME, EMPNO, JOB, DEPTNO, SLACKER, SAL, EXTRA, HIREDATE, MGR, COMM" + " from EMPDEFAULTS extend (EXTRA boolean)" + " where DEPTNO = 20", empModifiableViewNames2.subList(0, 2), ImmutableList.of(empModifiableViewNames.get(2)), true);
TranslatableTable empModifiableView2 = empModifiableViewMacro2.apply(ImmutableList.of());
MockModifiableViewRelOptTable mockEmpViewTable2 = MockModifiableViewRelOptTable.create((MockModifiableViewRelOptTable.MockModifiableViewTable) empModifiableView2, this, empModifiableViewNames2.get(0), empModifiableViewNames2.get(1), empModifiableViewNames2.get(2), false, 20, null);
registerTable(mockEmpViewTable2);
// Same as "EMP_MODIFIABLEVIEW" except that comm is not in the view.
List<String> empModifiableViewNames3 = ImmutableList.of(salesSchema.getCatalogName(), salesSchema.getName(), "EMP_MODIFIABLEVIEW3");
TableMacro empModifiableViewMacro3 = MockModifiableViewRelOptTable.viewMacro(rootSchema, "select EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, SLACKER from EMPDEFAULTS" + " where DEPTNO = 20", empModifiableViewNames3.subList(0, 2), ImmutableList.of(empModifiableViewNames3.get(2)), true);
TranslatableTable empModifiableView3 = empModifiableViewMacro3.apply(ImmutableList.of());
MockModifiableViewRelOptTable mockEmpViewTable3 = MockModifiableViewRelOptTable.create((MockModifiableViewRelOptTable.MockModifiableViewTable) empModifiableView3, this, empModifiableViewNames3.get(0), empModifiableViewNames3.get(1), empModifiableViewNames3.get(2), false, 20, null);
registerTable(mockEmpViewTable3);
MockSchema structTypeSchema = new MockSchema("STRUCT");
registerSchema(structTypeSchema);
final Fixture f = new Fixture(typeFactory);
final List<CompoundNameColumn> columnsExtended = Arrays.asList(new CompoundNameColumn("", "K0", f.varchar20TypeNull), new CompoundNameColumn("", "C1", f.varchar20TypeNull), new CompoundNameColumn("F0", "C0", f.intType), new CompoundNameColumn("F1", "C1", f.intTypeNull));
final List<CompoundNameColumn> extendedColumns = new ArrayList<>(columnsExtended);
extendedColumns.add(new CompoundNameColumn("F2", "C2", f.varchar20Type));
final CompoundNameColumnResolver structExtendedTableResolver = new CompoundNameColumnResolver(extendedColumns, "F0");
final MockTable structExtendedTypeTable = MockTable.create(this, structTypeSchema, "T_EXTEND", false, 100, structExtendedTableResolver);
for (CompoundNameColumn column : columnsExtended) {
structExtendedTypeTable.addColumn(column.getName(), column.type);
}
registerTable(structExtendedTypeTable);
// Defines a table with
// schema(A int, B bigint, C varchar(10), D as a + 1 stored, E as b * 3 virtual).
MockSchema virtualColumnsSchema = new MockSchema("VIRTUALCOLUMNS");
registerSchema(virtualColumnsSchema);
final MockTable virtualColumnsTable1 = MockTable.create(this, virtualColumnsSchema, "VC_T1", false, 100, null, new VirtualColumnsExpressionFactory(), true);
virtualColumnsTable1.addColumn("A", f.intTypeNull);
virtualColumnsTable1.addColumn("B", f.bigintType);
virtualColumnsTable1.addColumn("C", f.varchar10Type);
virtualColumnsTable1.addColumn("D", f.intTypeNull);
// Column E has the same type as column A because it's a virtual column
// with expression that references column A.
virtualColumnsTable1.addColumn("E", f.intTypeNull);
// Same schema with VC_T1 but with different table name.
final MockTable virtualColumnsTable2 = MockTable.create(this, virtualColumnsSchema, "VC_T2", false, 100, null, new VirtualColumnsExpressionFactory(), false);
virtualColumnsTable2.addColumn("A", f.intTypeNull);
virtualColumnsTable2.addColumn("B", f.bigintType);
virtualColumnsTable2.addColumn("C", f.varchar10Type);
virtualColumnsTable2.addColumn("D", f.intTypeNull);
virtualColumnsTable2.addColumn("E", f.bigintType);
registerTable(virtualColumnsTable1);
registerTable(virtualColumnsTable2);
// Register table with complex data type rows.
MockSchema complexTypeColumnsSchema = new MockSchema("COMPLEXTYPES");
registerSchema(complexTypeColumnsSchema);
final MockTable complexTypeColumnsTable = MockTable.create(this, complexTypeColumnsSchema, "CTC_T1", false, 100);
complexTypeColumnsTable.addColumn("A", f.recordType1);
complexTypeColumnsTable.addColumn("B", f.recordType2);
complexTypeColumnsTable.addColumn("C", f.recordType3);
complexTypeColumnsTable.addColumn("D", f.recordType4);
complexTypeColumnsTable.addColumn("E", f.recordType5);
complexTypeColumnsTable.addColumn("intArrayType", f.intArrayType);
complexTypeColumnsTable.addColumn("varchar5ArrayType", f.varchar5ArrayType);
complexTypeColumnsTable.addColumn("intArrayArrayType", f.intArrayArrayType);
complexTypeColumnsTable.addColumn("varchar5ArrayArrayType", f.varchar5ArrayArrayType);
complexTypeColumnsTable.addColumn("intMultisetType", f.intMultisetType);
complexTypeColumnsTable.addColumn("varchar5MultisetType", f.varchar5MultisetType);
complexTypeColumnsTable.addColumn("intMultisetArrayType", f.intMultisetArrayType);
complexTypeColumnsTable.addColumn("varchar5MultisetArrayType", f.varchar5MultisetArrayType);
complexTypeColumnsTable.addColumn("intArrayMultisetType", f.intArrayMultisetType);
complexTypeColumnsTable.addColumn("rowArrayMultisetType", f.rowArrayMultisetType);
registerTable(complexTypeColumnsTable);
MockSchema nullableRowsSchema = new MockSchema("NULLABLEROWS");
registerSchema(nullableRowsSchema);
final MockTable nullableRowsTable = MockTable.create(this, nullableRowsSchema, "NR_T1", false, 100);
RelDataType bigIntNotNull = typeFactory.createSqlType(SqlTypeName.BIGINT);
RelDataType nullableRecordType = typeFactory.builder().nullableRecord(true).add("NOT_NULL_FIELD", bigIntNotNull).add("NULLABLE_FIELD", bigIntNotNull).nullable(true).build();
nullableRowsTable.addColumn("ROW_COLUMN", nullableRecordType, false);
nullableRowsTable.addColumn("ROW_COLUMN_ARRAY", typeFactory.createArrayType(nullableRecordType, -1), true);
registerTable(nullableRowsTable);
MockSchema geoSchema = new MockSchema("GEO");
registerSchema(geoSchema);
final MockTable restaurantTable = MockTable.create(this, geoSchema, "RESTAURANTS", false, 100);
restaurantTable.addColumn("NAME", f.varchar20Type, true);
restaurantTable.addColumn("LATITUDE", f.intType);
restaurantTable.addColumn("LONGITUDE", f.intType);
restaurantTable.addColumn("CUISINE", f.varchar10Type);
restaurantTable.addColumn("HILBERT", f.bigintType);
restaurantTable.addMonotonic("HILBERT");
restaurantTable.addWrap(new BuiltInMetadata.AllPredicates.Handler() {
@Override
public RelOptPredicateList getAllPredicates(RelNode r, RelMetadataQuery mq) {
// Return the predicate:
// r.hilbert = hilbert(r.longitude, r.latitude)
//
// (Yes, x = longitude, y = latitude. Same as ST_MakePoint.)
final RexBuilder rexBuilder = r.getCluster().getRexBuilder();
final RexInputRef refLatitude = rexBuilder.makeInputRef(r, 1);
final RexInputRef refLongitude = rexBuilder.makeInputRef(r, 2);
final RexInputRef refHilbert = rexBuilder.makeInputRef(r, 4);
return RelOptPredicateList.of(rexBuilder, ImmutableList.of(rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, refHilbert, rexBuilder.makeCall(hilbertOp(), refLongitude, refLatitude))));
}
SqlOperator hilbertOp() {
for (SqlOperator op : SqlOperatorTables.spatialInstance().getOperatorList()) {
if (op.getKind() == SqlKind.HILBERT && op.getOperandCountRange().isValidCount(2)) {
return op;
}
}
throw new AssertionError();
}
@Override
public MetadataDef<BuiltInMetadata.AllPredicates> getDef() {
return BuiltInMetadata.AllPredicates.DEF;
}
});
registerTable(restaurantTable);
return this;
}
use of org.apache.calcite.rel.metadata.MetadataDef in project calcite by apache.
the class RelMetadataTest method testMetadataHandlerCacheLimit.
/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-1808">[CALCITE-1808]
* JaninoRelMetadataProvider loading cache might cause
* OutOfMemoryError</a>.
*
* <p>Too slow to run every day, and it does not reproduce the issue.
*/
@Tag("slow")
@Test
void testMetadataHandlerCacheLimit() {
assumeTrue(CalciteSystemProperty.METADATA_HANDLER_CACHE_MAXIMUM_SIZE.value() < 10_000, "If cache size is too large, this test may fail and the test won't be to blame");
final int iterationCount = 2_000;
final RelNode rel = sql("select * from emp").toRel();
final RelMetadataProvider metadataProvider = rel.getCluster().getMetadataProvider();
for (int i = 0; i < iterationCount; i++) {
RelMetadataProvider wrappedProvider = new RelMetadataProvider() {
// to be removed before 2.0
@Deprecated
@Override
@Nullable
public <M extends @Nullable Metadata> UnboundMetadata<M> apply(Class<? extends RelNode> relClass, Class<? extends M> metadataClass) {
return metadataProvider.apply(relClass, metadataClass);
}
// to be removed before 2.0
@Deprecated
@Override
public <M extends Metadata> Multimap<Method, MetadataHandler<M>> handlers(MetadataDef<M> def) {
return metadataProvider.handlers(def);
}
@Override
public List<MetadataHandler<?>> handlers(Class<? extends MetadataHandler<?>> handlerClass) {
return metadataProvider.handlers(handlerClass);
}
};
RelMetadataQuery.THREAD_PROVIDERS.set(JaninoRelMetadataProvider.of(wrappedProvider));
final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
final Double result = mq.getRowCount(rel);
assertThat(result, within(14d, 0.1d));
}
}
Aggregations