Search in sources :

Example 56 with SqlNodeList

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();
}
Also used : ArrayList(java.util.ArrayList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

Example 57 with SqlNodeList

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);
}
Also used : CalciteMySqlNodeVisitor(com.alibaba.druid.support.calcite.CalciteMySqlNodeVisitor) TDDLSqlSelect(com.alibaba.druid.support.calcite.TDDLSqlSelect) SqlNodeList(org.apache.calcite.sql.SqlNodeList) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SqlNode(org.apache.calcite.sql.SqlNode)

Example 58 with SqlNodeList

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);
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) DrillSqlDescribeTable(org.apache.drill.exec.planner.sql.parser.DrillSqlDescribeTable) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) UserException(org.apache.drill.common.exceptions.UserException) ValidationException(org.apache.calcite.tools.ValidationException) ForemanSetupException(org.apache.drill.exec.work.foreman.ForemanSetupException) RelConversionException(org.apache.calcite.tools.RelConversionException) SqlNode(org.apache.calcite.sql.SqlNode)

Example 59 with SqlNodeList

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());
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlCall(org.apache.calcite.sql.SqlCall) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 60 with SqlNodeList

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;
}
Also used : RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) CalciteContextException(org.apache.calcite.runtime.CalciteContextException) LiteralUtils(com.hazelcast.jet.sql.impl.validate.literal.LiteralUtils) RelDataTypeFamily(org.apache.calcite.rel.type.RelDataTypeFamily) HazelcastResources(com.hazelcast.jet.sql.impl.validate.HazelcastResources) SqlCall(org.apache.calcite.sql.SqlCall) SqlJsonEmptyOrError(org.apache.calcite.sql.SqlJsonEmptyOrError) BigDecimal(java.math.BigDecimal) RexUtil(org.apache.calcite.rex.RexUtil) SqlNode(org.apache.calcite.sql.SqlNode) SqlUtil(org.apache.calcite.sql.SqlUtil) RexNode(org.apache.calcite.rex.RexNode) Arrays.asList(java.util.Arrays.asList) LocalTime(java.time.LocalTime) TableModify(org.apache.calcite.rel.core.TableModify) DAY(org.apache.calcite.avatica.util.TimeUnit.DAY) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) YEAR(org.apache.calcite.avatica.util.TimeUnit.YEAR) RelOptCluster(org.apache.calcite.plan.RelOptCluster) SqlKind(org.apache.calcite.sql.SqlKind) IdentityHashMap(java.util.IdentityHashMap) HazelcastTypeUtils(com.hazelcast.jet.sql.impl.validate.types.HazelcastTypeUtils) HazelcastJsonParseFunction(com.hazelcast.jet.sql.impl.validate.operators.json.HazelcastJsonParseFunction) SqlOperandTypeChecker(org.apache.calcite.sql.type.SqlOperandTypeChecker) RexLiteral(org.apache.calcite.rex.RexLiteral) Set(java.util.Set) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) HazelcastJsonValueFunction(com.hazelcast.jet.sql.impl.validate.operators.json.HazelcastJsonValueFunction) List(java.util.List) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) SqlRowOperator(org.apache.calcite.sql.fun.SqlRowOperator) SqlErrorCode(com.hazelcast.sql.impl.SqlErrorCode) MONTH(org.apache.calcite.avatica.util.TimeUnit.MONTH) SqlInsert(org.apache.calcite.sql.SqlInsert) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) RelOptTable(org.apache.calcite.plan.RelOptTable) ArrayList(java.util.ArrayList) SqlJsonValueReturning(org.apache.calcite.sql.SqlJsonValueReturning) TimeString(org.apache.calcite.util.TimeString) SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) Lists(com.google.common.collect.Lists) SqlLiteral(org.apache.calcite.sql.SqlLiteral) ImmutableList(com.google.common.collect.ImmutableList) Pair(org.apache.calcite.util.Pair) Converter(com.hazelcast.sql.impl.type.converter.Converter) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlBetweenOperator(org.apache.calcite.sql.fun.SqlBetweenOperator) QueryException(com.hazelcast.sql.impl.QueryException) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIntervalQualifier(org.apache.calcite.sql.SqlIntervalQualifier) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlExtendedInsert(com.hazelcast.jet.sql.impl.parse.SqlExtendedInsert) SqlTypeFamily(org.apache.calcite.sql.type.SqlTypeFamily) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) HazelcastReturnTypeInference(com.hazelcast.jet.sql.impl.validate.operators.typeinference.HazelcastReturnTypeInference) RexBuilder(org.apache.calcite.rex.RexBuilder) Literal(com.hazelcast.jet.sql.impl.validate.literal.Literal) SqlRexConvertletTable(org.apache.calcite.sql2rel.SqlRexConvertletTable) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlValidatorException(org.apache.calcite.sql.validate.SqlValidatorException) RelNode(org.apache.calcite.rel.RelNode) LogicalTableInsert(com.hazelcast.jet.sql.impl.opt.logical.LogicalTableInsert) Prepare(org.apache.calcite.prepare.Prepare) SECOND(org.apache.calcite.avatica.util.TimeUnit.SECOND) HazelcastBetweenOperator(com.hazelcast.jet.sql.impl.validate.operators.predicate.HazelcastBetweenOperator) SqlJsonValueEmptyOrErrorBehavior(org.apache.calcite.sql.SqlJsonValueEmptyOrErrorBehavior) SqlInOperator(org.apache.calcite.sql.fun.SqlInOperator) Converters(com.hazelcast.sql.impl.type.converter.Converters) Util(org.apache.calcite.util.Util) LogicalTableSink(com.hazelcast.jet.sql.impl.opt.logical.LogicalTableSink) Resources(org.apache.calcite.runtime.Resources) SqlNodeList(org.apache.calcite.sql.SqlNodeList) Collections(java.util.Collections) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlCall(org.apache.calcite.sql.SqlCall) ArrayList(java.util.ArrayList) SqlRowOperator(org.apache.calcite.sql.fun.SqlRowOperator) RexNode(org.apache.calcite.rex.RexNode) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlNodeList (org.apache.calcite.sql.SqlNodeList)123 SqlNode (org.apache.calcite.sql.SqlNode)97 ArrayList (java.util.ArrayList)45 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)43 RelDataType (org.apache.calcite.rel.type.RelDataType)39 SqlCall (org.apache.calcite.sql.SqlCall)30 SqlSelect (org.apache.calcite.sql.SqlSelect)29 BitString (org.apache.calcite.util.BitString)23 RexNode (org.apache.calcite.rex.RexNode)13 SqlLiteral (org.apache.calcite.sql.SqlLiteral)11 ImmutableList (com.google.common.collect.ImmutableList)10 List (java.util.List)10 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)10 NlsString (org.apache.calcite.util.NlsString)9 RelNode (org.apache.calcite.rel.RelNode)8 SqlUpdate (org.apache.calcite.sql.SqlUpdate)8 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)7 SqlWriter (org.apache.calcite.sql.SqlWriter)7 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)7 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)6