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; }
private Expression translate0(RexNode expr) { if (expr instanceof RexInputRef) { // TODO: multiple inputs, e.g. joins final Expression input = getInput(0); final int index = ((RexInputRef) expr).getIndex(); final List<RelDataTypeField> fields = program.getInputRowType().getFieldList(); final RelDataTypeField field = fields.get(index); if (fields.size() == 1) { return input; } else if (input.getType() == Object[].class) { return Expressions.convert_( Expressions.arrayIndex(input, Expressions.constant(field.getIndex())), Types.box(JavaRules.EnumUtil.javaClass(typeFactory, field.getType()))); } else { return Expressions.field(input, field.getName()); } } if (expr instanceof RexLocalRef) { return translate(program.getExprList().get(((RexLocalRef) expr).getIndex())); } if (expr instanceof RexLiteral) { return Expressions.constant( ((RexLiteral) expr).getValue(), typeFactory.getJavaClass(expr.getType())); } if (expr instanceof RexCall) { final RexCall call = (RexCall) expr; final SqlOperator operator = call.getOperator(); final ExpressionType expressionType = SQL_TO_LINQ_OPERATOR_MAP.get(operator); if (expressionType != null) { switch (operator.getSyntax()) { case Binary: return Expressions.makeBinary( expressionType, translate(call.getOperands()[0]), translate(call.getOperands()[1])); case Postfix: case Prefix: return Expressions.makeUnary(expressionType, translate(call.getOperands()[0])); default: throw new RuntimeException("unknown syntax " + operator.getSyntax()); } } Method method = SQL_OP_TO_JAVA_METHOD_MAP.get(operator); if (method != null) { List<Expression> exprs = translateList(Arrays.asList(call.operands)); return !Modifier.isStatic(method.getModifiers()) ? Expressions.call(exprs.get(0), method, exprs.subList(1, exprs.size())) : Expressions.call(method, exprs); } switch (expr.getKind()) { default: throw new RuntimeException("cannot translate expression " + expr); } } throw new RuntimeException("cannot translate expression " + expr); }
private SqlOperator toOp(String name, TableFunction fun) { List<RelDataType> argTypes = new ArrayList<RelDataType>(); List<SqlTypeFamily> typeFamilies = new ArrayList<SqlTypeFamily>(); Parameter p; for (net.hydromatic.optiq.Parameter o : (List<net.hydromatic.optiq.Parameter>) fun.getParameters()) { argTypes.add(o.getType()); typeFamilies.add(SqlTypeFamily.ANY); } return new SqlFunction( name, SqlKind.OTHER_FUNCTION, new ExplicitReturnTypeInference(typeFactory.createType(fun.getElementType())), new ExplicitOperandTypeInference(argTypes.toArray(new RelDataType[argTypes.size()])), new FamilyOperandTypeChecker( typeFamilies.toArray(new SqlTypeFamily[typeFamilies.size()])), null); }