You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2014/10/01 20:39:42 UTC

[2/2] git commit: [OPTIQ-422] Add REGR_SXX and REGR_SYY regression functions

[OPTIQ-422] Add REGR_SXX and REGR_SYY regression functions


Project: http://git-wip-us.apache.org/repos/asf/incubator-optiq/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-optiq/commit/af86cd87
Tree: http://git-wip-us.apache.org/repos/asf/incubator-optiq/tree/af86cd87
Diff: http://git-wip-us.apache.org/repos/asf/incubator-optiq/diff/af86cd87

Branch: refs/heads/master
Commit: af86cd87a8c129867d49d87c15d62896a56f9108
Parents: d4ab395
Author: Yash Sharma <ya...@snapdeal.com>
Authored: Sun Sep 28 19:45:24 2014 +0530
Committer: Julian Hyde <jh...@apache.org>
Committed: Wed Oct 1 00:55:16 2014 -0700

----------------------------------------------------------------------
 .../main/codegen/templates/CombinedParser.jj    |  3 ++
 .../eigenbase/sql/fun/SqlCovarAggFunction.java  | 23 ++++-----
 .../eigenbase/sql/fun/SqlStdOperatorTable.java  | 18 +++++--
 .../org/eigenbase/sql/test/SqlAdvisorTest.java  |  2 +
 .../eigenbase/sql/test/SqlOperatorBaseTest.java | 54 +++++++++++++++++++-
 5 files changed, 83 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-optiq/blob/af86cd87/core/src/main/codegen/templates/CombinedParser.jj
----------------------------------------------------------------------
diff --git a/core/src/main/codegen/templates/CombinedParser.jj b/core/src/main/codegen/templates/CombinedParser.jj
index b5dff64..54b6df2 100644
--- a/core/src/main/codegen/templates/CombinedParser.jj
+++ b/core/src/main/codegen/templates/CombinedParser.jj
@@ -4129,6 +4129,8 @@ SqlIdentifier ReservedFunctionName() :
         | <PERCENT_RANK>
         | <POWER>
         | <RANK>
+        | <REGR_SXX>
+        | <REGR_SYY>
         | <ROW_NUMBER>
         | <SQRT>
         | <STDDEV_POP>
@@ -4741,6 +4743,7 @@ SqlPostfixOperator PostfixRowOperator() :
     | < REGR_SLOPE: "REGR_SLOPE" >
     | < REGR_SXX: "REGR_SXX" >
     | < REGR_SXY: "REGR_SXY" >
+    | < REGR_SYY: "REGR_SYY" >
     | < RELATIVE: "RELATIVE" >
     | < RELEASE: "RELEASE" >
     | < REPEATABLE: "REPEATABLE" >

http://git-wip-us.apache.org/repos/asf/incubator-optiq/blob/af86cd87/core/src/main/java/org/eigenbase/sql/fun/SqlCovarAggFunction.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/eigenbase/sql/fun/SqlCovarAggFunction.java b/core/src/main/java/org/eigenbase/sql/fun/SqlCovarAggFunction.java
index 77d7dbf..f759d0c 100644
--- a/core/src/main/java/org/eigenbase/sql/fun/SqlCovarAggFunction.java
+++ b/core/src/main/java/org/eigenbase/sql/fun/SqlCovarAggFunction.java
@@ -18,7 +18,6 @@ package org.eigenbase.sql.fun;
 
 import java.util.List;
 
-import com.google.common.collect.ImmutableList;
 import org.eigenbase.reltype.RelDataType;
 import org.eigenbase.reltype.RelDataTypeFactory;
 import org.eigenbase.sql.SqlAggFunction;
@@ -27,6 +26,7 @@ import org.eigenbase.sql.SqlKind;
 import org.eigenbase.sql.type.OperandTypes;
 import org.eigenbase.sql.type.ReturnTypes;
 
+import com.google.common.collect.ImmutableList;
 
 /**
  * <code>Covar</code> is an aggregator which returns the Covariance of the
@@ -43,16 +43,13 @@ public class SqlCovarAggFunction extends SqlAggFunction {
   //~ Constructors -----------------------------------------------------------
 
   /**
-   * Creates a SqlAvgAggFunction
+   * Creates a SqlCovarAggFunction.
    *
    * @param type    Data type
-   * @param subtype Specific function, e.g. AVG or STDDEV_POP
+   * @param subtype Specific function, e.g. COVAR_POP
    */
-  public SqlCovarAggFunction(
-          RelDataType type,
-          Subtype subtype) {
-    super(
-        subtype.name(),
+  public SqlCovarAggFunction(RelDataType type, Subtype subtype) {
+    super(subtype.name(),
         SqlKind.OTHER_FUNCTION,
         ReturnTypes.ARG0_NULLABLE_IF_EMPTY,
         null,
@@ -81,12 +78,14 @@ public class SqlCovarAggFunction extends SqlAggFunction {
     return subtype;
   }
 
-/**
- * Enum for defining specific types.
- */
+  /**
+   * Enum for defining specific types.
+   */
   public enum Subtype {
     COVAR_POP,
-    COVAR_SAMP
+    COVAR_SAMP,
+    REGR_SXX,
+    REGR_SYY
   }
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-optiq/blob/af86cd87/core/src/main/java/org/eigenbase/sql/fun/SqlStdOperatorTable.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/eigenbase/sql/fun/SqlStdOperatorTable.java b/core/src/main/java/org/eigenbase/sql/fun/SqlStdOperatorTable.java
index e46cc3f..7f7bf73 100644
--- a/core/src/main/java/org/eigenbase/sql/fun/SqlStdOperatorTable.java
+++ b/core/src/main/java/org/eigenbase/sql/fun/SqlStdOperatorTable.java
@@ -672,6 +672,18 @@ public class SqlStdOperatorTable extends ReflectiveSqlOperatorTable {
       new SqlAvgAggFunction(null, SqlAvgAggFunction.Subtype.STDDEV_POP);
 
   /**
+   * <code>REGR_SXX</code> aggregate function.
+   */
+  public static final SqlAggFunction REGR_SXX =
+      new SqlCovarAggFunction(null, SqlCovarAggFunction.Subtype.REGR_SXX);
+
+  /**
+   * <code>REGR_SYY</code> aggregate function.
+   */
+  public static final SqlAggFunction REGR_SYY =
+      new SqlCovarAggFunction(null, SqlCovarAggFunction.Subtype.REGR_SYY);
+
+  /**
    * <code>COVAR_POP</code> aggregate function.
    */
   public static final SqlAggFunction COVAR_POP =
@@ -705,7 +717,7 @@ public class SqlStdOperatorTable extends ReflectiveSqlOperatorTable {
   // WINDOW Aggregate Functions
   //-------------------------------------------------------------
   /**
-   * <code>HISTORAM</code> aggregate function support. Used by window
+   * <code>HISTOGRAM</code> aggregate function support. Used by window
    * aggregate versions of MIN/MAX
    */
   public static final SqlAggFunction HISTOGRAM_AGG =
@@ -880,8 +892,8 @@ public class SqlStdOperatorTable extends ReflectiveSqlOperatorTable {
    * function into a relation, e.g. "<code>SELECT * FROM
    * TABLE(ramp(5))</code>".
    *
-   * <p>This operator has function syntax (with one argument), whereas {@link
-   * #EXPLICIT_TABLE} is a prefix operator.
+   * <p>This operator has function syntax (with one argument), whereas
+   * {@link #EXPLICIT_TABLE} is a prefix operator.
    */
   public static final SqlSpecialOperator COLLECTION_TABLE =
       new SqlCollectionTableOperator(

http://git-wip-us.apache.org/repos/asf/incubator-optiq/blob/af86cd87/core/src/test/java/org/eigenbase/sql/test/SqlAdvisorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/eigenbase/sql/test/SqlAdvisorTest.java b/core/src/test/java/org/eigenbase/sql/test/SqlAdvisorTest.java
index f9ad12e..d8ebe27 100644
--- a/core/src/test/java/org/eigenbase/sql/test/SqlAdvisorTest.java
+++ b/core/src/test/java/org/eigenbase/sql/test/SqlAdvisorTest.java
@@ -151,6 +151,8 @@ public class SqlAdvisorTest extends SqlValidatorTestCase {
           "KEYWORD(POSITION)",
           "KEYWORD(POWER)",
           "KEYWORD(RANK)",
+          "KEYWORD(REGR_SXX)",
+          "KEYWORD(REGR_SYY)",
           "KEYWORD(ROW)",
           "KEYWORD(ROW_NUMBER)",
           "KEYWORD(SESSION_USER)",

http://git-wip-us.apache.org/repos/asf/incubator-optiq/blob/af86cd87/core/src/test/java/org/eigenbase/sql/test/SqlOperatorBaseTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/eigenbase/sql/test/SqlOperatorBaseTest.java b/core/src/test/java/org/eigenbase/sql/test/SqlOperatorBaseTest.java
index c04bec2..a51bc9b 100644
--- a/core/src/test/java/org/eigenbase/sql/test/SqlOperatorBaseTest.java
+++ b/core/src/test/java/org/eigenbase/sql/test/SqlOperatorBaseTest.java
@@ -4487,7 +4487,8 @@ public abstract class SqlOperatorBaseTest {
         "^covar_pop(cast(null as varchar(2)),cast(null as varchar(2)))^",
         "(?s)Cannot apply 'COVAR_POP' to arguments of type 'COVAR_POP\\(<VARCHAR\\(2\\)>, <VARCHAR\\(2\\)>\\)'\\. Supported form\\(s\\): 'COVAR_POP\\(<NUMERIC>, <NUMERIC>\\)'.*",
         false);
-    tester.checkType("covar_pop(CAST(NULL AS INTEGER),CAST(NULL AS INTEGER))", "INTEGER");
+    tester.checkType("covar_pop(CAST(NULL AS INTEGER),CAST(NULL AS INTEGER))",
+        "INTEGER");
     checkAggType(tester, "covar_pop(1.5, 2.5)", "DECIMAL(2, 1) NOT NULL");
     if (!enable) {
       return;
@@ -4510,7 +4511,8 @@ public abstract class SqlOperatorBaseTest {
         "^covar_samp(cast(null as varchar(2)),cast(null as varchar(2)))^",
         "(?s)Cannot apply 'COVAR_SAMP' to arguments of type 'COVAR_SAMP\\(<VARCHAR\\(2\\)>, <VARCHAR\\(2\\)>\\)'\\. Supported form\\(s\\): 'COVAR_SAMP\\(<NUMERIC>, <NUMERIC>\\)'.*",
         false);
-    tester.checkType("covar_samp(CAST(NULL AS INTEGER),CAST(NULL AS INTEGER))", "INTEGER");
+    tester.checkType("covar_samp(CAST(NULL AS INTEGER),CAST(NULL AS INTEGER))",
+        "INTEGER");
     checkAggType(tester, "covar_samp(1.5, 2.5)", "DECIMAL(2, 1) NOT NULL");
     if (!enable) {
       return;
@@ -4523,6 +4525,54 @@ public abstract class SqlOperatorBaseTest {
         0d);
   }
 
+  @Test public void testRegrSxxFunc() {
+    tester.setFor(SqlStdOperatorTable.REGR_SXX, VM_EXPAND);
+    tester.checkFails(
+        "regr_sxx(^*^)",
+        "Unknown identifier '\\*'",
+        false);
+    tester.checkFails(
+        "^regr_sxx(cast(null as varchar(2)), cast(null as varchar(2)))^",
+        "(?s)Cannot apply 'REGR_SXX' to arguments of type 'REGR_SXX\\(<VARCHAR\\(2\\)>, <VARCHAR\\(2\\)>\\)'\\. Supported form\\(s\\): 'REGR_SXX\\(<NUMERIC>, <NUMERIC>\\)'.*",
+        false);
+    tester.checkType("regr_sxx(CAST(NULL AS INTEGER), CAST(NULL AS INTEGER))",
+        "INTEGER");
+    checkAggType(tester, "regr_sxx(1.5, 2.5)", "DECIMAL(2, 1) NOT NULL");
+    if (!enable) {
+      return;
+    }
+    // with zero values
+    tester.checkAgg(
+        "regr_sxx(x)",
+        new String[]{},
+        null,
+        0d);
+  }
+
+  @Test public void testRegrSyyFunc() {
+    tester.setFor(SqlStdOperatorTable.REGR_SYY, VM_EXPAND);
+    tester.checkFails(
+        "regr_syy(^*^)",
+        "Unknown identifier '\\*'",
+        false);
+    tester.checkFails(
+        "^regr_syy(cast(null as varchar(2)), cast(null as varchar(2)))^",
+        "(?s)Cannot apply 'REGR_SYY' to arguments of type 'REGR_SYY\\(<VARCHAR\\(2\\)>, <VARCHAR\\(2\\)>\\)'\\. Supported form\\(s\\): 'REGR_SYY\\(<NUMERIC>, <NUMERIC>\\)'.*",
+        false);
+    tester.checkType("regr_syy(CAST(NULL AS INTEGER), CAST(NULL AS INTEGER))",
+        "INTEGER");
+    checkAggType(tester, "regr_syy(1.5, 2.5)", "DECIMAL(2, 1) NOT NULL");
+    if (!enable) {
+      return;
+    }
+    // with zero values
+    tester.checkAgg(
+        "regr_syy(x)",
+        new String[]{},
+        null,
+        0d);
+  }
+
   @Test public void testStddevPopFunc() {
     tester.setFor(SqlStdOperatorTable.STDDEV_POP, VM_EXPAND);
     tester.checkFails(