Example #1
0
    public PreparedResult prepareQueryable(Queryable queryable, RelDataType resultType) {
      queryString = null;
      Class runtimeContextClass = Object.class;
      final Argument[] arguments = {new Argument(connectionVariable, runtimeContextClass, null)};
      ClassDeclaration decl = init(arguments);

      final RelOptQuery query = new RelOptQuery(planner);
      final RelOptCluster cluster =
          query.createCluster(env, rexBuilder.getTypeFactory(), rexBuilder);

      RelNode rootRel = new LixToRelTranslator(cluster).translate(queryable);

      if (timingTracer != null) {
        timingTracer.traceTime("end sql2rel");
      }

      final RelDataType jdbcType = makeStruct(rexBuilder.getTypeFactory(), resultType);
      fieldOrigins = Collections.nCopies(jdbcType.getFieldCount(), null);

      // Structured type flattening, view expansion, and plugging in
      // physical storage.
      rootRel = flattenTypes(rootRel, true);

      // Trim unused fields.
      rootRel = trimUnusedFields(rootRel);

      rootRel = optimize(resultType, rootRel);
      containsJava = treeContainsJava(rootRel);

      if (timingTracer != null) {
        timingTracer.traceTime("end optimization");
      }

      return implement(resultType, rootRel, SqlKind.SELECT, decl, arguments);
    }
Example #2
0
  public void onMatch(RelOptRuleCall call) {
    assert matches(call);
    final JoinRel join = (JoinRel) call.rels[0];
    final List<Integer> leftKeys = new ArrayList<Integer>();
    final List<Integer> rightKeys = new ArrayList<Integer>();
    RelNode right = join.getRight();
    final RelNode left = join.getLeft();
    RexNode remainingCondition =
        RelOptUtil.splitJoinCondition(left, right, join.getCondition(), leftKeys, rightKeys);
    assert leftKeys.size() == rightKeys.size();
    final List<CorrelatorRel.Correlation> correlationList =
        new ArrayList<CorrelatorRel.Correlation>();
    if (leftKeys.size() > 0) {
      final RelOptCluster cluster = join.getCluster();
      final RexBuilder rexBuilder = cluster.getRexBuilder();
      int k = 0;
      RexNode condition = null;
      for (Integer leftKey : leftKeys) {
        Integer rightKey = rightKeys.get(k++);
        final String dyn_inIdStr = cluster.getQuery().createCorrel();
        final int dyn_inId = RelOptQuery.getCorrelOrdinal(dyn_inIdStr);

        // Create correlation to say 'each row, set variable #id
        // to the value of column #leftKey'.
        correlationList.add(new CorrelatorRel.Correlation(dyn_inId, leftKey));
        condition =
            RelOptUtil.andJoinFilters(
                rexBuilder,
                condition,
                rexBuilder.makeCall(
                    SqlStdOperatorTable.equalsOperator,
                    rexBuilder.makeInputRef(
                        right.getRowType().getFieldList().get(rightKey).getType(), rightKey),
                    rexBuilder.makeCorrel(
                        left.getRowType().getFieldList().get(leftKey).getType(), dyn_inIdStr)));
      }
      right = CalcRel.createFilter(right, condition);
    }
    RelNode newRel =
        new CorrelatorRel(
            join.getCluster(),
            left,
            right,
            remainingCondition,
            correlationList,
            join.getJoinType());
    call.transformTo(newRel);
  }
Example #3
0
 private SqlValidator getSqlValidator() {
   if (sqlValidator == null) {
     sqlValidator =
         new SqlValidatorImpl(
             SqlStdOperatorTable.instance(),
             catalogReader,
             rexBuilder.getTypeFactory(),
             SqlConformance.Default) {};
   }
   return sqlValidator;
 }
 public RexNode getIObject() {
   if (object == null) {
     RelDataTypeFactory typeFactory = getCluster().getTypeFactory();
     RexBuilder b = getBuilder();
     final SqlFunction GET_IOBJECT =
         new SqlUserDefinedFunction(
             new SqlIdentifier("GET_IOBJECT", SqlParserPos.ZERO),
             ReturnTypes.explicit(
                 typeFactory.createTypeWithNullability(
                     typeFactory.createJavaType(IObject.class), false)),
             null,
             OperandTypes.ANY_ANY,
             ImmutableList.of(
                 typeFactory.createTypeWithNullability(
                     typeFactory.createJavaType(ISnapshot.class), false),
                 typeFactory.createJavaType(int.class)),
             ScalarFunctionImpl.create(ISnapshotMethods.class, "getIObject"));
     object = b.makeCall(GET_IOBJECT, getSnapshot(), getIObjectId());
   }
   return object;
 }
Example #5
0
 /**
  * Trims a child relational expression, then adds back a dummy project to restore the fields that
  * were removed.
  *
  * <p>Sounds pointless? It causes unused fields to be removed further down the tree (towards the
  * leaves), but it ensure that the consuming relational expression continues to see the same
  * fields.
  *
  * @param rel Relational expression
  * @param input Input relational expression, whose fields to trim
  * @param fieldsUsed Bitmap of fields needed by the consumer
  * @return New relational expression and its field mapping
  */
 protected TrimResult trimChildRestore(
     RelNode rel, RelNode input, BitSet fieldsUsed, Set<RelDataTypeField> extraFields) {
   TrimResult trimResult = trimChild(rel, input, fieldsUsed, extraFields);
   if (trimResult.right.isIdentity()) {
     return trimResult;
   }
   final RelDataType rowType = input.getRowType();
   List<RelDataTypeField> fieldList = rowType.getFieldList();
   final List<RexNode> exprList = new ArrayList<RexNode>();
   final List<String> nameList = rowType.getFieldNames();
   RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
   assert trimResult.right.getSourceCount() == fieldList.size();
   for (int i = 0; i < fieldList.size(); i++) {
     int source = trimResult.right.getTargetOpt(i);
     RelDataTypeField field = fieldList.get(i);
     exprList.add(
         source < 0
             ? rexBuilder.makeZeroLiteral(field.getType())
             : rexBuilder.makeInputRef(field.getType(), source));
   }
   RelNode project = CalcRel.createProject(trimResult.left, exprList, nameList);
   return new TrimResult(project, Mappings.createIdentity(fieldList.size()));
 }
Example #6
0
  /** Creates a cluster. */
  RelOptCluster(
      RelOptQuery query,
      RelOptPlanner planner,
      RelDataTypeFactory typeFactory,
      RexBuilder rexBuilder) {
    assert planner != null;
    assert typeFactory != null;
    this.query = query;
    this.planner = planner;
    this.typeFactory = typeFactory;
    this.rexBuilder = rexBuilder;
    this.originalExpression = rexBuilder.makeLiteral("?");

    // set up a default rel metadata provider,
    // giving the planner first crack at everything
    metadataProvider = new DefaultRelMetadataProvider();
    this.emptyTraitSet = planner.emptyTraitSet();
  }
Example #7
0
 public RexNode toRex(Expression expression) {
   switch (expression.getNodeType()) {
     case MemberAccess:
       return rexBuilder.makeFieldAccess(
           toRex(((MemberExpression) expression).expression),
           ((MemberExpression) expression).field.getName());
     case GreaterThan:
       return binary(expression, SqlStdOperatorTable.greaterThanOperator);
     case LessThan:
       return binary(expression, SqlStdOperatorTable.lessThanOperator);
     case Parameter:
       return parameter((ParameterExpression) expression);
     case Call:
       MethodCallExpression call = (MethodCallExpression) expression;
       SqlOperator operator = RexToLixTranslator.JAVA_TO_SQL_METHOD_MAP.get(call.method);
       if (operator != null) {
         return rexBuilder.makeCall(
             operator,
             toRex(
                 Expressions.<Expression>list()
                     .appendIfNotNull(call.targetExpression)
                     .appendAll(call.expressions)));
       }
       throw new RuntimeException("Could translate call to method " + call.method);
     case Constant:
       final ConstantExpression constant = (ConstantExpression) expression;
       Object value = constant.value;
       if (value instanceof Number) {
         Number number = (Number) value;
         if (value instanceof Double || value instanceof Float) {
           return rexBuilder.makeApproxLiteral(BigDecimal.valueOf(number.doubleValue()));
         } else if (value instanceof BigDecimal) {
           return rexBuilder.makeExactLiteral((BigDecimal) value);
         } else {
           return rexBuilder.makeExactLiteral(BigDecimal.valueOf(number.longValue()));
         }
       } else if (value instanceof Boolean) {
         return rexBuilder.makeLiteral((Boolean) value);
       } else {
         return rexBuilder.makeLiteral(constant.toString());
       }
     default:
       throw new UnsupportedOperationException(
           "unknown expression type " + expression.getNodeType() + " " + expression);
   }
 }
 // override RexShuttle
 public RexNode visitCall(final RexCall call) {
   int i = reducibleExps.indexOf(call);
   if (i == -1) {
     return super.visitCall(call);
   }
   RexNode replacement = reducedValues.get(i);
   if (addCasts.get(i) && (replacement.getType() != call.getType())) {
     // Handle change from nullable to NOT NULL by claiming
     // that the result is still nullable, even though
     // we know it isn't.
     //
     // Also, we cannot reduce CAST('abc' AS VARCHAR(4)) to 'abc'.
     // If we make 'abc' of type VARCHAR(4), we may later encounter
     // the same expression in a ProjectRel's digest where it has
     // type VARCHAR(3), and that's wrong.
     replacement = rexBuilder.makeCast(call.getType(), replacement);
   }
   return replacement;
 }
 /**
  * Creates a new SargFactory.
  *
  * @param rexBuilder factory for instances of {@link RexNode}, needed internally in the sarg
  *     representation, and also for recomposing sargs into equivalent rex trees
  */
 public SargFactory(RexBuilder rexBuilder) {
   this.rexBuilder = rexBuilder;
   rexNull = rexBuilder.constantNull();
 }
Example #10
0
 private RexNode binary(Expression expression, SqlBinaryOperator op) {
   BinaryExpression call = (BinaryExpression) expression;
   return rexBuilder.makeCall(op, toRex(Arrays.asList(call.expression0, call.expression1)));
 }