All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.espertech.esper.compiler.internal.parse.ASTIndexHelper Maven / Gradle / Ivy

There is a newer version: 9.0.0
Show newest version
/*
 ***************************************************************************************
 *  Copyright (C) 2006 EsperTech, Inc. All rights reserved.                            *
 *  http://www.espertech.com/esper                                                     *
 *  http://www.espertech.com                                                           *
 *  ---------------------------------------------------------------------------------- *
 *  The software in this package is published under the terms of the GPL license       *
 *  a copy of which has been included with this distribution in the license.txt file.  *
 ***************************************************************************************
 */
package com.espertech.esper.compiler.internal.parse;

import com.espertech.esper.common.internal.compile.stage1.spec.CreateIndexDesc;
import com.espertech.esper.common.internal.compile.stage1.spec.CreateIndexItem;
import com.espertech.esper.common.internal.compile.stage1.spec.CreateIndexType;
import com.espertech.esper.common.internal.epl.expression.core.ExprNode;
import com.espertech.esper.compiler.internal.generated.EsperEPL2GrammarParser;
import org.antlr.v4.runtime.tree.Tree;

import java.util.*;

public class ASTIndexHelper {
    public static CreateIndexDesc walk(EsperEPL2GrammarParser.CreateIndexExprContext ctx, Map astExprNodeMap) {
        String indexName = ctx.n.getText();
        String windowName = ctx.w.getText();

        boolean unique = false;
        if (ctx.u != null) {
            String ident = ctx.u.getText();
            if (ident.toLowerCase(Locale.ENGLISH).trim().equals("unique")) {
                unique = true;
            } else {
                throw ASTWalkException.from("Invalid keyword '" + ident + "' in create-index encountered, expected 'unique'");
            }
        }

        List columns = new ArrayList<>();
        List cols = ctx.createIndexColumnList().createIndexColumn();
        for (EsperEPL2GrammarParser.CreateIndexColumnContext col : cols) {
            CreateIndexItem item = walk(col, astExprNodeMap);
            columns.add(item);
        }
        return new CreateIndexDesc(unique, indexName, windowName, columns);
    }

    private static CreateIndexItem walk(EsperEPL2GrammarParser.CreateIndexColumnContext col, Map astExprNodeMap) {
        List expressions = Collections.emptyList();
        if (col.i != null) {
            expressions = ASTExprHelper.exprCollectSubNodes(col.i, 0, astExprNodeMap);
        } else if (col.expression() != null) {
            expressions = ASTExprHelper.exprCollectSubNodes(col.expression(), 0, astExprNodeMap);
        }

        String type = CreateIndexType.HASH.getNameLower();
        if (col.t != null) {
            type = col.t.getText();
        }

        List parameters = Collections.emptyList();
        if (col.p != null) {
            parameters = ASTExprHelper.exprCollectSubNodes(col.p, 0, astExprNodeMap);
        }
        return new CreateIndexItem(expressions, type, parameters);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy