Search in sources :

Example 1 with NodeLocation

use of io.confluent.ksql.parser.NodeLocation in project ksql by confluentinc.

the class RewrittenAnalysis method getWindowExpression.

@Override
public Optional<WindowExpression> getWindowExpression() {
    final Optional<WindowExpression> windowExpression = original.getWindowExpression();
    final Optional<RefinementInfo> refinementInfo = original.getRefinementInfo();
    /* Return the original window expression, unless there is no grace period provided during a
    suppression, in which case we rewrite the window expression to have a default grace period
    of zero.
    */
    if (!(windowExpression.isPresent() && !windowExpression.get().getKsqlWindowExpression().getGracePeriod().isPresent() && refinementInfo.isPresent() && refinementInfo.get().getOutputRefinement() == OutputRefinement.FINAL)) {
        return original.getWindowExpression();
    }
    final WindowExpression window = original.getWindowExpression().get();
    final KsqlWindowExpression ksqlWindowNew;
    final KsqlWindowExpression ksqlWindowOld = window.getKsqlWindowExpression();
    final Optional<NodeLocation> location = ksqlWindowOld.getLocation();
    final Optional<WindowTimeClause> retention = ksqlWindowOld.getRetention();
    if (ksqlWindowOld instanceof HoppingWindowExpression) {
        ksqlWindowNew = new HoppingWindowExpression(location, ((HoppingWindowExpression) ksqlWindowOld).getSize(), ((HoppingWindowExpression) ksqlWindowOld).getAdvanceBy(), retention, Optional.of(zeroGracePeriod));
    } else if (ksqlWindowOld instanceof TumblingWindowExpression) {
        ksqlWindowNew = new TumblingWindowExpression(location, ((TumblingWindowExpression) ksqlWindowOld).getSize(), retention, Optional.of(zeroGracePeriod));
    } else if (ksqlWindowOld instanceof SessionWindowExpression) {
        ksqlWindowNew = new SessionWindowExpression(location, ((SessionWindowExpression) ksqlWindowOld).getGap(), retention, Optional.of(zeroGracePeriod));
    } else {
        throw new KsqlException("WINDOW type must be HOPPING, TUMBLING, or SESSION");
    }
    return Optional.of(new WindowExpression(original.getWindowExpression().get().getWindowName(), ksqlWindowNew));
}
Also used : HoppingWindowExpression(io.confluent.ksql.execution.windows.HoppingWindowExpression) TumblingWindowExpression(io.confluent.ksql.execution.windows.TumblingWindowExpression) WindowExpression(io.confluent.ksql.parser.tree.WindowExpression) KsqlWindowExpression(io.confluent.ksql.execution.windows.KsqlWindowExpression) SessionWindowExpression(io.confluent.ksql.execution.windows.SessionWindowExpression) HoppingWindowExpression(io.confluent.ksql.execution.windows.HoppingWindowExpression) KsqlException(io.confluent.ksql.util.KsqlException) SessionWindowExpression(io.confluent.ksql.execution.windows.SessionWindowExpression) NodeLocation(io.confluent.ksql.parser.NodeLocation) RefinementInfo(io.confluent.ksql.serde.RefinementInfo) KsqlWindowExpression(io.confluent.ksql.execution.windows.KsqlWindowExpression) WindowTimeClause(io.confluent.ksql.execution.windows.WindowTimeClause) TumblingWindowExpression(io.confluent.ksql.execution.windows.TumblingWindowExpression)

Example 2 with NodeLocation

use of io.confluent.ksql.parser.NodeLocation in project ksql by confluentinc.

the class SqlTypeParser method getType.

public Type getType(final SqlBaseParser.TypeContext type) {
    final Optional<NodeLocation> location = ParserUtil.getLocation(type);
    final SqlType sqlType = getSqlType(type);
    return new Type(location, sqlType);
}
Also used : Type(io.confluent.ksql.execution.expression.tree.Type) SqlPrimitiveType(io.confluent.ksql.schema.ksql.types.SqlPrimitiveType) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) NodeLocation(io.confluent.ksql.parser.NodeLocation) SqlType(io.confluent.ksql.schema.ksql.types.SqlType)

Example 3 with NodeLocation

use of io.confluent.ksql.parser.NodeLocation in project ksql by confluentinc.

the class ParserUtil method visitIntegerLiteral.

public static Literal visitIntegerLiteral(final IntegerLiteralContext context) {
    final Optional<NodeLocation> location = getLocation(context);
    final long valueAsLong;
    try {
        valueAsLong = Long.parseLong(context.getText());
    } catch (final NumberFormatException e) {
        throw new ParsingException("Invalid numeric literal: " + context.getText(), location);
    }
    if (valueAsLong <= Integer.MAX_VALUE && valueAsLong >= Integer.MIN_VALUE) {
        return new IntegerLiteral(location, (int) valueAsLong);
    } else {
        return new LongLiteral(location, valueAsLong);
    }
}
Also used : NodeLocation(io.confluent.ksql.parser.NodeLocation) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) ParsingException(io.confluent.ksql.parser.ParsingException) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral)

Example 4 with NodeLocation

use of io.confluent.ksql.parser.NodeLocation in project ksql by confluentinc.

the class DirectiveParser method parse.

public static TestDirective parse(final Token comment) {
    final NodeLocation loc = new NodeLocation(comment.getLine(), comment.getCharPositionInLine());
    final Matcher matcher = DIRECTIVE_REGEX.matcher(comment.getText().trim());
    if (!matcher.find()) {
        throw new ParsingException("Expected directive matching pattern " + DIRECTIVE_REGEX + " but got " + comment, null, loc.getLineNumber(), loc.getColumnNumber());
    }
    final Type type = Type.from(matcher.group("type").toLowerCase());
    final String contents = matcher.group("contents");
    return new TestDirective(type, contents, loc);
}
Also used : Type(io.confluent.ksql.test.parser.TestDirective.Type) NodeLocation(io.confluent.ksql.parser.NodeLocation) Matcher(java.util.regex.Matcher) ParsingException(io.confluent.ksql.parser.ParsingException)

Example 5 with NodeLocation

use of io.confluent.ksql.parser.NodeLocation in project ksql by confluentinc.

the class ColumnReferenceValidatorTest method shouldIncludeLocationInErrorIfKnown.

@Test
public void shouldIncludeLocationInErrorIfKnown() {
    // Given:
    final Expression expression = new UnqualifiedColumnReferenceExp(Optional.of(new NodeLocation(10, 23)), ColumnName.of("just-name"));
    when(sourceSchemas.sourcesWithField(any(), any())).thenReturn(ImmutableSet.of());
    // When:
    final Exception e = assertThrows(UnknownColumnException.class, () -> analyzer.analyzeExpression(expression, CLAUSE_TYPE));
    // Then:
    assertThat(e.getMessage(), containsString("Line: 10, Col: 24: " + CLAUSE_TYPE));
}
Also used : NodeLocation(io.confluent.ksql.parser.NodeLocation) Expression(io.confluent.ksql.execution.expression.tree.Expression) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) UnknownColumnException(io.confluent.ksql.util.UnknownColumnException) Test(org.junit.Test)

Aggregations

NodeLocation (io.confluent.ksql.parser.NodeLocation)7 Type (io.confluent.ksql.execution.expression.tree.Type)3 Test (org.junit.Test)3 ColumnName (io.confluent.ksql.name.ColumnName)2 ParsingException (io.confluent.ksql.parser.ParsingException)2 ParseFailedException (io.confluent.ksql.parser.exception.ParseFailedException)2 Expression (io.confluent.ksql.execution.expression.tree.Expression)1 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)1 LongLiteral (io.confluent.ksql.execution.expression.tree.LongLiteral)1 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)1 HoppingWindowExpression (io.confluent.ksql.execution.windows.HoppingWindowExpression)1 KsqlWindowExpression (io.confluent.ksql.execution.windows.KsqlWindowExpression)1 SessionWindowExpression (io.confluent.ksql.execution.windows.SessionWindowExpression)1 TumblingWindowExpression (io.confluent.ksql.execution.windows.TumblingWindowExpression)1 WindowTimeClause (io.confluent.ksql.execution.windows.WindowTimeClause)1 WindowExpression (io.confluent.ksql.parser.tree.WindowExpression)1 SqlPrimitiveType (io.confluent.ksql.schema.ksql.types.SqlPrimitiveType)1 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)1 RefinementInfo (io.confluent.ksql.serde.RefinementInfo)1 Type (io.confluent.ksql.test.parser.TestDirective.Type)1