use of org.apache.calcite.sql.SqlNode in project drill by apache.
the class UnsupportedOperatorsVisitor method detectMultiplePartitions.
/**
* Disable multiple partitions in a SELECT-CLAUSE
* If multiple partitions are defined in the query,
* SqlUnsupportedException would be thrown to inform
* @param sqlSelect SELECT-CLAUSE in the query
*/
private void detectMultiplePartitions(SqlSelect sqlSelect) {
for (SqlNode nodeInSelectList : sqlSelect.getSelectList()) {
// enter the first operand of AS operator
if (nodeInSelectList.getKind() == SqlKind.AS && (((SqlCall) nodeInSelectList).getOperandList().get(0).getKind() == SqlKind.OVER)) {
nodeInSelectList = ((SqlCall) nodeInSelectList).getOperandList().get(0);
}
if (nodeInSelectList.getKind() != SqlKind.OVER) {
continue;
}
// This is used to keep track of the window function which has been defined
SqlNode definedWindow = null;
SqlNode window = ((SqlCall) nodeInSelectList).operand(1);
// which is defined in the window list
if (window instanceof SqlIdentifier) {
// Expand the SqlIdentifier as the expression defined in the window list
for (SqlNode sqlNode : sqlSelect.getWindowList()) {
if (((SqlWindow) sqlNode).getDeclName().equalsDeep(window, false)) {
window = sqlNode;
break;
}
}
assert !(window instanceof SqlIdentifier) : "Identifier should have been expanded as a window defined in the window list";
}
// In a SELECT-SCOPE, only a partition can be defined
if (definedWindow == null) {
definedWindow = window;
} else {
if (!definedWindow.equalsDeep(window, false)) {
unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, "Multiple window definitions in a single SELECT list is not currently supported \n" + "See Apache Drill JIRA: DRILL-3196");
throw new UnsupportedOperationException();
}
}
}
}
use of org.apache.calcite.sql.SqlNode in project drill by apache.
the class DrillCompoundIdentifier method getAsSqlNode.
public SqlNode getAsSqlNode() {
if (ids.size() == 1) {
return new SqlIdentifier(Collections.singletonList(ids.get(0).value), ids.get(0).parserPos);
}
int startIndex;
SqlNode node;
if (ids.get(1).isArray()) {
// handle everything post zero index as item operator.
startIndex = 1;
node = new //
SqlIdentifier(//
ImmutableList.of(ids.get(0).value), //
null, //
ids.get(0).parserPos, ImmutableList.of(ids.get(0).parserPos));
} else {
// handle everything post two index as item operator.
startIndex = 2;
node = new //
SqlIdentifier(//
ImmutableList.of(ids.get(0).value, ids.get(1).value), //
null, //
ids.get(0).parserPos, ImmutableList.of(ids.get(0).parserPos, ids.get(1).parserPos));
}
for (int i = startIndex; i < ids.size(); i++) {
node = ids.get(i).getNode(node);
}
return node;
}
use of org.apache.calcite.sql.SqlNode in project drill by apache.
the class DrillAvgVarianceConvertlet method convertCall.
public RexNode convertCall(SqlRexContext cx, SqlCall call) {
assert call.operandCount() == 1;
final SqlNode arg = call.operand(0);
final SqlNode expr;
switch(subtype) {
case AVG:
expr = expandAvg(arg);
break;
case STDDEV_POP:
expr = expandVariance(arg, true, true);
break;
case STDDEV_SAMP:
expr = expandVariance(arg, false, true);
break;
case VAR_POP:
expr = expandVariance(arg, true, false);
break;
case VAR_SAMP:
expr = expandVariance(arg, false, false);
break;
default:
throw Util.unexpected(subtype);
}
return cx.convertExpression(expr);
}
use of org.apache.calcite.sql.SqlNode in project drill by apache.
the class DrillAvgVarianceConvertlet method expandVariance.
private SqlNode expandVariance(final SqlNode arg, boolean biased, boolean sqrt) {
/* stddev_pop(x) ==>
* power(
* (sum(x * x) - sum(x) * sum(x) / count(x))
* / count(x),
* .5)
* stddev_samp(x) ==>
* power(
* (sum(x * x) - sum(x) * sum(x) / count(x))
* / (count(x) - 1),
* .5)
* var_pop(x) ==>
* (sum(x * x) - sum(x) * sum(x) / count(x))
* / count(x)
* var_samp(x) ==>
* (sum(x * x) - sum(x) * sum(x) / count(x))
* / (count(x) - 1)
*/
final SqlParserPos pos = SqlParserPos.ZERO;
// cast the argument to double
final SqlNode castHighArg = CastHighOp.createCall(pos, arg);
final SqlNode argSquared = SqlStdOperatorTable.MULTIPLY.createCall(pos, castHighArg, castHighArg);
final SqlNode sumArgSquared = SqlStdOperatorTable.SUM.createCall(pos, argSquared);
final SqlNode sum = SqlStdOperatorTable.SUM.createCall(pos, castHighArg);
final SqlNode sumSquared = SqlStdOperatorTable.MULTIPLY.createCall(pos, sum, sum);
final SqlNode count = SqlStdOperatorTable.COUNT.createCall(pos, castHighArg);
final SqlNode avgSumSquared = SqlStdOperatorTable.DIVIDE.createCall(pos, sumSquared, count);
final SqlNode diff = SqlStdOperatorTable.MINUS.createCall(pos, sumArgSquared, avgSumSquared);
final SqlNode denominator;
if (biased) {
denominator = count;
} else {
final SqlNumericLiteral one = SqlLiteral.createExactNumeric("1", pos);
denominator = SqlStdOperatorTable.MINUS.createCall(pos, count, one);
}
final SqlNode diffAsDouble = CastHighOp.createCall(pos, diff);
final SqlNode div = SqlStdOperatorTable.DIVIDE.createCall(pos, diffAsDouble, denominator);
SqlNode result = div;
if (sqrt) {
final SqlNumericLiteral half = SqlLiteral.createExactNumeric("0.5", pos);
result = SqlStdOperatorTable.POWER.createCall(pos, div, half);
}
return result;
}
use of org.apache.calcite.sql.SqlNode in project storm by apache.
the class TestCompilerUtils method sqlOverNestedTable.
public static CalciteState sqlOverNestedTable(String sql) throws RelConversionException, ValidationException, SqlParseException {
SchemaPlus schema = Frameworks.createRootSchema(true);
JavaTypeFactory typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
StreamableTable streamableTable = new CompilerUtil.TableBuilderInfo(typeFactory).field("ID", SqlTypeName.INTEGER).field("MAPFIELD", typeFactory.createTypeWithNullability(typeFactory.createMapType(typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.VARCHAR), true), typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.INTEGER), true)), true)).field("NESTEDMAPFIELD", typeFactory.createTypeWithNullability(typeFactory.createMapType(typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.VARCHAR), true), typeFactory.createTypeWithNullability(typeFactory.createMapType(typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.VARCHAR), true), typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.INTEGER), true)), true)), true)).field("ARRAYFIELD", typeFactory.createTypeWithNullability(typeFactory.createArrayType(typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.INTEGER), true), -1L), true)).build();
Table table = streamableTable.stream();
schema.add("FOO", table);
schema.add("BAR", table);
schema.add("MYPLUS", ScalarFunctionImpl.create(MyPlus.class, "eval"));
List<SqlOperatorTable> sqlOperatorTables = new ArrayList<>();
sqlOperatorTables.add(SqlStdOperatorTable.instance());
sqlOperatorTables.add(new CalciteCatalogReader(CalciteSchema.from(schema), false, Collections.<String>emptyList(), typeFactory));
SqlOperatorTable chainedSqlOperatorTable = new ChainedSqlOperatorTable(sqlOperatorTables);
FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(schema).operatorTable(chainedSqlOperatorTable).build();
Planner planner = Frameworks.getPlanner(config);
SqlNode parse = planner.parse(sql);
SqlNode validate = planner.validate(parse);
RelNode tree = planner.convert(validate);
System.out.println(RelOptUtil.toString(tree, SqlExplainLevel.ALL_ATTRIBUTES));
return new CalciteState(schema, tree);
}
Aggregations