Search in sources :

Example 16 with ImmutableIntList

use of org.apache.calcite.util.ImmutableIntList in project calcite by apache.

the class MockCatalogReader method init.

/**
 * Initializes this catalog reader.
 */
public MockCatalogReader init() {
    final Fixture f = new Fixture();
    addressType = f.addressType;
    // Register "SALES" schema.
    MockSchema salesSchema = new MockSchema("SALES");
    registerSchema(salesSchema);
    // Register "EMP" table with customer InitializerExpressionFactory
    // to check whether newDefaultValue method called or not.
    final InitializerExpressionFactory countingInitializerExpressionFactory = new CountingFactory(ImmutableList.of("DEPTNO"));
    // Register "EMP" table.
    final MockTable empTable = MockTable.create(this, salesSchema, "EMP", false, 14, null, countingInitializerExpressionFactory);
    empTable.addColumn("EMPNO", f.intType, true);
    empTable.addColumn("ENAME", f.varchar20Type);
    empTable.addColumn("JOB", f.varchar10Type);
    empTable.addColumn("MGR", f.intTypeNull);
    empTable.addColumn("HIREDATE", f.timestampType);
    empTable.addColumn("SAL", f.intType);
    empTable.addColumn("COMM", f.intType);
    empTable.addColumn("DEPTNO", f.intType);
    empTable.addColumn("SLACKER", f.booleanType);
    registerTable(empTable);
    // Register "EMPNULLABLES" table with nullable columns.
    final MockTable empNullablesTable = MockTable.create(this, salesSchema, "EMPNULLABLES", false, 14);
    empNullablesTable.addColumn("EMPNO", f.intType, true);
    empNullablesTable.addColumn("ENAME", f.varchar20Type);
    empNullablesTable.addColumn("JOB", f.varchar10TypeNull);
    empNullablesTable.addColumn("MGR", f.intTypeNull);
    empNullablesTable.addColumn("HIREDATE", f.timestampTypeNull);
    empNullablesTable.addColumn("SAL", f.intTypeNull);
    empNullablesTable.addColumn("COMM", f.intTypeNull);
    empNullablesTable.addColumn("DEPTNO", f.intTypeNull);
    empNullablesTable.addColumn("SLACKER", f.booleanTypeNull);
    registerTable(empNullablesTable);
    // Register "EMPDEFAULTS" table with default values for some columns.
    final MockTable empDefaultsTable = MockTable.create(this, salesSchema, "EMPDEFAULTS", false, 14, null, new EmpInitializerExpressionFactory());
    empDefaultsTable.addColumn("EMPNO", f.intType, true);
    empDefaultsTable.addColumn("ENAME", f.varchar20Type);
    empDefaultsTable.addColumn("JOB", f.varchar10TypeNull);
    empDefaultsTable.addColumn("MGR", f.intTypeNull);
    empDefaultsTable.addColumn("HIREDATE", f.timestampTypeNull);
    empDefaultsTable.addColumn("SAL", f.intTypeNull);
    empDefaultsTable.addColumn("COMM", f.intTypeNull);
    empDefaultsTable.addColumn("DEPTNO", f.intTypeNull);
    empDefaultsTable.addColumn("SLACKER", f.booleanTypeNull);
    registerTable(empDefaultsTable);
    // Register "EMP_B" table. As "EMP", birth with a "BIRTHDATE" column.
    final MockTable empBTable = MockTable.create(this, salesSchema, "EMP_B", false, 14);
    empBTable.addColumn("EMPNO", f.intType, true);
    empBTable.addColumn("ENAME", f.varchar20Type);
    empBTable.addColumn("JOB", f.varchar10Type);
    empBTable.addColumn("MGR", f.intTypeNull);
    empBTable.addColumn("HIREDATE", f.timestampType);
    empBTable.addColumn("SAL", f.intType);
    empBTable.addColumn("COMM", f.intType);
    empBTable.addColumn("DEPTNO", f.intType);
    empBTable.addColumn("SLACKER", f.booleanType);
    empBTable.addColumn("BIRTHDATE", f.dateType);
    registerTable(empBTable);
    // Register "DEPT" table.
    MockTable deptTable = MockTable.create(this, salesSchema, "DEPT", false, 4);
    deptTable.addColumn("DEPTNO", f.intType, true);
    deptTable.addColumn("NAME", f.varchar10Type);
    registerTable(deptTable);
    // Register "DEPT_NESTED" table.
    MockTable deptNestedTable = MockTable.create(this, salesSchema, "DEPT_NESTED", false, 4);
    deptNestedTable.addColumn("DEPTNO", f.intType, true);
    deptNestedTable.addColumn("NAME", f.varchar10Type);
    deptNestedTable.addColumn("SKILL", f.skillRecordType);
    deptNestedTable.addColumn("EMPLOYEES", f.empListType);
    registerTable(deptNestedTable);
    // Register "BONUS" table.
    MockTable bonusTable = MockTable.create(this, salesSchema, "BONUS", false, 0);
    bonusTable.addColumn("ENAME", f.varchar20Type);
    bonusTable.addColumn("JOB", f.varchar10Type);
    bonusTable.addColumn("SAL", f.intType);
    bonusTable.addColumn("COMM", f.intType);
    registerTable(bonusTable);
    // Register "SALGRADE" table.
    MockTable salgradeTable = MockTable.create(this, salesSchema, "SALGRADE", false, 5);
    salgradeTable.addColumn("GRADE", f.intType, true);
    salgradeTable.addColumn("LOSAL", f.intType);
    salgradeTable.addColumn("HISAL", f.intType);
    registerTable(salgradeTable);
    // Register "EMP_ADDRESS" table
    MockTable contactAddressTable = MockTable.create(this, salesSchema, "EMP_ADDRESS", false, 26);
    contactAddressTable.addColumn("EMPNO", f.intType, true);
    contactAddressTable.addColumn("HOME_ADDRESS", addressType);
    contactAddressTable.addColumn("MAILING_ADDRESS", addressType);
    registerTable(contactAddressTable);
    // Register "DYNAMIC" schema.
    MockSchema dynamicSchema = new MockSchema("DYNAMIC");
    registerSchema(dynamicSchema);
    MockTable nationTable = new MockDynamicTable(this, dynamicSchema.getCatalogName(), dynamicSchema.getName(), "NATION", false, 100);
    registerTable(nationTable);
    MockTable customerTable = new MockDynamicTable(this, dynamicSchema.getCatalogName(), dynamicSchema.getName(), "CUSTOMER", false, 100);
    registerTable(customerTable);
    // Register "CUSTOMER" schema.
    MockSchema customerSchema = new MockSchema("CUSTOMER");
    registerSchema(customerSchema);
    // Register "CONTACT" table.
    MockTable contactTable = MockTable.create(this, customerSchema, "CONTACT", false, 1000);
    contactTable.addColumn("CONTACTNO", f.intType);
    contactTable.addColumn("FNAME", f.varchar10Type);
    contactTable.addColumn("LNAME", f.varchar10Type);
    contactTable.addColumn("EMAIL", f.varchar20Type);
    contactTable.addColumn("COORD", f.rectilinearCoordType);
    registerTable(contactTable);
    // Register "CONTACT_PEEK" table. The
    MockTable contactPeekTable = MockTable.create(this, customerSchema, "CONTACT_PEEK", false, 1000);
    contactPeekTable.addColumn("CONTACTNO", f.intType);
    contactPeekTable.addColumn("FNAME", f.varchar10Type);
    contactPeekTable.addColumn("LNAME", f.varchar10Type);
    contactPeekTable.addColumn("EMAIL", f.varchar20Type);
    contactPeekTable.addColumn("COORD", f.rectilinearPeekCoordType);
    contactPeekTable.addColumn("COORD_NE", f.rectilinearPeekNoExpandCoordType);
    registerTable(contactPeekTable);
    // Register "ACCOUNT" table.
    MockTable accountTable = MockTable.create(this, customerSchema, "ACCOUNT", false, 457);
    accountTable.addColumn("ACCTNO", f.intType);
    accountTable.addColumn("TYPE", f.varchar20Type);
    accountTable.addColumn("BALANCE", f.intType);
    registerTable(accountTable);
    // Register "ORDERS" stream.
    MockTable ordersStream = MockTable.create(this, salesSchema, "ORDERS", true, Double.POSITIVE_INFINITY);
    ordersStream.addColumn("ROWTIME", f.timestampType);
    ordersStream.addMonotonic("ROWTIME");
    ordersStream.addColumn("PRODUCTID", f.intType);
    ordersStream.addColumn("ORDERID", f.intType);
    registerTable(ordersStream);
    // Register "SHIPMENTS" stream.
    // "ROWTIME" is not column 0, just to mix things up.
    MockTable shipmentsStream = MockTable.create(this, salesSchema, "SHIPMENTS", true, Double.POSITIVE_INFINITY);
    shipmentsStream.addColumn("ORDERID", f.intType);
    shipmentsStream.addColumn("ROWTIME", f.timestampType);
    shipmentsStream.addMonotonic("ROWTIME");
    registerTable(shipmentsStream);
    // Register "PRODUCTS" table.
    MockTable productsTable = MockTable.create(this, salesSchema, "PRODUCTS", false, 200D);
    productsTable.addColumn("PRODUCTID", f.intType);
    productsTable.addColumn("NAME", f.varchar20Type);
    productsTable.addColumn("SUPPLIERID", f.intType);
    registerTable(productsTable);
    // Register "SUPPLIERS" table.
    MockTable suppliersTable = MockTable.create(this, salesSchema, "SUPPLIERS", false, 10D);
    suppliersTable.addColumn("SUPPLIERID", f.intType);
    suppliersTable.addColumn("NAME", f.varchar20Type);
    suppliersTable.addColumn("CITY", f.intType);
    registerTable(suppliersTable);
    // Register "EMP_20" and "EMPNULLABLES_20 views.
    // Same columns as "EMP" amd "EMPNULLABLES",
    // but "DEPTNO" not visible and set to 20 by default
    // and "SAL" is visible but must be greater than 1000,
    // which is the equivalent of:
    // SELECT EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, SLACKER
    // FROM EMP
    // WHERE DEPTNO = 20 AND SAL > 1000
    final ImmutableIntList m0 = ImmutableIntList.of(0, 1, 2, 3, 4, 5, 6, 8);
    MockTable emp20View = new MockViewTable(this, salesSchema.getCatalogName(), salesSchema.name, "EMP_20", false, 600, empTable, m0, null, NullInitializerExpressionFactory.INSTANCE) {

        public RexNode getConstraint(RexBuilder rexBuilder, RelDataType tableRowType) {
            final RelDataTypeField deptnoField = tableRowType.getFieldList().get(7);
            final RelDataTypeField salField = tableRowType.getFieldList().get(5);
            final List<RexNode> nodes = Arrays.asList(rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeInputRef(deptnoField.getType(), deptnoField.getIndex()), rexBuilder.makeExactLiteral(BigDecimal.valueOf(20L), deptnoField.getType())), rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN, rexBuilder.makeInputRef(salField.getType(), salField.getIndex()), rexBuilder.makeExactLiteral(BigDecimal.valueOf(1000L), salField.getType())));
            return RexUtil.composeConjunction(rexBuilder, nodes, false);
        }
    };
    salesSchema.addTable(Util.last(emp20View.getQualifiedName()));
    emp20View.addColumn("EMPNO", f.intType);
    emp20View.addColumn("ENAME", f.varchar20Type);
    emp20View.addColumn("JOB", f.varchar10Type);
    emp20View.addColumn("MGR", f.intTypeNull);
    emp20View.addColumn("HIREDATE", f.timestampType);
    emp20View.addColumn("SAL", f.intType);
    emp20View.addColumn("COMM", f.intType);
    emp20View.addColumn("SLACKER", f.booleanType);
    registerTable(emp20View);
    MockTable empNullables20View = new MockViewTable(this, salesSchema.getCatalogName(), salesSchema.name, "EMPNULLABLES_20", false, 600, empNullablesTable, m0, null, NullInitializerExpressionFactory.INSTANCE) {

        public RexNode getConstraint(RexBuilder rexBuilder, RelDataType tableRowType) {
            final RelDataTypeField deptnoField = tableRowType.getFieldList().get(7);
            final RelDataTypeField salField = tableRowType.getFieldList().get(5);
            final List<RexNode> nodes = Arrays.asList(rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeInputRef(deptnoField.getType(), deptnoField.getIndex()), rexBuilder.makeExactLiteral(BigDecimal.valueOf(20L), deptnoField.getType())), rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN, rexBuilder.makeInputRef(salField.getType(), salField.getIndex()), rexBuilder.makeExactLiteral(BigDecimal.valueOf(1000L), salField.getType())));
            return RexUtil.composeConjunction(rexBuilder, nodes, false);
        }
    };
    salesSchema.addTable(Util.last(empNullables20View.getQualifiedName()));
    empNullables20View.addColumn("EMPNO", f.intType);
    empNullables20View.addColumn("ENAME", f.varchar20Type);
    empNullables20View.addColumn("JOB", f.varchar10TypeNull);
    empNullables20View.addColumn("MGR", f.intTypeNull);
    empNullables20View.addColumn("HIREDATE", f.timestampTypeNull);
    empNullables20View.addColumn("SAL", f.intTypeNull);
    empNullables20View.addColumn("COMM", f.intTypeNull);
    empNullables20View.addColumn("SLACKER", f.booleanTypeNull);
    registerTable(empNullables20View);
    MockSchema structTypeSchema = new MockSchema("STRUCT");
    registerSchema(structTypeSchema);
    final List<CompoundNameColumn> columns = Arrays.asList(new CompoundNameColumn("", "K0", f.varchar20Type), new CompoundNameColumn("", "C1", f.varchar20Type), new CompoundNameColumn("F1", "A0", f.intType), new CompoundNameColumn("F2", "A0", f.booleanType), new CompoundNameColumn("F0", "C0", f.intType), new CompoundNameColumn("F1", "C0", f.intTypeNull), new CompoundNameColumn("F0", "C1", f.intType), new CompoundNameColumn("F1", "C2", f.intType), new CompoundNameColumn("F2", "C3", f.intType));
    final CompoundNameColumnResolver structTypeTableResolver = new CompoundNameColumnResolver(columns, "F0");
    final MockTable structTypeTable = MockTable.create(this, structTypeSchema, "T", false, 100, structTypeTableResolver);
    for (CompoundNameColumn column : columns) {
        structTypeTable.addColumn(column.getName(), column.type);
    }
    registerTable(structTypeTable);
    final List<CompoundNameColumn> columnsNullable = Arrays.asList(new CompoundNameColumn("", "K0", f.varchar20TypeNull), new CompoundNameColumn("", "C1", f.varchar20TypeNull), new CompoundNameColumn("F1", "A0", f.intTypeNull), new CompoundNameColumn("F2", "A0", f.booleanTypeNull), new CompoundNameColumn("F0", "C0", f.intTypeNull), new CompoundNameColumn("F1", "C0", f.intTypeNull), new CompoundNameColumn("F0", "C1", f.intTypeNull), new CompoundNameColumn("F1", "C2", f.intType), new CompoundNameColumn("F2", "C3", f.intTypeNull));
    final MockTable structNullableTypeTable = MockTable.create(this, structTypeSchema, "T_NULLABLES", false, 100, structTypeTableResolver);
    for (CompoundNameColumn column : columnsNullable) {
        structNullableTypeTable.addColumn(column.getName(), column.type);
    }
    registerTable(structNullableTypeTable);
    // Register "STRUCT.T_10" view.
    // Same columns as "STRUCT.T",
    // but "F0.C0" is set to 10 by default,
    // which is the equivalent of:
    // SELECT *
    // FROM T
    // WHERE F0.C0 = 10
    // This table uses MockViewTable which does not populate the constrained columns with default
    // values on INSERT.
    final ImmutableIntList m1 = ImmutableIntList.of(0, 1, 2, 3, 4, 5, 6, 7, 8);
    MockTable struct10View = new MockViewTable(this, structTypeSchema.getCatalogName(), structTypeSchema.name, "T_10", false, 20, structTypeTable, m1, structTypeTableResolver, NullInitializerExpressionFactory.INSTANCE) {

        @Override
        public RexNode getConstraint(RexBuilder rexBuilder, RelDataType tableRowType) {
            final RelDataTypeField c0Field = tableRowType.getFieldList().get(4);
            return rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeInputRef(c0Field.getType(), c0Field.getIndex()), rexBuilder.makeExactLiteral(BigDecimal.valueOf(10L), c0Field.getType()));
        }
    };
    structTypeSchema.addTable(Util.last(struct10View.getQualifiedName()));
    for (CompoundNameColumn column : columns) {
        struct10View.addColumn(column.getName(), column.type);
    }
    registerTable(struct10View);
    registerTablesWithRollUp(salesSchema, f);
    return this;
}
Also used : NullInitializerExpressionFactory(org.apache.calcite.sql2rel.NullInitializerExpressionFactory) InitializerExpressionFactory(org.apache.calcite.sql2rel.InitializerExpressionFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RexBuilder(org.apache.calcite.rex.RexBuilder) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) RexNode(org.apache.calcite.rex.RexNode)

Example 17 with ImmutableIntList

use of org.apache.calcite.util.ImmutableIntList in project calcite by apache.

the class RelRoot method of.

/**
 * Creates a simple RelRoot.
 */
public static RelRoot of(RelNode rel, RelDataType rowType, SqlKind kind) {
    final ImmutableIntList refs = ImmutableIntList.identity(rowType.getFieldCount());
    final List<String> names = rowType.getFieldNames();
    return new RelRoot(rel, rowType, kind, Pair.zip(refs, names), RelCollations.EMPTY);
}
Also used : ImmutableIntList(org.apache.calcite.util.ImmutableIntList)

Aggregations

ImmutableIntList (org.apache.calcite.util.ImmutableIntList)17 RexNode (org.apache.calcite.rex.RexNode)11 RexBuilder (org.apache.calcite.rex.RexBuilder)6 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)6 ArrayList (java.util.ArrayList)5 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)5 RelNode (org.apache.calcite.rel.RelNode)4 Join (org.apache.calcite.rel.core.Join)4 RelOptCluster (org.apache.calcite.plan.RelOptCluster)3 JoinInfo (org.apache.calcite.rel.core.JoinInfo)3 RelDataType (org.apache.calcite.rel.type.RelDataType)3 ImmutableList (com.google.common.collect.ImmutableList)2 Bindables (org.apache.calcite.interpreter.Bindables)2 RelOptTable (org.apache.calcite.plan.RelOptTable)2 HepRelVertex (org.apache.calcite.plan.hep.HepRelVertex)2 RelCollation (org.apache.calcite.rel.RelCollation)2 SemiJoin (org.apache.calcite.rel.core.SemiJoin)2 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)2 Pair (org.apache.calcite.util.Pair)2 HashMap (java.util.HashMap)1