Repository: tajo Updated Branches: refs/heads/index_support 7a38895d4 -> 3cb2a427f
TAJO-920: Add FIRST_VALUE and LAST_VALUE window functions. (Keuntae Park via hyunsik) Closes #308 Project: http://git-wip-us.apache.org/repos/asf/tajo/repo Commit: http://git-wip-us.apache.org/repos/asf/tajo/commit/fd49bff1 Tree: http://git-wip-us.apache.org/repos/asf/tajo/tree/fd49bff1 Diff: http://git-wip-us.apache.org/repos/asf/tajo/diff/fd49bff1 Branch: refs/heads/index_support Commit: fd49bff19dd27262cc7abdd4bbf3cd0d5de14dfb Parents: 4c713fb Author: Hyunsik Choi <[email protected]> Authored: Fri Dec 26 19:19:45 2014 +0900 Committer: Hyunsik Choi <[email protected]> Committed: Fri Dec 26 19:19:45 2014 +0900 ---------------------------------------------------------------------- CHANGES | 3 + .../org/apache/tajo/engine/parser/SQLLexer.g4 | 2 + .../org/apache/tajo/engine/parser/SQLParser.g4 | 4 ++ .../tajo/engine/function/builtin/LastValue.java | 67 +++++++++++++++++ .../engine/function/builtin/LastValueDate.java | 46 ++++++++++++ .../function/builtin/LastValueDouble.java | 46 ++++++++++++ .../engine/function/builtin/LastValueFloat.java | 46 ++++++++++++ .../engine/function/builtin/LastValueInt.java | 46 ++++++++++++ .../engine/function/builtin/LastValueLong.java | 46 ++++++++++++ .../function/builtin/LastValueString.java | 46 ++++++++++++ .../engine/function/builtin/LastValueTime.java | 46 ++++++++++++ .../function/builtin/LastValueTimestamp.java | 46 ++++++++++++ .../tajo/engine/function/window/FirstValue.java | 64 +++++++++++++++++ .../engine/function/window/FirstValueDate.java | 41 +++++++++++ .../function/window/FirstValueDouble.java | 41 +++++++++++ .../engine/function/window/FirstValueFloat.java | 41 +++++++++++ .../engine/function/window/FirstValueInt.java | 41 +++++++++++ .../engine/function/window/FirstValueLong.java | 41 +++++++++++ .../function/window/FirstValueString.java | 41 +++++++++++ .../engine/function/window/FirstValueTime.java | 41 +++++++++++ .../function/window/FirstValueTimestamp.java | 41 +++++++++++ .../apache/tajo/engine/parser/SQLAnalyzer.java | 4 ++ .../tajo/engine/query/TestWindowQuery.java | 75 ++++++++++++++++++++ .../queries/TestWindowQuery/firstValue1.sql | 18 +++++ .../queries/TestWindowQuery/lastValue1.sql | 18 +++++ .../results/TestWindowQuery/firstValue1.result | 7 ++ .../results/TestWindowQuery/lastValue1.result | 7 ++ .../org/apache/tajo/plan/ExprAnnotator.java | 2 +- 28 files changed, 966 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/CHANGES ---------------------------------------------------------------------- diff --git a/CHANGES b/CHANGES index 9cd2a48..c35c24a 100644 --- a/CHANGES +++ b/CHANGES @@ -5,6 +5,9 @@ Release 0.9.1 - unreleased NEW FEATURES + TAJO-920: Add FIRST_VALUE and LAST_VALUE window functions. + (Keuntae Park via hyunsik) + TAJO-1238: Add SET SESSION and RESET statement. (hyunsik) TAJO-1222: DelimitedTextFile should be tolerant against parsing errors. http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4 ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4 b/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4 index 3ba008f..48a6fbe 100644 --- a/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4 +++ b/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4 @@ -232,6 +232,7 @@ EXTRACT : E X T R A C T; FILTER : F I L T E R; FIRST : F I R S T; +FIRST_VALUE : F I R S T UNDERLINE V A L U E; FOLLOWING : F O L L O W I N G; FORMAT : F O R M A T; FUSION : F U S I O N; @@ -249,6 +250,7 @@ ISODOW : I S O D O W; ISOYEAR : I S O Y E A R; LAST : L A S T; +LAST_VALUE : L A S T UNDERLINE V A L U E; LESS : L E S S; LIST : L I S T; LOCATION : L O C A T I O N; http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4 ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4 b/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4 index f3cd298..0bc89db 100644 --- a/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4 +++ b/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4 @@ -257,6 +257,7 @@ nonreserved_keywords | EXTRACT | FILTER | FIRST + | FIRST_VALUE | FOLLOWING | FORMAT | FUSION @@ -268,6 +269,7 @@ nonreserved_keywords | ISODOW | ISOYEAR | LAST + | LAST_VALUE | LESS | LIST | LOCATION @@ -627,6 +629,8 @@ window_function_type : rank_function_type LEFT_PAREN RIGHT_PAREN | ROW_NUMBER LEFT_PAREN RIGHT_PAREN | aggregate_function + | FIRST_VALUE LEFT_PAREN column_reference RIGHT_PAREN + | LAST_VALUE LEFT_PAREN column_reference RIGHT_PAREN ; rank_function_type http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValue.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValue.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValue.java new file mode 100644 index 0000000..db4f79f --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValue.java @@ -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.tajo.engine.function.builtin; + +import org.apache.tajo.catalog.Column; +import org.apache.tajo.datum.Datum; +import org.apache.tajo.datum.NullDatum; +import org.apache.tajo.plan.function.AggFunction; +import org.apache.tajo.plan.function.FunctionContext; +import org.apache.tajo.storage.Tuple; + + +public abstract class LastValue extends AggFunction<Datum> { + + public LastValue(Column[] columns) { + super(columns); + } + + @Override + public FunctionContext newContext() { + return new LastValueContext(); + } + + @Override + public void eval(FunctionContext ctx, Tuple params) { + LastValueContext lastValueCtx = (LastValueContext) ctx; + Datum datum = params.get(0); + if ( datum.isNotNull() ) { + lastValueCtx.last = datum; + } + } + + @Override + public Datum getPartialResult(FunctionContext ctx) { + return ((LastValueContext) ctx).last; + } + + @Override + public Datum terminate(FunctionContext ctx) { + if (((LastValueContext) ctx).last == null) { + return NullDatum.get(); + } + else { + return ((LastValueContext) ctx).last; + } + } + + private class LastValueContext implements FunctionContext { + Datum last = null; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueDate.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueDate.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueDate.java new file mode 100644 index 0000000..950e6c4 --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueDate.java @@ -0,0 +1,46 @@ +/** + * 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.tajo.engine.function.builtin; + +import org.apache.tajo.catalog.CatalogUtil; +import org.apache.tajo.catalog.Column; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.engine.function.annotation.Description; +import org.apache.tajo.engine.function.annotation.ParamTypes; + +@Description( + functionName = "last_value", + description = "the last value of expr", + example = "> SELECT last_value(expr);", + returnType = TajoDataTypes.Type.DATE, + paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.DATE})} +) +public class LastValueDate extends LastValue { + + public LastValueDate() { + super(new Column[] { + new Column("expr", TajoDataTypes.Type.DATE) + }); + } + + @Override + public TajoDataTypes.DataType getPartialResultType() { + return CatalogUtil.newSimpleDataType(TajoDataTypes.Type.DATE); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueDouble.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueDouble.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueDouble.java new file mode 100644 index 0000000..3bd71f7 --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueDouble.java @@ -0,0 +1,46 @@ +/** + * 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.tajo.engine.function.builtin; + +import org.apache.tajo.catalog.CatalogUtil; +import org.apache.tajo.catalog.Column; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.engine.function.annotation.Description; +import org.apache.tajo.engine.function.annotation.ParamTypes; + +@Description( + functionName = "last_value", + description = "the last value of expr", + example = "> SELECT last_value(expr);", + returnType = TajoDataTypes.Type.FLOAT8, + paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.FLOAT8})} +) +public class LastValueDouble extends LastValue { + + public LastValueDouble() { + super(new Column[] { + new Column("expr", TajoDataTypes.Type.FLOAT8) + }); + } + + @Override + public TajoDataTypes.DataType getPartialResultType() { + return CatalogUtil.newSimpleDataType(TajoDataTypes.Type.FLOAT8); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueFloat.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueFloat.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueFloat.java new file mode 100644 index 0000000..d705cd7 --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueFloat.java @@ -0,0 +1,46 @@ +/** + * 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.tajo.engine.function.builtin; + +import org.apache.tajo.catalog.CatalogUtil; +import org.apache.tajo.catalog.Column; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.engine.function.annotation.Description; +import org.apache.tajo.engine.function.annotation.ParamTypes; + +@Description( + functionName = "last_value", + description = "the last value of expr", + example = "> SELECT last_value(expr);", + returnType = TajoDataTypes.Type.FLOAT4, + paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.FLOAT4})} +) +public class LastValueFloat extends LastValue { + + public LastValueFloat() { + super(new Column[] { + new Column("expr", TajoDataTypes.Type.FLOAT4) + }); + } + + @Override + public TajoDataTypes.DataType getPartialResultType() { + return CatalogUtil.newSimpleDataType(TajoDataTypes.Type.FLOAT4); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueInt.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueInt.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueInt.java new file mode 100644 index 0000000..5b44649 --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueInt.java @@ -0,0 +1,46 @@ +/** + * 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.tajo.engine.function.builtin; + +import org.apache.tajo.catalog.CatalogUtil; +import org.apache.tajo.catalog.Column; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.engine.function.annotation.Description; +import org.apache.tajo.engine.function.annotation.ParamTypes; + +@Description( + functionName = "last_value", + description = "the last value of expr", + example = "> SELECT last_value(expr);", + returnType = TajoDataTypes.Type.INT4, + paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.INT4})} +) +public class LastValueInt extends LastValue { + + public LastValueInt() { + super(new Column[] { + new Column("expr", TajoDataTypes.Type.INT4) + }); + } + + @Override + public TajoDataTypes.DataType getPartialResultType() { + return CatalogUtil.newSimpleDataType(TajoDataTypes.Type.INT4); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueLong.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueLong.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueLong.java new file mode 100644 index 0000000..210b9368 --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueLong.java @@ -0,0 +1,46 @@ +/** + * 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.tajo.engine.function.builtin; + +import org.apache.tajo.catalog.CatalogUtil; +import org.apache.tajo.catalog.Column; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.engine.function.annotation.Description; +import org.apache.tajo.engine.function.annotation.ParamTypes; + +@Description( + functionName = "last_value", + description = "the last value of expr", + example = "> SELECT last_value(expr);", + returnType = TajoDataTypes.Type.INT8, + paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.INT8})} +) +public class LastValueLong extends LastValue { + + public LastValueLong() { + super(new Column[] { + new Column("expr", TajoDataTypes.Type.INT8) + }); + } + + @Override + public TajoDataTypes.DataType getPartialResultType() { + return CatalogUtil.newSimpleDataType(TajoDataTypes.Type.INT8); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueString.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueString.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueString.java new file mode 100644 index 0000000..8ab8d2e --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueString.java @@ -0,0 +1,46 @@ +/** + * 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.tajo.engine.function.builtin; + +import org.apache.tajo.catalog.CatalogUtil; +import org.apache.tajo.catalog.Column; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.engine.function.annotation.Description; +import org.apache.tajo.engine.function.annotation.ParamTypes; + +@Description( + functionName = "last_value", + description = "the last value of expr", + example = "> SELECT last_value(expr);", + returnType = TajoDataTypes.Type.TEXT, + paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.TEXT})} +) +public class LastValueString extends LastValue { + + public LastValueString() { + super(new Column[] { + new Column("expr", TajoDataTypes.Type.TEXT) + }); + } + + @Override + public TajoDataTypes.DataType getPartialResultType() { + return CatalogUtil.newSimpleDataType(TajoDataTypes.Type.TEXT); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueTime.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueTime.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueTime.java new file mode 100644 index 0000000..146c1e5 --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueTime.java @@ -0,0 +1,46 @@ +/** + * 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.tajo.engine.function.builtin; + +import org.apache.tajo.catalog.CatalogUtil; +import org.apache.tajo.catalog.Column; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.engine.function.annotation.Description; +import org.apache.tajo.engine.function.annotation.ParamTypes; + +@Description( + functionName = "last_value", + description = "the last value of expr", + example = "> SELECT last_value(expr);", + returnType = TajoDataTypes.Type.TIME, + paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.TIME})} +) +public class LastValueTime extends LastValue { + + public LastValueTime() { + super(new Column[] { + new Column("expr", TajoDataTypes.Type.TIME) + }); + } + + @Override + public TajoDataTypes.DataType getPartialResultType() { + return CatalogUtil.newSimpleDataType(TajoDataTypes.Type.TIME); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueTimestamp.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueTimestamp.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueTimestamp.java new file mode 100644 index 0000000..cabe43b --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/LastValueTimestamp.java @@ -0,0 +1,46 @@ +/** + * 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.tajo.engine.function.builtin; + +import org.apache.tajo.catalog.CatalogUtil; +import org.apache.tajo.catalog.Column; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.engine.function.annotation.Description; +import org.apache.tajo.engine.function.annotation.ParamTypes; + +@Description( + functionName = "last_value", + description = "the last value of expr", + example = "> SELECT last_value(expr);", + returnType = TajoDataTypes.Type.TIMESTAMP, + paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.TIMESTAMP})} +) +public class LastValueTimestamp extends LastValue { + + public LastValueTimestamp() { + super(new Column[] { + new Column("expr", TajoDataTypes.Type.TIMESTAMP) + }); + } + + @Override + public TajoDataTypes.DataType getPartialResultType() { + return CatalogUtil.newSimpleDataType(TajoDataTypes.Type.TIMESTAMP); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValue.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValue.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValue.java new file mode 100644 index 0000000..93cdffb --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValue.java @@ -0,0 +1,64 @@ +/** + * 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.tajo.engine.function.window; + +import org.apache.tajo.catalog.Column; +import org.apache.tajo.datum.*; +import org.apache.tajo.plan.function.FunctionContext; +import org.apache.tajo.plan.function.WindowAggFunc; +import org.apache.tajo.storage.Tuple; + +public abstract class FirstValue extends WindowAggFunc<Datum> { + + public FirstValue(Column[] columns) { + super(columns); + } + + @Override + public FunctionContext newContext() { + return new FirstValueContext(); + } + + @Override + public void eval(FunctionContext ctx, Tuple params) { + FirstValueContext firstValueCtx = (FirstValueContext)ctx; + if(firstValueCtx.isSet == false) { + firstValueCtx.isSet = true; + if (params.get(0).isNotNull()) { + firstValueCtx.first = params.get(0); + } + } + } + + @Override + public Datum terminate(FunctionContext ctx) { + if (((FirstValueContext) ctx).first == null) { + return NullDatum.get(); + } + else { + return ((FirstValueContext) ctx).first; + } + } + + protected class FirstValueContext implements FunctionContext { + boolean isSet = false; + Datum first = null; + } + +} http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueDate.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueDate.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueDate.java new file mode 100644 index 0000000..44364ee --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueDate.java @@ -0,0 +1,41 @@ +/** + * 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.tajo.engine.function.window; + +import org.apache.tajo.catalog.Column; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.common.TajoDataTypes.Type; +import org.apache.tajo.engine.function.annotation.Description; +import org.apache.tajo.engine.function.annotation.ParamTypes; + +@Description( + functionName = "first_value", + description = "the first value of retrieved rows", + example = "> SELECT first_value(column) OVER ();", + returnType = Type.DATE, + paramTypes = {@ParamTypes(paramTypes = {Type.DATE})} +) +public class FirstValueDate extends FirstValue { + + public FirstValueDate() { + super(new Column[] { + new Column("expr", Type.DATE) + }); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueDouble.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueDouble.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueDouble.java new file mode 100644 index 0000000..01bbc7f --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueDouble.java @@ -0,0 +1,41 @@ +/** + * 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.tajo.engine.function.window; + +import org.apache.tajo.catalog.Column; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.common.TajoDataTypes.Type; +import org.apache.tajo.engine.function.annotation.Description; +import org.apache.tajo.engine.function.annotation.ParamTypes; + +@Description( + functionName = "first_value", + description = "the first value of retrieved rows", + example = "> SELECT first_value(column) OVER ();", + returnType = Type.FLOAT8, + paramTypes = {@ParamTypes(paramTypes = {Type.FLOAT8})} +) +public class FirstValueDouble extends FirstValue { + + public FirstValueDouble() { + super(new Column[] { + new Column("expr", Type.FLOAT8) + }); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueFloat.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueFloat.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueFloat.java new file mode 100644 index 0000000..317c7ef --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueFloat.java @@ -0,0 +1,41 @@ +/** + * 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.tajo.engine.function.window; + +import org.apache.tajo.catalog.Column; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.common.TajoDataTypes.Type; +import org.apache.tajo.engine.function.annotation.Description; +import org.apache.tajo.engine.function.annotation.ParamTypes; + +@Description( + functionName = "first_value", + description = "the first value of retrieved rows", + example = "> SELECT first_value(column) OVER ();", + returnType = Type.FLOAT4, + paramTypes = {@ParamTypes(paramTypes = {Type.FLOAT4})} +) +public class FirstValueFloat extends FirstValue { + + public FirstValueFloat() { + super(new Column[] { + new Column("expr", Type.FLOAT4) + }); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueInt.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueInt.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueInt.java new file mode 100644 index 0000000..8cb8e4e --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueInt.java @@ -0,0 +1,41 @@ +/** + * 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.tajo.engine.function.window; + +import org.apache.tajo.catalog.Column; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.common.TajoDataTypes.Type; +import org.apache.tajo.engine.function.annotation.Description; +import org.apache.tajo.engine.function.annotation.ParamTypes; + +@Description( + functionName = "first_value", + description = "the first value of retrieved rows", + example = "> SELECT first_value(column) OVER ();", + returnType = Type.INT4, + paramTypes = {@ParamTypes(paramTypes = {Type.INT4})} +) +public class FirstValueInt extends FirstValue { + + public FirstValueInt() { + super(new Column[] { + new Column("expr", Type.INT4) + }); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueLong.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueLong.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueLong.java new file mode 100644 index 0000000..3c07c0c --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueLong.java @@ -0,0 +1,41 @@ +/** + * 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.tajo.engine.function.window; + +import org.apache.tajo.catalog.Column; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.common.TajoDataTypes.Type; +import org.apache.tajo.engine.function.annotation.Description; +import org.apache.tajo.engine.function.annotation.ParamTypes; + +@Description( + functionName = "first_value", + description = "the first value of retrieved rows", + example = "> SELECT first_value(column) OVER ();", + returnType = Type.INT8, + paramTypes = {@ParamTypes(paramTypes = {Type.INT8})} +) +public class FirstValueLong extends FirstValue { + + public FirstValueLong() { + super(new Column[] { + new Column("expr", Type.INT8) + }); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueString.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueString.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueString.java new file mode 100644 index 0000000..2534174 --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueString.java @@ -0,0 +1,41 @@ +/** + * 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.tajo.engine.function.window; + +import org.apache.tajo.catalog.Column; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.common.TajoDataTypes.Type; +import org.apache.tajo.engine.function.annotation.Description; +import org.apache.tajo.engine.function.annotation.ParamTypes; + +@Description( + functionName = "first_value", + description = "the first value of retrieved rows", + example = "> SELECT first_value(column) OVER ();", + returnType = Type.TEXT, + paramTypes = {@ParamTypes(paramTypes = {Type.TEXT})} +) +public class FirstValueString extends FirstValue { + + public FirstValueString() { + super(new Column[] { + new Column("expr", Type.TEXT) + }); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueTime.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueTime.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueTime.java new file mode 100644 index 0000000..fc5adc4 --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueTime.java @@ -0,0 +1,41 @@ +/** + * 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.tajo.engine.function.window; + +import org.apache.tajo.catalog.Column; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.common.TajoDataTypes.Type; +import org.apache.tajo.engine.function.annotation.Description; +import org.apache.tajo.engine.function.annotation.ParamTypes; + +@Description( + functionName = "first_value", + description = "the first value of retrieved rows", + example = "> SELECT first_value(column) OVER ();", + returnType = Type.TIME, + paramTypes = {@ParamTypes(paramTypes = {Type.TIME})} +) +public class FirstValueTime extends FirstValue { + + public FirstValueTime() { + super(new Column[] { + new Column("expr", Type.TIME) + }); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueTimestamp.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueTimestamp.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueTimestamp.java new file mode 100644 index 0000000..1e7bd26 --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/window/FirstValueTimestamp.java @@ -0,0 +1,41 @@ +/** + * 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.tajo.engine.function.window; + +import org.apache.tajo.catalog.Column; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.common.TajoDataTypes.Type; +import org.apache.tajo.engine.function.annotation.Description; +import org.apache.tajo.engine.function.annotation.ParamTypes; + +@Description( + functionName = "first_value", + description = "the first value of retrieved rows", + example = "> SELECT first_value(column) OVER ();", + returnType = Type.TIMESTAMP, + paramTypes = {@ParamTypes(paramTypes = {Type.TIMESTAMP})} +) +public class FirstValueTimestamp extends FirstValue { + + public FirstValueTimestamp() { + super(new Column[] { + new Column("expr", Type.TIMESTAMP) + }); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java b/tajo-core/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java index 90230c9..3669625 100644 --- a/tajo-core/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java +++ b/tajo-core/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java @@ -379,6 +379,10 @@ public class SQLAnalyzer extends SQLParserBaseVisitor<Expr> { } } else if (checkIfExist(functionType.ROW_NUMBER())) { functionBody = new GeneralSetFunctionExpr("row_number", false, new Expr[] {}); + } else if (checkIfExist(functionType.FIRST_VALUE())) { + functionBody = new GeneralSetFunctionExpr("first_value", false, new Expr[]{ visitColumn_reference(functionType.column_reference())}); + } else if (checkIfExist(functionType.LAST_VALUE())) { + functionBody = new GeneralSetFunctionExpr("last_value", false, new Expr[]{visitColumn_reference(functionType.column_reference())}); } else { functionBody = visitAggregate_function(functionType.aggregate_function()); } http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/test/java/org/apache/tajo/engine/query/TestWindowQuery.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/engine/query/TestWindowQuery.java b/tajo-core/src/test/java/org/apache/tajo/engine/query/TestWindowQuery.java index d64dd20..2af5ce9 100644 --- a/tajo-core/src/test/java/org/apache/tajo/engine/query/TestWindowQuery.java +++ b/tajo-core/src/test/java/org/apache/tajo/engine/query/TestWindowQuery.java @@ -21,11 +21,18 @@ package org.apache.tajo.engine.query; import org.apache.tajo.IntegrationTest; import org.apache.tajo.QueryTestCaseBase; import org.apache.tajo.TajoConstants; +import org.apache.tajo.TajoTestingCluster; +import org.apache.tajo.catalog.Schema; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.storage.StorageConstants; +import org.apache.tajo.util.KeyValueSet; import org.junit.Test; import org.junit.experimental.categories.Category; import java.sql.ResultSet; +import static org.junit.Assert.assertEquals; + @Category(IntegrationTest.class) public class TestWindowQuery extends QueryTestCaseBase { @@ -244,4 +251,72 @@ public class TestWindowQuery extends QueryTestCaseBase { assertResultSet(res); cleanupQuery(res); } + + @Test + public final void firstValue1() throws Exception { + ResultSet res = executeQuery(); + assertResultSet(res); + cleanupQuery(res); + } + + @Test + public final void firstValueTime() throws Exception { + KeyValueSet tableOptions = new KeyValueSet(); + tableOptions.set(StorageConstants.TEXT_DELIMITER, StorageConstants.DEFAULT_FIELD_DELIMITER); + tableOptions.set(StorageConstants.TEXT_NULL, "\\\\N"); + + Schema schema = new Schema(); + schema.addColumn("id", TajoDataTypes.Type.INT4); + schema.addColumn("time", TajoDataTypes.Type.TIME); + String[] data = new String[]{ "1|12:11:12", "2|10:11:13", "2|05:42:41" }; + TajoTestingCluster.createTable("firstvaluetime", schema, tableOptions, data, 1); + + try { + ResultSet res = executeString("select id, first_value(time) over ( partition by id order by time ) as time_first from firstvaluetime"); + String ascExpected = "id,time_first\n" + + "-------------------------------\n" + + "1,12:11:12\n" + + "2,05:42:41\n" + + "2,05:42:41\n"; + + assertEquals(ascExpected, resultSetToString(res)); + res.close(); + } finally { + executeString("DROP TABLE firstvaluetime PURGE"); + } + } + + @Test + public final void lastValue1() throws Exception { + ResultSet res = executeQuery(); + assertResultSet(res); + cleanupQuery(res); + } + + @Test + public final void lastValueTime() throws Exception { + KeyValueSet tableOptions = new KeyValueSet(); + tableOptions.set(StorageConstants.TEXT_DELIMITER, StorageConstants.DEFAULT_FIELD_DELIMITER); + tableOptions.set(StorageConstants.TEXT_NULL, "\\\\N"); + + Schema schema = new Schema(); + schema.addColumn("id", TajoDataTypes.Type.INT4); + schema.addColumn("time", TajoDataTypes.Type.TIME); + String[] data = new String[]{ "1|12:11:12", "2|10:11:13", "2|05:42:41" }; + TajoTestingCluster.createTable("lastvaluetime", schema, tableOptions, data, 1); + + try { + ResultSet res = executeString("select id, last_value(time) over ( partition by id order by time ) as time_last from lastvaluetime"); + String ascExpected = "id,time_last\n" + + "-------------------------------\n" + + "1,12:11:12\n" + + "2,10:11:13\n" + + "2,10:11:13\n"; + + assertEquals(ascExpected, resultSetToString(res)); + res.close(); + } finally { + executeString("DROP TABLE lastvaluetime PURGE"); + } + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/test/resources/queries/TestWindowQuery/firstValue1.sql ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/resources/queries/TestWindowQuery/firstValue1.sql b/tajo-core/src/test/resources/queries/TestWindowQuery/firstValue1.sql new file mode 100644 index 0000000..e9c9c73 --- /dev/null +++ b/tajo-core/src/test/resources/queries/TestWindowQuery/firstValue1.sql @@ -0,0 +1,18 @@ +SELECT + l_orderkey, + first_value(l_shipmode) over (PARTITION BY L_ORDERKEY order by l_shipmode ) as shipmode_first, + first_value(l_linenumber) over (PARTITION BY L_ORDERKEY order by l_linenumber ) as linenumber_first, + first_value(l_suppkey_t) over (PARTITION BY L_ORDERKEY order by l_suppkey_t ) as suppkey_first, + first_value(l_shipdate_t) over (PARTITION BY L_ORDERKEY order by l_shipdate_t ) as shipdate_first, + first_value(l_commitdate_t) over (PARTITION BY L_ORDERKEY order by l_commitdate_t ) as commitdate_first, + first_value(l_extendedprice) over (PARTITION BY L_ORDERKEY order by l_extendedprice ) as extendedprice_first, + first_value(l_discount_t) over (PARTITION BY L_ORDERKEY order by l_discount_t ) as discount_first +FROM +( + SELECT + l_orderkey,l_partkey,l_suppkey::INT8 as l_suppkey_t,l_linenumber,l_quantity, + l_extendedprice,l_discount::FLOAT4 as l_discount_t,l_tax,l_returnflag,l_linestatus, + l_shipdate::DATE as l_shipdate_t,l_commitdate::TIMESTAMP as l_commitdate_t,l_receiptdate,l_shipinstruct,l_shipmode,l_comment + FROM + LINEITEM +) xx \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/test/resources/queries/TestWindowQuery/lastValue1.sql ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/resources/queries/TestWindowQuery/lastValue1.sql b/tajo-core/src/test/resources/queries/TestWindowQuery/lastValue1.sql new file mode 100644 index 0000000..150b912 --- /dev/null +++ b/tajo-core/src/test/resources/queries/TestWindowQuery/lastValue1.sql @@ -0,0 +1,18 @@ +SELECT + l_orderkey, + last_value(l_shipmode) over (PARTITION BY L_ORDERKEY order by l_shipmode ) as shipmode_last, + last_value(l_linenumber) over (PARTITION BY L_ORDERKEY order by l_linenumber ) as linenumber_last, + last_value(l_suppkey_t) over (PARTITION BY L_ORDERKEY order by l_suppkey_t ) as suppkey_last, + last_value(l_shipdate_t) over (PARTITION BY L_ORDERKEY order by l_shipdate_t ) as shipdate_last, + last_value(l_commitdate_t) over (PARTITION BY L_ORDERKEY order by l_commitdate_t ) as commitdate_last, + last_value(l_extendedprice) over (PARTITION BY L_ORDERKEY order by l_extendedprice ) as extendedprice_last, + last_value(l_discount_t) over (PARTITION BY L_ORDERKEY order by l_discount_t ) as discount_last +FROM +( + SELECT + l_orderkey,l_partkey,l_suppkey::INT8 as l_suppkey_t,l_linenumber,l_quantity, + l_extendedprice,l_discount::FLOAT4 as l_discount_t,l_tax,l_returnflag,l_linestatus, + l_shipdate::DATE as l_shipdate_t,l_commitdate::TIMESTAMP as l_commitdate_t,l_receiptdate,l_shipinstruct,l_shipmode,l_comment + FROM + LINEITEM +) xx \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/test/resources/results/TestWindowQuery/firstValue1.result ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/resources/results/TestWindowQuery/firstValue1.result b/tajo-core/src/test/resources/results/TestWindowQuery/firstValue1.result new file mode 100644 index 0000000..8ca4ea9 --- /dev/null +++ b/tajo-core/src/test/resources/results/TestWindowQuery/firstValue1.result @@ -0,0 +1,7 @@ +l_orderkey,shipmode_first,linenumber_first,suppkey_first,shipdate_first,commitdate_first,extendedprice_first,discount_first +------------------------------- +1,MAIL,1,7311,1996-03-13,1996-02-12 00:00:00,21168.23,0.04 +1,MAIL,1,7311,1996-03-13,1996-02-12 00:00:00,21168.23,0.04 +2,RAIL,1,1191,1997-01-28,1997-01-14 00:00:00,44694.46,0.0 +3,AIR,1,1798,1993-11-09,1993-12-20 00:00:00,46796.47,0.06 +3,AIR,1,1798,1993-11-09,1993-12-20 00:00:00,46796.47,0.06 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-core/src/test/resources/results/TestWindowQuery/lastValue1.result ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/resources/results/TestWindowQuery/lastValue1.result b/tajo-core/src/test/resources/results/TestWindowQuery/lastValue1.result new file mode 100644 index 0000000..825f982 --- /dev/null +++ b/tajo-core/src/test/resources/results/TestWindowQuery/lastValue1.result @@ -0,0 +1,7 @@ +l_orderkey,shipmode_last,linenumber_last,suppkey_last,shipdate_last,commitdate_last,extendedprice_last,discount_last +------------------------------- +1,TRUCK,2,7706,1996-04-12,1996-02-28 00:00:00,45983.16,0.09 +1,TRUCK,2,7706,1996-04-12,1996-02-28 00:00:00,45983.16,0.09 +2,RAIL,1,1191,1997-01-28,1997-01-14 00:00:00,44694.46,0.0 +3,RAIL,2,6540,1994-02-02,1994-01-04 00:00:00,54058.05,0.1 +3,RAIL,2,6540,1994-02-02,1994-01-04 00:00:00,54058.05,0.1 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tajo/blob/fd49bff1/tajo-plan/src/main/java/org/apache/tajo/plan/ExprAnnotator.java ---------------------------------------------------------------------- diff --git a/tajo-plan/src/main/java/org/apache/tajo/plan/ExprAnnotator.java b/tajo-plan/src/main/java/org/apache/tajo/plan/ExprAnnotator.java index fcbbb21..c8eaffc 100644 --- a/tajo-plan/src/main/java/org/apache/tajo/plan/ExprAnnotator.java +++ b/tajo-plan/src/main/java/org/apache/tajo/plan/ExprAnnotator.java @@ -679,7 +679,7 @@ public class ExprAnnotator extends BaseAlgebraVisitor<ExprAnnotator.Context, Eva } public static final Set<String> WINDOW_FUNCTIONS = - Sets.newHashSet("row_number", "rank", "dense_rank", "percent_rank", "cume_dist"); + Sets.newHashSet("row_number", "rank", "dense_rank", "percent_rank", "cume_dist", "first_value"); public EvalNode visitWindowFunction(Context ctx, Stack<Expr> stack, WindowFunctionExpr windowFunc) throws PlanningException {
