Search in sources :

Example 1 with SQLExternalRecordFormat

use of com.alibaba.druid.sql.ast.statement.SQLExternalRecordFormat in project druid by alibaba.

the class OdpsExprParser method primaryRest.

public SQLExpr primaryRest(SQLExpr expr) {
    if (lexer.token() == Token.COLON) {
        lexer.nextToken();
        if (lexer.token() == Token.LITERAL_INT && expr instanceof SQLPropertyExpr) {
            SQLPropertyExpr propertyExpr = (SQLPropertyExpr) expr;
            Number integerValue = lexer.integerValue();
            lexer.nextToken();
            propertyExpr.setName(propertyExpr.getName() + ':' + integerValue.intValue());
            return propertyExpr;
        }
        expr = dotRest(expr);
        return expr;
    }
    if (lexer.token() == Token.LBRACKET) {
        SQLArrayExpr array = new SQLArrayExpr();
        array.setExpr(expr);
        lexer.nextToken();
        this.exprList(array.getValues(), array);
        accept(Token.RBRACKET);
        return primaryRest(array);
    } else if ((lexer.token() == Token.LITERAL_CHARS || lexer.token() == Token.LITERAL_ALIAS) && expr instanceof SQLCharExpr) {
        SQLCharExpr charExpr = new SQLCharExpr(lexer.stringVal());
        lexer.nextTokenValue();
        SQLMethodInvokeExpr concat = new SQLMethodInvokeExpr("concat", null, expr, charExpr);
        while (lexer.token() == Token.LITERAL_CHARS || lexer.token() == Token.LITERAL_ALIAS) {
            charExpr = new SQLCharExpr(lexer.stringVal());
            lexer.nextToken();
            concat.addArgument(charExpr);
        }
        expr = concat;
    }
    if (lexer.token() == Token.LPAREN && expr instanceof SQLIdentifierExpr && ((SQLIdentifierExpr) expr).nameHashCode64() == FnvHash.Constants.TRANSFORM) {
        OdpsTransformExpr transformExpr = new OdpsTransformExpr();
        lexer.nextToken();
        this.exprList(transformExpr.getInputColumns(), transformExpr);
        accept(Token.RPAREN);
        if (lexer.identifierEquals(FnvHash.Constants.ROW)) {
            SQLExternalRecordFormat recordFormat = this.parseRowFormat();
            transformExpr.setInputRowFormat(recordFormat);
        }
        if (lexer.token() == Token.USING || lexer.identifierEquals(FnvHash.Constants.USING)) {
            lexer.nextToken();
            transformExpr.setUsing(this.expr());
        }
        if (lexer.identifierEquals(FnvHash.Constants.RESOURCES)) {
            lexer.nextToken();
            this.exprList(transformExpr.getResources(), transformExpr);
        }
        if (lexer.token() == Token.AS) {
            lexer.nextToken();
            List<SQLColumnDefinition> outputColumns = transformExpr.getOutputColumns();
            if (lexer.token() == Token.LPAREN) {
                lexer.nextToken();
                for (; ; ) {
                    SQLColumnDefinition column = this.parseColumn();
                    outputColumns.add(column);
                    if (lexer.token() == Token.COMMA) {
                        lexer.nextToken();
                        continue;
                    }
                    break;
                }
                accept(Token.RPAREN);
            } else {
                SQLColumnDefinition column = new SQLColumnDefinition();
                column.setName(this.name());
                outputColumns.add(column);
            }
        }
        if (lexer.identifierEquals(FnvHash.Constants.ROW)) {
            SQLExternalRecordFormat recordFormat = this.parseRowFormat();
            transformExpr.setOutputRowFormat(recordFormat);
        }
        return transformExpr;
    }
    if (expr instanceof SQLIdentifierExpr && ((SQLIdentifierExpr) expr).nameHashCode64() == FnvHash.Constants.NEW) {
        SQLIdentifierExpr ident = (SQLIdentifierExpr) expr;
        OdpsNewExpr newExpr = new OdpsNewExpr();
        if (lexer.token() == Token.IDENTIFIER) {
            // .GSON
            Lexer.SavePoint mark = lexer.mark();
            String methodName = lexer.stringVal();
            lexer.nextToken();
            switch(lexer.token()) {
                case ON:
                case WHERE:
                case GROUP:
                case ORDER:
                case INNER:
                case JOIN:
                case FULL:
                case OUTER:
                case LEFT:
                case RIGHT:
                case LATERAL:
                case FROM:
                case COMMA:
                case RPAREN:
                    return ident;
                default:
                    break;
            }
            while (lexer.token() == Token.DOT) {
                lexer.nextToken();
                methodName += '.' + lexer.stringVal();
                lexer.nextToken();
            }
            newExpr.setMethodName(methodName);
            if (lexer.token() == Token.LT) {
                lexer.nextToken();
                for (; ; ) {
                    if (lexer.token() == Token.GT) {
                        break;
                    }
                    SQLDataType paramType = this.parseDataType(false);
                    paramType.setParent(newExpr);
                    newExpr.getTypeParameters().add(paramType);
                    if (lexer.token() == Token.COMMA) {
                        lexer.nextToken();
                        continue;
                    }
                    break;
                }
                accept(Token.GT);
            }
            if (lexer.token() == Token.LBRACKET) {
                lexer.nextToken();
                this.exprList(newExpr.getArguments(), newExpr);
                accept(Token.RBRACKET);
                if (lexer.token() == Token.LBRACKET) {
                    lexer.nextToken();
                    accept(Token.RBRACKET);
                }
                newExpr.setArray(true);
                if (lexer.token() == Token.LBRACE) {
                    lexer.nextToken();
                    for (; ; ) {
                        if (lexer.token() == Token.RPAREN) {
                            break;
                        }
                        SQLExpr item = this.expr();
                        newExpr.getInitValues().add(item);
                        item.setParent(newExpr);
                        if (lexer.token() == Token.COMMA) {
                            lexer.nextToken();
                            continue;
                        }
                        break;
                    }
                    accept(Token.RBRACE);
                }
                if (lexer.token() == Token.LBRACKET) {
                    expr = primaryRest(newExpr);
                } else {
                    expr = newExpr;
                }
            } else {
                accept(Token.LPAREN);
                this.exprList(newExpr.getArguments(), newExpr);
                accept(Token.RPAREN);
                expr = newExpr;
            }
        } else if (lexer.identifierEquals("java") || lexer.identifierEquals("com")) {
            SQLName name = this.name();
            String strName = ident.getName() + ' ' + name.toString();
            if (lexer.token() == Token.LT) {
                lexer.nextToken();
                for (int i = 0; lexer.token() != Token.GT; i++) {
                    if (i != 0) {
                        strName += ", ";
                    }
                    SQLName arg = this.name();
                    strName += arg.toString();
                }
                lexer.nextToken();
            }
            ident.setName(strName);
        }
    }
    if (expr == null) {
        return null;
    }
    return super.primaryRest(expr);
}
Also used : OdpsTransformExpr(com.alibaba.druid.sql.dialect.odps.ast.OdpsTransformExpr) SQLExternalRecordFormat(com.alibaba.druid.sql.ast.statement.SQLExternalRecordFormat) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) OdpsNewExpr(com.alibaba.druid.sql.dialect.odps.ast.OdpsNewExpr)

Example 2 with SQLExternalRecordFormat

use of com.alibaba.druid.sql.ast.statement.SQLExternalRecordFormat in project druid by alibaba.

the class AntsparkExprParser method parseRowFormat.

public SQLExternalRecordFormat parseRowFormat() {
    lexer.nextToken();
    acceptIdentifier("FORMAT");
    if (lexer.identifierEquals(FnvHash.Constants.DELIMITED)) {
        lexer.nextToken();
    }
    SQLExternalRecordFormat format = new SQLExternalRecordFormat();
    if (lexer.identifierEquals(FnvHash.Constants.FIELDS)) {
        lexer.nextToken();
        acceptIdentifier("TERMINATED");
        accept(Token.BY);
        format.setTerminatedBy(this.expr());
    } else if (lexer.identifierEquals("FIELD")) {
        throw new ParserException("syntax error, expect FIELDS, " + lexer.info());
    }
    if (lexer.identifierEquals(FnvHash.Constants.LINES)) {
        lexer.nextToken();
        acceptIdentifier("TERMINATED");
        accept(Token.BY);
        format.setLinesTerminatedBy(this.expr());
    }
    if (lexer.token() == Token.ESCAPE || lexer.identifierEquals(FnvHash.Constants.ESCAPED)) {
        lexer.nextToken();
        accept(Token.BY);
        format.setEscapedBy(this.expr());
    }
    if (lexer.identifierEquals(FnvHash.Constants.COLLECTION)) {
        lexer.nextToken();
        acceptIdentifier("ITEMS");
        acceptIdentifier("TERMINATED");
        accept(Token.BY);
        format.setCollectionItemsTerminatedBy(this.expr());
    }
    if (lexer.identifierEquals(FnvHash.Constants.MAP)) {
        lexer.nextToken();
        acceptIdentifier("KEYS");
        acceptIdentifier("TERMINATED");
        accept(Token.BY);
        format.setMapKeysTerminatedBy(this.expr());
    }
    if (lexer.identifierEquals(FnvHash.Constants.SERDE)) {
        lexer.nextToken();
        format.setSerde(this.expr());
    }
    return format;
}
Also used : SQLExternalRecordFormat(com.alibaba.druid.sql.ast.statement.SQLExternalRecordFormat)

Example 3 with SQLExternalRecordFormat

use of com.alibaba.druid.sql.ast.statement.SQLExternalRecordFormat in project druid by alibaba.

the class OracleCreateTableParser method parseOrganization.

private void parseOrganization(OracleCreateTableStatement stmt) {
    OracleCreateTableStatement.Organization organization = new OracleCreateTableStatement.Organization();
    acceptIdentifier("ORGANIZATION");
    if (lexer.token() == Token.INDEX) {
        lexer.nextToken();
        organization.setType("INDEX");
        this.getExprParser().parseSegmentAttributes(organization);
        // index_org_table_clause http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_7002.htm#i2129638
        if (lexer.identifierEquals(FnvHash.Constants.PCTTHRESHOLD)) {
            lexer.nextToken();
            if (lexer.token() == Token.LITERAL_INT) {
                int pctthreshold = ((SQLNumericLiteralExpr) this.exprParser.primary()).getNumber().intValue();
                organization.setPctthreshold(pctthreshold);
            }
        }
    } else if (lexer.identifierEquals("HEAP")) {
        lexer.nextToken();
        organization.setType("HEAP");
        this.getExprParser().parseSegmentAttributes(organization);
    } else if (lexer.identifierEquals("EXTERNAL")) {
        lexer.nextToken();
        organization.setType("EXTERNAL");
        accept(Token.LPAREN);
        if (lexer.identifierEquals("TYPE")) {
            lexer.nextToken();
            organization.setExternalType(this.exprParser.name());
        }
        accept(Token.DEFAULT);
        acceptIdentifier("DIRECTORY");
        organization.setExternalDirectory(this.exprParser.expr());
        if (lexer.identifierEquals("ACCESS")) {
            lexer.nextToken();
            acceptIdentifier("PARAMETERS");
            if (lexer.token() == Token.LPAREN) {
                lexer.nextToken();
                SQLExternalRecordFormat recordFormat = new SQLExternalRecordFormat();
                if (lexer.identifierEquals("RECORDS")) {
                    lexer.nextToken();
                    if (lexer.identifierEquals("DELIMITED")) {
                        lexer.nextToken();
                        accept(Token.BY);
                        if (lexer.identifierEquals("NEWLINE")) {
                            lexer.nextToken();
                            recordFormat.setDelimitedBy(new SQLIdentifierExpr("NEWLINE"));
                        } else {
                            throw new ParserException("TODO " + lexer.info());
                        }
                        if (lexer.identifierEquals(FnvHash.Constants.NOLOGFILE)) {
                            lexer.nextToken();
                            recordFormat.setLogfile(false);
                        }
                        if (lexer.identifierEquals(FnvHash.Constants.NOBADFILE)) {
                            lexer.nextToken();
                            recordFormat.setBadfile(false);
                        }
                    } else {
                        throw new ParserException("TODO " + lexer.info());
                    }
                }
                if (lexer.identifierEquals(FnvHash.Constants.FIELDS)) {
                    lexer.nextToken();
                    if (lexer.identifierEquals(FnvHash.Constants.TERMINATED)) {
                        lexer.nextToken();
                        accept(Token.BY);
                        recordFormat.setTerminatedBy(this.exprParser.primary());
                    } else {
                        throw new ParserException("TODO " + lexer.info());
                    }
                    if (lexer.identifierEquals(FnvHash.Constants.LTRIM)) {
                        lexer.nextToken();
                        recordFormat.setLtrim(true);
                    }
                }
                if (lexer.identifierEquals(FnvHash.Constants.MISSING)) {
                    lexer.nextToken();
                    acceptIdentifier("FIELD");
                    accept(Token.VALUES);
                    acceptIdentifier("ARE");
                    accept(Token.NULL);
                    recordFormat.setMissingFieldValuesAreNull(true);
                }
                if (lexer.token() == Token.REJECT) {
                    lexer.nextToken();
                    acceptIdentifier("ROWS");
                    accept(Token.WITH);
                    accept(Token.ALL);
                    accept(Token.NULL);
                    acceptIdentifier("FIELDS");
                    recordFormat.setRejectRowsWithAllNullFields(true);
                }
                organization.setExternalDirectoryRecordFormat(recordFormat);
                accept(Token.RPAREN);
            } else if (lexer.token() == Token.USING) {
                lexer.nextToken();
                acceptIdentifier("CLOB");
                throw new ParserException("TODO " + lexer.info());
            }
        }
        acceptIdentifier("LOCATION");
        accept(Token.LPAREN);
        this.exprParser.exprList(organization.getExternalDirectoryLocation(), organization);
        accept(Token.RPAREN);
        accept(Token.RPAREN);
        if (lexer.token() == Token.REJECT) {
            lexer.nextToken();
            accept(Token.LIMIT);
            organization.setExternalRejectLimit(this.exprParser.primary());
        }
    // 
    } else {
        throw new ParserException("TODO " + lexer.info());
    }
    stmt.setOrganization(organization);
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLExternalRecordFormat(com.alibaba.druid.sql.ast.statement.SQLExternalRecordFormat) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)

Aggregations

SQLExternalRecordFormat (com.alibaba.druid.sql.ast.statement.SQLExternalRecordFormat)3 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)1 OdpsNewExpr (com.alibaba.druid.sql.dialect.odps.ast.OdpsNewExpr)1 OdpsTransformExpr (com.alibaba.druid.sql.dialect.odps.ast.OdpsTransformExpr)1 ParserException (com.alibaba.druid.sql.parser.ParserException)1