org.apache.calcite.schema.Schemas Java Examples
The following examples show how to use
org.apache.calcite.schema.Schemas.
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: 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 #2
Source File: QueryOperationConverter.java From flink with Apache License 2.0 | 6 votes |
@Override public <U> RelNode visit(TableSourceQueryOperation<U> tableSourceTable) { final Table relTable = new TableSourceTable<>( tableSourceTable.getTableSource(), !tableSourceTable.isBatch(), FlinkStatistic.UNKNOWN()); CatalogReader catalogReader = (CatalogReader) relBuilder.getRelOptSchema(); // TableSourceScan requires a unique name of a Table for computing a digest. // We are using the identity hash of the TableSource object. String refId = "unregistered_" + System.identityHashCode(tableSourceTable.getTableSource()); return new FlinkLogicalTableSourceScan( relBuilder.getCluster(), relBuilder.getCluster().traitSet().replace(FlinkConventions.LOGICAL()), RelOptTableImpl.create( catalogReader, relTable.getRowType(relBuilder.getTypeFactory()), relTable, Schemas.path(catalogReader.getRootSchema(), Collections.singletonList(refId))), tableSourceTable.getTableSource(), Option.empty() ); }
Example #3
Source File: ViewTableMacro.java From calcite with Apache License 2.0 | 6 votes |
public TranslatableTable apply(List<Object> arguments) { final CalciteConnection connection = MaterializedViewTable.MATERIALIZATION_CONNECTION; CalcitePrepare.AnalyzeViewResult parsed = Schemas.analyzeView(connection, schema, schemaPath, viewSql, viewPath, modifiable != null && modifiable); final List<String> schemaPath1 = schemaPath != null ? schemaPath : schema.path(null); if ((modifiable == null || modifiable) && parsed.modifiable && parsed.table != null) { return modifiableViewTable(parsed, viewSql, schemaPath1, viewPath, schema); } else { return viewTable(parsed, viewSql, schemaPath1, viewPath); } }
Example #4
Source File: RelOptTableImpl.java From calcite with Apache License 2.0 | 6 votes |
public <T> T unwrap(Class<T> clazz) { if (clazz.isInstance(this)) { return clazz.cast(this); } if (clazz.isInstance(table)) { return clazz.cast(table); } if (table instanceof Wrapper) { final T t = ((Wrapper) table).unwrap(clazz); if (t != null) { return t; } } if (clazz == CalciteSchema.class) { return clazz.cast( Schemas.subSchema(((CalciteCatalogReader) schema).rootSchema, Util.skipLast(getQualifiedName()))); } return null; }
Example #5
Source File: JournalledJdbcSchema.java From calcite-sql-rewriter with Apache License 2.0 | 6 votes |
public static JournalledJdbcSchema create( SchemaPlus parentSchema, String name, Map<String, Object> operand ) { DataSource dataSource; try { dataSource = parseDataSource(operand); } catch (Exception e) { throw new IllegalArgumentException("Error while reading dataSource", e); } String catalog = (String) operand.get("jdbcCatalog"); String schema = (String) operand.get("jdbcSchema"); Expression expression = null; if (parentSchema != null) { expression = Schemas.subSchemaExpression(parentSchema, name, JdbcSchema.class); } final SqlDialect dialect = createDialect(dataSource); final JdbcConvention convention = JdbcConvention.of(dialect, expression, name); return new JournalledJdbcSchema(dataSource, dialect, convention, catalog, schema, operand); }
Example #6
Source File: JdbcCatalogSchema.java From calcite with Apache License 2.0 | 6 votes |
public static JdbcCatalogSchema create( SchemaPlus parentSchema, String name, DataSource dataSource, SqlDialectFactory dialectFactory, String catalog) { final Expression expression = parentSchema != null ? Schemas.subSchemaExpression(parentSchema, name, JdbcCatalogSchema.class) : Expressions.call(DataContext.ROOT, BuiltInMethod.DATA_CONTEXT_GET_ROOT_SCHEMA.method); final SqlDialect dialect = JdbcSchema.createDialect(dialectFactory, dataSource); final JdbcConvention convention = JdbcConvention.of(dialect, expression, name); return new JdbcCatalogSchema(dataSource, dialect, convention, catalog); }
Example #7
Source File: LinqFrontJdbcBackTest.java From calcite with Apache License 2.0 | 6 votes |
@Test void testTableWhere() throws SQLException, ClassNotFoundException { final Connection connection = CalciteAssert.that(CalciteAssert.Config.JDBC_FOODMART).connect(); final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); final SchemaPlus rootSchema = calciteConnection.getRootSchema(); ParameterExpression c = Expressions.parameter(JdbcTest.Customer.class, "c"); String s = Schemas.queryable(Schemas.createDataContext(connection, rootSchema), rootSchema.getSubSchema("foodmart"), JdbcTest.Customer.class, "customer") .where( Expressions.lambda( Expressions.lessThan( Expressions.field(c, "customer_id"), Expressions.constant(5)), c)) .toList() .toString(); Util.discard(s); }
Example #8
Source File: SqlWorker.java From quark with Apache License 2.0 | 6 votes |
private void populateMaterializationsAndLattice( QuarkMaterializeCluster.RelOptPlannerHolder plannerHolder, CalciteSchema rootSchema) { if (materializations == null) { materializations = MaterializationService.instance().query(rootSchema); } Materializer materializer = new Materializer(materializations); materializer.populateMaterializations(context.getPrepareContext(), plannerHolder); List<CalciteSchema.LatticeEntry> lattices = Schemas.getLatticeEntries(rootSchema); for (CalciteSchema.LatticeEntry lattice : lattices) { final CalciteSchema.TableEntry starTable = lattice.getStarTable(); final JavaTypeFactory typeFactory = context.getTypeFactory(); final RelOptTableImpl starRelOptTable = RelOptTableImpl.create(catalogReader, starTable.getTable().getRowType(typeFactory), starTable, null); plannerHolder.getPlanner().addLattice( new RelOptLattice(lattice.getLattice(), starRelOptTable)); } }
Example #9
Source File: QueryOperationConverter.java From flink with Apache License 2.0 | 6 votes |
@Override public <U> RelNode visit(TableSourceQueryOperation<U> tableSourceTable) { final Table relTable = new TableSourceTable<>( tableSourceTable.getTableSchema(), tableSourceTable.getTableSource(), !tableSourceTable.isBatch(), FlinkStatistic.UNKNOWN()); CatalogReader catalogReader = (CatalogReader) relBuilder.getRelOptSchema(); // TableSourceScan requires a unique name of a Table for computing a digest. // We are using the identity hash of the TableSource object. String refId = "unregistered_" + System.identityHashCode(tableSourceTable.getTableSource()); return new FlinkLogicalTableSourceScan( relBuilder.getCluster(), relBuilder.getCluster().traitSet().replace(FlinkConventions.LOGICAL()), RelOptTableImpl.create( catalogReader, relTable.getRowType(relBuilder.getTypeFactory()), relTable, Schemas.path(catalogReader.getRootSchema(), Collections.singletonList(refId))), tableSourceTable.getTableSchema(), tableSourceTable.getTableSource(), Option.empty() ); }
Example #10
Source File: MockCatalogReader.java From calcite with Apache License 2.0 | 5 votes |
@Override public Path getTablePath() { final ImmutableList.Builder<Pair<String, Schema>> builder = ImmutableList.builder(); for (String name : fromTable.names) { builder.add(Pair.of(name, null)); } return Schemas.path(builder.build()); }
Example #11
Source File: CalciteConnectionImpl.java From calcite with Apache License 2.0 | 5 votes |
/** Called after the constructor has completed and the model has been * loaded. */ void init() { final MaterializationService service = MaterializationService.instance(); for (CalciteSchema.LatticeEntry e : Schemas.getLatticeEntries(rootSchema)) { final Lattice lattice = e.getLattice(); for (Lattice.Tile tile : lattice.computeTiles()) { service.defineTile(lattice, tile.bitSet(), tile.measures, e.schema, true, true); } } }
Example #12
Source File: ExtensionDdlExecutor.java From calcite with Apache License 2.0 | 5 votes |
/** Populates the table called {@code name} by executing {@code query}. */ protected static void populate(SqlIdentifier name, SqlNode query, CalcitePrepare.Context context) { // Generate, prepare and execute an "INSERT INTO table query" statement. // (It's a bit inefficient that we convert from SqlNode to SQL and back // again.) final FrameworkConfig config = Frameworks.newConfigBuilder() .defaultSchema( Objects.requireNonNull( Schemas.subSchema(context.getRootSchema(), context.getDefaultSchemaPath())).plus()) .build(); final Planner planner = Frameworks.getPlanner(config); try { final StringBuilder buf = new StringBuilder(); final SqlPrettyWriter w = new SqlPrettyWriter( SqlPrettyWriter.config() .withDialect(CalciteSqlDialect.DEFAULT) .withAlwaysUseParentheses(false), buf); buf.append("INSERT INTO "); name.unparse(w, 0, 0); buf.append(" "); query.unparse(w, 0, 0); final String sql = buf.toString(); final SqlNode query1 = planner.parse(sql); final SqlNode query2 = planner.validate(query1); final RelRoot r = planner.rel(query2); final PreparedStatement prepare = context.getRelRunner().prepare(r.rel); int rowCount = prepare.executeUpdate(); Util.discard(rowCount); prepare.close(); } catch (SqlParseException | ValidationException | RelConversionException | SQLException e) { throw new RuntimeException(e); } }
Example #13
Source File: ReflectiveSchema.java From calcite with Apache License 2.0 | 5 votes |
/** Returns an expression for the object wrapped by this schema (not the * schema itself). */ Expression getTargetExpression(SchemaPlus parentSchema, String name) { return EnumUtils.convert( Expressions.call( Schemas.unwrap( getExpression(parentSchema, name), ReflectiveSchema.class), BuiltInMethod.REFLECTIVE_SCHEMA_GET_TARGET.method), target.getClass()); }
Example #14
Source File: ViewTableMacro.java From Bats with Apache License 2.0 | 5 votes |
/** Allows a sub-class to return an extension of {@link ModifiableViewTable} * by overriding this method. */ protected ModifiableViewTable modifiableViewTable(CalciteResult.AnalyzeViewResult parsed, String viewSql, List<String> schemaPath, List<String> viewPath, CalciteSchema schema) { final JavaTypeFactory typeFactory = (JavaTypeFactory) parsed.typeFactory; final Type elementType = typeFactory.getJavaClass(parsed.rowType); return new ModifiableViewTable(elementType, RelDataTypeImpl.proto(parsed.rowType), viewSql, schemaPath, viewPath, parsed.table, Schemas.path(schema.root(), parsed.tablePath), parsed.constraint, parsed.columnMapping); }
Example #15
Source File: RexExecutorTest.java From calcite with Apache License 2.0 | 5 votes |
protected void check(final Action action) throws Exception { Frameworks.withPrepare((cluster, relOptSchema, rootSchema, statement) -> { final RexBuilder rexBuilder = cluster.getRexBuilder(); DataContext dataContext = Schemas.createDataContext(statement.getConnection(), rootSchema); final RexExecutorImpl executor = new RexExecutorImpl(dataContext); action.check(rexBuilder, executor); return null; }); }
Example #16
Source File: JdbcSchema.java From calcite with Apache License 2.0 | 5 votes |
public static JdbcSchema create( SchemaPlus parentSchema, String name, DataSource dataSource, SqlDialectFactory dialectFactory, String catalog, String schema) { final Expression expression = Schemas.subSchemaExpression(parentSchema, name, JdbcSchema.class); final SqlDialect dialect = createDialect(dialectFactory, dataSource); final JdbcConvention convention = JdbcConvention.of(dialect, expression, name); return new JdbcSchema(dataSource, dialect, convention, catalog, schema); }
Example #17
Source File: MycatReflectiveSchema.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
/** Returns an expression for the object wrapped by this schema (not the * schema itself). */ Expression getTargetExpression(SchemaPlus parentSchema, String name) { return EnumUtils.convert( Expressions.call( Schemas.unwrap( getExpression(parentSchema, name), MycatReflectiveSchema.class), BuiltInMethod.REFLECTIVE_SCHEMA_GET_TARGET.method), target.getClass()); }
Example #18
Source File: CloneSchema.java From calcite with Apache License 2.0 | 5 votes |
private Table createCloneTable(QueryProvider queryProvider, QueryableTable sourceTable, String name) { final Queryable<Object> queryable = sourceTable.asQueryable(queryProvider, sourceSchema, name); final JavaTypeFactory typeFactory = ((CalciteConnection) queryProvider).getTypeFactory(); return createCloneTable(typeFactory, Schemas.proto(sourceTable), ImmutableList.of(), null, queryable); }
Example #19
Source File: HiveTableEnv.java From marble with Apache License 2.0 | 5 votes |
public static TableEnv getTableEnv() { TableConfig tableConfig = new TableConfig(); tableConfig.setSqlOperatorTable( ChainedSqlOperatorTable.of(HiveSqlOperatorTable.instance(), SqlStdOperatorTable.instance())); tableConfig.setSqlParserConfig(SqlParser .configBuilder() .setLex(Lex.JAVA).setCaseSensitive(false).setConformance( SqlConformanceEnum.HIVE) .setParserFactory(HiveSqlParserImpl.FACTORY) .build()); // tableConfig.setRelDataTypeSystem(new HiveTypeSystemImpl()); Properties prop = new Properties(); prop.setProperty(CalciteConnectionProperty.CASE_SENSITIVE.camelName(), String.valueOf(tableConfig.getSqlParserConfig().caseSensitive())); tableConfig.setCalciteConnectionConfig( new CalciteConnectionConfigImpl(prop)); tableConfig.setConvertletTable(new HiveConvertletTable()); RexExecutor rexExecutor = new HiveRexExecutorImpl( Schemas.createDataContext(null, null)); tableConfig.setRexExecutor(rexExecutor); TableEnv tableEnv = new HiveTableEnv(tableConfig); //add table functions tableEnv.addFunction("", "explode", "org.apache.calcite.adapter.hive.udtf.UDTFExplode", "eval"); return tableEnv; }
Example #20
Source File: MockCatalogReader.java From calcite with Apache License 2.0 | 5 votes |
@Override protected ModifiableViewTable modifiableViewTable( CalcitePrepare.AnalyzeViewResult parsed, String viewSql, List<String> schemaPath, List<String> viewPath, CalciteSchema schema) { final JavaTypeFactory typeFactory = (JavaTypeFactory) parsed.typeFactory; final Type elementType = typeFactory.getJavaClass(parsed.rowType); return new MockModifiableViewTable(elementType, RelDataTypeImpl.proto(parsed.rowType), viewSql, schemaPath, viewPath, parsed.table, Schemas.path(schema.root(), parsed.tablePath), parsed.constraint, parsed.columnMapping); }
Example #21
Source File: ViewTableMacro.java From calcite with Apache License 2.0 | 5 votes |
/** Allows a sub-class to return an extension of {@link ModifiableViewTable} * by overriding this method. */ protected ModifiableViewTable modifiableViewTable(CalcitePrepare.AnalyzeViewResult parsed, String viewSql, List<String> schemaPath, List<String> viewPath, CalciteSchema schema) { final JavaTypeFactory typeFactory = (JavaTypeFactory) parsed.typeFactory; final Type elementType = typeFactory.getJavaClass(parsed.rowType); return new ModifiableViewTable(elementType, RelDataTypeImpl.proto(parsed.rowType), viewSql, schemaPath, viewPath, parsed.table, Schemas.path(schema.root(), parsed.tablePath), parsed.constraint, parsed.columnMapping); }
Example #22
Source File: FrameworksTest.java From calcite with Apache License 2.0 | 5 votes |
/** Test for {@link Path}. */ @Test void testSchemaPath() { final SchemaPlus rootSchema = Frameworks.createRootSchema(true); final FrameworkConfig config = Frameworks.newConfigBuilder() .defaultSchema( CalciteAssert.addSchema(rootSchema, CalciteAssert.SchemaSpec.HR)) .build(); final Path path = Schemas.path(config.getDefaultSchema()); assertThat(path.size(), is(2)); assertThat(path.get(0).left, is("")); assertThat(path.get(1).left, is("hr")); assertThat(path.names().size(), is(1)); assertThat(path.names().get(0), is("hr")); assertThat(path.schemas().size(), is(2)); final Path parent = path.parent(); assertThat(parent.size(), is(1)); assertThat(parent.names().size(), is(0)); final Path grandparent = parent.parent(); assertThat(grandparent.size(), is(0)); try { Object o = grandparent.parent(); fail("expected exception, got " + o); } catch (IllegalArgumentException e) { // ok } }
Example #23
Source File: RexImplicationCheckerTest.java From calcite with Apache License 2.0 | 4 votes |
public Fixture() { typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT); rexBuilder = new RexBuilder(typeFactory); boolRelDataType = typeFactory.createJavaType(Boolean.class); intRelDataType = typeFactory.createJavaType(Integer.class); decRelDataType = typeFactory.createJavaType(Double.class); longRelDataType = typeFactory.createJavaType(Long.class); shortDataType = typeFactory.createJavaType(Short.class); byteDataType = typeFactory.createJavaType(Byte.class); floatDataType = typeFactory.createJavaType(Float.class); charDataType = typeFactory.createJavaType(Character.class); dateDataType = typeFactory.createJavaType(Date.class); timestampDataType = typeFactory.createJavaType(Timestamp.class); timeDataType = typeFactory.createJavaType(Time.class); stringDataType = typeFactory.createJavaType(String.class); bl = ref(0, this.boolRelDataType); i = ref(1, intRelDataType); dec = ref(2, decRelDataType); lg = ref(3, longRelDataType); sh = ref(4, shortDataType); by = ref(5, byteDataType); fl = ref(6, floatDataType); ch = ref(7, charDataType); d = ref(8, dateDataType); ts = ref(9, timestampDataType); t = ref(10, timeDataType); str = ref(11, stringDataType); rowType = typeFactory.builder() .add("bool", this.boolRelDataType) .add("int", intRelDataType) .add("dec", decRelDataType) .add("long", longRelDataType) .add("short", shortDataType) .add("byte", byteDataType) .add("float", floatDataType) .add("char", charDataType) .add("date", dateDataType) .add("timestamp", timestampDataType) .add("time", timeDataType) .add("string", stringDataType) .build(); executor = Frameworks.withPrepare( (cluster, relOptSchema, rootSchema, statement) -> new RexExecutorImpl( Schemas.createDataContext(statement.getConnection(), rootSchema))); simplify = new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, executor) .withParanoid(true); checker = new RexImplicationChecker(rexBuilder, executor, rowType); }
Example #24
Source File: MockRelOptPlanner.java From calcite with Apache License 2.0 | 4 votes |
/** Creates MockRelOptPlanner. */ public MockRelOptPlanner(Context context) { super(RelOptCostImpl.FACTORY, context); setExecutor(new RexExecutorImpl(Schemas.createDataContext(null, null))); }
Example #25
Source File: ExtensionDdlExecutor.java From calcite with Apache License 2.0 | 4 votes |
/** Executes a {@code CREATE TABLE} command. Called via reflection. */ public void execute(SqlCreateTable create, CalcitePrepare.Context context) { final CalciteSchema schema = Schemas.subSchema(context.getRootSchema(), context.getDefaultSchemaPath()); final JavaTypeFactory typeFactory = context.getTypeFactory(); final RelDataType queryRowType; if (create.query != null) { // A bit of a hack: pretend it's a view, to get its row type final String sql = create.query.toSqlString(CalciteSqlDialect.DEFAULT).getSql(); final ViewTableMacro viewTableMacro = ViewTable.viewMacro(schema.plus(), sql, schema.path(null), context.getObjectPath(), false); final TranslatableTable x = viewTableMacro.apply(ImmutableList.of()); queryRowType = x.getRowType(typeFactory); if (create.columnList != null && queryRowType.getFieldCount() != create.columnList.size()) { throw SqlUtil.newContextException(create.columnList.getParserPosition(), RESOURCE.columnCountMismatch()); } } else { queryRowType = null; } final RelDataTypeFactory.Builder builder = typeFactory.builder(); if (create.columnList != null) { final SqlValidator validator = new ContextSqlValidator(context, false); create.forEachNameType((name, typeSpec) -> builder.add(name.getSimple(), typeSpec.deriveType(validator, true))); } else { if (queryRowType == null) { // "CREATE TABLE t" is invalid; because there is no "AS query" we need // a list of column names and types, "CREATE TABLE t (INT c)". throw SqlUtil.newContextException(create.name.getParserPosition(), RESOURCE.createTableRequiresColumnList()); } builder.addAll(queryRowType.getFieldList()); } final RelDataType rowType = builder.build(); schema.add(create.name.getSimple(), new MutableArrayTable(create.name.getSimple(), RelDataTypeImpl.proto(rowType))); if (create.query != null) { populate(create.name, create.query, context); } }
Example #26
Source File: ExtensionDdlExecutor.java From calcite with Apache License 2.0 | 4 votes |
public Expression getExpression(SchemaPlus schema, String tableName, Class clazz) { return Schemas.tableExpression(schema, getElementType(), tableName, clazz); }
Example #27
Source File: AbstractQueryableTable.java From calcite with Apache License 2.0 | 4 votes |
public Expression getExpression(SchemaPlus schema, String tableName, Class clazz) { return Schemas.tableExpression(schema, elementType, tableName, clazz); }
Example #28
Source File: FlowFileTable.java From nifi with Apache License 2.0 | 4 votes |
@Override @SuppressWarnings("rawtypes") public Expression getExpression(final SchemaPlus schema, final String tableName, final Class clazz) { return Schemas.tableExpression(schema, getElementType(), tableName, clazz); }
Example #29
Source File: ProvenanceTable.java From nifi with Apache License 2.0 | 4 votes |
@Override public Expression getExpression(final SchemaPlus schema, final String tableName, final Class clazz) { return Schemas.tableExpression(schema, getElementType(), tableName, clazz); }
Example #30
Source File: ConnectionStatusPredictionsTable.java From nifi with Apache License 2.0 | 4 votes |
@Override @SuppressWarnings("rawtypes") public Expression getExpression(final SchemaPlus schema, final String tableName, final Class clazz) { return Schemas.tableExpression(schema, getElementType(), tableName, clazz); }