use of org.apache.calcite.sql.SqlSelect in project incubator-gobblin by apache.
the class JdbcExtractor method hasJoinOperation.
/**
* Check if the SELECT query has join operation
*/
public static boolean hasJoinOperation(String selectQuery) {
if (selectQuery == null || selectQuery.length() == 0) {
return false;
}
SqlParser sqlParser = SqlParser.create(selectQuery);
try {
SqlNode all = sqlParser.parseQuery();
SqlSelect query;
if (all instanceof SqlSelect) {
query = (SqlSelect) all;
} else if (all instanceof SqlOrderBy) {
query = (SqlSelect) ((SqlOrderBy) all).query;
} else {
throw new UnsupportedOperationException("The select query is type of " + all.getClass() + " which is not supported here");
}
return query.getFrom().getKind() == SqlKind.JOIN;
} catch (SqlParseException e) {
return false;
}
}
use of org.apache.calcite.sql.SqlSelect in project calcite by apache.
the class JdbcTable method generateSql.
SqlString generateSql() {
final SqlNodeList selectList = new SqlNodeList(Collections.singletonList(SqlIdentifier.star(SqlParserPos.ZERO)), SqlParserPos.ZERO);
SqlSelect node = new SqlSelect(SqlParserPos.ZERO, SqlNodeList.EMPTY, selectList, tableName(), null, null, null, null, null, null, null);
final SqlPrettyWriter writer = new SqlPrettyWriter(jdbcSchema.dialect);
node.unparse(writer, 0, 0);
return writer.toSqlString();
}
use of org.apache.calcite.sql.SqlSelect in project calcite by apache.
the class SqlMultisetQueryConstructor method deriveType.
public RelDataType deriveType(SqlValidator validator, SqlValidatorScope scope, SqlCall call) {
SqlSelect subSelect = call.operand(0);
subSelect.validateExpr(validator, scope);
SqlValidatorNamespace ns = validator.getNamespace(subSelect);
assert null != ns.getRowType();
return SqlTypeUtil.createMultisetType(validator.getTypeFactory(), ns.getRowType(), false);
}
use of org.apache.calcite.sql.SqlSelect in project calcite by apache.
the class SqlAbstractGroupFunction method validateCall.
@Override
public void validateCall(SqlCall call, SqlValidator validator, SqlValidatorScope scope, SqlValidatorScope operandScope) {
super.validateCall(call, validator, scope, operandScope);
final SelectScope selectScope = SqlValidatorUtil.getEnclosingSelectScope(scope);
assert selectScope != null;
final SqlSelect select = selectScope.getNode();
if (!validator.isAggregate(select)) {
throw validator.newValidationError(call, Static.RESOURCE.groupingInAggregate(getName()));
}
final AggregatingSelectScope aggregatingSelectScope = SqlValidatorUtil.getEnclosingAggregateSelectScope(scope);
if (aggregatingSelectScope == null) {
// We're probably in the GROUP BY clause
throw validator.newValidationError(call, Static.RESOURCE.groupingInWrongClause(getName()));
}
for (SqlNode operand : call.getOperandList()) {
if (scope instanceof OrderByScope) {
operand = validator.expandOrderExpr(select, operand);
} else {
operand = validator.expand(operand, scope);
}
if (!aggregatingSelectScope.resolved.get().isGroupingExpr(operand)) {
throw validator.newValidationError(operand, Static.RESOURCE.groupingArgument(getName()));
}
}
}
use of org.apache.calcite.sql.SqlSelect 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);
}
Aggregations