Search in sources :

Example 1 with SqlWriterConfig

use of org.apache.calcite.sql.SqlWriterConfig in project calcite by apache.

the class JdbcTable method generateSql.

SqlString generateSql() {
    final SqlNodeList selectList = SqlNodeList.SINGLETON_STAR;
    SqlSelect node = new SqlSelect(SqlParserPos.ZERO, SqlNodeList.EMPTY, selectList, tableName(), null, null, null, null, null, null, null, null);
    final SqlWriterConfig config = SqlPrettyWriter.config().withAlwaysUseParentheses(true).withDialect(jdbcSchema.dialect);
    final SqlPrettyWriter writer = new SqlPrettyWriter(config);
    node.unparse(writer, 0, 0);
    return writer.toSqlString();
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlWriterConfig(org.apache.calcite.sql.SqlWriterConfig)

Example 2 with SqlWriterConfig

use of org.apache.calcite.sql.SqlWriterConfig in project calcite by apache.

the class IntervalSqlType method generateTypeString.

// ~ Methods ----------------------------------------------------------------
@Override
protected void generateTypeString(StringBuilder sb, boolean withDetail) {
    sb.append("INTERVAL ");
    final SqlDialect dialect = AnsiSqlDialect.DEFAULT;
    final SqlWriterConfig config = SqlPrettyWriter.config().withAlwaysUseParentheses(false).withSelectListItemsOnSeparateLines(false).withIndentation(0).withDialect(dialect);
    final SqlPrettyWriter writer = new SqlPrettyWriter(config);
    intervalQualifier.unparse(writer, 0, 0);
    final String sql = writer.toString();
    sb.append(new SqlString(dialect, sql).getSql());
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) AnsiSqlDialect(org.apache.calcite.sql.dialect.AnsiSqlDialect) SqlDialect(org.apache.calcite.sql.SqlDialect) SqlWriterConfig(org.apache.calcite.sql.SqlWriterConfig) SqlString(org.apache.calcite.sql.util.SqlString) SqlString(org.apache.calcite.sql.util.SqlString)

Example 3 with SqlWriterConfig

use of org.apache.calcite.sql.SqlWriterConfig in project calcite by apache.

the class RelToSqlConverterTest method toSql.

/**
 * Converts a relational expression to SQL in a given dialect
 * and with a particular writer configuration.
 */
private static String toSql(RelNode root, SqlDialect dialect, UnaryOperator<SqlWriterConfig> transform) {
    final RelToSqlConverter converter = new RelToSqlConverter(dialect);
    final SqlNode sqlNode = converter.visitRoot(root).asStatement();
    return sqlNode.toSqlString(c -> transform.apply(c.withDialect(dialect))).getSql();
}
Also used : AggregateProjectMergeRule(org.apache.calcite.rel.rules.AggregateProjectMergeRule) CoreMatchers.is(org.hamcrest.CoreMatchers.is) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) Programs(org.apache.calcite.tools.Programs) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) Frameworks(org.apache.calcite.tools.Frameworks) CalciteAssert(org.apache.calcite.test.CalciteAssert) UnaryOperator(java.util.function.UnaryOperator) SqlConformance(org.apache.calcite.sql.validate.SqlConformance) NullCollation(org.apache.calcite.config.NullCollation) AnsiSqlDialect(org.apache.calcite.sql.dialect.AnsiSqlDialect) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) SqlCall(org.apache.calcite.sql.SqlCall) Planner(org.apache.calcite.tools.Planner) SqlNode(org.apache.calcite.sql.SqlNode) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) RelBuilder(org.apache.calcite.tools.RelBuilder) RelHint(org.apache.calcite.rel.hint.RelHint) Map(java.util.Map) SqlSelect(org.apache.calcite.sql.SqlSelect) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) TestUtil(org.apache.calcite.util.TestUtil) Program(org.apache.calcite.tools.Program) SqlWriter(org.apache.calcite.sql.SqlWriter) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) MssqlSqlDialect(org.apache.calcite.sql.dialect.MssqlSqlDialect) Set(java.util.Set) Collectors(java.util.stream.Collectors) PostgresqlSqlDialect(org.apache.calcite.sql.dialect.PostgresqlSqlDialect) Test(org.junit.jupiter.api.Test) HintPredicates(org.apache.calcite.rel.hint.HintPredicates) List(java.util.List) AggregateJoinTransposeRule(org.apache.calcite.rel.rules.AggregateJoinTransposeRule) FilterJoinRule(org.apache.calcite.rel.rules.FilterJoinRule) RelDataTypeSystemImpl(org.apache.calcite.rel.type.RelDataTypeSystemImpl) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) SqlOperatorTables(org.apache.calcite.sql.util.SqlOperatorTables) MysqlSqlDialect(org.apache.calcite.sql.dialect.MysqlSqlDialect) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RuleSet(org.apache.calcite.tools.RuleSet) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelTraitDef(org.apache.calcite.plan.RelTraitDef) Hook(org.apache.calcite.runtime.Hook) HiveSqlDialect(org.apache.calcite.sql.dialect.HiveSqlDialect) ProjectToWindowRule(org.apache.calcite.rel.rules.ProjectToWindowRule) IntStream(java.util.stream.IntStream) DatabaseProduct(org.apache.calcite.sql.SqlDialect.DatabaseProduct) SqlWriterConfig(org.apache.calcite.sql.SqlWriterConfig) SqlShuttle(org.apache.calcite.sql.util.SqlShuttle) Function(java.util.function.Function) Matchers.isLinux(org.apache.calcite.test.Matchers.isLinux) RelBuilderTest(org.apache.calcite.test.RelBuilderTest) SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) RuleSets(org.apache.calcite.tools.RuleSets) ImmutableList(com.google.common.collect.ImmutableList) OracleSqlDialect(org.apache.calcite.sql.dialect.OracleSqlDialect) HintStrategyTable(org.apache.calcite.rel.hint.HintStrategyTable) Objects.requireNonNull(java.util.Objects.requireNonNull) SqlLibrary(org.apache.calcite.sql.fun.SqlLibrary) MockSqlOperatorTable(org.apache.calcite.test.MockSqlOperatorTable) SqlLibraryOperatorTableFactory(org.apache.calcite.sql.fun.SqlLibraryOperatorTableFactory) CalciteSqlDialect(org.apache.calcite.sql.dialect.CalciteSqlDialect) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) RelDataTypeSystem(org.apache.calcite.rel.type.RelDataTypeSystem) Nullable(org.checkerframework.checker.nullness.qual.Nullable) RelDataType(org.apache.calcite.rel.type.RelDataType) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) HepPlanner(org.apache.calcite.plan.hep.HepPlanner) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) RelNode(org.apache.calcite.rel.RelNode) SqlDialect(org.apache.calcite.sql.SqlDialect) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) Consumer(java.util.function.Consumer) RelOptRule(org.apache.calcite.plan.RelOptRule) CoreRules(org.apache.calcite.rel.rules.CoreRules) LogicalAggregate(org.apache.calcite.rel.logical.LogicalAggregate) FlatLists(org.apache.calcite.runtime.FlatLists) SqlParser(org.apache.calcite.sql.parser.SqlParser) JoinRelType(org.apache.calcite.rel.core.JoinRelType) JethroDataSqlDialect(org.apache.calcite.sql.dialect.JethroDataSqlDialect) Util(org.apache.calcite.util.Util) PruneEmptyRules(org.apache.calcite.rel.rules.PruneEmptyRules) SqlNode(org.apache.calcite.sql.SqlNode)

Example 4 with SqlWriterConfig

use of org.apache.calcite.sql.SqlWriterConfig in project calcite by apache.

the class SqlPrettyWriterFixture method check.

SqlPrettyWriterFixture check() {
    final SqlWriterConfig config = transform.apply(SqlPrettyWriter.config().withDialect(AnsiSqlDialect.DEFAULT));
    final SqlPrettyWriter prettyWriter = new SqlPrettyWriter(config);
    final SqlNode node;
    if (expression) {
        final SqlCall valuesCall = (SqlCall) parseQuery("VALUES (" + sql + ")");
        final SqlCall rowCall = valuesCall.operand(0);
        node = rowCall.operand(0);
    } else {
        node = parseQuery(sql);
    }
    // Describe settings
    if (desc != null) {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw);
        prettyWriter.describe(pw, true);
        pw.flush();
        final String desc = sw.toString();
        diffRepos().assertEquals("desc", this.desc, desc);
    }
    // Format
    final String formatted = prettyWriter.format(node);
    diffRepos().assertEquals("formatted", this.formatted, formatted);
    // Now parse the result, and make sure it is structurally equivalent
    // to the original.
    final String actual2 = formatted.replace("`", "\"");
    final SqlNode node2;
    if (expression) {
        final SqlCall valuesCall = (SqlCall) parseQuery("VALUES (" + actual2 + ")");
        final SqlCall rowCall = valuesCall.operand(0);
        node2 = rowCall.operand(0);
    } else {
        node2 = parseQuery(actual2);
    }
    assertTrue(node.equalsDeep(node2, Litmus.THROW));
    return this;
}
Also used : StringWriter(java.io.StringWriter) SqlCall(org.apache.calcite.sql.SqlCall) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlWriterConfig(org.apache.calcite.sql.SqlWriterConfig) SqlNode(org.apache.calcite.sql.SqlNode) PrintWriter(java.io.PrintWriter)

Example 5 with SqlWriterConfig

use of org.apache.calcite.sql.SqlWriterConfig in project calcite by apache.

the class SqlPrettyWriterTest method main.

public static void main(String[] args) throws SqlParseException {
    final String sql = "select x as a, b as b, c as c, d," + " 'mixed-Case string'," + " unquotedCamelCaseId," + " \"quoted id\" " + "from" + " (select *" + " from t" + " where x = y and a > 5" + " group by z, zz" + " window w as (partition by c)," + "  w1 as (partition by c,d order by a, b" + "   range between interval '2:2' hour to minute preceding" + "    and interval '1' day following)) " + "order by gg desc nulls last, hh asc";
    final SqlNode node = SqlParser.create(sql).parseQuery();
    final SqlWriterConfig config = SqlPrettyWriter.config().withLineFolding(SqlWriterConfig.LineFolding.STEP).withSelectFolding(SqlWriterConfig.LineFolding.TALL).withFromFolding(SqlWriterConfig.LineFolding.TALL).withWhereFolding(SqlWriterConfig.LineFolding.TALL).withHavingFolding(SqlWriterConfig.LineFolding.TALL).withIndentation(4).withClauseEndsLine(true);
    System.out.println(new SqlPrettyWriter(config).format(node));
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlWriterConfig(org.apache.calcite.sql.SqlWriterConfig) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlWriterConfig (org.apache.calcite.sql.SqlWriterConfig)8 SqlPrettyWriter (org.apache.calcite.sql.pretty.SqlPrettyWriter)7 SqlNode (org.apache.calcite.sql.SqlNode)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 SqlCall (org.apache.calcite.sql.SqlCall)2 SqlDialect (org.apache.calcite.sql.SqlDialect)2 SqlSelect (org.apache.calcite.sql.SqlSelect)2 AnsiSqlDialect (org.apache.calcite.sql.dialect.AnsiSqlDialect)2 SqlParseException (org.apache.calcite.sql.parser.SqlParseException)2 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 CqlSession (com.datastax.oss.driver.api.core.CqlSession)1 Row (com.datastax.oss.driver.api.core.cql.Row)1 ClusteringOrder (com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)1 ColumnMetadata (com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata)1 KeyspaceMetadata (com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata)1