public RelOptTableImpl getTable(final String[] names) { List<Pair<String, Object>> pairs = new ArrayList<Pair<String, Object>>(); Schema schema2 = schema; for (int i = 0; i < names.length; i++) { final String name = names[i]; Schema subSchema = schema2.getSubSchema(name); if (subSchema != null) { pairs.add(Pair.<String, Object>of(name, subSchema)); schema2 = subSchema; continue; } final Table table = schema2.getTable(name); if (table != null) { pairs.add(Pair.<String, Object>of(name, table)); if (i != names.length - 1) { // not enough objects to match all names return null; } return new RelOptTableImpl( this, typeFactory.createType(table.getElementType()), names, table); } return null; } return null; }
/** Returns the root schema. */ public static Schema root(Schema schema) { for (Schema s = schema; ; ) { Schema previous = s; s = s.getParentSchema(); if (s == null) { return previous; } } }
/** Returns the path of a schema, appending the name of a table if not null. */ public static List<String> path(Schema schema, String name) { final List<String> list = new ArrayList<String>(); if (name != null) { list.add(name); } for (Schema s = schema; s != null; s = s.getParentSchema()) { if (s.getParentSchema() != null || !s.getName().equals("")) { // Omit the root schema's name from the path if it's the empty string, // which it usually is. list.add(s.getName()); } } return ImmutableList.copyOf(Lists.reverse(list)); }
/** Returns the expression to access a table within a schema. */ public static Expression tableExpression( Schema schema, Type elementType, String tableName, Class clazz) { final MethodCallExpression expression; if (Table.class.isAssignableFrom(clazz)) { expression = Expressions.call( schema.getExpression(), BuiltinMethod.SCHEMA_GET_TABLE.method, Expressions.constant(tableName)); } else { expression = Expressions.call( BuiltinMethod.SCHEMAS_QUERYABLE.method, DataContext.ROOT, schema.getExpression(), Expressions.constant(elementType), Expressions.constant(tableName)); } return Types.castIfNecessary(clazz, expression); }
public List<SqlOperator> lookupOperatorOverloads( SqlIdentifier opName, SqlFunctionCategory category, SqlSyntax syntax) { if (syntax != SqlSyntax.Function) { return Collections.emptyList(); } // FIXME: ignoring prefix of opName String name = opName.names[opName.names.length - 1]; List<TableFunction> tableFunctions = rootSchema.getTableFunctions(name); if (tableFunctions.isEmpty()) { return Collections.emptyList(); } return toOps(name, tableFunctions); }
/** Returns the expression for a sub-schema. */ public static Expression subSchemaExpression(Schema schema, String name, Class type) { // (Type) schemaExpression.getSubSchema("name") Expression call = Expressions.call( schema.getExpression(), BuiltinMethod.SCHEMA_GET_SUB_SCHEMA.method, Expressions.constant(name)); // CHECKSTYLE: IGNORE 2 //noinspection unchecked if (false && type != null && !type.isAssignableFrom(Schema.class)) { return unwrap(call, type); } return call; }
@Override protected PreparedExecution implement( RelDataType rowType, RelNode rootRel, SqlKind sqlKind, ClassDeclaration decl, Argument[] args) { RelDataType resultType = rootRel.getRowType(); boolean isDml = sqlKind.belongsTo(SqlKind.DML); javaCompiler = createCompiler(); EnumerableRelImplementor relImplementor = getRelImplementor(rootRel.getCluster().getRexBuilder()); BlockExpression expr = relImplementor.implementRoot((EnumerableRel) rootRel); ParameterExpression root0 = Expressions.parameter(DataContext.class, "root0"); String s = Expressions.toString( Blocks.create( Expressions.declare( Modifier.FINAL, (ParameterExpression) schema.getExpression(), root0), expr), false); final Executable executable; try { executable = (Executable) ExpressionEvaluator.createFastScriptEvaluator( s, Executable.class, new String[] {root0.name}); } catch (Exception e) { throw Helper.INSTANCE.wrap("Error while compiling generated Java code:\n" + s, e); } if (timingTracer != null) { timingTracer.traceTime("end codegen"); } if (timingTracer != null) { timingTracer.traceTime("end compilation"); } return new PreparedExecution( null, rootRel, resultType, isDml, mapTableModOp(isDml, sqlKind), null) { public Object execute() { return executable.execute(schema); } }; }