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());
}
}
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;
}
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);
}
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);
}
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;
}
Aggregations