org.apache.calcite.sql.util.SqlVisitor Java Examples

The following examples show how to use org.apache.calcite.sql.util.SqlVisitor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SqlMatchRecognize.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public <R> void acceptCall(
    SqlVisitor<R> visitor,
    SqlCall call,
    boolean onlyExpressions,
    SqlBasicVisitor.ArgHandler<R> argHandler) {
  if (onlyExpressions) {
    List<SqlNode> operands = call.getOperandList();
    for (int i = 0; i < operands.size(); i++) {
      SqlNode operand = operands.get(i);
      if (operand == null) {
        continue;
      }
      argHandler.visitChild(visitor, call, i, operand);
    }
  } else {
    super.acceptCall(visitor, call, onlyExpressions, argHandler);
  }
}
 
Example #2
Source File: CalciteParser.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
public static void ensureNoAliasInExpr(String expr) {
    SqlNode sqlNode = getExpNode(expr);

    SqlVisitor sqlVisitor = new SqlBasicVisitor() {
        @Override
        public Object visit(SqlIdentifier id) {
            if (id.names.size() > 1) {
                throw new IllegalArgumentException(
                        "Column Identifier in the computed column expression should only contain COLUMN");
            }
            return null;
        }
    };

    sqlNode.accept(sqlVisitor);
}
 
Example #3
Source File: SqlMatchRecognize.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public <R> void acceptCall(
    SqlVisitor<R> visitor,
    SqlCall call,
    boolean onlyExpressions,
    SqlBasicVisitor.ArgHandler<R> argHandler) {
  if (onlyExpressions) {
    List<SqlNode> operands = call.getOperandList();
    for (int i = 0; i < operands.size(); i++) {
      SqlNode operand = operands.get(i);
      if (operand == null) {
        continue;
      }
      argHandler.visitChild(visitor, call, i, operand);
    }
  } else {
    super.acceptCall(visitor, call, onlyExpressions, argHandler);
  }
}
 
Example #4
Source File: SqlWindow.java    From Bats with Apache License 2.0 6 votes vote down vote up
public <R> void acceptCall(
    SqlVisitor<R> visitor,
    SqlCall call,
    boolean onlyExpressions,
    SqlBasicVisitor.ArgHandler<R> argHandler) {
  if (onlyExpressions) {
    for (Ord<SqlNode> operand : Ord.zip(call.getOperandList())) {
      // if the second param is an Identifier then it's supposed to
      // be a name from a window clause and isn't part of the
      // group by check
      if (operand.e == null) {
        continue;
      }
      if (operand.i == 1 && operand.e instanceof SqlIdentifier) {
        // skip refName
        continue;
      }
      argHandler.visitChild(visitor, call, operand.i, operand.e);
    }
  } else {
    super.acceptCall(visitor, call, onlyExpressions, argHandler);
  }
}
 
Example #5
Source File: CalciteParser.java    From kylin with Apache License 2.0 6 votes vote down vote up
public static void ensureNoAliasInExpr(String expr) {
    SqlNode sqlNode = getExpNode(expr);

    SqlVisitor sqlVisitor = new SqlBasicVisitor() {
        @Override
        public Object visit(SqlIdentifier id) {
            if (id.names.size() > 1) {
                throw new IllegalArgumentException(
                        "Column Identifier in the computed column expression should only contain COLUMN");
            }
            return null;
        }
    };

    sqlNode.accept(sqlVisitor);
}
 
Example #6
Source File: SqlOverOperator.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Accepts a {@link SqlVisitor}, and tells it to visit each child.
 *
 * @param visitor Visitor
 */
public <R> void acceptCall(
    SqlVisitor<R> visitor,
    SqlCall call,
    boolean onlyExpressions,
    SqlBasicVisitor.ArgHandler<R> argHandler) {
  if (onlyExpressions) {
    for (Ord<SqlNode> operand : Ord.zip(call.getOperandList())) {
      // if the second param is an Identifier then it's supposed to
      // be a name from a window clause and isn't part of the
      // group by check
      if (operand == null) {
        continue;
      }
      if (operand.i == 1 && operand.e instanceof SqlIdentifier) {
        continue;
      }
      argHandler.visitChild(visitor, call, operand.i, operand.e);
    }
  } else {
    super.acceptCall(visitor, call, onlyExpressions, argHandler);
  }
}
 
Example #7
Source File: SqlOverOperator.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Accepts a {@link SqlVisitor}, and tells it to visit each child.
 *
 * @param visitor Visitor
 */
public <R> void acceptCall(
    SqlVisitor<R> visitor,
    SqlCall call,
    boolean onlyExpressions,
    SqlBasicVisitor.ArgHandler<R> argHandler) {
  if (onlyExpressions) {
    for (Ord<SqlNode> operand : Ord.zip(call.getOperandList())) {
      // if the second param is an Identifier then it's supposed to
      // be a name from a window clause and isn't part of the
      // group by check
      if (operand == null) {
        continue;
      }
      if (operand.i == 1 && operand.e instanceof SqlIdentifier) {
        continue;
      }
      argHandler.visitChild(visitor, call, operand.i, operand.e);
    }
  } else {
    super.acceptCall(visitor, call, onlyExpressions, argHandler);
  }
}
 
Example #8
Source File: SqlWindow.java    From calcite with Apache License 2.0 6 votes vote down vote up
public <R> void acceptCall(
    SqlVisitor<R> visitor,
    SqlCall call,
    boolean onlyExpressions,
    SqlBasicVisitor.ArgHandler<R> argHandler) {
  if (onlyExpressions) {
    for (Ord<SqlNode> operand : Ord.zip(call.getOperandList())) {
      // if the second param is an Identifier then it's supposed to
      // be a name from a window clause and isn't part of the
      // group by check
      if (operand.e == null) {
        continue;
      }
      if (operand.i == 1 && operand.e instanceof SqlIdentifier) {
        // skip refName
        continue;
      }
      argHandler.visitChild(visitor, call, operand.i, operand.e);
    }
  } else {
    super.acceptCall(visitor, call, onlyExpressions, argHandler);
  }
}
 
Example #9
Source File: SqlDotOperator.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public <R> void acceptCall(SqlVisitor<R> visitor, SqlCall call,
    boolean onlyExpressions, SqlBasicVisitor.ArgHandler<R> argHandler) {
  if (onlyExpressions) {
    // Do not visit operands[1] -- it is not an expression.
    argHandler.visitChild(visitor, call, 0, call.operand(0));
  } else {
    super.acceptCall(visitor, call, onlyExpressions, argHandler);
  }
}
 
Example #10
Source File: SqlAsOperator.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <R> void acceptCall(
    SqlVisitor<R> visitor,
    SqlCall call,
    boolean onlyExpressions,
    SqlBasicVisitor.ArgHandler<R> argHandler) {
  if (onlyExpressions) {
    // Do not visit operands[1] -- it is not an expression.
    argHandler.visitChild(visitor, call, 0, call.operand(0));
  } else {
    super.acceptCall(visitor, call, onlyExpressions, argHandler);
  }
}
 
Example #11
Source File: SqlSnapshot.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public <R> void acceptCall(
    SqlVisitor<R> visitor,
    SqlCall call,
    boolean onlyExpressions,
    SqlBasicVisitor.ArgHandler<R> argHandler) {
  if (onlyExpressions) {
    List<SqlNode> operands = call.getOperandList();
    // skip the first operand
    for (int i = 1; i < operands.size(); i++) {
      argHandler.visitChild(visitor, call, i, operands.get(i));
    }
  } else {
    super.acceptCall(visitor, call, false, argHandler);
  }
}
 
Example #12
Source File: CalciteParser.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static String replaceAliasInExpr(String expr, Map<String, String> renaming) {
    String prefix = "select ";
    String suffix = " from t";
    String sql = prefix + expr + suffix;
    SqlNode sqlNode = CalciteParser.getOnlySelectNode(sql);

    final Set<SqlIdentifier> s = Sets.newHashSet();
    SqlVisitor sqlVisitor = new SqlBasicVisitor() {
        @Override
        public Object visit(SqlIdentifier id) {
            Preconditions.checkState(id.names.size() == 2);
            s.add(id);
            return null;
        }
    };

    sqlNode.accept(sqlVisitor);
    List<SqlIdentifier> sqlIdentifiers = Lists.newArrayList(s);

    CalciteParser.descSortByPosition(sqlIdentifiers);

    for (SqlIdentifier sqlIdentifier : sqlIdentifiers) {
        Pair<Integer, Integer> replacePos = CalciteParser.getReplacePos(sqlIdentifier, sql);
        int start = replacePos.getFirst();
        int end = replacePos.getSecond();
        String aliasInExpr = sqlIdentifier.names.get(0);
        String col = sqlIdentifier.names.get(1);
        String renamedAlias = renaming.get(aliasInExpr);
        Preconditions.checkNotNull(renamedAlias,
                "rename for alias " + aliasInExpr + " in expr (" + expr + ") is not found");
        sql = sql.substring(0, start) + renamedAlias + "." + col + sql.substring(end);
    }

    return sql.substring(prefix.length(), sql.length() - suffix.length());
}
 
Example #13
Source File: CalciteParser.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static String insertAliasInExpr(String expr, String alias) {
    String prefix = "select ";
    String suffix = " from t";
    String sql = prefix + expr + suffix;
    SqlNode sqlNode = getOnlySelectNode(sql);

    final Set<SqlIdentifier> s = Sets.newHashSet();
    SqlVisitor sqlVisitor = new SqlBasicVisitor() {
        @Override
        public Object visit(SqlIdentifier id) {
            if (id.names.size() > 1) {
                throw new IllegalArgumentException("SqlIdentifier " + id + " contains DB/Table name");
            }
            s.add(id);
            return null;
        }
    };

    sqlNode.accept(sqlVisitor);
    List<SqlIdentifier> sqlIdentifiers = Lists.newArrayList(s);

    descSortByPosition(sqlIdentifiers);

    for (SqlIdentifier sqlIdentifier : sqlIdentifiers) {
        Pair<Integer, Integer> replacePos = getReplacePos(sqlIdentifier, sql);
        int start = replacePos.getFirst();
        sql = sql.substring(0, start) + alias + "." + sql.substring(start);
    }

    return sql.substring(prefix.length(), sql.length() - suffix.length());
}
 
Example #14
Source File: SqlSelectOperator.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <R> void acceptCall(
    SqlVisitor<R> visitor,
    SqlCall call,
    boolean onlyExpressions,
    SqlBasicVisitor.ArgHandler<R> argHandler) {
  if (!onlyExpressions) {
    // None of the arguments to the SELECT operator are expressions.
    super.acceptCall(visitor, call, onlyExpressions, argHandler);
  }
}
 
Example #15
Source File: CompoundIdentifierConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public SqlNode visitChild(
    SqlVisitor<SqlNode> visitor,
    SqlNode expr,
    int i,
    SqlNode operand) {
  if (operand == null) {
    return null;
  }

  boolean localEnableComplex = enableComplex;
  if(rewriteTypes != null){
    switch(rewriteTypes[i]){
    case DISABLE:
      enableComplex = false;
      break;
    case ENABLE:
      enableComplex = true;
    }
  }
  SqlNode newOperand = operand.accept(CompoundIdentifierConverter.this);
  enableComplex = localEnableComplex;
  if (newOperand != operand) {
    update = true;
  }
  clonedOperands[i] = newOperand;
  return newOperand;
}
 
Example #16
Source File: CalciteParser.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static String insertAliasInExpr(String expr, String alias) {
    String prefix = "select ";
    String suffix = " from t";
    String sql = prefix + expr + suffix;
    SqlNode sqlNode = getOnlySelectNode(sql);

    final Set<SqlIdentifier> s = Sets.newHashSet();
    SqlVisitor sqlVisitor = new SqlBasicVisitor() {
        @Override
        public Object visit(SqlIdentifier id) {
            if (id.names.size() > 1) {
                throw new IllegalArgumentException("SqlIdentifier " + id + " contains DB/Table name");
            }
            s.add(id);
            return null;
        }
    };

    sqlNode.accept(sqlVisitor);
    List<SqlIdentifier> sqlIdentifiers = Lists.newArrayList(s);

    descSortByPosition(sqlIdentifiers);

    for (SqlIdentifier sqlIdentifier : sqlIdentifiers) {
        Pair<Integer, Integer> replacePos = getReplacePos(sqlIdentifier, sql);
        int start = replacePos.getFirst();
        sql = sql.substring(0, start) + alias + "." + sql.substring(start);
    }

    return sql.substring(prefix.length(), sql.length() - suffix.length());
}
 
Example #17
Source File: SqlOperator.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Accepts a {@link SqlVisitor}, visiting each operand of a call. Returns
 * null.
 *
 * @param visitor Visitor
 * @param call    Call to visit
 */
public <R> R acceptCall(SqlVisitor<R> visitor, SqlCall call) {
  for (SqlNode operand : call.getOperandList()) {
    if (operand == null) {
      continue;
    }
    operand.accept(visitor);
  }
  return null;
}
 
Example #18
Source File: CalciteParser.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static String replaceAliasInExpr(String expr, Map<String, String> renaming) {
    String prefix = "select ";
    String suffix = " from t";
    String sql = prefix + expr + suffix;
    SqlNode sqlNode = CalciteParser.getOnlySelectNode(sql);

    final Set<SqlIdentifier> s = Sets.newHashSet();
    SqlVisitor sqlVisitor = new SqlBasicVisitor() {
        @Override
        public Object visit(SqlIdentifier id) {
            Preconditions.checkState(id.names.size() == 2);
            s.add(id);
            return null;
        }
    };

    sqlNode.accept(sqlVisitor);
    List<SqlIdentifier> sqlIdentifiers = Lists.newArrayList(s);

    CalciteParser.descSortByPosition(sqlIdentifiers);

    for (SqlIdentifier sqlIdentifier : sqlIdentifiers) {
        Pair<Integer, Integer> replacePos = CalciteParser.getReplacePos(sqlIdentifier, sql);
        int start = replacePos.getFirst();
        int end = replacePos.getSecond();
        String aliasInExpr = sqlIdentifier.names.get(0);
        String col = sqlIdentifier.names.get(1);
        String renamedAlias = renaming.get(aliasInExpr);
        Preconditions.checkNotNull(renamedAlias,
                "rename for alias " + aliasInExpr + " in expr (" + expr + ") is not found");
        sql = sql.substring(0, start) + renamedAlias + "." + col + sql.substring(end);
    }

    return sql.substring(prefix.length(), sql.length() - suffix.length());
}
 
Example #19
Source File: SqlSnapshot.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public <R> void acceptCall(
    SqlVisitor<R> visitor,
    SqlCall call,
    boolean onlyExpressions,
    SqlBasicVisitor.ArgHandler<R> argHandler) {
  if (onlyExpressions) {
    List<SqlNode> operands = call.getOperandList();
    // skip the first operand
    for (int i = 1; i < operands.size(); i++) {
      argHandler.visitChild(visitor, call, i, operands.get(i));
    }
  } else {
    super.acceptCall(visitor, call, false, argHandler);
  }
}
 
Example #20
Source File: CompoundIdentifierConverter.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public SqlNode visitChild(
    SqlVisitor<SqlNode> visitor,
    SqlNode expr,
    int i,
    SqlNode operand) {
  if (operand == null) {
    return null;
  }

  boolean localEnableComplex = enableComplex;
  if (rewriteTypes != null) {
    switch (rewriteTypes[i]) {
      case DISABLE:
        enableComplex = false;
        break;
      case ENABLE:
        enableComplex = true;
    }
  }
  SqlNode newOperand = operand.accept(CompoundIdentifierConverter.this);
  enableComplex = localEnableComplex;
  if (newOperand != operand) {
    update = true;
  }
  clonedOperands[i] = newOperand;
  return newOperand;
}
 
Example #21
Source File: SqlOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Accepts a {@link SqlVisitor}, visiting each operand of a call. Returns
 * null.
 *
 * @param visitor Visitor
 * @param call    Call to visit
 */
public <R> R acceptCall(SqlVisitor<R> visitor, SqlCall call) {
  for (SqlNode operand : call.getOperandList()) {
    if (operand == null) {
      continue;
    }
    operand.accept(visitor);
  }
  return null;
}
 
Example #22
Source File: SqlAsOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
public <R> void acceptCall(
    SqlVisitor<R> visitor,
    SqlCall call,
    boolean onlyExpressions,
    SqlBasicVisitor.ArgHandler<R> argHandler) {
  if (onlyExpressions) {
    // Do not visit operands[1] -- it is not an expression.
    argHandler.visitChild(visitor, call, 0, call.operand(0));
  } else {
    super.acceptCall(visitor, call, onlyExpressions, argHandler);
  }
}
 
Example #23
Source File: SqlSelectOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
public <R> void acceptCall(
    SqlVisitor<R> visitor,
    SqlCall call,
    boolean onlyExpressions,
    SqlBasicVisitor.ArgHandler<R> argHandler) {
  if (!onlyExpressions) {
    // None of the arguments to the SELECT operator are expressions.
    super.acceptCall(visitor, call, onlyExpressions, argHandler);
  }
}
 
Example #24
Source File: SqlDotOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public <R> void acceptCall(SqlVisitor<R> visitor, SqlCall call,
    boolean onlyExpressions, SqlBasicVisitor.ArgHandler<R> argHandler) {
  if (onlyExpressions) {
    // Do not visit operands[1] -- it is not an expression.
    argHandler.visitChild(visitor, call, 0, call.operand(0));
  } else {
    super.acceptCall(visitor, call, onlyExpressions, argHandler);
  }
}
 
Example #25
Source File: SqlIntervalQualifier.java    From calcite with Apache License 2.0 4 votes vote down vote up
public <R> R accept(SqlVisitor<R> visitor) {
  return visitor.visit(this);
}
 
Example #26
Source File: SqlIdentifier.java    From Bats with Apache License 2.0 4 votes vote down vote up
public <R> R accept(SqlVisitor<R> visitor) {
  return visitor.visit(this);
}
 
Example #27
Source File: SqlIntervalQualifier.java    From Bats with Apache License 2.0 4 votes vote down vote up
public <R> R accept(SqlVisitor<R> visitor) {
  return visitor.visit(this);
}
 
Example #28
Source File: SqlCall.java    From calcite with Apache License 2.0 4 votes vote down vote up
public <R> R accept(SqlVisitor<R> visitor) {
  return visitor.visit(this);
}
 
Example #29
Source File: SqlDataTypeSpec.java    From calcite with Apache License 2.0 4 votes vote down vote up
public <R> R accept(SqlVisitor<R> visitor) {
  return visitor.visit(this);
}
 
Example #30
Source File: SqlLiteral.java    From calcite with Apache License 2.0 4 votes vote down vote up
public <R> R accept(SqlVisitor<R> visitor) {
  return visitor.visit(this);
}