Search in sources :

Example 1 with MetadataDef

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;
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) SqlOperator(org.apache.calcite.sql.SqlOperator) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) RexBuilder(org.apache.calcite.rex.RexBuilder) TranslatableTable(org.apache.calcite.schema.TranslatableTable) TableMacro(org.apache.calcite.schema.TableMacro) MetadataDef(org.apache.calcite.rel.metadata.MetadataDef) BuiltInMetadata(org.apache.calcite.rel.metadata.BuiltInMetadata) RelNode(org.apache.calcite.rel.RelNode) RexInputRef(org.apache.calcite.rex.RexInputRef)

Example 2 with MetadataDef

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));
    }
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode) BuiltInMetadata(org.apache.calcite.rel.metadata.BuiltInMetadata) Metadata(org.apache.calcite.rel.metadata.Metadata) UnboundMetadata(org.apache.calcite.rel.metadata.UnboundMetadata) JaninoRelMetadataProvider(org.apache.calcite.rel.metadata.JaninoRelMetadataProvider) DefaultRelMetadataProvider(org.apache.calcite.rel.metadata.DefaultRelMetadataProvider) ChainedRelMetadataProvider(org.apache.calcite.rel.metadata.ChainedRelMetadataProvider) ReflectiveRelMetadataProvider(org.apache.calcite.rel.metadata.ReflectiveRelMetadataProvider) RelMetadataProvider(org.apache.calcite.rel.metadata.RelMetadataProvider) MetadataDef(org.apache.calcite.rel.metadata.MetadataDef) Method(java.lang.reflect.Method) MetadataHandler(org.apache.calcite.rel.metadata.MetadataHandler) RelHint(org.apache.calcite.rel.hint.RelHint) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Aggregations

RelNode (org.apache.calcite.rel.RelNode)2 BuiltInMetadata (org.apache.calcite.rel.metadata.BuiltInMetadata)2 MetadataDef (org.apache.calcite.rel.metadata.MetadataDef)2 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 RelOptPredicateList (org.apache.calcite.plan.RelOptPredicateList)1 RelHint (org.apache.calcite.rel.hint.RelHint)1 ChainedRelMetadataProvider (org.apache.calcite.rel.metadata.ChainedRelMetadataProvider)1 DefaultRelMetadataProvider (org.apache.calcite.rel.metadata.DefaultRelMetadataProvider)1 JaninoRelMetadataProvider (org.apache.calcite.rel.metadata.JaninoRelMetadataProvider)1 Metadata (org.apache.calcite.rel.metadata.Metadata)1 MetadataHandler (org.apache.calcite.rel.metadata.MetadataHandler)1 ReflectiveRelMetadataProvider (org.apache.calcite.rel.metadata.ReflectiveRelMetadataProvider)1 RelMetadataProvider (org.apache.calcite.rel.metadata.RelMetadataProvider)1 UnboundMetadata (org.apache.calcite.rel.metadata.UnboundMetadata)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RexBuilder (org.apache.calcite.rex.RexBuilder)1 RexInputRef (org.apache.calcite.rex.RexInputRef)1 TableMacro (org.apache.calcite.schema.TableMacro)1