use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.
the class InterpreterTest method testAggregateGroup.
@Test
public void testAggregateGroup() throws Exception {
rootSchema.add("beatles", new ScannableTableTest.BeatlesTable());
SqlNode parse = planner.parse("select \"j\", count(*) from \"beatles\" group by \"j\"");
SqlNode validate = planner.validate(parse);
RelNode convert = planner.rel(validate).rel;
final Interpreter interpreter = new Interpreter(dataContext, convert);
assertRowsUnordered(interpreter, "[George, 1]", "[Paul, 1]", "[John, 1]", "[Ringo, 1]");
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.
the class InterpreterTest method testAggregate.
@Test
public void testAggregate() throws Exception {
rootSchema.add("beatles", new ScannableTableTest.BeatlesTable());
SqlNode parse = planner.parse("select count(*) from \"beatles\"");
SqlNode validate = planner.validate(parse);
RelNode convert = planner.rel(validate).rel;
final Interpreter interpreter = new Interpreter(dataContext, convert);
assertRows(interpreter, "[4]");
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.
the class RelToSqlConverter method visit.
/**
* @see #dispatch
*/
public Result visit(Join e) {
final Result leftResult = visitChild(0, e.getLeft()).resetAlias();
final Result rightResult = visitChild(1, e.getRight()).resetAlias();
final Context leftContext = leftResult.qualifiedContext();
final Context rightContext = rightResult.qualifiedContext();
SqlNode sqlCondition = null;
SqlLiteral condType = JoinConditionType.ON.symbol(POS);
JoinType joinType = joinType(e.getJoinType());
if (e.getJoinType() == JoinRelType.INNER && e.getCondition().isAlwaysTrue()) {
joinType = JoinType.COMMA;
condType = JoinConditionType.NONE.symbol(POS);
} else {
sqlCondition = convertConditionToSqlNode(e.getCondition(), leftContext, rightContext, e.getLeft().getRowType().getFieldCount());
}
SqlNode join = new SqlJoin(POS, leftResult.asFrom(), SqlLiteral.createBoolean(false, POS), joinType.symbol(POS), rightResult.asFrom(), condType, sqlCondition);
return result(join, leftResult, rightResult);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.
the class RelToSqlConverter method visit.
/**
* @see #dispatch
*/
public Result visit(Values e) {
final List<Clause> clauses = ImmutableList.of(Clause.SELECT);
final Map<String, RelDataType> pairs = ImmutableMap.of();
final Context context = aliasContext(pairs, false);
SqlNode query;
final boolean rename = stack.size() <= 1 || !(Iterables.get(stack, 1).r instanceof TableModify);
final List<String> fieldNames = e.getRowType().getFieldNames();
if (!dialect.supportsAliasedValues() && rename) {
// Oracle does not support "AS t (c1, c2)". So instead of
// (VALUES (v0, v1), (v2, v3)) AS t (c0, c1)
// we generate
// SELECT v0 AS c0, v1 AS c1 FROM DUAL
// UNION ALL
// SELECT v2 AS c0, v3 AS c1 FROM DUAL
List<SqlSelect> list = new ArrayList<>();
for (List<RexLiteral> tuple : e.getTuples()) {
final List<SqlNode> values2 = new ArrayList<>();
final SqlNodeList exprList = exprList(context, tuple);
for (Pair<SqlNode, String> value : Pair.zip(exprList, fieldNames)) {
values2.add(SqlStdOperatorTable.AS.createCall(POS, value.left, new SqlIdentifier(value.right, POS)));
}
list.add(new SqlSelect(POS, null, new SqlNodeList(values2, POS), new SqlIdentifier("DUAL", POS), null, null, null, null, null, null, null));
}
if (list.size() == 1) {
query = list.get(0);
} else {
query = SqlStdOperatorTable.UNION_ALL.createCall(new SqlNodeList(list, POS));
}
} else {
// Generate ANSI syntax
// (VALUES (v0, v1), (v2, v3))
// or, if rename is required
// (VALUES (v0, v1), (v2, v3)) AS t (c0, c1)
final SqlNodeList selects = new SqlNodeList(POS);
for (List<RexLiteral> tuple : e.getTuples()) {
selects.add(ANONYMOUS_ROW.createCall(exprList(context, tuple)));
}
query = SqlStdOperatorTable.VALUES.createCall(selects);
if (rename) {
final List<SqlNode> list = new ArrayList<>();
list.add(query);
list.add(new SqlIdentifier("t", POS));
for (String fieldName : fieldNames) {
list.add(new SqlIdentifier(fieldName, POS));
}
query = SqlStdOperatorTable.AS.createCall(POS, list);
}
}
return result(query, clauses, e, null);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.
the class RelToSqlConverter method visit.
/**
* @see #dispatch
*/
public Result visit(Match e) {
final RelNode input = e.getInput();
final Result x = visitChild(0, input);
final Context context = matchRecognizeContext(x.qualifiedContext());
SqlNode tableRef = x.asQueryOrValues();
final List<SqlNode> partitionSqlList = new ArrayList<>();
if (e.getPartitionKeys() != null) {
for (RexNode rex : e.getPartitionKeys()) {
SqlNode sqlNode = context.toSql(null, rex);
partitionSqlList.add(sqlNode);
}
}
final SqlNodeList partitionList = new SqlNodeList(partitionSqlList, POS);
final List<SqlNode> orderBySqlList = new ArrayList<>();
if (e.getOrderKeys() != null) {
for (RelFieldCollation fc : e.getOrderKeys().getFieldCollations()) {
if (fc.nullDirection != RelFieldCollation.NullDirection.UNSPECIFIED) {
boolean first = fc.nullDirection == RelFieldCollation.NullDirection.FIRST;
SqlNode nullDirectionNode = dialect.emulateNullDirection(context.field(fc.getFieldIndex()), first, fc.direction.isDescending());
if (nullDirectionNode != null) {
orderBySqlList.add(nullDirectionNode);
fc = new RelFieldCollation(fc.getFieldIndex(), fc.getDirection(), RelFieldCollation.NullDirection.UNSPECIFIED);
}
}
orderBySqlList.add(context.toSql(fc));
}
}
final SqlNodeList orderByList = new SqlNodeList(orderBySqlList, SqlParserPos.ZERO);
final SqlLiteral rowsPerMatch = e.isAllRows() ? SqlMatchRecognize.RowsPerMatchOption.ALL_ROWS.symbol(POS) : SqlMatchRecognize.RowsPerMatchOption.ONE_ROW.symbol(POS);
final SqlNode after;
if (e.getAfter() instanceof RexLiteral) {
SqlMatchRecognize.AfterOption value = (SqlMatchRecognize.AfterOption) ((RexLiteral) e.getAfter()).getValue2();
after = SqlLiteral.createSymbol(value, POS);
} else {
RexCall call = (RexCall) e.getAfter();
String operand = RexLiteral.stringValue(call.getOperands().get(0));
after = call.getOperator().createCall(POS, new SqlIdentifier(operand, POS));
}
RexNode rexPattern = e.getPattern();
final SqlNode pattern = context.toSql(null, rexPattern);
final SqlLiteral strictStart = SqlLiteral.createBoolean(e.isStrictStart(), POS);
final SqlLiteral strictEnd = SqlLiteral.createBoolean(e.isStrictEnd(), POS);
RexLiteral rexInterval = (RexLiteral) e.getInterval();
SqlIntervalLiteral interval = null;
if (rexInterval != null) {
interval = (SqlIntervalLiteral) context.toSql(null, rexInterval);
}
final SqlNodeList subsetList = new SqlNodeList(POS);
for (Map.Entry<String, SortedSet<String>> entry : e.getSubsets().entrySet()) {
SqlNode left = new SqlIdentifier(entry.getKey(), POS);
List<SqlNode> rhl = Lists.newArrayList();
for (String right : entry.getValue()) {
rhl.add(new SqlIdentifier(right, POS));
}
subsetList.add(SqlStdOperatorTable.EQUALS.createCall(POS, left, new SqlNodeList(rhl, POS)));
}
final SqlNodeList measureList = new SqlNodeList(POS);
for (Map.Entry<String, RexNode> entry : e.getMeasures().entrySet()) {
final String alias = entry.getKey();
final SqlNode sqlNode = context.toSql(null, entry.getValue());
measureList.add(as(sqlNode, alias));
}
final SqlNodeList patternDefList = new SqlNodeList(POS);
for (Map.Entry<String, RexNode> entry : e.getPatternDefinitions().entrySet()) {
final String alias = entry.getKey();
final SqlNode sqlNode = context.toSql(null, entry.getValue());
patternDefList.add(as(sqlNode, alias));
}
final SqlNode matchRecognize = new SqlMatchRecognize(POS, tableRef, pattern, strictStart, strictEnd, patternDefList, measureList, after, subsetList, rowsPerMatch, partitionList, orderByList, interval);
return result(matchRecognize, Expressions.list(Clause.FROM), e, null);
}
Aggregations