forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-23544][table-planner] Window TVF Supports session window in plan
This closes apache#16669
- Loading branch information
1 parent
3921231
commit 34d5100
Showing
19 changed files
with
1,004 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...r/src/main/java/org/apache/flink/table/planner/functions/sql/SqlSessionTableFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.flink.table.planner.functions.sql; | ||
|
||
import org.apache.flink.shaded.guava30.com.google.common.collect.ImmutableList; | ||
|
||
import org.apache.calcite.sql.SqlCallBinding; | ||
import org.apache.calcite.sql.SqlKind; | ||
import org.apache.calcite.sql.SqlOperator; | ||
|
||
/** | ||
* SqlSessionTableFunction implements an operator for session. | ||
* | ||
* <p>It allows three parameters: | ||
* | ||
* <ol> | ||
* <li>a table | ||
* <li>a descriptor to provide a time attribute column name from the input table | ||
* <li>an interval parameter to specify an inactive activity gap to break sessions | ||
* </ol> | ||
*/ | ||
public class SqlSessionTableFunction extends SqlWindowTableFunction { | ||
|
||
public SqlSessionTableFunction() { | ||
super(SqlKind.SESSION.name(), new OperandMetadataImpl()); | ||
} | ||
|
||
/** Operand type checker for SESSION. */ | ||
private static class OperandMetadataImpl extends AbstractOperandMetadata { | ||
OperandMetadataImpl() { | ||
super(ImmutableList.of(PARAM_DATA, PARAM_TIMECOL, PARAM_SESSION_GAP), 3); | ||
} | ||
|
||
@Override | ||
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) { | ||
if (!checkTableAndDescriptorOperands(callBinding, 1)) { | ||
return throwValidationSignatureErrorOrReturnFalse(callBinding, throwOnFailure); | ||
} | ||
if (!checkIntervalOperands(callBinding, 2)) { | ||
return throwValidationSignatureErrorOrReturnFalse(callBinding, throwOnFailure); | ||
} | ||
// check time attribute | ||
return throwExceptionOrReturnFalse( | ||
checkTimeColumnDescriptorOperand(callBinding, 1), throwOnFailure); | ||
} | ||
|
||
@Override | ||
public String getAllowedSignatures(SqlOperator op, String opName) { | ||
return opName + "(TABLE table_name, DESCRIPTOR(timecol), datetime interval)"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...-planner/src/main/java/org/apache/flink/table/planner/plan/logical/SessionWindowSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.flink.table.planner.plan.logical; | ||
|
||
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonCreator; | ||
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty; | ||
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonTypeName; | ||
|
||
import java.time.Duration; | ||
import java.util.Objects; | ||
|
||
import static org.apache.flink.util.Preconditions.checkNotNull; | ||
import static org.apache.flink.util.TimeUtils.formatWithHighestUnit; | ||
|
||
/** Logical representation of a session window specification. */ | ||
@JsonTypeName("SessionWindow") | ||
public class SessionWindowSpec implements WindowSpec { | ||
public static final String FIELD_NAME_GAP = "gap"; | ||
|
||
@JsonProperty(FIELD_NAME_GAP) | ||
private final Duration gap; | ||
|
||
@JsonCreator | ||
public SessionWindowSpec(@JsonProperty(FIELD_NAME_GAP) Duration gap) { | ||
this.gap = checkNotNull(gap); | ||
} | ||
|
||
@Override | ||
public String toSummaryString(String windowing) { | ||
return String.format("SESSION(%s, gap=[%s])", windowing, formatWithHighestUnit(gap)); | ||
} | ||
|
||
public Duration getGap() { | ||
return gap; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
SessionWindowSpec that = (SessionWindowSpec) o; | ||
return gap.equals(that.gap); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(SessionWindowSpec.class, gap); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("SESSION(gap=[%s])", formatWithHighestUnit(gap)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.