Search in sources :

Example 11 with SqlBasicCall

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

the class SparkRelToSparkSqlConverter method visit.

@Override
public Result visit(Join e) {
    Result result = super.visit(e);
    SqlJoin joinNode = (SqlJoin) (result.node);
    SqlNode rightNode = joinNode.getRight();
    // If so, let's replace the JOIN node with SqlLateralJoin node, which unparses as "LATERAL VIEW".
    if (rightNode instanceof SqlBasicCall && ((SqlBasicCall) rightNode).getOperator() instanceof SqlLateralViewAsOperator) {
        SqlLateralJoin join = new SqlLateralJoin(POS, joinNode.getLeft(), SqlLiteral.createBoolean(false, POS), JoinType.COMMA.symbol(POS), joinNode.getRight(), JoinConditionType.NONE.symbol(POS), joinNode.getCondition(), isCorrelateRightChildOuter(joinNode.getRight()));
        result = new Result(join, Expressions.list(Clause.FROM), null, null, result.aliases);
    }
    return result;
}
Also used : SqlLateralJoin(com.linkedin.coral.spark.functions.SqlLateralJoin) SqlLateralViewAsOperator(com.linkedin.coral.spark.functions.SqlLateralViewAsOperator) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlJoin(org.apache.calcite.sql.SqlJoin) SqlNode(org.apache.calcite.sql.SqlNode)

Example 12 with SqlBasicCall

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

the class ParseTreeBuilder method visitCTE.

@Override
protected SqlNode visitCTE(ASTNode node, ParseContext ctx) {
    // ASTNode tree from Hive Antlr
    // TOK_QUERY
    // - TOK_FROM
    // - TOK_INSERT
    // -- TOK_DESTINATION
    // -- TOK_SELECT
    // - TOK_CTE       <-- processed by this method visitCTE
    // -- TOK_SUBQUERY
    // --- TOK_QUERY
    // --- LITERAL (alias of the subquery)
    // -- TOK_SUBQUERY
    // --- TOK_QUERY
    // --- LITERAL (alias of the subquery)
    // SqlNode tree expected by Calcite
    // - SqlWith
    // -- withList: SqlNodeList  <-- returned by this method visitCTE
    // --- element: SqlWithItem
    // ---- id: SimpleIdentifier
    // ---- columnList: SqlNodeList (column aliases - not supported by Hive)
    // ---- definition: SqlSelect
    // -- node: SqlSelect
    ArrayList<Node> children = node.getChildren();
    checkState(children != null && !children.isEmpty());
    // First, visit the children to capture all their translation result (in List<SqlNode>)
    // All children are expected to be translated into SqlBasicCall(definition, alias) by visitSubquery
    /**
     * See {@link #visitSubquery(ASTNode, ParseContext) visitSubquery} for details
     */
    List<SqlNode> sqlNodeList = visitChildren(node, ctx);
    // Second, translate the list of SqlBasicCall to list of SqlWithItem
    List<SqlWithItem> withItemList = new ArrayList<>();
    for (SqlNode sqlNode : sqlNodeList) {
        SqlBasicCall call = (SqlBasicCall) sqlNode;
        SqlNode definition = call.getOperandList().get(0);
        SqlNode alias = call.getOperandList().get(1);
        SqlWithItem withItem = new SqlWithItem(ZERO, (SqlIdentifier) alias, null, definition);
        withItemList.add(withItem);
    }
    // Return a SqlNodeList with the contents of the withItemList
    SqlNodeList result = new SqlNodeList(withItemList, ZERO);
    return result;
}
Also used : SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlWithItem(org.apache.calcite.sql.SqlWithItem) 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) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 13 with SqlBasicCall

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

the class RelToSqlConverter method visit.

public Result visit(TableFunctionScan e) {
    final List<SqlNode> inputSqlNodes = new ArrayList<>();
    final int inputSize = e.getInputs().size();
    for (int i = 0; i < inputSize; i++) {
        final Result x = visitInput(e, i);
        inputSqlNodes.add(x.asStatement());
    }
    final Context context = tableFunctionScanContext(inputSqlNodes);
    SqlNode callNode = context.toSql(null, e.getCall());
    // Convert to table function call, "TABLE($function_name(xxx))"
    SqlNode tableCall = new SqlBasicCall(SqlStdOperatorTable.COLLECTION_TABLE, ImmutableList.of(callNode), SqlParserPos.ZERO);
    SqlNode select = new SqlSelect(SqlParserPos.ZERO, null, SqlNodeList.SINGLETON_STAR, tableCall, null, null, null, null, null, null, null, SqlNodeList.EMPTY);
    return result(select, ImmutableList.of(Clause.SELECT), e, null);
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) ArrayList(java.util.ArrayList) RelHint(org.apache.calcite.rel.hint.RelHint) SqlHint(org.apache.calcite.sql.SqlHint) SqlNode(org.apache.calcite.sql.SqlNode)

Example 14 with SqlBasicCall

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

the class RexSqlStandardConvertletTable method convertCall.

// ~ Methods ----------------------------------------------------------------
/**
 * Converts a call to an operator into a {@link SqlCall} to the same
 * operator.
 *
 * <p>Called automatically via reflection.
 *
 * @param converter Converter
 * @param call      Call
 * @return Sql call
 */
@Nullable
public SqlNode convertCall(RexToSqlNodeConverter converter, RexCall call) {
    if (get(call) == null) {
        return null;
    }
    final SqlOperator op = call.getOperator();
    final List<RexNode> operands = call.getOperands();
    @Nullable final List<@Nullable SqlNode> exprs = convertExpressionList(converter, operands);
    if (exprs == null) {
        return null;
    }
    return new SqlBasicCall(op, exprs, SqlParserPos.ZERO);
}
Also used : SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlOperator(org.apache.calcite.sql.SqlOperator) Nullable(org.checkerframework.checker.nullness.qual.Nullable) SqlNode(org.apache.calcite.sql.SqlNode) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 15 with SqlBasicCall

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

the class SqlValidatorImpl method deriveConstructorType.

@Override
public RelDataType deriveConstructorType(SqlValidatorScope scope, SqlCall call, SqlFunction unresolvedConstructor, @Nullable SqlFunction resolvedConstructor, List<RelDataType> argTypes) {
    SqlIdentifier sqlIdentifier = unresolvedConstructor.getSqlIdentifier();
    assert sqlIdentifier != null;
    RelDataType type = catalogReader.getNamedType(sqlIdentifier);
    if (type == null) {
        // TODO jvs 12-Feb-2005:  proper type name formatting
        throw newValidationError(sqlIdentifier, RESOURCE.unknownDatatypeName(sqlIdentifier.toString()));
    }
    if (resolvedConstructor == null) {
        if (call.operandCount() > 0) {
            // no user-defined constructor could be found
            throw handleUnresolvedFunction(call, unresolvedConstructor, argTypes, null);
        }
    } else {
        SqlCall testCall = resolvedConstructor.createCall(call.getParserPosition(), call.getOperandList());
        RelDataType returnType = resolvedConstructor.validateOperands(this, scope, testCall);
        assert type == returnType;
    }
    if (config.identifierExpansion()) {
        if (resolvedConstructor != null) {
            ((SqlBasicCall) call).setOperator(resolvedConstructor);
        } else {
            // fake a fully-qualified call to the default constructor
            ((SqlBasicCall) call).setOperator(new SqlFunction(requireNonNull(type.getSqlIdentifier(), () -> "sqlIdentifier of " + type), ReturnTypes.explicit(type), null, null, null, SqlFunctionCategory.USER_DEFINED_CONSTRUCTOR));
        }
    }
    return type;
}
Also used : SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlCall(org.apache.calcite.sql.SqlCall) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlFunction(org.apache.calcite.sql.SqlFunction)

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