public SqlNode go(SqlNode node) { return node.accept(this); } }
public SqlNode go() { return root.accept(this); }
@Override public Void visit(SqlCall call) { for (int i = 0; i < call.getOperandList().size(); i++) { call.getOperandList().get(i).accept(this); } return null; }
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); }
/** * Derives the type of a node, never null. */ RelDataType deriveTypeImpl( SqlValidatorScope scope, SqlNode operand) { DeriveTypeVisitor v = new DeriveTypeVisitor(scope); final RelDataType type = operand.accept(v); return Objects.requireNonNull(scope.nullifyType(operand, type)); }
public RelDataType getParameterRowType(SqlNode sqlQuery) { // NOTE: We assume that bind variables occur in depth-first tree // traversal in the same order that they occurred in the SQL text. final List<RelDataType> types = new ArrayList<>(); // NOTE: but parameters on fetch/offset would be counted twice // as they are counted in the SqlOrderBy call and the inner SqlSelect call final Set<SqlNode> alreadyVisited = new HashSet<>(); sqlQuery.accept( new SqlShuttle() { @Override public SqlNode visit(SqlDynamicParam param) { if (alreadyVisited.add(param)) { RelDataType type = getValidatedNodeType(param); types.add(type); } return param; } }); return typeFactory.createStructType( types, new AbstractList<String>() { @Override public String get(int index) { return "?" + index; } @Override public int size() { return types.size(); } }); }
public SqlNode expand(SqlNode expr, SqlValidatorScope scope) { final Expander expander = new Expander(this, scope); SqlNode newExpr = expr.accept(expander); if (expr != newExpr) { setOriginal(newExpr, expr); } return newExpr; }
public SqlNode expandGroupByOrHavingExpr(SqlNode expr, SqlValidatorScope scope, SqlSelect select, boolean havingExpression) { final Expander expander = new ExtendedExpander(this, scope, select, expr, havingExpression); SqlNode newExpr = expr.accept(expander); if (expr != newExpr) { setOriginal(newExpr, expr); } return newExpr; }
private SqlNode validateNode(SqlNode sqlNode) throws ValidationException { SqlNode validatedSqlNode = planner.validate(sqlNode); validatedSqlNode.accept(new UnsupportedOperatorsVisitor()); return validatedSqlNode; } }
@Override public SqlNode visit(SqlCall call) { if (call instanceof SqlSelect) { SqlSelect select = (SqlSelect) call; select.getFrom().accept(this); return null; } if (call instanceof SqlOrderBy) { SqlOrderBy orderBy = (SqlOrderBy) call; orderBy.query.accept(this); return null; } if (call instanceof SqlWith) { SqlWith sqlWith = (SqlWith) call; sqlWith.body.accept(this); sqlWith.withList.accept(this); } if (call instanceof SqlBasicCall) { SqlBasicCall node = (SqlBasicCall) call; node.getOperands()[0].accept(this); return null; } if (call instanceof SqlJoin) { SqlJoin node = (SqlJoin) call; node.getLeft().accept(this); node.getRight().accept(this); return null; } return null; }
@Override public SqlNode visit(SqlIdentifier id) { String maybeParam = id.toString(); int idx = ParamNodeParser.parseParamIdx(maybeParam); if (idx >= 0 && operands.containsKey(idx)) { SqlNode sqlNode = operands.get(idx); if (sqlNode instanceof SqlIdentifier) { return sqlNode; } else { return sqlNode.accept(SqlNodeConverter.this); } } return id; }
@Override public SqlNode visit(SqlNodeList nodeList) { for (int i = 0; i < nodeList.size(); i++) { SqlNode node = nodeList.get(i); if (node instanceof SqlWithItem) { SqlWithItem item = (SqlWithItem) node; item.query.accept(this); } } return null; }
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()); }
public String convertColumn(String column, String originQuote) { String converted = column.replace(originQuote, ""); try { SqlNode sqlNode = SqlParser.create(converted).parseExpression(); sqlNode = sqlNode.accept(sqlNodeConverter); converted = sqlWriter.format(sqlNode); } catch (Throwable e) { logger.error("Failed to default convert Column, will use the input: {}", column, e); } finally { sqlWriter.reset(); } return converted; }
sqlNode.accept(sqlVisitor); List<SqlIdentifier> sqlIdentifiers = Lists.newArrayList(s);
/** Checks that all pattern variables within a function are the same, * and canonizes expressions such as {@code PREV(B.price)} to * {@code LAST(B.price, 0)}. */ private SqlNode navigationInDefine(SqlNode node, String alpha) { Set<String> prefix = node.accept(new PatternValidator(false)); Util.discard(prefix); node = new NavigationExpander().go(node); node = new NavigationReplacer(alpha).go(node); return node; }
private SqlNode navigationInMeasure(SqlNode node, boolean allRows) { final Set<String> prefix = node.accept(new PatternValidator(true)); Util.discard(prefix); final List<SqlNode> ops = ((SqlCall) node).getOperandList(); final SqlOperator defaultOp = allRows ? SqlStdOperatorTable.RUNNING : SqlStdOperatorTable.FINAL; final SqlNode op0 = ops.get(0); if (!isRunningOrFinal(op0.getKind()) || !allRows && op0.getKind() == SqlKind.RUNNING) { SqlNode newNode = defaultOp.createCall(SqlParserPos.ZERO, op0); node = SqlStdOperatorTable.AS.createCall(SqlParserPos.ZERO, newNode, ops.get(1)); } node = new NavigationExpander().go(node); return node; }
public String convertSql(String orig) { // for jdbc source, convert quote from backtick to double quote String converted = orig.replaceAll("`", "\""); if (!configurer.skipHandleDefault()) { String escapedDefault = SqlDialect.CALCITE .quoteIdentifier(configurer.useUppercaseDefault() ? "DEFAULT" : "default"); converted = converted.replaceAll("(?i)default\\.", escapedDefault + "."); // use Calcite dialect to cater to SqlParser converted = converted.replaceAll("\"(?i)default\"\\.", escapedDefault + "."); } if (!configurer.skipDefaultConvert()) { try { SqlNode sqlNode = SqlParser.create(converted).parseQuery(); sqlNode = sqlNode.accept(sqlNodeConverter); converted = sqlWriter.format(sqlNode); } catch (Throwable e) { logger.error("Failed to default convert sql, will use the input: {}", orig, e); } finally { sqlWriter.reset(); } } converted = configurer.fixAfterDefaultConvert(converted); return converted; }
inner.accept(new NavigationExpander(call.getOperator(), offset)); if (op != null) { newInnerNode = op.createCall(SqlParserPos.ZERO, newInnerNode, for (SqlNode node : operands) { if (node != null) { SqlNode newNode = node.accept(new NavigationExpander()); if (op != null) { newNode = op.createCall(SqlParserPos.ZERO, newNode, offset);
private SqlNode convertSqlCall(SqlCall sqlCall) { SqlOperator operator = sqlCall.getOperator(); if (operator != null) { Pair<SqlNode, SqlNode> matched = convMaster.matchSqlFunc(sqlCall); if (matched != null) { Preconditions.checkState(matched.getFirst() instanceof SqlCall); SqlCall sourceTmpl = (SqlCall) matched.getFirst(); Preconditions.checkState(sourceTmpl.operandCount() == sqlCall.operandCount()); SqlNode targetTmpl = matched.getSecond(); boolean isWindowCall = sourceTmpl.getOperator() instanceof SqlOverOperator; SqlParamsFinder sqlParamsFinder = SqlParamsFinder.newInstance(sourceTmpl, sqlCall, isWindowCall); return targetTmpl.accept(new SqlFuncFiller(sqlParamsFinder.getParamNodes(), isWindowCall)); } } return null; }