TAJO-843: implements COALESCE for BOOLEAN, DATE, TIME, TIMESTAMP. (Hyoungjun Kim via hyunsik)
Closes #26 Project: http://git-wip-us.apache.org/repos/asf/tajo/repo Commit: http://git-wip-us.apache.org/repos/asf/tajo/commit/692ae664 Tree: http://git-wip-us.apache.org/repos/asf/tajo/tree/692ae664 Diff: http://git-wip-us.apache.org/repos/asf/tajo/diff/692ae664 Branch: refs/heads/window_function Commit: 692ae6648be0b50b4289ca395f7d9d659033907d Parents: 47f6f56 Author: Hyunsik Choi <[email protected]> Authored: Thu Jun 5 14:38:50 2014 -0700 Committer: Hyunsik Choi <[email protected]> Committed: Thu Jun 5 14:40:48 2014 -0700 ---------------------------------------------------------------------- CHANGES | 3 ++ .../org/apache/tajo/catalog/CatalogUtil.java | 8 ++++ .../function/builtin/CoalesceBoolean.java | 44 ++++++++++++++++++++ .../engine/function/builtin/CoalesceDate.java | 44 ++++++++++++++++++++ .../engine/function/builtin/CoalesceTime.java | 44 ++++++++++++++++++++ .../function/builtin/CoalesceTimestamp.java | 44 ++++++++++++++++++++ .../function/TestConditionalExpressions.java | 40 ++++++++++++++++++ 7 files changed, 227 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tajo/blob/692ae664/CHANGES ---------------------------------------------------------------------- diff --git a/CHANGES b/CHANGES index 0ebb899..36345b2 100644 --- a/CHANGES +++ b/CHANGES @@ -19,6 +19,9 @@ Release 0.9.0 - unreleased IMPROVEMENT + TAJO-843: implements COALESCE for BOOLEAN, DATE, TIME, TIMESTAMP. + (Hyoungjun Kim via hyunsik) + TAJO-854: Supports INSERT INTO with UNION. (Hyoungjun Kim via jihoon) TAJO-793: CLI should be able to exit when single query is failed. http://git-wip-us.apache.org/repos/asf/tajo/blob/692ae664/tajo-catalog/tajo-catalog-common/src/main/java/org/apache/tajo/catalog/CatalogUtil.java ---------------------------------------------------------------------- diff --git a/tajo-catalog/tajo-catalog-common/src/main/java/org/apache/tajo/catalog/CatalogUtil.java b/tajo-catalog/tajo-catalog-common/src/main/java/org/apache/tajo/catalog/CatalogUtil.java index 331bdae..4f86028 100644 --- a/tajo-catalog/tajo-catalog-common/src/main/java/org/apache/tajo/catalog/CatalogUtil.java +++ b/tajo-catalog/tajo-catalog-common/src/main/java/org/apache/tajo/catalog/CatalogUtil.java @@ -658,6 +658,14 @@ public class CatalogUtil { if (Type.TEXT_ARRAY.getNumber() <= argTypeNumber && argTypeNumber <= exitingTypeNumber) { flag = true; } + } else if (givenType == Type.BOOLEAN && (definedType == Type.BOOLEAN || definedType == Type.BOOLEAN_ARRAY)) { + flag = true; + } else if (givenType == Type.DATE && (definedType == Type.DATE || definedType == Type.DATE_ARRAY)) { + flag = true; + } else if (givenType == Type.TIME && (definedType == Type.TIME || definedType == Type.TIME_ARRAY)) { + flag = true; + } else if (givenType == Type.TIMESTAMP && (definedType == Type.TIMESTAMP || definedType == Type.TIMESTAMP_ARRAY)) { + flag = true; } } return flag; http://git-wip-us.apache.org/repos/asf/tajo/blob/692ae664/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/CoalesceBoolean.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/CoalesceBoolean.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/CoalesceBoolean.java new file mode 100644 index 0000000..8c714c5 --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/CoalesceBoolean.java @@ -0,0 +1,44 @@ +/** + * 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.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 = "coalesce", + description = "Returns the first of its arguments that is not null.", + detail = "Like a CASE expression, COALESCE only evaluates the arguments that are needed to determine the result; " + + "that is, arguments to the right of the first non-null argument are not evaluated", + example = "> SELECT coalesce(null, null, true);\n" + + "true", + returnType = Type.BOOLEAN, + paramTypes = {@ParamTypes(paramTypes = {Type.BOOLEAN, TajoDataTypes.Type.BOOLEAN_ARRAY})} +) +public class CoalesceBoolean extends Coalesce { + public CoalesceBoolean() { + super(new Column[] { + new Column("column", TajoDataTypes.Type.BOOLEAN), + new Column("params", TajoDataTypes.Type.BOOLEAN_ARRAY), + }); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/692ae664/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/CoalesceDate.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/CoalesceDate.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/CoalesceDate.java new file mode 100644 index 0000000..23f8f0c --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/CoalesceDate.java @@ -0,0 +1,44 @@ +/** + * 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.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 = "coalesce", + description = "Returns the first of its arguments that is not null.", + detail = "Like a CASE expression, COALESCE only evaluates the arguments that are needed to determine the result; " + + "that is, arguments to the right of the first non-null argument are not evaluated", + example = "> SELECT coalesce(null, null, date '2014-01-01');\n" + + "2014-01-01", + returnType = Type.DATE, + paramTypes = {@ParamTypes(paramTypes = {Type.DATE, Type.DATE_ARRAY})} +) +public class CoalesceDate extends Coalesce { + public CoalesceDate() { + super(new Column[] { + new Column("column", TajoDataTypes.Type.DATE), + new Column("params", TajoDataTypes.Type.DATE_ARRAY), + }); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/692ae664/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/CoalesceTime.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/CoalesceTime.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/CoalesceTime.java new file mode 100644 index 0000000..01bb6de --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/CoalesceTime.java @@ -0,0 +1,44 @@ +/** + * 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.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 = "coalesce", + description = "Returns the first of its arguments that is not null.", + detail = "Like a CASE expression, COALESCE only evaluates the arguments that are needed to determine the result; " + + "that is, arguments to the right of the first non-null argument are not evaluated", + example = "> SELECT coalesce(null, null, time '12:10:00');\n" + + "12:10:00", + returnType = Type.TIME, + paramTypes = {@ParamTypes(paramTypes = {Type.TIME, Type.TIME_ARRAY})} +) +public class CoalesceTime extends Coalesce { + public CoalesceTime() { + super(new Column[] { + new Column("column", TajoDataTypes.Type.TIME), + new Column("params", TajoDataTypes.Type.TIME_ARRAY), + }); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/692ae664/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/CoalesceTimestamp.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/CoalesceTimestamp.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/CoalesceTimestamp.java new file mode 100644 index 0000000..2609717 --- /dev/null +++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/CoalesceTimestamp.java @@ -0,0 +1,44 @@ +/** + * 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.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 = "coalesce", + description = "Returns the first of its arguments that is not null.", + detail = "Like a CASE expression, COALESCE only evaluates the arguments that are needed to determine the result; " + + "that is, arguments to the right of the first non-null argument are not evaluated", + example = "> SELECT coalesce(null, null, timestamp '2014-01-01');\n" + + "2014-01-01 00:00:00", + returnType = Type.TIMESTAMP, + paramTypes = {@ParamTypes(paramTypes = {Type.TIMESTAMP, Type.TIMESTAMP_ARRAY})} +) +public class CoalesceTimestamp extends Coalesce { + public CoalesceTimestamp() { + super(new Column[] { + new Column("column", TajoDataTypes.Type.TIMESTAMP), + new Column("params", TajoDataTypes.Type.TIMESTAMP_ARRAY), + }); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/692ae664/tajo-core/src/test/java/org/apache/tajo/engine/function/TestConditionalExpressions.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/engine/function/TestConditionalExpressions.java b/tajo-core/src/test/java/org/apache/tajo/engine/function/TestConditionalExpressions.java index af86387..ece34e7 100644 --- a/tajo-core/src/test/java/org/apache/tajo/engine/function/TestConditionalExpressions.java +++ b/tajo-core/src/test/java/org/apache/tajo/engine/function/TestConditionalExpressions.java @@ -79,4 +79,44 @@ public class TestConditionalExpressions extends ExprTestBase { //success } } + + @Test + public void testCoalesceBoolean() throws Exception { + testSimpleEval("select coalesce(null, false);", new String[]{"f"}); + testSimpleEval("select coalesce(null, null, true);", new String[]{"t"}); + testSimpleEval("select coalesce(true, null, false);", new String[]{"t"}); + testSimpleEval("select coalesce(null, true, false);", new String[]{"t"}); + } + + @Test + public void testCoalesceTimestamp() throws Exception { + testSimpleEval("select coalesce(null, timestamp '2014-01-01 00:00:00');", + new String[]{"2014-01-01 00:00:00" + getUserTimeZoneDisplay()}); + testSimpleEval("select coalesce(null, null, timestamp '2014-01-01 00:00:00');", + new String[]{"2014-01-01 00:00:00" + getUserTimeZoneDisplay()}); + testSimpleEval("select coalesce(timestamp '2014-01-01 00:00:00', null, timestamp '2014-01-02 00:00:00');", + new String[]{"2014-01-01 00:00:00" + getUserTimeZoneDisplay()}); + testSimpleEval("select coalesce(null, timestamp '2014-01-01 00:00:00', timestamp '2014-02-01 00:00:00');", + new String[]{"2014-01-01 00:00:00" + getUserTimeZoneDisplay()}); + } + + @Test + public void testCoalesceTime() throws Exception { + testSimpleEval("select coalesce(null, time '12:00:00');", + new String[]{"12:00:00" + getUserTimeZoneDisplay()}); + testSimpleEval("select coalesce(null, null, time '12:00:00');", + new String[]{"12:00:00" + getUserTimeZoneDisplay()}); + testSimpleEval("select coalesce(time '12:00:00', null, time '13:00:00');", + new String[]{"12:00:00" + getUserTimeZoneDisplay()}); + testSimpleEval("select coalesce(null, time '12:00:00', time '13:00:00');", + new String[]{"12:00:00" + getUserTimeZoneDisplay()}); + } + + @Test + public void testCoalesceDate() throws Exception { + testSimpleEval("select coalesce(null, date '2014-01-01');", new String[]{"2014-01-01"}); + testSimpleEval("select coalesce(null, null, date '2014-01-01');", new String[]{"2014-01-01"}); + testSimpleEval("select coalesce(date '2014-01-01', null, date '2014-02-01');", new String[]{"2014-01-01"}); + testSimpleEval("select coalesce(null, date '2014-01-01', date '2014-02-01');", new String[]{"2014-01-01"}); + } }
