Search in sources :

Example 1 with AbstractMemberCalc

use of mondrian.calc.impl.AbstractMemberCalc in project mondrian by pentaho.

the class AncestorFunDef method compileCall.

public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
    final MemberCalc memberCalc = compiler.compileMember(call.getArg(0));
    final Type type1 = call.getArg(1).getType();
    if (type1 instanceof LevelType) {
        final LevelCalc levelCalc = compiler.compileLevel(call.getArg(1));
        return new AbstractMemberCalc(call, new Calc[] { memberCalc, levelCalc }) {

            public Member evaluateMember(Evaluator evaluator) {
                Level level = levelCalc.evaluateLevel(evaluator);
                Member member = memberCalc.evaluateMember(evaluator);
                int distance = member.getLevel().getDepth() - level.getDepth();
                return ancestor(evaluator, member, distance, level);
            }
        };
    } else {
        final IntegerCalc distanceCalc = compiler.compileInteger(call.getArg(1));
        return new AbstractMemberCalc(call, new Calc[] { memberCalc, distanceCalc }) {

            public Member evaluateMember(Evaluator evaluator) {
                int distance = distanceCalc.evaluateInteger(evaluator);
                Member member = memberCalc.evaluateMember(evaluator);
                return ancestor(evaluator, member, distance, null);
            }
        };
    }
}
Also used : Type(mondrian.olap.type.Type) LevelType(mondrian.olap.type.LevelType) LevelType(mondrian.olap.type.LevelType) AbstractMemberCalc(mondrian.calc.impl.AbstractMemberCalc) AbstractMemberCalc(mondrian.calc.impl.AbstractMemberCalc)

Example 2 with AbstractMemberCalc

use of mondrian.calc.impl.AbstractMemberCalc in project mondrian by pentaho.

the class LeadLagFunDef method compileCall.

public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
    final MemberCalc memberCalc = compiler.compileMember(call.getArg(0));
    final IntegerCalc integerCalc = compiler.compileInteger(call.getArg(1));
    final boolean lag = call.getFunName().equals("Lag");
    return new AbstractMemberCalc(call, new Calc[] { memberCalc, integerCalc }) {

        public Member evaluateMember(Evaluator evaluator) {
            Member member = memberCalc.evaluateMember(evaluator);
            int n = integerCalc.evaluateInteger(evaluator);
            if (lag) {
                if (n == Integer.MIN_VALUE) {
                    // Bump up lagValue by one, otherwise -n (used
                    // in the getLeadMember call below) is out of
                    // range because Integer.MAX_VALUE ==
                    // -(Integer.MIN_VALUE + 1).
                    n += 1;
                }
                n = -n;
            }
            return evaluator.getSchemaReader().getLeadMember(member, n);
        }
    };
}
Also used : AbstractMemberCalc(mondrian.calc.impl.AbstractMemberCalc) AbstractMemberCalc(mondrian.calc.impl.AbstractMemberCalc)

Example 3 with AbstractMemberCalc

use of mondrian.calc.impl.AbstractMemberCalc in project mondrian by pentaho.

the class OpeningClosingPeriodFunDef method compileCall.

public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
    final Exp[] args = call.getArgs();
    final LevelCalc levelCalc;
    final MemberCalc memberCalc;
    RolapHierarchy defaultTimeHierarchy = null;
    switch(args.length) {
        case 0:
            defaultTimeHierarchy = ((RolapCube) compiler.getEvaluator().getCube()).getTimeHierarchy(getName());
            memberCalc = new HierarchyCurrentMemberFunDef.FixedCalcImpl(new DummyExp(MemberType.forHierarchy(defaultTimeHierarchy)), defaultTimeHierarchy);
            levelCalc = null;
            break;
        case 1:
            defaultTimeHierarchy = ((RolapCube) compiler.getEvaluator().getCube()).getTimeHierarchy(getName());
            levelCalc = compiler.compileLevel(call.getArg(0));
            memberCalc = new HierarchyCurrentMemberFunDef.FixedCalcImpl(new DummyExp(MemberType.forHierarchy(defaultTimeHierarchy)), defaultTimeHierarchy);
            break;
        default:
            levelCalc = compiler.compileLevel(call.getArg(0));
            memberCalc = compiler.compileMember(call.getArg(1));
            break;
    }
    // Make sure the member and the level come from the same dimension.
    if (levelCalc != null) {
        final Dimension memberDimension = memberCalc.getType().getDimension();
        final Dimension levelDimension = levelCalc.getType().getDimension();
        if (!memberDimension.equals(levelDimension)) {
            throw MondrianResource.instance().FunctionMbrAndLevelHierarchyMismatch.ex(opening ? "OpeningPeriod" : "ClosingPeriod", levelDimension.getUniqueName(), memberDimension.getUniqueName());
        }
    }
    return new AbstractMemberCalc(call, new Calc[] { levelCalc, memberCalc }) {

        public Member evaluateMember(Evaluator evaluator) {
            Member member = memberCalc.evaluateMember(evaluator);
            // If the level argument is present, use it. Otherwise use the
            // level immediately after that of the member argument.
            Level level;
            if (levelCalc == null) {
                int targetDepth = member.getLevel().getDepth() + 1;
                Level[] levels = member.getHierarchy().getLevels();
                if (levels.length <= targetDepth) {
                    return member.getHierarchy().getNullMember();
                }
                level = levels[targetDepth];
            } else {
                level = levelCalc.evaluateLevel(evaluator);
            }
            // Shortcut if the level is above the member.
            if (level.getDepth() < member.getLevel().getDepth()) {
                return member.getHierarchy().getNullMember();
            }
            // Shortcut if the level is the same as the member
            if (level == member.getLevel()) {
                return member;
            }
            return getDescendant(evaluator.getSchemaReader(), member, level, opening);
        }
    };
}
Also used : AbstractMemberCalc(mondrian.calc.impl.AbstractMemberCalc) RolapHierarchy(mondrian.rolap.RolapHierarchy) AbstractMemberCalc(mondrian.calc.impl.AbstractMemberCalc)

Example 4 with AbstractMemberCalc

use of mondrian.calc.impl.AbstractMemberCalc in project mondrian by pentaho.

the class TupleItemFunDef method compileCall.

public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
    final Type type = call.getArg(0).getType();
    if (type instanceof MemberType) {
        final MemberCalc memberCalc = compiler.compileMember(call.getArg(0));
        final IntegerCalc indexCalc = compiler.compileInteger(call.getArg(1));
        return new AbstractMemberCalc(call, new Calc[] { memberCalc, indexCalc }) {

            public Member evaluateMember(Evaluator evaluator) {
                final Member member = memberCalc.evaluateMember(evaluator);
                final int index = indexCalc.evaluateInteger(evaluator);
                if (index != 0) {
                    return null;
                }
                return member;
            }
        };
    } else {
        final TupleCalc tupleCalc = compiler.compileTuple(call.getArg(0));
        final IntegerCalc indexCalc = compiler.compileInteger(call.getArg(1));
        return new AbstractMemberCalc(call, new Calc[] { tupleCalc, indexCalc }) {

            final Member[] nullTupleMembers = makeNullTuple((TupleType) tupleCalc.getType());

            public Member evaluateMember(Evaluator evaluator) {
                final Member[] members = tupleCalc.evaluateTuple(evaluator);
                assert members == null || members.length == nullTupleMembers.length;
                final int index = indexCalc.evaluateInteger(evaluator);
                if (members == null) {
                    return nullTupleMembers[index];
                }
                if (index >= members.length || index < 0) {
                    return null;
                }
                return members[index];
            }
        };
    }
}
Also used : AbstractMemberCalc(mondrian.calc.impl.AbstractMemberCalc) AbstractMemberCalc(mondrian.calc.impl.AbstractMemberCalc)

Example 5 with AbstractMemberCalc

use of mondrian.calc.impl.AbstractMemberCalc in project mondrian by pentaho.

the class CalculatedChildFunDef method compileCall.

public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
    final MemberCalc memberCalc = compiler.compileMember(call.getArg(0));
    final StringCalc stringCalc = compiler.compileString(call.getArg(1));
    return new AbstractMemberCalc(call, new Calc[] { memberCalc, stringCalc }) {

        public Member evaluateMember(Evaluator evaluator) {
            Member member = memberCalc.evaluateMember(evaluator);
            String name = stringCalc.evaluateString(evaluator);
            return getCalculatedChild(member, name, evaluator);
        }
    };
}
Also used : AbstractMemberCalc(mondrian.calc.impl.AbstractMemberCalc) AbstractMemberCalc(mondrian.calc.impl.AbstractMemberCalc)

Aggregations

AbstractMemberCalc (mondrian.calc.impl.AbstractMemberCalc)7 RolapHierarchy (mondrian.rolap.RolapHierarchy)2 AbstractTupleCalc (mondrian.calc.impl.AbstractTupleCalc)1 NamedSetExpr (mondrian.mdx.NamedSetExpr)1 LevelType (mondrian.olap.type.LevelType)1 Type (mondrian.olap.type.Type)1 RolapCube (mondrian.rolap.RolapCube)1