org.apache.calcite.schema.Table Java Examples
The following examples show how to use
org.apache.calcite.schema.Table.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BatsOptimizerTest.java From Bats with Apache License 2.0 | 6 votes |
static Table createTable() { return new AbstractTable() { @Override public RelDataType getRowType(final RelDataTypeFactory typeFactory) { RelDataTypeFactory.Builder builder = typeFactory.builder(); RelDataType t1 = typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.INTEGER), true); RelDataType t2 = typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.INTEGER), true); RelDataType t3 = typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.INTEGER), true); builder.add("f1", t1); builder.add("f2", t2); builder.add("f3", t3); return builder.build(); } }; }
Example #2
Source File: ScannableTableTest.java From calcite with Apache License 2.0 | 6 votes |
/** Test case for * <a href="https://issues.apache.org/jira/browse/CALCITE-3405">[CALCITE-3405] * Prune columns for ProjectableFilterable when project is not simple mapping</a>. */ @Test void testPushSimpleMappingProject() throws Exception { final StringBuilder buf = new StringBuilder(); final Table table = new BeatlesProjectableFilterableTable(buf, true); // Note that no redundant Project on EnumerableInterpreter final String explain = "PLAN=" + "EnumerableInterpreter\n" + " BindableTableScan(table=[[s, beatles]], projects=[[2, 0]])"; CalciteAssert.that() .with(newSchema("s", Pair.of("beatles", table))) .query("select \"k\", \"i\" from \"s\".\"beatles\"") .explainContains(explain) .returnsUnordered( "k=1940; i=4", "k=1940; i=5", "k=1942; i=4", "k=1943; i=6"); assertThat(buf.toString(), is("returnCount=4, projects=[2, 0]")); }
Example #3
Source File: StreamRules.java From Bats with Apache License 2.0 | 6 votes |
@Override public void onMatch(RelOptRuleCall call) { final Delta delta = call.rel(0); final TableScan scan = call.rel(1); final RelOptCluster cluster = delta.getCluster(); final RelOptTable relOptTable = scan.getTable(); final StreamableTable streamableTable = relOptTable.unwrap(StreamableTable.class); if (streamableTable != null) { final Table table1 = streamableTable.stream(); final RelOptTable relOptTable2 = RelOptTableImpl.create(relOptTable.getRelOptSchema(), relOptTable.getRowType(), table1, ImmutableList.<String>builder() .addAll(relOptTable.getQualifiedName()) .add("(STREAM)").build()); final LogicalTableScan newScan = LogicalTableScan.create(cluster, relOptTable2); call.transformTo(newScan); } }
Example #4
Source File: ScannableTableTest.java From calcite with Apache License 2.0 | 6 votes |
@Test void testPFPushDownProjectFilterAggregateNested() { final StringBuilder buf = new StringBuilder(); final String sql = "select \"k\", count(*) as c\n" + "from (\n" + " select \"k\", \"i\" from \"s\".\"beatles\" group by \"k\", \"i\") t\n" + "where \"k\" = 1940\n" + "group by \"k\""; final Table table = new BeatlesProjectableFilterableTable(buf, false); final String explain = "PLAN=" + "EnumerableAggregate(group=[{0}], C=[COUNT()])\n" + " EnumerableAggregate(group=[{0, 1}])\n" + " EnumerableInterpreter\n" + " BindableTableScan(table=[[s, beatles]], filters=[[=($2, 1940)]], projects=[[2, 0]])"; CalciteAssert.that() .with(newSchema("s", Pair.of("beatles", table))) .query(sql) .explainContains(explain) .returnsUnordered("k=1940; C=2"); }
Example #5
Source File: ModifiableViewTable.java From Bats with Apache License 2.0 | 6 votes |
/** * Creates a mapping from the view index to the index in the underlying table. */ private static ImmutableIntList getNewColumnMapping(Table underlying, ImmutableIntList oldColumnMapping, List<RelDataTypeField> extendedColumns, RelDataTypeFactory typeFactory) { final List<RelDataTypeField> baseColumns = underlying.getRowType(typeFactory).getFieldList(); final Map<String, Integer> nameToIndex = mapNameToIndex(baseColumns); final ImmutableList.Builder<Integer> newMapping = ImmutableList.builder(); newMapping.addAll(oldColumnMapping); int newMappedIndex = baseColumns.size(); for (RelDataTypeField extendedColumn : extendedColumns) { if (nameToIndex.containsKey(extendedColumn.getName())) { // The extended column duplicates a column in the underlying table. // Map to the index in the underlying table. newMapping.add(nameToIndex.get(extendedColumn.getName())); } else { // The extended column is not in the underlying table. newMapping.add(newMappedIndex++); } } return ImmutableIntList.copyOf(newMapping.build()); }
Example #6
Source File: JdbcRelBuilder.java From calcite-sql-rewriter with Apache License 2.0 | 6 votes |
public JdbcRelBuilder insertCopying( LogicalTableModify original, Table table ) { List<String> name = JdbcTableUtils.getQualifiedName(original.getTable(), table); push(new LogicalTableModify( cluster, original.getTraitSet(), relOptSchema.getTableForMember(name), original.getCatalogReader(), peek(), TableModify.Operation.INSERT, null, null, original.isFlattened() )); return this; }
Example #7
Source File: RelOptTableImpl.java From calcite with Apache License 2.0 | 6 votes |
private static Function<Class, Expression> getClassExpressionFunction( final SchemaPlus schema, final String tableName, final Table table) { if (table instanceof QueryableTable) { final QueryableTable queryableTable = (QueryableTable) table; return clazz -> queryableTable.getExpression(schema, tableName, clazz); } else if (table instanceof ScannableTable || table instanceof FilterableTable || table instanceof ProjectableFilterableTable) { return clazz -> Schemas.tableExpression(schema, Object[].class, tableName, table.getClass()); } else if (table instanceof StreamableTable) { return getClassExpressionFunction(schema, tableName, ((StreamableTable) table).stream()); } else { return input -> { throw new UnsupportedOperationException(); }; } }
Example #8
Source File: TpchSchema.java From calcite with Apache License 2.0 | 6 votes |
public TpchSchema(double scaleFactor, int part, int partCount, boolean columnPrefix) { this.scaleFactor = scaleFactor; this.part = part; this.partCount = partCount; this.columnPrefix = columnPrefix; final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder(); for (TpchTable<?> tpchTable : TpchTable.getTables()) { builder.put(tpchTable.getTableName().toUpperCase(Locale.ROOT), new TpchQueryableTable(tpchTable)); } this.tableMap = builder.build(); this.columnPrefixes = ImmutableMap.<String, String>builder() .put("LINEITEM", "L_") .put("CUSTOMER", "C_") .put("SUPPLIER", "S_") .put("PARTSUPP", "PS_") .put("PART", "P_") .put("ORDERS", "O_") .put("NATION", "N_") .put("REGION", "R_") .build(); }
Example #9
Source File: SqlExprToRexConverterImpl.java From flink with Apache License 2.0 | 6 votes |
/** * Creates a catalog reader that contains a single {@link Table} with temporary table name * and specified {@code rowType}. * * @param rowType table row type * @return the {@link CalciteCatalogReader} instance */ private static CalciteCatalogReader createSingleTableCatalogReader( boolean lenientCaseSensitivity, FrameworkConfig config, FlinkTypeFactory typeFactory, RelDataType rowType) { // connection properties boolean caseSensitive = !lenientCaseSensitivity && config.getParserConfig().caseSensitive(); Properties properties = new Properties(); properties.put( CalciteConnectionProperty.CASE_SENSITIVE.camelName(), String.valueOf(caseSensitive)); CalciteConnectionConfig connectionConfig = new CalciteConnectionConfigImpl(properties); // prepare root schema final RowTypeSpecifiedTable table = new RowTypeSpecifiedTable(rowType); final Map<String, Table> tableMap = Collections.singletonMap(TEMPORARY_TABLE_NAME, table); CalciteSchema schema = CalciteSchemaBuilder.asRootSchema(new TableSpecifiedSchema(tableMap)); return new FlinkCalciteCatalogReader( schema, new ArrayList<>(new ArrayList<>()), typeFactory, connectionConfig); }
Example #10
Source File: CalciteCatalogReader.java From calcite with Apache License 2.0 | 6 votes |
public Prepare.PreparingTable getTable(final List<String> names) { // First look in the default schema, if any. // If not found, look in the root schema. CalciteSchema.TableEntry entry = SqlValidatorUtil.getTableEntry(this, names); if (entry != null) { final Table table = entry.getTable(); if (table instanceof Wrapper) { final Prepare.PreparingTable relOptTable = ((Wrapper) table).unwrap(Prepare.PreparingTable.class); if (relOptTable != null) { return relOptTable; } } return RelOptTableImpl.create(this, table.getRowType(typeFactory), entry, null); } return null; }
Example #11
Source File: ReflectiveSchema.java From calcite with Apache License 2.0 | 6 votes |
/** Returns a table based on a particular field of this schema. If the * field is not of the right type to be a relation, returns null. */ private <T> Table fieldRelation(final Field field) { final Type elementType = getElementType(field.getType()); if (elementType == null) { return null; } Object o; try { o = field.get(target); } catch (IllegalAccessException e) { throw new RuntimeException( "Error while accessing field " + field, e); } @SuppressWarnings("unchecked") final Enumerable<T> enumerable = toEnumerable(o); return new FieldTable<>(field, elementType, enumerable); }
Example #12
Source File: Prepare.java From calcite with Apache License 2.0 | 6 votes |
public final RelOptTable extend(List<RelDataTypeField> extendedFields) { final Table table = unwrap(Table.class); // Get the set of extended columns that do not have the same name as a column // in the base table. final List<RelDataTypeField> baseColumns = getRowType().getFieldList(); final List<RelDataTypeField> dedupedFields = RelOptUtil.deduplicateColumns(baseColumns, extendedFields); final List<RelDataTypeField> dedupedExtendedFields = dedupedFields.subList(baseColumns.size(), dedupedFields.size()); if (table instanceof ExtensibleTable) { final Table extendedTable = ((ExtensibleTable) table).extend(dedupedExtendedFields); return extend(extendedTable); } else if (table instanceof ModifiableViewTable) { final ModifiableViewTable modifiableViewTable = (ModifiableViewTable) table; final ModifiableViewTable extendedView = modifiableViewTable.extend(dedupedExtendedFields, getRelOptSchema().getTypeFactory()); return extend(extendedView); } throw new RuntimeException("Cannot extend " + table); }
Example #13
Source File: MockStorageEngine.java From Bats with Apache License 2.0 | 6 votes |
private Table getDirectTable(String name) { Pattern p = Pattern.compile("(\\w+)_(\\d+)(k|m)?", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(name); if (! m.matches()) { return null; } @SuppressWarnings("unused") String baseName = m.group(1); int n = Integer.parseInt(m.group(2)); String unit = m.group(3); if (unit == null) { } else if (unit.equalsIgnoreCase("K")) { n *= 1000; } else if (unit.equalsIgnoreCase("M")) { n *= 1_000_000; } MockTableDef.MockScanEntry entry = new MockTableDef.MockScanEntry(n, true, 0, 1, null); List<MockTableDef.MockScanEntry> list = new ArrayList<>(); list.add(entry); return new DynamicDrillTable(engine, this.name, list); }
Example #14
Source File: SqlValidatorImpl.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
private boolean isRolledUpColumnAllowedInAgg(SqlIdentifier identifier, SqlValidatorScope scope, SqlCall aggCall, SqlNode parent) { Pair<String, String> pair = findTableColumnPair(identifier, scope); if (pair == null) { return true; } String tableAlias = pair.left; String columnName = pair.right; Table table = findTable(tableAlias); if (table != null) { return table.rolledUpColumnValidInsideAgg(columnName, aggCall, parent, catalogReader.getConfig()); } return true; }
Example #15
Source File: SqlValidatorImpl.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
public void validateSequenceValue(SqlValidatorScope scope, SqlIdentifier id) { // Resolve identifier as a table. final SqlValidatorScope.ResolvedImpl resolved = new SqlValidatorScope.ResolvedImpl(); scope.resolveTable(id.names, catalogReader.nameMatcher(), SqlValidatorScope.Path.EMPTY, resolved); if (resolved.count() != 1) { throw newValidationError(id, RESOURCE.tableNameNotFound(id.toString())); } // We've found a table. But is it a sequence? final SqlValidatorNamespace ns = resolved.only().namespace; if (ns instanceof TableNamespace) { final Table table = ns.getTable().unwrap(Table.class); switch (table.getJdbcTableType()) { case SEQUENCE: case TEMPORARY_SEQUENCE: return; } } throw newValidationError(id, RESOURCE.notASequence(id.toString())); }
Example #16
Source File: SqlValidatorImpl.java From flink with Apache License 2.0 | 6 votes |
private boolean isRolledUpColumnAllowedInAgg(SqlIdentifier identifier, SqlValidatorScope scope, SqlCall aggCall, SqlNode parent) { Pair<String, String> pair = findTableColumnPair(identifier, scope); if (pair == null) { return true; } String columnName = pair.right; SqlValidatorTable sqlValidatorTable = scope.fullyQualify(identifier).namespace.getTable(); if (sqlValidatorTable != null) { Table table = sqlValidatorTable.unwrap(Table.class); return table.rolledUpColumnValidInsideAgg(columnName, aggCall, parent, catalogReader.getConfig()); } return true; }
Example #17
Source File: SqlValidatorImpl.java From flink with Apache License 2.0 | 6 votes |
public void validateSequenceValue(SqlValidatorScope scope, SqlIdentifier id) { // Resolve identifier as a table. final SqlValidatorScope.ResolvedImpl resolved = new SqlValidatorScope.ResolvedImpl(); scope.resolveTable(id.names, catalogReader.nameMatcher(), SqlValidatorScope.Path.EMPTY, resolved); if (resolved.count() != 1) { throw newValidationError(id, RESOURCE.tableNameNotFound(id.toString())); } // We've found a table. But is it a sequence? final SqlValidatorNamespace ns = resolved.only().namespace; if (ns instanceof TableNamespace) { final Table table = ns.getTable().unwrap(Table.class); switch (table.getJdbcTableType()) { case SEQUENCE: case TEMPORARY_SEQUENCE: return; } } throw newValidationError(id, RESOURCE.notASequence(id.toString())); }
Example #18
Source File: CachingCalciteSchema.java From calcite with Apache License 2.0 | 6 votes |
protected void addImplicitTablesBasedOnNullaryFunctionsToBuilder( ImmutableSortedMap.Builder<String, Table> builder) { ImmutableSortedMap<String, Table> explicitTables = builder.build(); final long now = System.currentTimeMillis(); final NameSet set = implicitFunctionCache.get(now); for (String s : set.iterable()) { // explicit table wins. if (explicitTables.containsKey(s)) { continue; } for (Function function : schema.getFunctions(s)) { if (function instanceof TableMacro && function.getParameters().isEmpty()) { final Table table = ((TableMacro) function).apply(ImmutableList.of()); builder.put(s, table); } } } }
Example #19
Source File: DruidTable.java From calcite with Apache License 2.0 | 6 votes |
/** Creates a {@link DruidTable} by copying the given parameters. * * @param druidSchema Druid schema * @param dataSourceName Data source name in Druid, also table name * @param intervals Intervals, or null to use default * @param fieldMap Fully populated map of fields (dimensions plus metrics) * @param metricNameSet Fully populated set of metric names * @param timestampColumnName Name of timestamp column, or null * @param complexMetrics List of complex metrics in Druid (thetaSketch, hyperUnique) * * @return A table */ static Table create(DruidSchema druidSchema, String dataSourceName, List<Interval> intervals, Map<String, SqlTypeName> fieldMap, Set<String> metricNameSet, String timestampColumnName, Map<String, List<ComplexMetric>> complexMetrics) { final ImmutableMap<String, SqlTypeName> fields = ImmutableMap.copyOf(fieldMap); return new DruidTable(druidSchema, dataSourceName, new MapRelProtoDataType(fields, timestampColumnName), ImmutableSet.copyOf(metricNameSet), timestampColumnName, intervals, complexMetrics, fieldMap); }
Example #20
Source File: ServerDdlExecutor.java From calcite with Apache License 2.0 | 6 votes |
/** Executes a {@code DROP MATERIALIZED VIEW} command. */ public void execute(SqlDropMaterializedView drop, CalcitePrepare.Context context) { final Pair<CalciteSchema, String> pair = schema(context, true, drop.name); final Table table = pair.left.plus().getTable(pair.right); if (table != null) { // Materialized view exists. execute((SqlDropObject) drop, context); if (table instanceof Wrapper) { final MaterializationKey materializationKey = ((Wrapper) table).unwrap(MaterializationKey.class); if (materializationKey != null) { MaterializationService.instance() .removeMaterialization(materializationKey); } } } }
Example #21
Source File: DatabaseCalciteSchemaTest.java From flink with Apache License 2.0 | 6 votes |
@Test public void testCatalogTable() throws TableAlreadyExistException, DatabaseNotExistException { GenericInMemoryCatalog catalog = new GenericInMemoryCatalog(catalogName, databaseName); DatabaseCalciteSchema calciteSchema = new DatabaseCalciteSchema(true, databaseName, catalogName, catalog); catalog.createTable(new ObjectPath(databaseName, tableName), new TestCatalogBaseTable(), false); Table table = calciteSchema.getTable(tableName); assertThat(table, instanceOf(TableSourceTable.class)); TableSourceTable tableSourceTable = (TableSourceTable) table; assertThat(tableSourceTable.tableSource(), instanceOf(TestExternalTableSource.class)); assertThat(tableSourceTable.isStreamingMode(), is(true)); }
Example #22
Source File: TableFunctionTest.java From calcite with Apache License 2.0 | 6 votes |
/** Test case for * <a href="https://issues.apache.org/jira/browse/CALCITE-2382">[CALCITE-2382] * Sub-query lateral joined to table function</a>. */ @Test void testInlineViewLateralTableFunction() throws SQLException { try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) { CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); SchemaPlus rootSchema = calciteConnection.getRootSchema(); SchemaPlus schema = rootSchema.add("s", new AbstractSchema()); final TableFunction table = TableFunctionImpl.create(Smalls.GENERATE_STRINGS_METHOD); schema.add("GenerateStrings", table); Table tbl = new ScannableTableTest.SimpleTable(); schema.add("t", tbl); final String sql = "select *\n" + "from (select 5 as f0 from \"s\".\"t\") \"a\",\n" + " lateral table(\"s\".\"GenerateStrings\"(f0)) as t(n, c)\n" + "where char_length(c) > 3"; ResultSet resultSet = connection.createStatement().executeQuery(sql); final String expected = "F0=5; N=4; C=abcd\n" + "F0=5; N=4; C=abcd\n" + "F0=5; N=4; C=abcd\n" + "F0=5; N=4; C=abcd\n"; assertThat(CalciteAssert.toString(resultSet), equalTo(expected)); } }
Example #23
Source File: Lattice.java From calcite with Apache License 2.0 | 5 votes |
public StarTable createStarTable() { final List<Table> tables = new ArrayList<>(); for (LatticeNode node : rootNode.descendants) { tables.add(node.table.t.unwrap(Table.class)); } return StarTable.of(this, tables); }
Example #24
Source File: Lattice.java From Bats with Apache License 2.0 | 5 votes |
public StarTable createStarTable() { final List<Table> tables = new ArrayList<>(); for (LatticeNode node : rootNode.descendants) { tables.add(node.table.t.unwrap(Table.class)); } return StarTable.of(this, tables); }
Example #25
Source File: EnumerableTableScan.java From Bats with Apache License 2.0 | 5 votes |
/** Creates an EnumerableTableScan. */ public static EnumerableTableScan create(RelOptCluster cluster, RelOptTable relOptTable) { final Table table = relOptTable.unwrap(Table.class); Class elementType = EnumerableTableScan.deduceElementType(table); final RelTraitSet traitSet = cluster.traitSetOf(EnumerableConvention.INSTANCE) .replaceIfs(RelCollationTraitDef.INSTANCE, () -> { if (table != null) { return table.getStatistic().getCollations(); } return ImmutableList.of(); }); return new EnumerableTableScan(cluster, traitSet, relOptTable, elementType); }
Example #26
Source File: ProfilerLatticeStatisticProvider.java From calcite with Apache License 2.0 | 5 votes |
/** Creates a ProfilerLatticeStatisticProvider. */ private ProfilerLatticeStatisticProvider(Lattice lattice) { Objects.requireNonNull(lattice); this.profile = Suppliers.memoize(() -> { final ProfilerImpl profiler = ProfilerImpl.builder() .withPassSize(200) .withMinimumSurprise(0.3D) .build(); final List<Profiler.Column> columns = new ArrayList<>(); for (Lattice.Column column : lattice.columns) { columns.add(new Profiler.Column(column.ordinal, column.alias)); } final String sql = lattice.sql(ImmutableBitSet.range(lattice.columns.size()), false, ImmutableList.of()); final Table table = new MaterializationService.DefaultTableFactory() .createTable(lattice.rootSchema, sql, ImmutableList.of()); final ImmutableList<ImmutableBitSet> initialGroups = ImmutableList.of(); final Enumerable<List<Comparable>> rows = ((ScannableTable) table).scan(null) .select(values -> { for (int i = 0; i < values.length; i++) { if (values[i] == null) { values[i] = NullSentinel.INSTANCE; } } //noinspection unchecked return (List<Comparable>) (List) Arrays.asList(values); }); return profiler.profile(rows, columns, initialGroups); })::get; }
Example #27
Source File: JSqlSchema.java From kafka-eagle with Apache License 2.0 | 5 votes |
@Override protected Map<String, Table> getTableMap() { Map<String, Table> tables = new HashMap<String, Table>(); Database database = JSqlMapData.MAP.get(this.dbName); if (database == null) return tables; for (JSqlMapData.Table table : database.tables) { tables.put(table.tableName, new JSqlTable(table)); } return tables; }
Example #28
Source File: JournalledJdbcSchemaTest.java From calcite-sql-rewriter with Apache License 2.0 | 5 votes |
@Test public void testGetTableConvertsMatchingTables() { JournalledJdbcSchema schema = makeSchema(); Table table = schema.getTable("MY_TABLE"); Assert.assertNotNull(table); Assert.assertTrue(table instanceof JournalledJdbcTable); JournalledJdbcTable journalTable = (JournalledJdbcTable) table; Assert.assertEquals(journalTable.getVersionField(), "myvfield"); Assert.assertEquals(journalTable.getSubsequentVersionField(), "mysvfield"); Assert.assertNotNull(journalTable.getJournalTable()); }
Example #29
Source File: StarTable.java From Bats with Apache License 2.0 | 5 votes |
public RelDataType getRowType(RelDataTypeFactory typeFactory) { final List<RelDataType> typeList = new ArrayList<>(); final List<Integer> 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()); }
Example #30
Source File: ScannableTableTest.java From calcite with Apache License 2.0 | 5 votes |
protected ConnectionPostProcessor newSchema(final String schemaName, Pair<String, Table>... tables) { return connection -> { CalciteConnection con = connection.unwrap(CalciteConnection.class); SchemaPlus rootSchema = con.getRootSchema(); SchemaPlus schema = rootSchema.add(schemaName, new AbstractSchema()); for (Pair<String, Table> t : tables) { schema.add(t.left, t.right); } connection.setSchema(schemaName); return connection; }; }