Search in sources :

Example 6 with TranslatableTable

use of org.apache.calcite.schema.TranslatableTable in project calcite by apache.

the class MockCatalogReader method init2.

/**
 * Adds some extra tables to the mock catalog. These increase the time and
 * complexity of initializing the catalog (because they contain views whose
 * SQL needs to be parsed) and so are not used for all tests.
 */
public MockCatalogReader init2() {
    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.name, "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.name, "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.name, "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();
    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<CompoundNameColumn>(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);
    return this;
}
Also used : ViewTableMacro(org.apache.calcite.schema.impl.ViewTableMacro) TableMacro(org.apache.calcite.schema.TableMacro) ArrayList(java.util.ArrayList) TranslatableTable(org.apache.calcite.schema.TranslatableTable)

Example 7 with TranslatableTable

use of org.apache.calcite.schema.TranslatableTable in project calcite by apache.

the class SqlCreateView method execute.

public void execute(CalcitePrepare.Context context) {
    final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
    final SchemaPlus schemaPlus = pair.left.plus();
    for (Function function : schemaPlus.getFunctions(pair.right)) {
        if (function.getParameters().isEmpty()) {
            if (!getReplace()) {
                throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.viewExists(pair.right));
            }
            pair.left.removeFunction(pair.right);
        }
    }
    final SqlNode q = SqlDdlNodes.renameColumns(columnList, query);
    final String sql = q.toSqlString(CalciteSqlDialect.DEFAULT).getSql();
    final ViewTableMacro viewTableMacro = ViewTable.viewMacro(schemaPlus, sql, pair.left.path(null), context.getObjectPath(), false);
    final TranslatableTable x = viewTableMacro.apply(ImmutableList.of());
    Util.discard(x);
    schemaPlus.add(pair.right, viewTableMacro);
}
Also used : Function(org.apache.calcite.schema.Function) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) ViewTableMacro(org.apache.calcite.schema.impl.ViewTableMacro) SchemaPlus(org.apache.calcite.schema.SchemaPlus) TranslatableTable(org.apache.calcite.schema.TranslatableTable) SqlNode(org.apache.calcite.sql.SqlNode)

Example 8 with TranslatableTable

use of org.apache.calcite.schema.TranslatableTable in project calcite by apache.

the class SqlToRelConverter method convertCollectionTable.

protected void convertCollectionTable(Blackboard bb, SqlCall call) {
    final SqlOperator operator = call.getOperator();
    if (operator == SqlStdOperatorTable.TABLESAMPLE) {
        final String sampleName = SqlLiteral.unchain(call.operand(0)).getValueAs(String.class);
        datasetStack.push(sampleName);
        SqlCall cursorCall = call.operand(1);
        SqlNode query = cursorCall.operand(0);
        RelNode converted = convertQuery(query, false, false).rel;
        bb.setRoot(converted, false);
        datasetStack.pop();
        return;
    }
    replaceSubQueries(bb, call, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
    // Expand table macro if possible. It's more efficient than
    // LogicalTableFunctionScan.
    final SqlCallBinding callBinding = new SqlCallBinding(bb.scope.getValidator(), bb.scope, call);
    if (operator instanceof SqlUserDefinedTableMacro) {
        final SqlUserDefinedTableMacro udf = (SqlUserDefinedTableMacro) operator;
        final TranslatableTable table = udf.getTable(typeFactory, callBinding.operands());
        final RelDataType rowType = table.getRowType(typeFactory);
        RelOptTable relOptTable = RelOptTableImpl.create(null, rowType, table, udf.getNameAsId().names);
        RelNode converted = toRel(relOptTable);
        bb.setRoot(converted, true);
        return;
    }
    Type elementType;
    if (operator instanceof SqlUserDefinedTableFunction) {
        SqlUserDefinedTableFunction udtf = (SqlUserDefinedTableFunction) operator;
        elementType = udtf.getElementType(typeFactory, callBinding.operands());
    } else {
        elementType = null;
    }
    RexNode rexCall = bb.convertExpression(call);
    final List<RelNode> inputs = bb.retrieveCursors();
    Set<RelColumnMapping> columnMappings = getColumnMappings(operator);
    LogicalTableFunctionScan callRel = LogicalTableFunctionScan.create(cluster, inputs, rexCall, elementType, validator.getValidatedNodeType(call), columnMappings);
    bb.setRoot(callRel, true);
    afterTableFunction(bb, call, callRel);
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) SqlCall(org.apache.calcite.sql.SqlCall) SqlUserDefinedTableMacro(org.apache.calcite.sql.validate.SqlUserDefinedTableMacro) RelDataType(org.apache.calcite.rel.type.RelDataType) NlsString(org.apache.calcite.util.NlsString) LogicalTableFunctionScan(org.apache.calcite.rel.logical.LogicalTableFunctionScan) JoinType(org.apache.calcite.sql.JoinType) RelDataType(org.apache.calcite.rel.type.RelDataType) JoinRelType(org.apache.calcite.rel.core.JoinRelType) JoinConditionType(org.apache.calcite.sql.JoinConditionType) Type(java.lang.reflect.Type) SemiJoinType(org.apache.calcite.sql.SemiJoinType) SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) RelNode(org.apache.calcite.rel.RelNode) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) TranslatableTable(org.apache.calcite.schema.TranslatableTable) RelOptTable(org.apache.calcite.plan.RelOptTable) RelColumnMapping(org.apache.calcite.rel.metadata.RelColumnMapping) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

TranslatableTable (org.apache.calcite.schema.TranslatableTable)7 CalciteSchema (org.apache.calcite.jdbc.CalciteSchema)4 RelDataType (org.apache.calcite.rel.type.RelDataType)4 ViewTableMacro (org.apache.calcite.schema.impl.ViewTableMacro)4 SqlNode (org.apache.calcite.sql.SqlNode)4 RelOptTable (org.apache.calcite.plan.RelOptTable)3 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)2 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)2 ColumnStrategy (org.apache.calcite.schema.ColumnStrategy)2 QueryableTable (org.apache.calcite.schema.QueryableTable)2 ImmutableList (com.google.common.collect.ImmutableList)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 CalciteConnectionConfigImpl (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.config.CalciteConnectionConfigImpl)1 RelOptTable (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptTable)1 CalciteCatalogReader (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.prepare.CalciteCatalogReader)1 RelOptTableImpl (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.prepare.RelOptTableImpl)1 SchemaPlus (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus)1 Table (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Table)1