use of org.apache.calcite.sql.SqlSelect in project hazelcast by hazelcast.
the class ParserNameResolutionTest method checkSuccess.
private static void checkSuccess(OptimizerContext context, String fieldName, String tableFqn, String... tableComponents) {
QueryParseResult res = context.parse(composeSelect(fieldName, tableComponents));
SqlSelect select = (SqlSelect) res.getNode();
SqlNodeList selectList = select.getSelectList();
assertEquals(1, selectList.size());
SqlIdentifier fieldIdentifier = (SqlIdentifier) selectList.get(0);
assertEquals(SqlIdentifier.getString(Arrays.asList(last(tableComponents), fieldName)), fieldIdentifier.toString());
SqlCall from = (SqlCall) select.getFrom();
assertEquals(from.getKind(), SqlKind.AS);
assertEquals(tableFqn, from.operand(0).toString());
assertEquals(last(tableComponents), from.operand(1).toString());
}
use of org.apache.calcite.sql.SqlSelect in project hazelcast by hazelcast.
the class HazelcastTypeCoercion method rowTypeCoercion.
/**
* {@inheritDoc}
* <p>
* We change the contract of the superclass' return type. According to the
* superclass contract we're supposed to return true iff we successfully
* added a CAST. This method returns true if the expression can now be
* assigned to {@code targetType}, either because a CAST was added, or
* because it already was assignable (e.g. the type was same). This is
* needed for {@link #querySourceCoercion} method, which calls this method.
*
* @return True, if the source column can now be assigned to {@code
* targetType}
*/
@Override
public boolean rowTypeCoercion(SqlValidatorScope scope, SqlNode query, int columnIndex, RelDataType targetType) {
switch(query.getKind()) {
case SELECT:
SqlSelect selectNode = (SqlSelect) query;
SqlValidatorScope selectScope = validator.getSelectScope(selectNode);
if (!rowTypeElementCoercion(selectScope, selectNode.getSelectList().get(columnIndex), targetType, newNode -> selectNode.getSelectList().set(columnIndex, newNode))) {
return false;
}
updateInferredColumnType(selectScope, query, columnIndex, targetType);
return true;
case VALUES:
for (SqlNode rowConstructor : ((SqlCall) query).getOperandList()) {
if (!rowTypeElementCoercion(scope, ((SqlCall) rowConstructor).operand(columnIndex), targetType, newNode -> ((SqlCall) rowConstructor).setOperand(columnIndex, newNode))) {
return false;
}
}
updateInferredColumnType(scope, query, columnIndex, targetType);
return true;
default:
throw new UnsupportedOperationException("unexpected: " + query.getKind());
}
}
use of org.apache.calcite.sql.SqlSelect in project hazelcast by hazelcast.
the class HazelcastSqlValidator method createSourceSelectForUpdate.
@Override
protected SqlSelect createSourceSelectForUpdate(SqlUpdate update) {
SqlNodeList selectList = new SqlNodeList(SqlParserPos.ZERO);
Table table = extractTable((SqlIdentifier) update.getTargetTable());
if (table != null) {
if (table instanceof ViewTable) {
throw QueryException.error("DML operations not supported for views");
}
SqlConnector connector = getJetSqlConnector(table);
// only tables with primary keys can be updated
if (connector.getPrimaryKey(table).isEmpty()) {
throw QueryException.error("Cannot UPDATE " + update.getTargetTable() + ": it doesn't have a primary key");
}
// add all fields, even hidden ones...
table.getFields().forEach(field -> selectList.add(new SqlIdentifier(field.getName(), SqlParserPos.ZERO)));
}
int ordinal = 0;
for (SqlNode exp : update.getSourceExpressionList()) {
// Force unique aliases to avoid a duplicate for Y with
// SET X=Y
String alias = SqlUtil.deriveAliasFromOrdinal(ordinal);
selectList.add(SqlValidatorUtil.addAlias(exp, alias));
++ordinal;
}
SqlNode sourceTable = update.getTargetTable();
if (update.getAlias() != null) {
sourceTable = SqlValidatorUtil.addAlias(sourceTable, update.getAlias().getSimple());
}
return new SqlSelect(SqlParserPos.ZERO, null, selectList, sourceTable, update.getCondition(), null, null, null, null, null, null, null);
}
use of org.apache.calcite.sql.SqlSelect in project hazelcast by hazelcast.
the class HazelcastSqlValidator method createSourceSelectForDelete.
@Override
protected SqlSelect createSourceSelectForDelete(SqlDelete delete) {
SqlNodeList selectList = new SqlNodeList(SqlParserPos.ZERO);
Table table = extractTable((SqlIdentifier) delete.getTargetTable());
if (table != null) {
if (table instanceof ViewTable) {
throw QueryException.error("DML operations not supported for views");
}
SqlConnector connector = getJetSqlConnector(table);
// We need to feed primary keys to the delete processor so that it can directly delete the records.
// Therefore we use the primary key for the select list.
connector.getPrimaryKey(table).forEach(name -> selectList.add(new SqlIdentifier(name, SqlParserPos.ZERO)));
if (selectList.size() == 0) {
throw QueryException.error("Cannot DELETE from " + delete.getTargetTable() + ": it doesn't have a primary key");
}
}
SqlNode sourceTable = delete.getTargetTable();
if (delete.getAlias() != null) {
sourceTable = SqlValidatorUtil.addAlias(sourceTable, delete.getAlias().getSimple());
}
return new SqlSelect(SqlParserPos.ZERO, null, selectList, sourceTable, delete.getCondition(), null, null, null, null, null, null, null);
}
use of org.apache.calcite.sql.SqlSelect in project samza by apache.
the class SamzaSqlQueryParser method getSource.
private static void getSource(SqlNode node, ArrayList<String> sourceList) {
if (node instanceof SqlJoin) {
SqlJoin joinNode = (SqlJoin) node;
ArrayList<String> sourcesLeft = new ArrayList<>();
ArrayList<String> sourcesRight = new ArrayList<>();
getSource(joinNode.getLeft(), sourcesLeft);
getSource(joinNode.getRight(), sourcesRight);
sourceList.addAll(sourcesLeft);
sourceList.addAll(sourcesRight);
} else if (node instanceof SqlIdentifier) {
sourceList.add(node.toString());
} else if (node instanceof SqlBasicCall) {
SqlBasicCall basicCall = (SqlBasicCall) node;
if (basicCall.getOperator() instanceof SqlAsOperator) {
getSource(basicCall.operand(0), sourceList);
} else if (basicCall.getOperator() instanceof SqlUnnestOperator && basicCall.operand(0) instanceof SqlSelect) {
sourceList.addAll(getSourcesFromSelectQuery(basicCall.operand(0)));
}
} else if (node instanceof SqlSelect) {
getSource(((SqlSelect) node).getFrom(), sourceList);
}
}
Aggregations