Search in sources :

Example 6 with SqlBasicCall

use of org.apache.calcite.sql.SqlBasicCall in project Taier by DTStack.

the class UnionCall method handleOperand.

/**
 * @param node
 */
private void handleOperand(SqlNode node) {
    Pair<String, SqlNode> sqlNodePair = removeAs(node);
    String alias = null;
    SqlNode handledNode = node;
    if (sqlNodePair != null) {
        alias = sqlNodePair.getKey();
        handledNode = sqlNodePair.getValue();
    }
    List<Node> operands = getOperands();
    if (operands == null) {
        operands = Lists.newArrayList();
        setOperands(operands);
    }
    // union select
    if (handledNode instanceof SqlSelect) {
        SelectNode selectNode = new SelectNode(getDefaultDb(), getTableColumnMap());
        selectNode.setAlias(alias);
        selectNode.setContext(Context.INSERT_FROM_UNION);
        selectNode.parseSql(handledNode);
        operands.add(selectNode);
        comboFromList.add(selectNode);
    } else // 多union
    if (handledNode instanceof SqlBasicCall && SqlKind.UNION == ((SqlBasicCall) handledNode).getOperator().getKind()) {
        UnionCall unionCall = new UnionCall(getDefaultDb(), getTableColumnMap());
        unionCall.setAlias(alias);
        unionCall.parseSql(handledNode);
        operands.add(unionCall);
        comboFromList.addAll(unionCall.getComboFromList());
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlNode(org.apache.calcite.sql.SqlNode) SqlNode(org.apache.calcite.sql.SqlNode)

Example 7 with SqlBasicCall

use of org.apache.calcite.sql.SqlBasicCall in project Taier by DTStack.

the class UnionCall method parseSql.

@Override
public Node parseSql(SqlNode node) {
    SqlBasicCall sqlBasicCall = unionCallCheckNode(node);
    List<SqlNode> operandList = sqlBasicCall.getOperandList();
    comboFromList = Lists.newArrayList();
    SqlNode sqlNode0 = operandList.get(0);
    handleOperand(sqlNode0);
    SqlNode sqlNode1 = operandList.get(1);
    handleOperand(sqlNode1);
    return null;
}
Also used : SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlNode(org.apache.calcite.sql.SqlNode)

Example 8 with SqlBasicCall

use of org.apache.calcite.sql.SqlBasicCall in project coral by linkedin.

the class ParseTreeBuilder method visitSubqueryExpr.

@Override
protected SqlNode visitSubqueryExpr(ASTNode node, ParseContext ctx) {
    ArrayList<Node> children = node.getChildren();
    checkState(children.size() >= 2);
    SqlOperator op = getSubQueryOp((ASTNode) node.getChildren().get(0), ctx);
    SqlNode subQuery = visit((ASTNode) children.get(1), ctx);
    List<SqlNode> operands = new ArrayList<>();
    operands.add(subQuery);
    if (children.size() == 3) {
        SqlNode lhs = visit(((ASTNode) children.get(2)), ctx);
        operands.add(0, lhs);
    }
    return new SqlBasicCall(op, operands.toArray(new SqlNode[0]), ZERO);
}
Also used : SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlNode(org.apache.calcite.sql.SqlNode) Node(com.linkedin.coral.hive.hive2rel.parsetree.parser.Node) ASTNode(com.linkedin.coral.hive.hive2rel.parsetree.parser.ASTNode) ArrayList(java.util.ArrayList) ASTNode(com.linkedin.coral.hive.hive2rel.parsetree.parser.ASTNode) SqlNode(org.apache.calcite.sql.SqlNode)

Example 9 with SqlBasicCall

use of org.apache.calcite.sql.SqlBasicCall in project coral by linkedin.

the class ParseTreeBuilder method visitFunctionInternal.

private SqlNode visitFunctionInternal(ASTNode node, ParseContext ctx, SqlLiteral quantifier) {
    ArrayList<Node> children = node.getChildren();
    checkState(children.size() > 0);
    ASTNode functionNode = (ASTNode) children.get(0);
    String functionName = functionNode.getText();
    List<SqlNode> sqlOperands = visitChildren(children, ctx);
    Function hiveFunction = functionResolver.tryResolve(functionName, ctx.hiveTable.orElse(null), // The first element of sqlOperands is the operator itself. The actual # of operands is sqlOperands.size() - 1
    sqlOperands.size() - 1);
    // Special treatment for Window Function
    SqlNode lastSqlOperand = sqlOperands.get(sqlOperands.size() - 1);
    if (lastSqlOperand instanceof SqlWindow) {
        // For a SQL example of "func() OVER (PARTITIONED BY ...)":
        // In Hive, TOK_WINDOWSPEC (the window spec) is the last operand of the function "func":
        // TOK_FUNCTION will have 1+N+1 children, where the first is the function name, the last is TOK_WINDOWSPEC
        // and everything in between are the operands of the function
        // In Calcite, SqlWindow (the window spec) is a sibling of the function "func":
        // SqlBasicCall("OVER") will have 2 children: "func" and SqlWindow
        /**
         * See {@link #visitWindowSpec(ASTNode, ParseContext)} for SQL, AST Tree and SqlNode Tree examples
         */
        SqlNode func = hiveFunction.createCall(sqlOperands.get(0), sqlOperands.subList(1, sqlOperands.size() - 1), quantifier);
        SqlNode window = lastSqlOperand;
        return new SqlBasicCall(SqlStdOperatorTable.OVER, new SqlNode[] { func, window }, ZERO);
    }
    if (functionName.equalsIgnoreCase("SUBSTRING")) {
        // Calcite overrides instance of SUBSTRING with its default SUBSTRING function as defined in SqlStdOperatorTable,
        // so we rewrite instances of SUBSTRING as SUBSTR
        SqlNode originalNode = sqlOperands.get(0);
        SqlNode substrNode = new SqlIdentifier(ImmutableList.of("SUBSTR"), null, originalNode.getParserPosition(), null);
        hiveFunction = functionResolver.tryResolve("SUBSTR", ctx.hiveTable.orElse(null), sqlOperands.size() - 1);
        return hiveFunction.createCall(substrNode, sqlOperands.subList(1, sqlOperands.size()), quantifier);
    }
    return hiveFunction.createCall(sqlOperands.get(0), sqlOperands.subList(1, sqlOperands.size()), quantifier);
}
Also used : VersionedSqlUserDefinedFunction(com.linkedin.coral.hive.hive2rel.functions.VersionedSqlUserDefinedFunction) Function(com.linkedin.coral.common.functions.Function) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlNode(org.apache.calcite.sql.SqlNode) Node(com.linkedin.coral.hive.hive2rel.parsetree.parser.Node) ASTNode(com.linkedin.coral.hive.hive2rel.parsetree.parser.ASTNode) ASTNode(com.linkedin.coral.hive.hive2rel.parsetree.parser.ASTNode) SqlWindow(org.apache.calcite.sql.SqlWindow) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 10 with SqlBasicCall

use of org.apache.calcite.sql.SqlBasicCall in project coral by linkedin.

the class SparkRelToSparkSqlConverter method isCorrelateRightChildOuter.

/**
 * Calcite's RelNode doesn't support 'OUTER' lateral views, Source: https://calcite.apache.org/docs/reference.html
 * The way Coral deals with this is by creating an IF function which will simulate an outer lateral view.
 *
 * For Example:
 *  LATERAL VIEW OUTER explode(complex.c)
 *          will be translated to
 *  LATERAL VIEW explode(if(complex.c IS NOT NULL AND size(complex.c) > 0, complex.c, ARRAY (NULL)))
 *
 * Spark needs an explicit 'OUTER' keyword for it to consider empty arrays,
 * therefore this function helps in finding out whether OUTER keyword is needed.
 *
 * This functions checks if a SqlNode
 *    - has 'if' child
 *    - has ARRAY(NULL) as the else result
 */
private boolean isCorrelateRightChildOuter(SqlNode rightChild) {
    if (rightChild instanceof SqlBasicCall && rightChild.getKind() == SqlKind.AS) {
        SqlBasicCall rightCall = (SqlBasicCall) rightChild;
        SqlNode unnestNode = getOrDefault(rightCall.getOperandList(), 0, null);
        if (unnestNode instanceof SqlBasicCall && unnestNode.getKind() == SqlKind.UNNEST) {
            SqlBasicCall unnestCall = (SqlBasicCall) unnestNode;
            SqlNode ifNode = getOrDefault(unnestCall.getOperandList(), 0, null);
            if (ifNode instanceof SqlBasicCall) {
                SqlBasicCall ifCall = (SqlBasicCall) ifNode;
                if (ifCall.getOperator().getName().equals("if") && ifCall.operandCount() == 3) {
                    SqlNode arrayOrMapNode = getOrDefault(ifCall.getOperandList(), 2, null);
                    if (arrayOrMapNode instanceof SqlBasicCall) {
                        SqlBasicCall arrayOrMapCall = (SqlBasicCall) arrayOrMapNode;
                        if (arrayOrMapCall.getOperator() instanceof SqlMultisetValueConstructor && arrayOrMapCall.getOperandList().get(0) instanceof SqlLiteral) {
                            SqlLiteral sqlLiteral = (SqlLiteral) arrayOrMapCall.getOperandList().get(0);
                            return sqlLiteral.getTypeName().toString().equals("NULL");
                        }
                    }
                }
            }
        }
    }
    return false;
}
Also used : SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlMultisetValueConstructor(org.apache.calcite.sql.fun.SqlMultisetValueConstructor) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)67 SqlNode (org.apache.calcite.sql.SqlNode)50 SqlOperator (org.apache.calcite.sql.SqlOperator)27 ArrayList (java.util.ArrayList)26 SqlCall (org.apache.calcite.sql.SqlCall)25 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)20 RelDataType (org.apache.calcite.rel.type.RelDataType)19 SqlNodeList (org.apache.calcite.sql.SqlNodeList)17 SqlLiteral (org.apache.calcite.sql.SqlLiteral)14 SqlSelect (org.apache.calcite.sql.SqlSelect)13 SqlKind (org.apache.calcite.sql.SqlKind)12 RexNode (org.apache.calcite.rex.RexNode)8 SqlJoin (org.apache.calcite.sql.SqlJoin)8 SqlWindowTableFunction (org.apache.calcite.sql.SqlWindowTableFunction)8 IdentityHashMap (java.util.IdentityHashMap)6 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)6 SqlWith (org.apache.calcite.sql.SqlWith)6 ImmutableList (com.google.common.collect.ImmutableList)5 HashMap (java.util.HashMap)5 List (java.util.List)5