Beispiel #1
0
 /** Returns the path of an object in this schema. */
 public List<String> path(String name) {
   final List<String> list = new ArrayList<String>();
   if (name != null) {
     list.add(name);
   }
   for (OptiqSchema s = this; s != null; s = s.parent) {
     if (s.parent != null || !s.name.equals("")) {
       // Omit the root schema's name from the path if it's the empty string,
       // which it usually is.
       list.add(s.name);
     }
   }
   return ImmutableList.copyOf(Lists.reverse(list));
 }
Beispiel #2
0
 public List<RexNode> toRexList(BlockExpression expression) {
   final List<Expression> simpleList = simpleList(expression);
   final List<RexNode> list = new ArrayList<RexNode>();
   for (Expression expression1 : simpleList) {
     list.add(toRex(expression1));
   }
   return list;
 }
Beispiel #3
0
 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);
 }
 private SqlOperator toOp(SqlIdentifier name, Function function) {
   List<RelDataType> argTypes = new ArrayList<RelDataType>();
   List<SqlTypeFamily> typeFamilies = new ArrayList<SqlTypeFamily>();
   for (FunctionParameter o : function.getParameters()) {
     final RelDataType type = o.getType(typeFactory);
     argTypes.add(type);
     typeFamilies.add(Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
   }
   final RelDataType returnType;
   if (function instanceof ScalarFunction) {
     return new SqlUserDefinedFunction(
         name,
         ReturnTypes.explicit(Schemas.proto((ScalarFunction) function)),
         InferTypes.explicit(argTypes),
         OperandTypes.family(typeFamilies),
         toSql(argTypes),
         function);
   } else if (function instanceof AggregateFunction) {
     returnType = ((AggregateFunction) function).getReturnType(typeFactory);
     return new SqlUserDefinedAggFunction(
         name,
         ReturnTypes.explicit(returnType),
         InferTypes.explicit(argTypes),
         OperandTypes.family(typeFamilies),
         (AggregateFunction) function);
   } else if (function instanceof TableMacro) {
     return new SqlUserDefinedTableMacro(
         name,
         ReturnTypes.CURSOR,
         InferTypes.explicit(argTypes),
         OperandTypes.family(typeFamilies),
         (TableMacro) function);
   } else if (function instanceof TableFunction) {
     return new SqlUserDefinedTableFunction(
         name,
         ReturnTypes.CURSOR,
         InferTypes.explicit(argTypes),
         OperandTypes.family(typeFamilies),
         toSql(argTypes),
         (TableFunction) function);
   } else {
     throw new AssertionError("unknown function type " + function);
   }
 }
  public List<SqlMoniker> getAllSchemaObjectNames(List<String> names) {
    final OptiqSchema schema = getSchema(names);
    if (schema == null) {
      return ImmutableList.of();
    }
    final List<SqlMoniker> result = new ArrayList<SqlMoniker>();
    final Map<String, OptiqSchema> schemaMap = schema.getSubSchemaMap();

    for (String subSchema : schemaMap.keySet()) {
      result.add(new SqlMonikerImpl(schema.path(subSchema), SqlMonikerType.SCHEMA));
    }

    for (String table : schema.getTableNames()) {
      result.add(new SqlMonikerImpl(schema.path(table), SqlMonikerType.TABLE));
    }

    final NavigableSet<String> functions = schema.getFunctionNames();
    for (String function : functions) { // views are here as well
      result.add(new SqlMonikerImpl(schema.path(function), SqlMonikerType.FUNCTION));
    }
    return result;
  }
Beispiel #6
0
  <T> PrepareResult<T> prepare_(
      Context context, String sql, Queryable<T> queryable, Type elementType) {
    final JavaTypeFactory typeFactory = context.getTypeFactory();
    OptiqCatalogReader catalogReader = new OptiqCatalogReader(context.getRootSchema(), typeFactory);
    final OptiqPreparingStmt preparingStmt =
        new OptiqPreparingStmt(catalogReader, typeFactory, context.getRootSchema());
    preparingStmt.setResultCallingConvention(CallingConvention.ENUMERABLE);

    final RelDataType x;
    final PreparedResult preparedResult;
    if (sql != null) {
      assert queryable == null;
      SqlParser parser = new SqlParser(sql);
      SqlNode sqlNode;
      try {
        sqlNode = parser.parseQuery();
      } catch (SqlParseException e) {
        throw new RuntimeException("parse failed", e);
      }
      final Schema rootSchema = context.getRootSchema();
      SqlValidator validator =
          new SqlValidatorImpl(
              new ChainedSqlOperatorTable(
                  Arrays.<SqlOperatorTable>asList(
                      SqlStdOperatorTable.instance(),
                      new MySqlOperatorTable(rootSchema, typeFactory))),
              catalogReader,
              typeFactory,
              SqlConformance.Default) {};
      preparedResult = preparingStmt.prepareSql(sqlNode, Object.class, validator, true);
      x = validator.getValidatedNodeType(sqlNode);
    } else {
      assert queryable != null;
      x = context.getTypeFactory().createType(elementType);
      preparedResult = preparingStmt.prepareQueryable(queryable, x);
    }

    // TODO: parameters
    final List<Parameter> parameters = Collections.emptyList();
    // TODO: column meta data
    final List<ColumnMetaData> columns = new ArrayList<ColumnMetaData>();
    RelDataType jdbcType = makeStruct(typeFactory, x);
    for (RelDataTypeField field : jdbcType.getFields()) {
      RelDataType type = field.getType();
      SqlTypeName sqlTypeName = type.getSqlTypeName();
      columns.add(
          new ColumnMetaData(
              columns.size(),
              false,
              true,
              false,
              false,
              type.isNullable() ? 1 : 0,
              true,
              0,
              field.getName(),
              field.getName(),
              null,
              sqlTypeName.allowsPrec() && false ? type.getPrecision() : -1,
              sqlTypeName.allowsScale() ? type.getScale() : -1,
              null,
              null,
              sqlTypeName.getJdbcOrdinal(),
              sqlTypeName.getName(),
              true,
              false,
              false,
              null));
    }
    return new PrepareResult<T>(sql, parameters, columns, (Enumerable<T>) preparedResult.execute());
  }
 public void addStatement(OptiqServerStatement statement) {
   statementList.add(statement);
 }
 public void removeStatement(OptiqServerStatement optiqServerStatement) {
   statementList.add(optiqServerStatement);
 }