Exemple #1
0
 private Planner getPlanner(RuleSet... ruleSets) {
   return Frameworks.getPlanner(
       ConnectionConfig.Lex.ORACLE,
       new Function1<SchemaPlus, Schema>() {
         public Schema apply(SchemaPlus parentSchema) {
           return new ReflectiveSchema(parentSchema, "hr", new JdbcTest.HrSchema());
         }
       },
       SqlStdOperatorTable.instance(),
       ruleSets);
 }
Exemple #2
0
 private SqlValidator getSqlValidator() {
   if (sqlValidator == null) {
     sqlValidator =
         new SqlValidatorImpl(
             SqlStdOperatorTable.instance(),
             catalogReader,
             rexBuilder.getTypeFactory(),
             SqlConformance.Default) {};
   }
   return sqlValidator;
 }
Exemple #3
0
  public ParseResult parse(Context context, String sql) {
    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);

    SqlParser parser = new SqlParser(sql);
    SqlNode sqlNode;
    try {
      sqlNode = parser.parseQuery();
    } catch (SqlParseException e) {
      throw new RuntimeException("parse failed", e);
    }
    SqlValidator validator =
        new SqlValidatorImpl(
            SqlStdOperatorTable.instance(), catalogReader, typeFactory, SqlConformance.Default) {};
    SqlNode sqlNode1 = validator.validate(sqlNode);
    return new ParseResult(sql, sqlNode1, validator.getValidatedNodeType(sqlNode1));
  }
Exemple #4
0
 /**
  * Creates an operator table.
  *
  * @return New operator table
  */
 protected SqlOperatorTable createOperatorTable() {
   final MockSqlOperatorTable opTab = new MockSqlOperatorTable(SqlStdOperatorTable.instance());
   MockSqlOperatorTable.addRamp(opTab);
   return opTab;
 }
Exemple #5
0
  /**
   * A builder to help you build a {@link FrameworkConfig} using defaults where values aren't
   * required.
   */
  public static class ConfigBuilder {
    private SqlRexConvertletTable convertletTable = StandardConvertletTable.INSTANCE;
    private SqlOperatorTable operatorTable = SqlStdOperatorTable.instance();
    private ImmutableList<Program> programs = ImmutableList.of();
    private Context context;
    private ImmutableList<RelTraitDef> traitDefs;
    private Lex lex = Lex.ORACLE;
    private SchemaPlus defaultSchema;
    private RelOptCostFactory costFactory;
    private SqlParserImplFactory parserFactory = SqlParserImpl.FACTORY;

    private ConfigBuilder() {}

    public FrameworkConfig build() {
      return new StdFrameworkConfig(
          context,
          convertletTable,
          operatorTable,
          programs,
          traitDefs,
          lex,
          defaultSchema,
          costFactory,
          parserFactory);
    }

    public ConfigBuilder context(Context c) {
      this.context = Preconditions.checkNotNull(c);
      return this;
    }

    public ConfigBuilder convertletTable(SqlRexConvertletTable convertletTable) {
      this.convertletTable = Preconditions.checkNotNull(convertletTable);
      return this;
    }

    public ConfigBuilder operatorTable(SqlOperatorTable operatorTable) {
      this.operatorTable = Preconditions.checkNotNull(operatorTable);
      return this;
    }

    public ConfigBuilder traitDefs(List<RelTraitDef> traitDefs) {
      if (traitDefs == null) {
        this.traitDefs = null;
      } else {
        this.traitDefs = ImmutableList.copyOf(traitDefs);
      }
      return this;
    }

    public ConfigBuilder traitDefs(RelTraitDef... traitDefs) {
      this.traitDefs = ImmutableList.copyOf(traitDefs);
      return this;
    }

    public ConfigBuilder lex(Lex lex) {
      this.lex = Preconditions.checkNotNull(lex);
      return this;
    }

    public ConfigBuilder defaultSchema(SchemaPlus defaultSchema) {
      this.defaultSchema = defaultSchema;
      return this;
    }

    public ConfigBuilder costFactory(RelOptCostFactory costFactory) {
      this.costFactory = costFactory;
      return this;
    }

    public ConfigBuilder ruleSets(RuleSet... ruleSets) {
      return programs(Programs.listOf(ruleSets));
    }

    public ConfigBuilder ruleSets(List<RuleSet> ruleSets) {
      return programs(Programs.listOf(Preconditions.checkNotNull(ruleSets)));
    }

    public ConfigBuilder programs(List<Program> programs) {
      this.programs = ImmutableList.copyOf(programs);
      return this;
    }

    public ConfigBuilder programs(Program... programs) {
      this.programs = ImmutableList.copyOf(programs);
      return this;
    }

    public ConfigBuilder parserFactory(SqlParserImplFactory parserFactory) {
      this.parserFactory = Preconditions.checkNotNull(parserFactory);
      return this;
    }
  }
Exemple #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());
  }