Ejemplo n.º 1
0
 @Override
 public ParseTree visitChildInternal(RelNode child, int ordinal) {
   final Convention convention = child.getConvention();
   if (!(child instanceof JavaRel)) {
     throw Util.newInternal(
         "Relational expression '"
             + child
             + "' has '"
             + convention
             + "' calling convention, so must implement interface "
             + JavaRel.class);
   }
   JavaRel javaRel = (JavaRel) child;
   final ParseTree p = javaRel.implement(this);
   if ((convention == CallingConvention.JAVA) && (p != null)) {
     throw Util.newInternal(
         "Relational expression '"
             + child
             + "' returned '"
             + p
             + " on implement, but should have "
             + "returned null, because it has JAVA calling-convention. "
             + "(Note that similar calling-conventions, such as "
             + "Iterator, must return a value.)");
   }
   return p;
 }
 // implement BytePointer
 protected byte[] getBytesForString(String string) {
   try {
     return string.getBytes(getCharsetName());
   } catch (UnsupportedEncodingException ex) {
     throw Util.newInternal(ex);
   }
 }
Ejemplo n.º 3
0
  /** Variant of {@link #trimFields(RelNode, BitSet, Set)} for {@link TableModificationRel}. */
  public TrimResult trimFields(
      TableModificationRel modifier, BitSet fieldsUsed, Set<RelDataTypeField> extraFields) {
    // Ignore what consumer wants. We always project all columns.
    Util.discard(fieldsUsed);

    final RelDataType rowType = modifier.getRowType();
    final int fieldCount = rowType.getFieldCount();
    RelNode input = modifier.getChild();

    // We want all fields from the child.
    final int inputFieldCount = input.getRowType().getFieldCount();
    BitSet inputFieldsUsed = Util.bitSetBetween(0, inputFieldCount);

    // Create input with trimmed columns.
    final Set<RelDataTypeField> inputExtraFields = Collections.emptySet();
    TrimResult trimResult = trimChild(modifier, input, inputFieldsUsed, inputExtraFields);
    RelNode newInput = trimResult.left;
    final Mapping inputMapping = trimResult.right;
    if (!inputMapping.isIdentity()) {
      // We asked for all fields. Can't believe that the child decided
      // to permute them!
      throw Util.newInternal("Expected identity mapping, got " + inputMapping);
    }

    TableModificationRel newModifier = modifier;
    if (newInput != input) {
      newModifier = modifier.copy(modifier.getTraitSet(), Collections.singletonList(newInput));
    }
    assert newModifier.getClass() == modifier.getClass();

    // Always project all fields.
    Mapping mapping = Mappings.createIdentity(fieldCount);
    return new TrimResult(newModifier, mapping);
  }
Ejemplo n.º 4
0
 private void bindDeferred(JavaFrame frame, final RelNode rel) {
   final StatementList statementList = getStatementList();
   if (frame.bind == null) {
     // this relational expression has not bound itself, so we presume
     // that we can call its implementSelf() method
     if (!(rel instanceof JavaSelfRel)) {
       throw Util.newInternal(
           "In order to bind-deferred, a "
               + "relational expression must implement JavaSelfRel: "
               + rel);
     }
     final JavaSelfRel selfRel = (JavaSelfRel) rel;
     LazyBind lazyBind =
         new LazyBind(
             newVariable(),
             statementList,
             getTypeFactory(),
             rel.getRowType(),
             new VariableInitializerThunk() {
               public VariableInitializer getInitializer() {
                 return selfRel.implementSelf(JavaRelImplementor.this);
               }
             });
     bind(rel, lazyBind);
   } else if ((frame.bind instanceof LazyBind)
       && (((LazyBind) frame.bind).statementList != statementList)) {
     // Frame is already bound, but to a variable declared in a different
     // scope. Re-bind it.
     final LazyBind lazyBind = (LazyBind) frame.bind;
     lazyBind.statementList = statementList;
     lazyBind.bound = false;
   }
 }
 public Interpreter() {
   argumentList = new ArrayList();
   final Class clazz;
   try {
     clazz = Class.forName("sales.SalesInMemoryConnection");
   } catch (ClassNotFoundException e) {
     throw Util.newInternal(e, "Could not create interpreter's default connection");
   }
   try {
     connection = (RelOptConnection) clazz.newInstance();
   } catch (InstantiationException e) {
     throw Util.newInternal(e, "Could not create interpreter's default connection");
   } catch (IllegalAccessException e) {
     throw Util.newInternal(e, "Could not create interpreter's default connection");
   }
 }
 // TODO:  preallocate a CharsetDecoder
 public String toString() {
   if (buf == null) {
     return null;
   }
   try {
     return new String(buf, pos, count - pos, getCharsetName());
   } catch (UnsupportedEncodingException ex) {
     throw Util.newInternal(ex);
   }
 }
 /** Clones a SqlNode with a different position. */
 public SqlNode clone(SqlParserPos pos) {
   // REVIEW jvs 26-July-2006:  shouldn't pos be used here?  Or are
   // subclasses always supposed to override, in which case this
   // method should probably be abstract?
   try {
     return (SqlNode) super.clone();
   } catch (CloneNotSupportedException e) {
     throw Util.newInternal(e, "error while cloning " + this);
   }
 }
Ejemplo n.º 8
0
 private static int find(StatementList list, Statement statement) {
   if (statement == null) {
     return 0;
   } else {
     for (int i = 0, n = list.size(); i < n; i++) {
       if (list.get(i) == statement) {
         return i + 1;
       }
     }
     throw Util.newInternal("could not find statement " + statement + " in list " + list);
   }
 }
Ejemplo n.º 9
0
        /** @pre SqlTypeUtil.sameNamedType(argTypes[0], (argTypes[1])) */
        public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
          final RelDataType argType0 = opBinding.getOperandType(0);
          final RelDataType argType1 = opBinding.getOperandType(1);
          if (!(SqlTypeUtil.inCharOrBinaryFamilies(argType0)
              && SqlTypeUtil.inCharOrBinaryFamilies(argType1))) {
            Util.pre(
                SqlTypeUtil.sameNamedType(argType0, argType1),
                "SqlTypeUtil.sameNamedType(argTypes[0], argTypes[1])");
          }
          SqlCollation pickedCollation = null;
          if (SqlTypeUtil.inCharFamily(argType0)) {
            if (!SqlTypeUtil.isCharTypeComparable(opBinding.collectOperandTypes().subList(0, 2))) {
              throw opBinding.newError(
                  RESOURCE.typeNotComparable(
                      argType0.getFullTypeString(), argType1.getFullTypeString()));
            }

            pickedCollation =
                SqlCollation.getCoercibilityDyadicOperator(
                    argType0.getCollation(), argType1.getCollation());
            assert null != pickedCollation;
          }

          // Determine whether result is variable-length
          SqlTypeName typeName = argType0.getSqlTypeName();
          if (SqlTypeUtil.isBoundedVariableWidth(argType1)) {
            typeName = argType1.getSqlTypeName();
          }

          RelDataType ret;
          ret =
              opBinding
                  .getTypeFactory()
                  .createSqlType(typeName, argType0.getPrecision() + argType1.getPrecision());
          if (null != pickedCollation) {
            RelDataType pickedType;
            if (argType0.getCollation().equals(pickedCollation)) {
              pickedType = argType0;
            } else if (argType1.getCollation().equals(pickedCollation)) {
              pickedType = argType1;
            } else {
              throw Util.newInternal("should never come here");
            }
            ret =
                opBinding
                    .getTypeFactory()
                    .createTypeWithCharsetAndCollation(
                        ret, pickedType.getCharset(), pickedType.getCollation());
          }
          return ret;
        }
Ejemplo n.º 10
0
 /**
  * Replaces the operands of a call. The new operands' types must match the old operands' types.
  */
 public static RexCall replaceOperands(RexCall call, RexNode[] operands) {
   if (call.operands == operands) {
     return call;
   }
   for (int i = 0; i < operands.length; i++) {
     RelDataType oldType = call.operands[i].getType();
     RelDataType newType = operands[i].getType();
     if (!oldType.isNullable() && newType.isNullable()) {
       throw Util.newInternal("invalid nullability");
     }
     assert (oldType.toString().equals(newType.toString()));
   }
   return new RexCall(call.getType(), call.getOperator(), operands);
 }
Ejemplo n.º 11
0
  // implement SargExpr
  public SargIntervalSequence evaluate() {
    if (setOp == SargSetOperator.COMPLEMENT) {
      assert (children.size() == 1);
      SargExpr child = children.get(0);
      return child.evaluateComplemented();
    }

    List<SargIntervalSequence> list = evaluateChildren(this);

    switch (setOp) {
      case UNION:
        return evaluateUnion(list);
      case INTERSECTION:
        return evaluateIntersection(list);
      default:
        throw Util.newInternal(setOp.toString());
    }
  }
Ejemplo n.º 12
0
 public RelNode convertSqlToRel(String sql) {
   Util.pre(sql != null, "sql != null");
   final SqlNode sqlQuery;
   try {
     sqlQuery = parseQuery(sql);
   } catch (Exception e) {
     throw Util.newInternal(e); // todo: better handling
   }
   final RelDataTypeFactory typeFactory = getTypeFactory();
   final Prepare.CatalogReader catalogReader = createCatalogReader(typeFactory);
   final SqlValidator validator = createValidator(catalogReader, typeFactory);
   final SqlToRelConverter converter =
       createSqlToRelConverter(validator, catalogReader, typeFactory);
   converter.setTrimUnusedFields(true);
   final SqlNode validatedQuery = validator.validate(sqlQuery);
   final RelNode rel = converter.convertQuery(validatedQuery, false, true);
   Util.post(rel != null, "return != null");
   return rel;
 }
Ejemplo n.º 13
0
  public boolean canCastFrom(SqlTypeName to, SqlTypeName from, boolean coerce) {
    assert (null != to);
    assert (null != from);

    Map<SqlTypeName, Set<SqlTypeName>> ruleset = coerce ? coerceRules : rules;

    if (to.equals(SqlTypeName.NULL)) {
      return false;
    } else if (from.equals(SqlTypeName.NULL)) {
      return true;
    }

    Set<SqlTypeName> rule = ruleset.get(to);
    if (null == rule) {
      // if you hit this assert, see the constructor of this class on how
      // to add new rule
      throw Util.newInternal("No assign rules for " + to + " defined");
    }

    return rule.contains(from);
  }
Ejemplo n.º 14
0
  // implement SargExpr
  public SargIntervalSequence evaluateComplemented() {
    if (setOp == SargSetOperator.COMPLEMENT) {
      // Double negation is a nop
      return children.get(0).evaluate();
    }

    // Use DeMorgan's Law:  complement of union is intersection of
    // complements, and vice versa
    List<SargIntervalSequence> list = new ArrayList<SargIntervalSequence>();
    for (SargExpr child : children) {
      SargIntervalSequence newSeq = child.evaluateComplemented();
      list.add(newSeq);
    }
    switch (setOp) {
      case INTERSECTION:
        return evaluateUnion(list);
      case UNION:
        return evaluateIntersection(list);
      default:
        throw Util.newInternal(setOp.toString());
    }
  }
Ejemplo n.º 15
0
 public void unparse(
     SqlWriter writer, SqlOperator operator, SqlCall call, int leftPrec, int rightPrec) {
   throw Util.newInternal("Internal operator '" + operator + "' cannot be un-parsed");
 }