Search in sources :

Example 1 with MatchRecognizeScope

use of org.apache.calcite.sql.validate.MatchRecognizeScope in project calcite by apache.

the class SqlToRelConverter method convertIdentifier.

/**
 * Converts an identifier into an expression in a given scope. For example,
 * the "empno" in "select empno from emp join dept" becomes "emp.empno".
 */
private RexNode convertIdentifier(Blackboard bb, SqlIdentifier identifier) {
    // first check for reserved identifiers like CURRENT_USER
    final SqlCall call = SqlUtil.makeCall(opTab, identifier);
    if (call != null) {
        return bb.convertExpression(call);
    }
    String pv = null;
    if (bb.isPatternVarRef && identifier.names.size() > 1) {
        pv = identifier.names.get(0);
    }
    final SqlQualified qualified;
    if (bb.scope != null) {
        qualified = bb.scope.fullyQualify(identifier);
    } else {
        qualified = SqlQualified.create(null, 1, null, identifier);
    }
    final Pair<RexNode, Map<String, Integer>> e0 = bb.lookupExp(qualified);
    RexNode e = e0.left;
    for (String name : qualified.suffix()) {
        if (e == e0.left && e0.right != null) {
            int i = e0.right.get(name);
            e = rexBuilder.makeFieldAccess(e, i);
        } else {
            // name already fully-qualified
            final boolean caseSensitive = true;
            if (identifier.isStar() && bb.scope instanceof MatchRecognizeScope) {
                e = rexBuilder.makeFieldAccess(e, 0);
            } else {
                e = rexBuilder.makeFieldAccess(e, name, caseSensitive);
            }
        }
    }
    if (e instanceof RexInputRef) {
        // adjust the type to account for nulls introduced by outer joins
        e = adjustInputRef(bb, (RexInputRef) e);
        if (pv != null) {
            e = RexPatternFieldRef.of(pv, (RexInputRef) e);
        }
    }
    if (e0.left instanceof RexCorrelVariable) {
        assert e instanceof RexFieldAccess;
        final RexNode prev = bb.mapCorrelateToRex.put(((RexCorrelVariable) e0.left).id, (RexFieldAccess) e);
        assert prev == null;
    }
    return e;
}
Also used : RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) SqlCall(org.apache.calcite.sql.SqlCall) SqlQualified(org.apache.calcite.sql.validate.SqlQualified) RexInputRef(org.apache.calcite.rex.RexInputRef) NlsString(org.apache.calcite.util.NlsString) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) RexNode(org.apache.calcite.rex.RexNode) MatchRecognizeScope(org.apache.calcite.sql.validate.MatchRecognizeScope)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 RexCorrelVariable (org.apache.calcite.rex.RexCorrelVariable)1 RexFieldAccess (org.apache.calcite.rex.RexFieldAccess)1 RexInputRef (org.apache.calcite.rex.RexInputRef)1 RexNode (org.apache.calcite.rex.RexNode)1 SqlCall (org.apache.calcite.sql.SqlCall)1 MatchRecognizeScope (org.apache.calcite.sql.validate.MatchRecognizeScope)1 SqlQualified (org.apache.calcite.sql.validate.SqlQualified)1 NlsString (org.apache.calcite.util.NlsString)1