@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; }
public Exp validate(Exp exp, boolean scalar) { Exp resolved; try { resolved = (Exp) resolvedNodes.get(exp); } catch (ClassCastException e) { // A classcast exception will occur if there is a String // placeholder in the map. This is an internal error -- should // not occur for any query, valid or invalid. throw Util.newInternal( e, "Infinite recursion encountered while validating '" + Util.unparse(exp) + "'"); } if (resolved == null) { try { stack.push((QueryPart) exp); // To prevent recursion, put in a placeholder while we're // resolving. resolvedNodes.put((QueryPart) exp, placeHolder); resolved = exp.accept(this); Util.assertTrue(resolved != null); resolvedNodes.put((QueryPart) exp, (QueryPart) resolved); } finally { stack.pop(); } } if (scalar) { final Type type = resolved.getType(); if (!TypeUtil.canEvaluate(type)) { String exprString = Util.unparse(resolved); throw MondrianResource.instance().MdxMemberExpIsSet.ex(exprString); } } return resolved; }
/** 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); }
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; } }
/** 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); } }
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); } }
/** * 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); }
// 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()); } }
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; }
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); }
// 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()); } }