com.hazelcast.org.apache.calcite.schema.impl.StarTable Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.org.apache.calcite.schema.impl;
import com.hazelcast.org.apache.calcite.materialize.Lattice;
import com.hazelcast.org.apache.calcite.plan.Convention;
import com.hazelcast.org.apache.calcite.plan.RelOptCluster;
import com.hazelcast.org.apache.calcite.plan.RelOptCost;
import com.hazelcast.org.apache.calcite.plan.RelOptPlanner;
import com.hazelcast.org.apache.calcite.plan.RelOptTable;
import com.hazelcast.org.apache.calcite.plan.RelOptUtil;
import com.hazelcast.org.apache.calcite.rel.RelNode;
import com.hazelcast.org.apache.calcite.rel.core.TableScan;
import com.hazelcast.org.apache.calcite.rel.metadata.RelMetadataQuery;
import com.hazelcast.org.apache.calcite.rel.type.RelDataType;
import com.hazelcast.org.apache.calcite.rel.type.RelDataTypeFactory;
import com.hazelcast.org.apache.calcite.schema.Schema;
import com.hazelcast.org.apache.calcite.schema.Table;
import com.hazelcast.org.apache.calcite.schema.TranslatableTable;
import com.hazelcast.org.apache.calcite.util.ImmutableIntList;
import com.hazelcast.org.apache.calcite.util.Pair;
import com.hazelcast.com.google.common.collect.ImmutableList;
import com.hazelcast.org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import com.hazelcast.org.checkerframework.checker.nullness.qual.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static com.hazelcast.org.apache.calcite.linq4j.Nullness.castNonNull;
/**
* Virtual table that is composed of two or more tables joined together.
*
* Star tables do not occur in end-user queries. They are introduced by the
* optimizer to help matching queries to materializations, and used only
* during the planning process.
*
* When a materialization is defined, if it involves a join, it is converted
* to a query on top of a star table. Queries that are candidates to map onto
* the materialization are mapped onto the same star table.
*/
public class StarTable extends AbstractTable implements TranslatableTable {
public final Lattice lattice;
// TODO: we'll also need a list of join conditions between tables. For now
// we assume that join conditions match
public final ImmutableList tables;
/** Number of fields in each table's row type. */
public @MonotonicNonNull ImmutableIntList fieldCounts;
/** Creates a StarTable. */
private StarTable(Lattice lattice, ImmutableList tables) {
this.lattice = Objects.requireNonNull(lattice, "lattice");
this.tables = tables;
}
/** Creates a StarTable and registers it in a schema. */
public static StarTable of(Lattice lattice, List tables) {
return new StarTable(lattice, ImmutableList.copyOf(tables));
}
@Override public Schema.TableType getJdbcTableType() {
return Schema.TableType.STAR;
}
@Override public RelDataType getRowType(RelDataTypeFactory typeFactory) {
final List typeList = new ArrayList<>();
final List fieldCounts = new ArrayList<>();
for (Table table : tables) {
final RelDataType rowType = table.getRowType(typeFactory);
typeList.addAll(RelOptUtil.getFieldTypeList(rowType));
fieldCounts.add(rowType.getFieldCount());
}
// Compute fieldCounts the first time this method is called. Safe to assume
// 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());
}
@Override public RelNode toRel(RelOptTable.ToRelContext context, RelOptTable table) {
// Create a table scan of infinite cost.
return new StarTableScan(context.getCluster(), table);
}
public StarTable add(Table table) {
return of(lattice,
ImmutableList.builder().addAll(tables).add(table).build());
}
/** Returns the column offset of the first column of {@code table} in this
* star table's output row type.
*
* @param table Table
* @return Column offset
* @throws IllegalArgumentException if table is not in this star
*/
public int columnOffset(Table table) {
int n = 0;
for (Pair pair : Pair.zip(tables, castNonNull(fieldCounts))) {
if (pair.left == table) {
return n;
}
n += pair.right;
}
throw new IllegalArgumentException("star table " + this
+ " does not contain table " + table);
}
/** Relational expression that scans a {@link StarTable}.
*
* It has infinite cost.
*/
public static class StarTableScan extends TableScan {
public StarTableScan(RelOptCluster cluster, RelOptTable relOptTable) {
super(cluster, cluster.traitSetOf(Convention.NONE), ImmutableList.of(), relOptTable);
}
@Override public @Nullable RelOptCost computeSelfCost(RelOptPlanner planner,
RelMetadataQuery mq) {
return planner.getCostFactory().makeInfiniteCost();
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy