Java Code Examples for org.apache.calcite.sql.SqlNode#accept()
The following examples show how to use
org.apache.calcite.sql.SqlNode#accept() .
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: AggregatingSelectScope.java From Bats with Apache License 2.0 | 6 votes |
public boolean checkAggregateExpr(SqlNode expr, boolean deep) { // Fully-qualify any identifiers in expr. if (deep) { expr = validator.expand(expr, this); } // Make sure expression is valid, throws if not. Pair<ImmutableList<SqlNode>, ImmutableList<SqlNode>> pair = getGroupExprs(); final AggChecker aggChecker = new AggChecker(validator, this, pair.left, pair.right, distinct); if (deep) { expr.accept(aggChecker); } // Return whether expression exactly matches one of the group // expressions. return aggChecker.isGroupExpr(expr); }
Example 2
Source File: Hoist.java From calcite with Apache License 2.0 | 6 votes |
/** Hoists literals in a given SQL string, returning a {@link Hoisted}. */ public Hoisted hoist(String sql) { final List<Variable> variables = new ArrayList<>(); final SqlParser parser = SqlParser.create(sql, config.parserConfig()); final SqlNode node; try { node = parser.parseQuery(); } catch (SqlParseException e) { throw new RuntimeException(e); } node.accept(new SqlShuttle() { @Override public SqlNode visit(SqlLiteral literal) { variables.add(new Variable(sql, variables.size(), literal)); return super.visit(literal); } }); return new Hoisted(sql, variables); }
Example 3
Source File: AggregatingSelectScope.java From calcite with Apache License 2.0 | 6 votes |
public boolean checkAggregateExpr(SqlNode expr, boolean deep) { // Fully-qualify any identifiers in expr. if (deep) { expr = validator.expand(expr, this); } // Make sure expression is valid, throws if not. Pair<ImmutableList<SqlNode>, ImmutableList<SqlNode>> pair = getGroupExprs(); final AggChecker aggChecker = new AggChecker(validator, this, pair.left, pair.right, distinct); if (deep) { expr.accept(aggChecker); } // Return whether expression exactly matches one of the group // expressions. return aggChecker.isGroupExpr(expr); }
Example 4
Source File: SqlValidatorImpl.java From flink with Apache License 2.0 | 6 votes |
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; }
Example 5
Source File: SqlValidatorImpl.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
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(); } }); }
Example 6
Source File: AggFinder.java From Bats with Apache License 2.0 | 5 votes |
/** * Finds an aggregate. * * @param node Parse tree to search * @return First aggregate function in parse tree, or null if not found */ public SqlCall findAgg(SqlNode node) { try { node.accept(this); return null; } catch (Util.FoundOne e) { Util.swallow(e, null); return (SqlCall) e.getNode(); } }
Example 7
Source File: SqlBetweenOperator.java From calcite with Apache License 2.0 | 5 votes |
boolean containsAnd(SqlNode node) { try { node.accept(this); return false; } catch (Util.FoundOne e) { return true; } }
Example 8
Source File: SqlValidatorImpl.java From flink with Apache License 2.0 | 5 votes |
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; }
Example 9
Source File: CalciteParser.java From kylin-on-parquet-v2 with Apache License 2.0 | 5 votes |
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 10
Source File: SqlShuttle.java From calcite with Apache License 2.0 | 5 votes |
public SqlNode visitChild( SqlVisitor<SqlNode> visitor, SqlNode expr, int i, SqlNode operand) { if (operand == null) { return null; } SqlNode newOperand = operand.accept(SqlShuttle.this); if (newOperand != operand) { update = true; } clonedOperands[i] = newOperand; return newOperand; }
Example 11
Source File: SqlBasicVisitor.java From Bats with Apache License 2.0 | 5 votes |
public R visit(SqlNodeList nodeList) { R result = null; for (int i = 0; i < nodeList.size(); i++) { SqlNode node = nodeList.get(i); result = node.accept(this); } return result; }
Example 12
Source File: AggFinder.java From calcite with Apache License 2.0 | 5 votes |
/** Creates a copy of this finder that has the same parameters as this, * then returns the list of all aggregates found. */ Iterable<SqlCall> findAll(Iterable<SqlNode> nodes) { final AggIterable aggIterable = new AggIterable(opTab, over, aggregate, group, delegate, nameMatcher); for (SqlNode node : nodes) { node.accept(aggIterable); } return aggIterable.calls; }
Example 13
Source File: SqlNodeConverter.java From kylin-on-parquet-v2 with Apache License 2.0 | 5 votes |
@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; }
Example 14
Source File: AggFinder.java From Bats with Apache License 2.0 | 5 votes |
public SqlCall findAgg(List<SqlNode> nodes) { try { for (SqlNode node : nodes) { node.accept(this); } return null; } catch (Util.FoundOne e) { Util.swallow(e, null); return (SqlCall) e.getNode(); } }
Example 15
Source File: UnsupportedOperatorsVisitor.java From Bats with Apache License 2.0 | 4 votes |
private boolean checkDirExplorers(SqlNode sqlNode) { final ExprFinder dirExplorersFinder = new ExprFinder(DirExplorersCondition); sqlNode.accept(dirExplorersFinder); return dirExplorersFinder.find(); }
Example 16
Source File: DrillParserWithCompoundIdConverter.java From Bats with Apache License 2.0 | 4 votes |
@Override public SqlNode parseSqlExpressionEof() throws Exception { SqlNode originalSqlNode = super.parseSqlExpressionEof(); return originalSqlNode.accept(createConverter()); }
Example 17
Source File: ParserWithCompoundIdConverter.java From dremio-oss with Apache License 2.0 | 4 votes |
@Override public SqlNode parseSqlExpressionEof() throws Exception { SqlNode originalSqlNode = super.parseSqlExpressionEof(); return originalSqlNode.accept(createConverter()); }
Example 18
Source File: AncestorsVisitor.java From dremio-oss with Apache License 2.0 | 4 votes |
private List<SqlIdentifier> extractAncestorsFromFrom(SqlNode from) { return from.accept(new BaseSqlVisitor<List<SqlIdentifier>>() { @Override public List<SqlIdentifier> visit(SqlIdentifier id) { return Arrays.asList(id); } @Override public List<SqlIdentifier> visit(SqlCall call) { SqlOperator operator = call.getOperator(); switch (operator.getKind()) { case AS: SqlNode sqlNode = call.getOperandList().get(0); switch (sqlNode.getKind()) { case IDENTIFIER: return asList((SqlIdentifier)sqlNode); case SELECT: return extractAncestors(sqlNode); case COLLECTION_TABLE: // table function SqlNode operand = ((SqlCall)sqlNode).operand(0); if (operand.getKind() == SqlKind.OTHER_FUNCTION) { SqlFunction tableFunction = (SqlFunction)((SqlCall)operand).getOperator(); return asList(tableFunction.getSqlIdentifier()); } // pass through case VALUES: return Collections.emptyList(); default: logger.warn("Failure while extracting parents from sql. Unexpected 1st operand in AS: {}. SQL: \n {}", sqlNode.getKind() ,SqlNodes.toTreeString(sqlNode)); return Collections.emptyList(); } case JOIN: SqlJoin join = (SqlJoin)call; List<SqlIdentifier> result = new ArrayList<>(); result.addAll(join.getLeft().accept(this)); result.addAll(join.getRight().accept(this)); return result; default: throw new UnsupportedOperationException("Unexpected operator in call: " + operator.getKind() + "\n" + SqlNodes.toTreeString(call)); } } }); }
Example 19
Source File: TestSqlBracketlessSyntax.java From dremio-oss with Apache License 2.0 | 4 votes |
@Test public void checkComplexExpressionParsing() throws Exception{ FrameworkConfig config = Frameworks.newConfigBuilder() .parserConfig(SqlParser.configBuilder() .setLex(Lex.MYSQL) .setIdentifierMaxLength(PlannerSettings.DEFAULT_IDENTIFIER_MAX_LENGTH) .setParserFactory(ParserImpl.FACTORY) .build()) .defaultSchema(CalciteSchema.createRootSchema(false /* addMetadata */, false /* cache */).plus()) .convertletTable(new ConvertletTable(new ContextInformation() { @Override public String getQueryUser() { return null; } @Override public String getCurrentDefaultSchema() { return null; } @Override public long getQueryStartTime() { return 0; } @Override public int getRootFragmentTimeZone() { return 0; } @Override public QueryId getLastQueryId() { return null; } @Override public void registerAdditionalInfo(AdditionalContext object) { } @Override public <T extends AdditionalContext> T getAdditionalInfo(Class<T> claz) { return null; } })) .build(); Planner planner = Frameworks.getPlanner(config); SqlNode node = planner.parse("" + "select a[4].c \n" + "from x.y.z \n" + "where a.c.b = 5 and x[2] = 7 \n" + "group by d \n" + "having a.c < 5 \n" + "order by x.a.a.a.a.a"); String expected = "SELECT `a`[4]['c']\n" + "FROM `x`.`y`.`z`\n" + "WHERE `a`.`c`['b'] = 5 AND `x`[2] = 7\n" + "GROUP BY `d`\n" + "HAVING `a`.`c` < 5\n" + "ORDER BY `x`.`a`['a']['a']['a']['a']"; SqlNode rewritten = node.accept(new CompoundIdentifierConverter()); String rewrittenQuery = rewritten.toString(); DremioAssert.assertMultiLineStringEquals(expected, rewrittenQuery); }
Example 20
Source File: UnsupportedOperatorsVisitor.java From dremio-oss with Apache License 2.0 | 4 votes |
private boolean containsFlatten(SqlNode sqlNode) throws UnsupportedOperationException { final ExprFinder dirExplorersFinder = new ExprFinder(FLATTEN_FINDER_CONDITION); sqlNode.accept(dirExplorersFinder); return dirExplorersFinder.find(); }