Search in sources :

Example 31 with SqlWriter

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter in project hazelcast by hazelcast.

the class HazelcastJsonQueryFunction method unparse.

@Override
public void unparse(final SqlWriter writer, final SqlCall call, final int leftPrec, final int rightPrec) {
    final SqlWriter.Frame frame = writer.startFunCall(this.getName());
    call.operand(0).unparse(writer, leftPrec, rightPrec);
    writer.sep(",", true);
    call.operand(1).unparse(writer, leftPrec, rightPrec);
    final SqlJsonQueryWrapperBehavior wrapperBehavior = (SqlJsonQueryWrapperBehavior) ((SqlLiteral) call.operand(2)).getValue();
    final SqlJsonQueryEmptyOrErrorBehavior onEmpty = (SqlJsonQueryEmptyOrErrorBehavior) ((SqlLiteral) call.operand(3)).getValue();
    final SqlJsonQueryEmptyOrErrorBehavior onError = (SqlJsonQueryEmptyOrErrorBehavior) ((SqlLiteral) call.operand(4)).getValue();
    unparseWrapperBehavior(wrapperBehavior, writer);
    unparseEmptyOrErrorBehavior(onEmpty, writer);
    writer.keyword("ON EMPTY");
    unparseEmptyOrErrorBehavior(onError, writer);
    writer.keyword("ON ERROR");
    writer.endFunCall(frame);
}
Also used : SqlJsonQueryEmptyOrErrorBehavior(org.apache.calcite.sql.SqlJsonQueryEmptyOrErrorBehavior) SqlWriter(org.apache.calcite.sql.SqlWriter) SqlJsonQueryWrapperBehavior(org.apache.calcite.sql.SqlJsonQueryWrapperBehavior)

Example 32 with SqlWriter

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter 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);
}
Also used : SqlWriter(org.apache.calcite.sql.SqlWriter) HazelcastSqlCase(com.hazelcast.jet.sql.impl.validate.operators.special.HazelcastSqlCase) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 33 with SqlWriter

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter in project beam by apache.

the class BeamBigQuerySqlDialect method unparseExtractFunctions.

private void unparseExtractFunctions(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
    String funName = call.getOperator().getName();
    int operandCount = call.operandCount();
    SqlNode tz = null;
    final SqlWriter.Frame frame = writer.startFunCall("EXTRACT");
    if (!funName.equals("$extract") && (operandCount == 1 || operandCount == 2)) {
        // EXTRACT(DATE/TIME/DATETIME FROM timestamp_expression [AT TIME ZONE tz])
        // operand0: timestamp_expression
        // operand1: tz (optional)
        writer.literal(EXTRACT_FUNCTIONS.get(funName));
        if (operandCount == 2) {
            tz = call.operand(1);
        }
    } else if (funName.equals("$extract") && (operandCount == 2 || operandCount == 3)) {
        // EXTRACT(date_part FROM timestamp_expression [AT TIME ZONE tz])
        // operand0: timestamp_expression
        // operand1: date_part
        // operand2: tz (optional)
        call.operand(1).unparse(writer, leftPrec, rightPrec);
        if (operandCount == 3) {
            tz = call.operand(2);
        }
    } else {
        throw new IllegalArgumentException(String.format("Unable to unparse %s with %d operands.", funName, operandCount));
    }
    writer.literal("FROM");
    call.operand(0).unparse(writer, leftPrec, rightPrec);
    if (tz != null) {
        writer.literal("AT TIME ZONE");
        tz.unparse(writer, leftPrec, rightPrec);
    }
    writer.endFunCall(frame);
}
Also used : SqlWriter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode)

Example 34 with SqlWriter

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter in project calcite by apache.

the class CassandraSchema method addMaterializedViews.

/**
 * Add all materialized views defined in the schema to this column family
 */
private void addMaterializedViews() {
    // Close the hook use to get us here
    hook.close();
    for (MaterializedViewMetadata view : getKeyspace().getMaterializedViews()) {
        String tableName = view.getBaseTable().getName();
        StringBuilder queryBuilder = new StringBuilder("SELECT ");
        // Add all the selected columns to the query
        List<String> columnNames = new ArrayList<String>();
        for (ColumnMetadata column : view.getColumns()) {
            columnNames.add("\"" + column.getName() + "\"");
        }
        queryBuilder.append(Util.toString(columnNames, "", ", ", ""));
        queryBuilder.append(" FROM \"" + tableName + "\"");
        // Get the where clause from the system schema
        String whereQuery = "SELECT where_clause from system_schema.views " + "WHERE keyspace_name='" + keyspace + "' AND view_name='" + view.getName() + "'";
        queryBuilder.append(" WHERE " + session.execute(whereQuery).one().getString(0));
        // Parse and unparse the view query to get properly quoted field names
        String query = queryBuilder.toString();
        SqlParser.ConfigBuilder configBuilder = SqlParser.configBuilder();
        configBuilder.setUnquotedCasing(Casing.UNCHANGED);
        SqlSelect parsedQuery;
        try {
            parsedQuery = (SqlSelect) SqlParser.create(query, configBuilder.build()).parseQuery();
        } catch (SqlParseException e) {
            LOGGER.warn("Could not parse query {} for CQL view {}.{}", query, keyspace, view.getName());
            continue;
        }
        StringWriter stringWriter = new StringWriter(query.length());
        PrintWriter printWriter = new PrintWriter(stringWriter);
        SqlWriter writer = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT, true, printWriter);
        parsedQuery.unparse(writer, 0, 0);
        query = stringWriter.toString();
        // Add the view for this query
        String viewName = "$" + getTableNames().size();
        SchemaPlus schema = parentSchema.getSubSchema(name);
        CalciteSchema calciteSchema = CalciteSchema.from(schema);
        List<String> viewPath = calciteSchema.path(viewName);
        schema.add(viewName, MaterializedViewTable.create(calciteSchema, query, null, viewPath, view.getName(), true));
    }
}
Also used : ColumnMetadata(com.datastax.driver.core.ColumnMetadata) SqlWriter(org.apache.calcite.sql.SqlWriter) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) ArrayList(java.util.ArrayList) SqlParser(org.apache.calcite.sql.parser.SqlParser) SchemaPlus(org.apache.calcite.schema.SchemaPlus) MaterializedViewMetadata(com.datastax.driver.core.MaterializedViewMetadata) SqlSelect(org.apache.calcite.sql.SqlSelect) StringWriter(java.io.StringWriter) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) PrintWriter(java.io.PrintWriter)

Example 35 with SqlWriter

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter in project calcite by apache.

the class SqlCreateTable method unparse.

@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
    writer.keyword("CREATE");
    writer.keyword("TABLE");
    if (ifNotExists) {
        writer.keyword("IF NOT EXISTS");
    }
    name.unparse(writer, leftPrec, rightPrec);
    if (columnList != null) {
        SqlWriter.Frame frame = writer.startList("(", ")");
        for (SqlNode c : columnList) {
            writer.sep(",");
            c.unparse(writer, 0, 0);
        }
        writer.endList(frame);
    }
    if (query != null) {
        writer.keyword("AS");
        writer.newlineAndIndent();
        query.unparse(writer, 0, 0);
    }
}
Also used : SqlWriter(org.apache.calcite.sql.SqlWriter) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlWriter (org.apache.calcite.sql.SqlWriter)48 SqlNode (org.apache.calcite.sql.SqlNode)40 SqlNodeList (org.apache.calcite.sql.SqlNodeList)5 SqlNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode)4 SqlWriter (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter)4 SqlJsonConstructorNullClause (org.apache.calcite.sql.SqlJsonConstructorNullClause)3 SqlIdentifier (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier)2 SqlPrettyWriter (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.pretty.SqlPrettyWriter)2 SqlLiteral (org.apache.calcite.sql.SqlLiteral)2 SqlTableConstraint (org.apache.flink.sql.parser.ddl.constraint.SqlTableConstraint)2 Test (org.junit.Test)2 ColumnMetadata (com.datastax.driver.core.ColumnMetadata)1 MaterializedViewMetadata (com.datastax.driver.core.MaterializedViewMetadata)1 HazelcastSqlCase (com.hazelcast.jet.sql.impl.validate.operators.special.HazelcastSqlCase)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 TimeUnitRange (org.apache.calcite.avatica.util.TimeUnitRange)1 CalciteSchema (org.apache.calcite.jdbc.CalciteSchema)1 SchemaPlus (org.apache.calcite.schema.SchemaPlus)1