Search in sources :

Example 26 with SqlPrettyWriter

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

the class SqlPrettyWriterTest method testDamiansSubQueryStyle.

@Test
public void testDamiansSubQueryStyle() throws Exception {
    // Note that ( is at the indent, SELECT is on the same line, and ) is
    // below it.
    final SqlPrettyWriter prettyWriter = new SqlPrettyWriter(AnsiSqlDialect.DEFAULT);
    prettyWriter.setSubQueryStyle(SqlWriter.SubQueryStyle.BLACK);
    checkSimple(prettyWriter, "${desc}", "${formatted}");
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) Test(org.junit.Test)

Example 27 with SqlPrettyWriter

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

the class SqlOperatorTest method testArgumentBounds.

/**
 * Test that calls all operators with all possible argument types, and for
 * each type, with a set of tricky values.
 *
 * <p>This is not really a unit test since there are no assertions;
 * it either succeeds or fails in the preparation of the operator case
 * and not when actually testing (validating/executing) the call.
 *
 * <p>Nevertheless the log messages conceal many problems which potentially
 * need to be fixed especially cases where the query passes from the
 * validation stage and fails at runtime.
 */
@Disabled("Too slow and not really a unit test")
@Tag("slow")
@Test
void testArgumentBounds() {
    final SqlOperatorFixture f = fixture();
    final SqlValidatorImpl validator = (SqlValidatorImpl) f.getFactory().createValidator();
    final SqlValidatorScope scope = validator.getEmptyScope();
    final RelDataTypeFactory typeFactory = validator.getTypeFactory();
    final Builder builder = new Builder(typeFactory);
    builder.add0(SqlTypeName.BOOLEAN, true, false);
    builder.add0(SqlTypeName.TINYINT, 0, 1, -3, Byte.MAX_VALUE, Byte.MIN_VALUE);
    builder.add0(SqlTypeName.SMALLINT, 0, 1, -4, Short.MAX_VALUE, Short.MIN_VALUE);
    builder.add0(SqlTypeName.INTEGER, 0, 1, -2, Integer.MIN_VALUE, Integer.MAX_VALUE);
    builder.add0(SqlTypeName.BIGINT, 0, 1, -5, Integer.MAX_VALUE, Long.MAX_VALUE, Long.MIN_VALUE);
    builder.add1(SqlTypeName.VARCHAR, 11, "", " ", "hello world");
    builder.add1(SqlTypeName.CHAR, 5, "", "e", "hello");
    builder.add0(SqlTypeName.TIMESTAMP, 0L, DateTimeUtils.MILLIS_PER_DAY);
    Set<SqlOperator> operatorsToSkip = new HashSet<>();
    if (!Bug.CALCITE_3243_FIXED) {
        // TODO: Remove entirely the if block when the bug is fixed
        // REVIEW zabetak 12-August-2019: It may still make sense to avoid the
        // JSON functions since for most of the values above they are expected
        // to raise an error and due to the big number of operands they accept
        // they increase significantly the running time of the method.
        operatorsToSkip.add(SqlStdOperatorTable.JSON_VALUE);
        operatorsToSkip.add(SqlStdOperatorTable.JSON_QUERY);
    }
    // Skip since ClassCastException is raised in SqlOperator#unparse
    // since the operands of the call do not have the expected type.
    // Moreover, the values above do not make much sense for this operator.
    operatorsToSkip.add(SqlStdOperatorTable.WITHIN_GROUP);
    // can't handle the flag argument
    operatorsToSkip.add(SqlStdOperatorTable.TRIM);
    operatorsToSkip.add(SqlStdOperatorTable.EXISTS);
    for (SqlOperator op : SqlStdOperatorTable.instance().getOperatorList()) {
        if (operatorsToSkip.contains(op)) {
            continue;
        }
        if (op.getSyntax() == SqlSyntax.SPECIAL) {
            continue;
        }
        final SqlOperandTypeChecker typeChecker = op.getOperandTypeChecker();
        if (typeChecker == null) {
            continue;
        }
        final SqlOperandCountRange range = typeChecker.getOperandCountRange();
        for (int n = range.getMin(), max = range.getMax(); n <= max; n++) {
            final List<List<ValueType>> argValues = Collections.nCopies(n, builder.values);
            for (final List<ValueType> args : Linq4j.product(argValues)) {
                SqlNodeList nodeList = new SqlNodeList(SqlParserPos.ZERO);
                int nullCount = 0;
                for (ValueType arg : args) {
                    if (arg.value == null) {
                        ++nullCount;
                    }
                    nodeList.add(arg.node);
                }
                final SqlCall call = op.createCall(nodeList);
                final SqlCallBinding binding = new SqlCallBinding(validator, scope, call);
                if (!typeChecker.checkOperandTypes(binding, false)) {
                    continue;
                }
                final SqlPrettyWriter writer = new SqlPrettyWriter();
                op.unparse(writer, call, 0, 0);
                final String s = writer.toSqlString().toString();
                if (s.startsWith("OVERLAY(") || s.contains(" / 0") || s.matches("MOD\\(.*, 0\\)")) {
                    continue;
                }
                final Strong.Policy policy = Strong.policy(op);
                try {
                    if (nullCount > 0 && policy == Strong.Policy.ANY) {
                        f.checkNull(s);
                    } else {
                        final String query;
                        if (op instanceof SqlAggFunction) {
                            if (op.requiresOrder()) {
                                query = "SELECT " + s + " OVER () FROM (VALUES (1))";
                            } else {
                                query = "SELECT " + s + " FROM (VALUES (1))";
                            }
                        } else {
                            query = AbstractSqlTester.buildQuery(s);
                        }
                        f.check(query, SqlTests.ANY_TYPE_CHECKER, SqlTests.ANY_PARAMETER_CHECKER, result -> {
                        });
                    }
                } catch (Throwable e) {
                    // Logging the top-level throwable directly makes the message
                    // difficult to read since it either contains too much information
                    // or very few details.
                    Throwable cause = findMostDescriptiveCause(e);
                    LOGGER.info("Failed: " + s + ": " + cause);
                }
            }
        }
    }
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlCall(org.apache.calcite.sql.SqlCall) TimestampString(org.apache.calcite.util.TimestampString) SqlString(org.apache.calcite.sql.util.SqlString) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) Strong(org.apache.calcite.plan.Strong) SqlOperandCountRange(org.apache.calcite.sql.SqlOperandCountRange) SqlValidatorImpl(org.apache.calcite.sql.validate.SqlValidatorImpl) SqlOperatorFixture(org.apache.calcite.sql.test.SqlOperatorFixture) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlOperandTypeChecker(org.apache.calcite.sql.type.SqlOperandTypeChecker) ArrayList(java.util.ArrayList) List(java.util.List) SqlNodeList(org.apache.calcite.sql.SqlNodeList) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag) Disabled(org.junit.jupiter.api.Disabled)

Example 28 with SqlPrettyWriter

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

the class ServerDdlExecutor method populate.

/**
 * Populates the table called {@code name} by executing {@code query}.
 */
static void populate(SqlIdentifier name, SqlNode query, CalcitePrepare.Context context) {
    // Generate, prepare and execute an "INSERT INTO table query" statement.
    // (It's a bit inefficient that we convert from SqlNode to SQL and back
    // again.)
    final FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(context.getRootSchema().plus()).build();
    final Planner planner = Frameworks.getPlanner(config);
    try {
        final StringBuilder buf = new StringBuilder();
        final SqlWriterConfig writerConfig = SqlPrettyWriter.config().withAlwaysUseParentheses(false);
        final SqlPrettyWriter w = new SqlPrettyWriter(writerConfig, buf);
        buf.append("INSERT INTO ");
        name.unparse(w, 0, 0);
        buf.append(' ');
        query.unparse(w, 0, 0);
        final String sql = buf.toString();
        final SqlNode query1 = planner.parse(sql);
        final SqlNode query2 = planner.validate(query1);
        final RelRoot r = planner.rel(query2);
        final PreparedStatement prepare = context.getRelRunner().prepareStatement(r.rel);
        int rowCount = prepare.executeUpdate();
        Util.discard(rowCount);
        prepare.close();
    } catch (SqlParseException | ValidationException | RelConversionException | SQLException e) {
        throw Util.throwAsRuntime(e);
    }
}
Also used : ValidationException(org.apache.calcite.tools.ValidationException) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) SQLException(java.sql.SQLException) SqlWriterConfig(org.apache.calcite.sql.SqlWriterConfig) RelRoot(org.apache.calcite.rel.RelRoot) PreparedStatement(java.sql.PreparedStatement) NlsString(org.apache.calcite.util.NlsString) RelConversionException(org.apache.calcite.tools.RelConversionException) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) Planner(org.apache.calcite.tools.Planner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) SqlNode(org.apache.calcite.sql.SqlNode)

Example 29 with SqlPrettyWriter

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

the class SqlParserTest method testSqlOptions.

@Test
void testSqlOptions() {
    SqlNode node = sql("alter system set schema = true").node();
    SqlSetOption opt = (SqlSetOption) node;
    assertThat(opt.getScope(), equalTo("SYSTEM"));
    SqlPrettyWriter writer = new SqlPrettyWriter();
    assertThat(writer.format(opt.getName()), equalTo("\"SCHEMA\""));
    writer = new SqlPrettyWriter();
    assertThat(writer.format(opt.getValue()), equalTo("TRUE"));
    writer = new SqlPrettyWriter();
    assertThat(writer.format(opt), equalTo("ALTER SYSTEM SET \"SCHEMA\" = TRUE"));
    sql("alter system set \"a number\" = 1").ok("ALTER SYSTEM SET `a number` = 1").node(isDdl());
    sql("alter system set flag = false").ok("ALTER SYSTEM SET `FLAG` = FALSE");
    sql("alter system set approx = -12.3450").ok("ALTER SYSTEM SET `APPROX` = -12.3450");
    sql("alter system set onOff = on").ok("ALTER SYSTEM SET `ONOFF` = `ON`");
    sql("alter system set onOff = off").ok("ALTER SYSTEM SET `ONOFF` = `OFF`");
    sql("alter system set baz = foo").ok("ALTER SYSTEM SET `BAZ` = `FOO`");
    sql("alter system set \"a\".\"number\" = 1").ok("ALTER SYSTEM SET `a`.`number` = 1");
    sql("set approx = -12.3450").ok("SET `APPROX` = -12.3450").node(isDdl());
    node = sql("reset schema").node();
    opt = (SqlSetOption) node;
    assertThat(opt.getScope(), equalTo(null));
    writer = new SqlPrettyWriter();
    assertThat(writer.format(opt.getName()), equalTo("\"SCHEMA\""));
    assertThat(opt.getValue(), equalTo(null));
    writer = new SqlPrettyWriter();
    assertThat(writer.format(opt), equalTo("RESET \"SCHEMA\""));
    sql("alter system RESET flag").ok("ALTER SYSTEM RESET `FLAG`");
    sql("reset onOff").ok("RESET `ONOFF`").node(isDdl());
    sql("reset \"this\".\"is\".\"sparta\"").ok("RESET `this`.`is`.`sparta`");
    sql("alter system reset all").ok("ALTER SYSTEM RESET `ALL`");
    sql("reset all").ok("RESET `ALL`");
    // expressions not allowed
    sql("alter system set aString = 'abc' ^||^ 'def' ").fails("(?s)Encountered \"\\|\\|\" at line 1, column 34\\..*");
    // multiple assignments not allowed
    sql("alter system set x = 1^,^ y = 2").fails("(?s)Encountered \",\" at line 1, column 23\\..*");
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlSetOption(org.apache.calcite.sql.SqlSetOption) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.jupiter.api.Test)

Example 30 with SqlPrettyWriter

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

the class CassandraSchema method addMaterializedViews.

/**
 * Adds all materialized views defined in the schema to this column family.
 */
private void addMaterializedViews() {
    // Close the hook used to get us here
    hook.close();
    for (ViewMetadata view : getKeyspace().getViews().values()) {
        String tableName = view.getBaseTable().asInternal();
        StringBuilder queryBuilder = new StringBuilder("SELECT ");
        // Add all the selected columns to the query
        String columnsList = view.getColumns().values().stream().map(c -> c.getName().asInternal()).collect(Collectors.joining(", "));
        queryBuilder.append(columnsList);
        queryBuilder.append(" FROM ").append(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().asInternal() + "'";
        Row whereClauseRow = Objects.requireNonNull(session.execute(whereQuery).one());
        queryBuilder.append(" WHERE ").append(whereClauseRow.getString(0));
        // Parse and unparse the view query to get properly quoted field names
        String query = queryBuilder.toString();
        SqlParser.Config parserConfig = SqlParser.config().withUnquotedCasing(Casing.UNCHANGED);
        SqlSelect parsedQuery;
        try {
            parsedQuery = (SqlSelect) SqlParser.create(query, parserConfig).parseQuery();
        } catch (SqlParseException e) {
            LOGGER.warn("Could not parse query {} for CQL view {}.{}", query, keyspace, view.getName().asInternal());
            continue;
        }
        final StringBuilder buf = new StringBuilder(query.length());
        final SqlWriterConfig config = SqlPrettyWriter.config().withAlwaysUseParentheses(true);
        final SqlWriter writer = new SqlPrettyWriter(config, buf);
        parsedQuery.unparse(writer, 0, 0);
        query = buf.toString();
        // Add the view for this query
        String viewName = "$" + getTableNames().size();
        SchemaPlus schema = parentSchema.getSubSchema(name);
        if (schema == null) {
            throw new IllegalStateException("Cannot find schema " + name + " in parent schema " + parentSchema.getName());
        }
        CalciteSchema calciteSchema = CalciteSchema.from(schema);
        List<String> viewPath = calciteSchema.path(viewName);
        schema.add(viewName, MaterializedViewTable.create(calciteSchema, query, null, viewPath, view.getName().asInternal(), true));
    }
}
Also used : RelDataTypeImpl(org.apache.calcite.rel.type.RelDataTypeImpl) IntStream(java.util.stream.IntStream) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) SqlWriterConfig(org.apache.calcite.sql.SqlWriterConfig) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ListType(com.datastax.oss.driver.api.core.type.ListType) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder) ViewMetadata(com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata) ArrayList(java.util.ArrayList) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Pair(org.apache.calcite.util.Pair) Map(java.util.Map) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) SqlSelect(org.apache.calcite.sql.SqlSelect) RelDataTypeSystem(org.apache.calcite.rel.type.RelDataTypeSystem) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) Row(com.datastax.oss.driver.api.core.cql.Row) RelDataType(org.apache.calcite.rel.type.RelDataType) TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) MaterializedViewTable(org.apache.calcite.schema.impl.MaterializedViewTable) RelationMetadata(com.datastax.oss.driver.api.core.metadata.schema.RelationMetadata) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SqlWriter(org.apache.calcite.sql.SqlWriter) Logger(org.slf4j.Logger) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) MapType(com.datastax.oss.driver.api.core.type.MapType) ImmutableMap(com.google.common.collect.ImmutableMap) TupleType(com.datastax.oss.driver.api.core.type.TupleType) Collection(java.util.Collection) Casing(org.apache.calcite.avatica.util.Casing) DataType(com.datastax.oss.driver.api.core.type.DataType) Table(org.apache.calcite.schema.Table) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RelProtoDataType(org.apache.calcite.rel.type.RelProtoDataType) Collectors(java.util.stream.Collectors) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) Objects(java.util.Objects) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) List(java.util.List) SqlParser(org.apache.calcite.sql.parser.SqlParser) CalciteTrace(org.apache.calcite.util.trace.CalciteTrace) Optional(java.util.Optional) SetType(com.datastax.oss.driver.api.core.type.SetType) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) Hook(org.apache.calcite.runtime.Hook) SqlWriter(org.apache.calcite.sql.SqlWriter) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) SqlParser(org.apache.calcite.sql.parser.SqlParser) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SqlWriterConfig(org.apache.calcite.sql.SqlWriterConfig) SqlSelect(org.apache.calcite.sql.SqlSelect) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) Row(com.datastax.oss.driver.api.core.cql.Row) ViewMetadata(com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata)

Aggregations

SqlPrettyWriter (org.apache.calcite.sql.pretty.SqlPrettyWriter)30 SqlNode (org.apache.calcite.sql.SqlNode)12 Test (org.junit.Test)12 SqlWriterConfig (org.apache.calcite.sql.SqlWriterConfig)7 SQLException (java.sql.SQLException)4 SqlCall (org.apache.calcite.sql.SqlCall)4 SqlParseException (org.apache.calcite.sql.parser.SqlParseException)4 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 PreparedStatement (java.sql.PreparedStatement)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 RelRoot (org.apache.calcite.rel.RelRoot)3 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)3 SqlString (org.apache.calcite.sql.util.SqlString)3 FrameworkConfig (org.apache.calcite.tools.FrameworkConfig)3 Planner (org.apache.calcite.tools.Planner)3 RelConversionException (org.apache.calcite.tools.RelConversionException)3 ValidationException (org.apache.calcite.tools.ValidationException)3 SqlIdentifier (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier)2