use of org.apache.calcite.sql.SqlNodeList in project hive by apache.
the class HiveJdbcImplementor method visit.
@Override
public Result visit(Project e) {
// This variant keeps the column aliases instead of producing STAR
Result x = visitInput(e, 0, Clause.SELECT);
parseCorrelTable(e, x);
final Builder builder = x.builder(e);
final List<SqlNode> selectList = new ArrayList<>();
for (RexNode ref : e.getProjects()) {
SqlNode sqlExpr = builder.context.toSql(null, ref);
addSelect(selectList, sqlExpr, e.getRowType());
}
builder.setSelect(new SqlNodeList(selectList, POS));
return builder.result();
}
use of org.apache.calcite.sql.SqlNodeList in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_47_hints method test_headHint_2.
public void test_headHint_2() throws Exception {
String sql = "/*TDDL : construct()*/ select /*TDDL : add_ms(sort=t.pk, asc=true) add_agg(agg=\"SUM\", group=\"pk\", column=\"c\") add_pj(c=\"c\") add_ts(c, true) */ pk, count(*) c from tb1 where id in(1,2,3,4) group by pk order by pk";
MySqlStatementParser parser = new MySqlStatementParser(sql, SQLParserFeature.TDDLHint);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
CalciteMySqlNodeVisitor visitor = new CalciteMySqlNodeVisitor();
statemen.accept(visitor);
SqlNode sqlNode = visitor.getSqlNode();
System.out.println(sqlNode);
SqlNodeList hints = ((TDDLSqlSelect) sqlNode).getHints();
Assert.assertEquals(hints.size(), 1);
Assert.assertEquals(((SqlNodeList) hints.get(0)).size(), 4);
SqlNodeList headHints = ((TDDLSqlSelect) sqlNode).getHeadHints();
Assert.assertEquals(headHints.size(), 1);
Assert.assertEquals(((SqlNodeList) headHints.get(0)).size(), 1);
}
use of org.apache.calcite.sql.SqlNodeList in project drill by axbaretto.
the class DescribeTableHandler method rewrite.
/**
* Rewrite the parse tree as SELECT ... FROM INFORMATION_SCHEMA.COLUMNS ...
*/
@Override
public SqlNode rewrite(SqlNode sqlNode) throws RelConversionException, ForemanSetupException {
DrillSqlDescribeTable node = unwrap(sqlNode, DrillSqlDescribeTable.class);
try {
List<SqlNode> selectList = ImmutableList.of((SqlNode) new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO), new SqlIdentifier(COLS_COL_DATA_TYPE, SqlParserPos.ZERO), new SqlIdentifier(COLS_COL_IS_NULLABLE, SqlParserPos.ZERO));
SqlNode fromClause = new SqlIdentifier(ImmutableList.of(IS_SCHEMA_NAME, TAB_COLUMNS), null, SqlParserPos.ZERO, null);
final SqlIdentifier table = node.getTable();
final SchemaPlus defaultSchema = config.getConverter().getDefaultSchema();
final List<String> schemaPathGivenInCmd = Util.skipLast(table.names);
final SchemaPlus schema = SchemaUtilites.findSchema(defaultSchema, schemaPathGivenInCmd);
final String charset = Util.getDefaultCharset().name();
if (schema == null) {
SchemaUtilites.throwSchemaNotFoundException(defaultSchema, SchemaUtilites.SCHEMA_PATH_JOINER.join(schemaPathGivenInCmd));
}
if (SchemaUtilites.isRootSchema(schema)) {
throw UserException.validationError().message("No schema selected.").build(logger);
}
final String tableName = Util.last(table.names);
// find resolved schema path
final String schemaPath = SchemaUtilites.unwrapAsDrillSchemaInstance(schema).getFullSchemaName();
if (schema.getTable(tableName) == null) {
throw UserException.validationError().message("Unknown table [%s] in schema [%s]", tableName, schemaPath).build(logger);
}
SqlNode schemaCondition = null;
if (!SchemaUtilites.isRootSchema(schema)) {
schemaCondition = DrillParserUtil.createCondition(new SqlIdentifier(SHRD_COL_TABLE_SCHEMA, SqlParserPos.ZERO), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(schemaPath, charset, SqlParserPos.ZERO));
}
SqlNode where = DrillParserUtil.createCondition(new SqlIdentifier(SHRD_COL_TABLE_NAME, SqlParserPos.ZERO), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(tableName, charset, SqlParserPos.ZERO));
where = DrillParserUtil.createCondition(schemaCondition, SqlStdOperatorTable.AND, where);
SqlNode columnFilter = null;
if (node.getColumn() != null) {
columnFilter = DrillParserUtil.createCondition(new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(node.getColumn().toString(), charset, SqlParserPos.ZERO));
} else if (node.getColumnQualifier() != null) {
columnFilter = DrillParserUtil.createCondition(new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO), SqlStdOperatorTable.LIKE, node.getColumnQualifier());
}
where = DrillParserUtil.createCondition(where, SqlStdOperatorTable.AND, columnFilter);
return new SqlSelect(SqlParserPos.ZERO, null, new SqlNodeList(selectList, SqlParserPos.ZERO), fromClause, where, null, null, null, null, null, null);
} catch (Exception ex) {
throw UserException.planError(ex).message("Error while rewriting DESCRIBE query: %d", ex.getMessage()).build(logger);
}
}
use of org.apache.calcite.sql.SqlNodeList 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.SqlNodeList in project hazelcast by hazelcast.
the class HazelcastSqlToRelConverter method constructComparisons.
/**
* Constructs comparisons between
* left-hand operand (as a rule, SqlIdentifier) and right-hand list.
*/
private List<RexNode> constructComparisons(Blackboard bb, List<RexNode> leftKeys, SqlNodeList valuesList) {
final List<RexNode> comparisons = new ArrayList<>();
for (SqlNode rightValues : valuesList) {
RexNode rexComparison;
final SqlOperator comparisonOp = SqlStdOperatorTable.EQUALS;
if (leftKeys.size() == 1) {
rexComparison = rexBuilder.makeCall(comparisonOp, leftKeys.get(0), ensureSqlType(leftKeys.get(0).getType(), bb.convertExpression(rightValues)));
} else {
assert rightValues instanceof SqlCall;
final SqlBasicCall basicCall = (SqlBasicCall) rightValues;
assert basicCall.getOperator() instanceof SqlRowOperator && basicCall.operandCount() == leftKeys.size();
rexComparison = RexUtil.composeConjunction(rexBuilder, Pair.zip(leftKeys, basicCall.getOperandList()).stream().map(pair -> rexBuilder.makeCall(comparisonOp, pair.left, ensureSqlType(pair.left.getType(), bb.convertExpression(pair.right)))).collect(Collectors.toList()));
}
comparisons.add(rexComparison);
}
return comparisons;
}
Aggregations