use of org.apache.calcite.sql.SqlNodeList in project hazelcast by hazelcast.
the class HazelcastTypeCoercion method coerceSourceRowType.
// originally copied from TypeCoercionImpl
private boolean coerceSourceRowType(SqlValidatorScope sourceScope, SqlNode query, int columnIndex, int totalColumns, RelDataType targetType) {
switch(query.getKind()) {
case INSERT:
SqlInsert insert = (SqlInsert) query;
return coerceSourceRowType(sourceScope, insert.getSource(), columnIndex, totalColumns, targetType);
case UPDATE:
// trailing elements of selectList are equal to elements of sourceExpressionList
// see JetSqlValidator.validateUpdate()
SqlUpdate update = (SqlUpdate) query;
SqlNodeList selectList = update.getSourceSelect().getSelectList();
return coerceSourceRowType(sourceScope, selectList, selectList.size() - totalColumns + columnIndex, targetType);
default:
return rowTypeCoercion(sourceScope, query, columnIndex, targetType);
}
}
use of org.apache.calcite.sql.SqlNodeList in project hazelcast by hazelcast.
the class SqlExtendedInsert method validate.
@Override
public void validate(SqlValidator validator, SqlValidatorScope scope) {
SqlValidatorTable table0 = validator.getCatalogReader().getTable(tableNames());
if (table0 == null) {
super.validate(validator, scope);
// should have failed with "Object not found"
assert false;
}
HazelcastTable table = table0.unwrap(HazelcastTable.class);
if (getTargetColumnList() == null) {
RelDataType rowType = table.getRowType(validator.getTypeFactory());
List<SqlNode> columnListWithoutHidden = new ArrayList<>();
for (RelDataTypeField f : rowType.getFieldList()) {
if (!table.isHidden(f.getName())) {
columnListWithoutHidden.add(new SqlIdentifier(f.getName(), SqlParserPos.ZERO));
}
}
overrideColumnList = new SqlNodeList(columnListWithoutHidden, SqlParserPos.ZERO);
}
super.validate(validator, scope);
Map<String, TableField> fieldsMap = table.getTarget().getFields().stream().collect(Collectors.toMap(TableField::getName, f -> f));
for (SqlNode fieldNode : getTargetColumnList()) {
TableField field = fieldsMap.get(((SqlIdentifier) fieldNode).getSimple());
if (field instanceof MapTableField) {
QueryPath path = ((MapTableField) field).getPath();
if (path.getPath() == null && field.getType().getTypeFamily() == QueryDataTypeFamily.OBJECT) {
throw validator.newValidationError(fieldNode, RESOURCE.insertToTopLevelObject());
}
}
}
}
use of org.apache.calcite.sql.SqlNodeList 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.SqlNodeList 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.SqlNodeList in project hazelcast by hazelcast.
the class HazelcastCaseOperator method unparse.
@Override
public void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
assert call instanceof HazelcastSqlCase;
final SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.CASE, "CASE", "END");
HazelcastSqlCase sqlCase = (HazelcastSqlCase) call;
SqlNodeList whenList = sqlCase.getWhenOperands();
SqlNodeList thenList = sqlCase.getThenOperands();
assert whenList.size() == thenList.size();
for (Pair<SqlNode, SqlNode> pair : Pair.zip(whenList, thenList)) {
writer.sep("WHEN");
pair.left.unparse(writer, 0, 0);
writer.sep("THEN");
pair.right.unparse(writer, 0, 0);
}
writer.sep("ELSE");
SqlNode elseExpr = sqlCase.getElseOperand();
elseExpr.unparse(writer, 0, 0);
writer.endList(frame);
}
Aggregations