Search in sources :

Example 86 with Table

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

the class SqlLatticeStatisticProvider method cardinality.

private double cardinality(Lattice lattice, Lattice.Column column) {
    final String sql = lattice.countSql(ImmutableBitSet.of(column.ordinal));
    final Table table = new MaterializationService.DefaultTableFactory().createTable(lattice.rootSchema, sql, ImmutableList.<String>of());
    final Object[] values = Iterables.getOnlyElement(((ScannableTable) table).scan(null));
    return ((Number) values[0]).doubleValue();
}
Also used : ScannableTable(org.apache.calcite.schema.ScannableTable) Table(org.apache.calcite.schema.Table)

Example 87 with Table

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

the class ReflectiveSchema method createTableMap.

private Map<String, Table> createTableMap() {
    final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder();
    for (Field field : clazz.getFields()) {
        final String fieldName = field.getName();
        final Table table = fieldRelation(field);
        if (table == null) {
            continue;
        }
        builder.put(fieldName, table);
    }
    Map<String, Table> tableMap = builder.build();
    // Unique-Key - Foreign-Key
    for (Field field : clazz.getFields()) {
        if (RelReferentialConstraint.class.isAssignableFrom(field.getType())) {
            RelReferentialConstraint rc;
            try {
                rc = (RelReferentialConstraint) field.get(target);
            } catch (IllegalAccessException e) {
                throw new RuntimeException("Error while accessing field " + field, e);
            }
            FieldTable table = (FieldTable) tableMap.get(Util.last(rc.getSourceQualifiedName()));
            assert table != null;
            table.statistic = Statistics.of(ImmutableList.copyOf(Iterables.concat(table.getStatistic().getReferentialConstraints(), Collections.singleton(rc))));
        }
    }
    return tableMap;
}
Also used : Field(java.lang.reflect.Field) RelReferentialConstraint(org.apache.calcite.rel.RelReferentialConstraint) ScannableTable(org.apache.calcite.schema.ScannableTable) TranslatableTable(org.apache.calcite.schema.TranslatableTable) Table(org.apache.calcite.schema.Table) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 88 with Table

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

the class StarTable method getRowType.

public RelDataType getRowType(RelDataTypeFactory typeFactory) {
    final List<RelDataType> typeList = new ArrayList<RelDataType>();
    final List<Integer> fieldCounts = new ArrayList<Integer>();
    for (Table table : tables) {
        final RelDataType rowType = table.getRowType(typeFactory);
        typeList.addAll(RelOptUtil.getFieldTypeList(rowType));
        fieldCounts.add(rowType.getFieldCount());
    }
    // that the field counts will be the same whichever type factory is used.
    if (this.fieldCounts == null) {
        this.fieldCounts = ImmutableIntList.copyOf(fieldCounts);
    }
    return typeFactory.createStructType(typeList, lattice.uniqueColumnNames);
}
Also used : TranslatableTable(org.apache.calcite.schema.TranslatableTable) Table(org.apache.calcite.schema.Table) RelOptTable(org.apache.calcite.plan.RelOptTable) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 89 with Table

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

the class FileSchema method addTable.

private boolean addTable(ImmutableMap.Builder<String, Table> builder, Source source, String tableName, Map<String, Object> tableDef) {
    final Source sourceSansGz = source.trim(".gz");
    final Source sourceSansJson = sourceSansGz.trimOrNull(".json");
    if (sourceSansJson != null) {
        JsonTable table = new JsonTable(source);
        builder.put(Util.first(tableName, sourceSansJson.path()), table);
        return true;
    }
    final Source sourceSansCsv = sourceSansGz.trimOrNull(".csv");
    if (sourceSansCsv != null) {
        final Table table = new CsvFilterableTable(source, null);
        builder.put(Util.first(tableName, sourceSansCsv.path()), table);
        return true;
    }
    if (tableDef != null) {
        try {
            FileTable table = FileTable.create(source, tableDef);
            builder.put(Util.first(tableName, source.path()), table);
            return true;
        } catch (Exception e) {
            throw new RuntimeException("Unable to instantiate table for: " + tableName);
        }
    }
    return false;
}
Also used : JsonTable(org.apache.calcite.adapter.csv.JsonTable) Table(org.apache.calcite.schema.Table) CsvFilterableTable(org.apache.calcite.adapter.csv.CsvFilterableTable) JsonTable(org.apache.calcite.adapter.csv.JsonTable) CsvFilterableTable(org.apache.calcite.adapter.csv.CsvFilterableTable) Source(org.apache.calcite.util.Source) MalformedURLException(java.net.MalformedURLException)

Example 90 with Table

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

the class FileSchema method getTableMap.

@Override
protected Map<String, Table> getTableMap() {
    final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder();
    for (Map<String, Object> tableDef : this.tables) {
        String tableName = (String) tableDef.get("name");
        try {
            addTable(builder, tableDef);
        } catch (MalformedURLException e) {
            throw new RuntimeException("Unable to instantiate table for: " + tableName);
        }
    }
    // Look for files in the directory ending in ".csv", ".csv.gz", ".json",
    // ".json.gz".
    final Source baseSource = Sources.of(baseDirectory);
    File[] files = baseDirectory.listFiles(new FilenameFilter() {

        public boolean accept(File dir, String name) {
            final String nameSansGz = trim(name, ".gz");
            return nameSansGz.endsWith(".csv") || nameSansGz.endsWith(".json");
        }
    });
    if (files == null) {
        System.out.println("directory " + baseDirectory + " not found");
        files = new File[0];
    }
    // Build a map from table name to table; each file becomes a table.
    for (File file : files) {
        Source source = Sources.of(file);
        Source sourceSansGz = source.trim(".gz");
        final Source sourceSansJson = sourceSansGz.trimOrNull(".json");
        if (sourceSansJson != null) {
            JsonTable table = new JsonTable(source);
            builder.put(sourceSansJson.relative(baseSource).path(), table);
            continue;
        }
        final Source sourceSansCsv = sourceSansGz.trimOrNull(".csv");
        if (sourceSansCsv != null) {
            addTable(builder, source, sourceSansCsv.relative(baseSource).path(), null);
        }
    }
    return builder.build();
}
Also used : MalformedURLException(java.net.MalformedURLException) Table(org.apache.calcite.schema.Table) CsvFilterableTable(org.apache.calcite.adapter.csv.CsvFilterableTable) JsonTable(org.apache.calcite.adapter.csv.JsonTable) JsonTable(org.apache.calcite.adapter.csv.JsonTable) ImmutableMap(com.google.common.collect.ImmutableMap) Source(org.apache.calcite.util.Source) FilenameFilter(java.io.FilenameFilter) File(java.io.File)

Aggregations

Table (org.apache.calcite.schema.Table)104 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)38 RelOptTable (org.apache.calcite.plan.RelOptTable)33 Test (org.junit.Test)27 RelDataType (org.apache.calcite.rel.type.RelDataType)22 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)22 SchemaPlus (org.apache.calcite.schema.SchemaPlus)20 ProjectableFilterableTable (org.apache.calcite.schema.ProjectableFilterableTable)17 ScannableTable (org.apache.calcite.schema.ScannableTable)17 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)16 FilterableTable (org.apache.calcite.schema.FilterableTable)15 AbstractTable (org.apache.calcite.schema.impl.AbstractTable)15 StreamableTable (org.apache.calcite.schema.StreamableTable)14 ArrayList (java.util.ArrayList)13 ModifiableViewTable (org.apache.calcite.schema.impl.ModifiableViewTable)13 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)12 RelNode (org.apache.calcite.rel.RelNode)12 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)10 BitString (org.apache.calcite.util.BitString)10 ImmutableMap (com.google.common.collect.ImmutableMap)9