private SqlNode convertSqlCall(SqlCall sqlCall) { SqlOperator operator = sqlCall.getOperator(); if (operator != null) { Pair<SqlNode, SqlNode> matched = convMaster.matchSqlFunc(sqlCall); if (matched != null) { Preconditions.checkState(matched.getFirst() instanceof SqlCall); SqlCall sourceTmpl = (SqlCall) matched.getFirst(); Preconditions.checkState(sourceTmpl.operandCount() == sqlCall.operandCount()); SqlNode targetTmpl = matched.getSecond(); boolean isWindowCall = sourceTmpl.getOperator() instanceof SqlOverOperator; SqlParamsFinder sqlParamsFinder = SqlParamsFinder.newInstance(sourceTmpl, sqlCall, isWindowCall); return targetTmpl.accept(new SqlFuncFiller(sqlParamsFinder.getParamNodes(), isWindowCall)); } } return null; }
@Test public void testWindowCallParams() throws SqlParseException { SqlParser sqlParser1 = SqlParser.create("STDDEV_POP($0) OVER($1)"); SqlNode sqlPattern = sqlParser1.parseExpression(); SqlParser sqlParser2 = SqlParser.create("STDDEV_POP(C1) OVER (ORDER BY C1)"); SqlNode sqlCall = sqlParser2.parseExpression(); SqlParamsFinder sqlParamsFinder = SqlParamsFinder.newInstance((SqlCall)sqlPattern, (SqlCall)sqlCall, true); Map<Integer, SqlNode> paramNodes = sqlParamsFinder.getParamNodes(); Assert.assertEquals("C1", paramNodes.get(0).toString()); Assert.assertEquals("(ORDER BY `C1`)", paramNodes.get(1).toString()); Assert.assertTrue(paramNodes.get(1) instanceof SqlWindow); } }
@Test public void testParamFinder() throws SqlParseException { SqlParser sqlParser1 = SqlParser.create("POWER($0, $1) + AVG(LN($3)) + EXP($5)"); SqlNode sqlPattern = sqlParser1.parseExpression(); SqlParser sqlParser2 = SqlParser.create("POWER(3, POWER(2, POWER(2, 3))) + AVG(LN(EXP(4))) + EXP(CAST('2018-03-22' AS DATE))"); SqlNode sqlCall = sqlParser2.parseExpression(); SqlParamsFinder sqlParamsFinder = new SqlParamsFinder((SqlCall)sqlPattern, (SqlCall)sqlCall); Map<Integer, SqlNode> paramNodes = sqlParamsFinder.getParamNodes(); Assert.assertEquals("3", paramNodes.get(0).toString()); Assert.assertEquals("POWER(2, POWER(2, 3))", paramNodes.get(1).toString()); Assert.assertEquals("EXP(4)", paramNodes.get(3).toString()); Assert.assertEquals("CAST('2018-03-22' AS DATE)", paramNodes.get(5).toString()); }
private SqlNode convertSqlCall(SqlCall sqlCall) { SqlOperator operator = sqlCall.getOperator(); if (operator != null) { Pair<SqlNode, SqlNode> matched = convMaster.matchSqlFunc(sqlCall); if (matched != null) { Preconditions.checkState(matched.getFirst() instanceof SqlCall); SqlCall sourceTmpl = (SqlCall) matched.getFirst(); Preconditions.checkState(sourceTmpl.operandCount() == sqlCall.operandCount()); SqlNode targetTmpl = matched.getSecond(); boolean isWindowCall = sourceTmpl.getOperator() instanceof SqlOverOperator; SqlParamsFinder sqlParamsFinder = SqlParamsFinder.newInstance(sourceTmpl, sqlCall, isWindowCall); return targetTmpl.accept(new SqlFuncFiller(sqlParamsFinder.getParamNodes(), isWindowCall)); } } return null; }