Search in sources :

Example 11 with SqlPrettyWriter

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

the class BeamDDLTest method unparseScalarFunction.

@Test
public void unparseScalarFunction() {
    SqlIdentifier name = new SqlIdentifier("foo", SqlParserPos.ZERO);
    SqlNode jarPath = SqlLiteral.createCharString("path/to/udf.jar", SqlParserPos.ZERO);
    SqlCreateFunction createFunction = new SqlCreateFunction(SqlParserPos.ZERO, false, name, jarPath, false);
    SqlWriter sqlWriter = new SqlPrettyWriter(BeamBigQuerySqlDialect.DEFAULT);
    createFunction.unparse(sqlWriter, 0, 0);
    assertEquals("CREATE FUNCTION foo USING JAR 'path/to/udf.jar'", sqlWriter.toSqlString().getSql());
}
Also used : SqlWriter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter) SqlPrettyWriter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlIdentifier(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Example 12 with SqlPrettyWriter

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.pretty.SqlPrettyWriter 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 13 with SqlPrettyWriter

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

the class SqlPrettyWriterTest method testSelectListExtraIndentFlag.

@Test
public void testSelectListExtraIndentFlag() throws Exception {
    final SqlPrettyWriter prettyWriter = new SqlPrettyWriter(AnsiSqlDialect.DEFAULT);
    prettyWriter.setSelectListItemsOnSeparateLines(true);
    prettyWriter.setSelectListExtraIndentFlag(false);
    checkSimple(prettyWriter, "${desc}", "${formatted}");
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) Test(org.junit.Test)

Example 14 with SqlPrettyWriter

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

the class SqlPrettyWriterTest method testOnlyQuoteIdentifiersWhichNeedIt.

@Test
public void testOnlyQuoteIdentifiersWhichNeedIt() throws Exception {
    final SqlPrettyWriter prettyWriter = new SqlPrettyWriter(AnsiSqlDialect.DEFAULT);
    prettyWriter.setQuoteAllIdentifiers(false);
    checkSimple(prettyWriter, "${desc}", "${formatted}");
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) Test(org.junit.Test)

Example 15 with SqlPrettyWriter

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

the class SqlPrettyWriterTest method checkPrettySeparateLines.

private void checkPrettySeparateLines(String sql) {
    final SqlPrettyWriter prettyWriter = new SqlPrettyWriter(AnsiSqlDialect.DEFAULT);
    prettyWriter.setSelectListItemsOnSeparateLines(true);
    prettyWriter.setSelectListExtraIndentFlag(false);
    final SqlNode node = parseQuery(sql);
    // Describe settings
    final StringWriter sw = new StringWriter();
    final PrintWriter pw = new PrintWriter(sw);
    prettyWriter.describe(pw, true);
    pw.flush();
    String desc = sw.toString();
    getDiffRepos().assertEquals("desc", "${desc}", desc);
    prettyWriter.setWhereListItemsOnSeparateLines(true);
    // Format
    String actual = prettyWriter.format(node);
    getDiffRepos().assertEquals("formatted", "${formatted}", actual);
}
Also used : StringWriter(java.io.StringWriter) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlNode(org.apache.calcite.sql.SqlNode) PrintWriter(java.io.PrintWriter)

Aggregations

SqlPrettyWriter (org.apache.calcite.sql.pretty.SqlPrettyWriter)22 Test (org.junit.Test)13 SqlNode (org.apache.calcite.sql.SqlNode)7 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 SqlString (org.apache.calcite.sql.util.SqlString)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 SqlIdentifier (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier)2 SqlNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode)2 SqlWriter (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter)2 SqlPrettyWriter (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.pretty.SqlPrettyWriter)2 SqlCall (org.apache.calcite.sql.SqlCall)2 SqlNodeList (org.apache.calcite.sql.SqlNodeList)2 SqlSelect (org.apache.calcite.sql.SqlSelect)2 SqlWriter (org.apache.calcite.sql.SqlWriter)2 SqlParseException (org.apache.calcite.sql.parser.SqlParseException)2 ColumnMetadata (com.datastax.driver.core.ColumnMetadata)1 MaterializedViewMetadata (com.datastax.driver.core.MaterializedViewMetadata)1 MappingField (com.hazelcast.sql.impl.schema.MappingField)1