Search in sources :

Example 6 with TableMacro

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

the class CachingCalciteSchema method getImplicitTableBasedOnNullaryFunction.

protected TableEntry getImplicitTableBasedOnNullaryFunction(String tableName, boolean caseSensitive) {
    final long now = System.currentTimeMillis();
    final NameSet set = implicitFunctionCache.get(now);
    for (String s : set.range(tableName, caseSensitive)) {
        for (Function function : schema.getFunctions(s)) {
            if (function instanceof TableMacro && function.getParameters().isEmpty()) {
                final Table table = ((TableMacro) function).apply(ImmutableList.of());
                return tableEntry(tableName, table);
            }
        }
    }
    return null;
}
Also used : Function(org.apache.calcite.schema.Function) TableMacro(org.apache.calcite.schema.TableMacro) Table(org.apache.calcite.schema.Table) NameSet(org.apache.calcite.util.NameSet)

Example 7 with TableMacro

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

the class JdbcTest method testTableMacroMap.

/**
 * Table macro that takes a MAP as a parameter.
 *
 * <p>Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-588">[CALCITE-588]
 * Allow TableMacro to consume Maps and Collections</a>.
 */
@Test
public void testTableMacroMap() throws SQLException, ClassNotFoundException {
    Connection connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
    final TableMacro tableMacro = TableMacroImpl.create(Smalls.STR_METHOD);
    schema.add("Str", tableMacro);
    ResultSet resultSet = connection.createStatement().executeQuery("select *\n" + "from table(\"s\".\"Str\"(MAP['a', 1, 'baz', 2],\n" + "                         ARRAY[3, 4, CAST(null AS INTEGER)])) as t(n)");
    // The call to "View('(10), (2)')" expands to 'values (1), (3), (10), (20)'.
    assertThat(CalciteAssert.toString(resultSet), equalTo("N={'a'=1, 'baz'=2}\n" + "N=[3, 4, null]    \n"));
    connection.close();
}
Also used : TableMacro(org.apache.calcite.schema.TableMacro) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) Connection(java.sql.Connection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 8 with TableMacro

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

the class JdbcTest method testTableMacro.

/**
 * Tests a relation that is accessed via method syntax.
 *
 * <p>The function ({@link Smalls#view(String)} has a return type
 * {@link Table} and the actual returned value implements
 * {@link org.apache.calcite.schema.TranslatableTable}.
 */
@Test
public void testTableMacro() throws SQLException, ClassNotFoundException {
    Connection connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
    final TableMacro tableMacro = TableMacroImpl.create(Smalls.VIEW_METHOD);
    schema.add("View", tableMacro);
    ResultSet resultSet = connection.createStatement().executeQuery("select *\n" + "from table(\"s\".\"View\"('(10), (20)')) as t(n)\n" + "where n < 15");
    // The call to "View('(10), (2)')" expands to 'values (1), (3), (10), (20)'.
    assertThat(CalciteAssert.toString(resultSet), equalTo("N=1\n" + "N=3\n" + "N=10\n"));
    connection.close();
}
Also used : TableMacro(org.apache.calcite.schema.TableMacro) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) Connection(java.sql.Connection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 9 with TableMacro

use of org.apache.calcite.schema.TableMacro 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 10 with TableMacro

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

the class ReflectiveSchema method createFunctionMap.

private Multimap<String, Function> createFunctionMap() {
    final ImmutableMultimap.Builder<String, Function> builder = ImmutableMultimap.builder();
    for (Method method : clazz.getMethods()) {
        final String methodName = method.getName();
        if (method.getDeclaringClass() == Object.class || methodName.equals("toString")) {
            continue;
        }
        if (TranslatableTable.class.isAssignableFrom(method.getReturnType())) {
            final TableMacro tableMacro = new MethodTableMacro(this, method);
            builder.put(methodName, tableMacro);
        }
    }
    return builder.build();
}
Also used : Function(org.apache.calcite.schema.Function) TableMacro(org.apache.calcite.schema.TableMacro) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) BuiltInMethod(org.apache.calcite.util.BuiltInMethod) Method(java.lang.reflect.Method)

Aggregations

TableMacro (org.apache.calcite.schema.TableMacro)11 Function (org.apache.calcite.schema.Function)5 Table (org.apache.calcite.schema.Table)4 Connection (java.sql.Connection)3 Map (java.util.Map)3 AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)3 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)3 SchemaPlus (org.apache.calcite.schema.SchemaPlus)3 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)3 Test (org.junit.Test)3 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 AggregateFunction (org.apache.calcite.schema.AggregateFunction)2 ScalarFunction (org.apache.calcite.schema.ScalarFunction)2 TableFunction (org.apache.calcite.schema.TableFunction)2 NameSet (org.apache.calcite.util.NameSet)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 Method (java.lang.reflect.Method)1