[calcite] branch master updated: [CALCITE-2841] Simplification: push negation into Case expression

2019-02-22 Thread kgyrtkirk
This is an automated email from the ASF dual-hosted git repository.

kgyrtkirk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/master by this push:
 new c3fd74a  [CALCITE-2841] Simplification: push negation into Case 
expression
c3fd74a is described below

commit c3fd74a897ca1b469b6b776baeaa3c660ce5876a
Author: Zoltan Haindrich 
AuthorDate: Thu Oct 4 15:25:29 2018 +0200

[CALCITE-2841] Simplification: push negation into Case expression

Earlier NOT was not pushed into CASE expressions.
Also ensure that recursion under NOT happens.
---
 .../java/org/apache/calcite/rex/RexSimplify.java   | 26 +-
 .../org/apache/calcite/test/RexProgramTest.java| 16 +
 .../org/apache/calcite/test/RelOptRulesTest.xml|  4 ++--
 .../apache/calcite/test/SqlToRelConverterTest.xml  |  4 ++--
 core/src/test/resources/sql/some.iq|  2 +-
 core/src/test/resources/sql/sub-query.iq   | 14 ++--
 6 files changed, 53 insertions(+), 13 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java 
b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
index ab29c96..7ae8195 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
@@ -507,7 +507,31 @@ public class RexSimplify {
   return simplify(
   rexBuilder.makeCall(SqlStdOperatorTable.AND, newOperands), 
unknownAs);
 }
-return call;
+if (a.getKind() == SqlKind.CASE) {
+  final List newOperands = new ArrayList<>();
+  List operands = ((RexCall) a).getOperands();
+  for (int i = 0; i < operands.size(); i += 2) {
+if (i + 1 == operands.size()) {
+  newOperands.add(rexBuilder.makeCall(SqlStdOperatorTable.NOT, 
operands.get(i + 0)));
+} else {
+  newOperands.add(operands.get(i + 0));
+  newOperands.add(rexBuilder.makeCall(SqlStdOperatorTable.NOT, 
operands.get(i + 1)));
+}
+  }
+  return simplify(
+  rexBuilder.makeCall(SqlStdOperatorTable.CASE, newOperands), 
unknownAs);
+}
+RexNode a2 = simplify(a, unknownAs.negate());
+if (a == a2) {
+  return call;
+}
+if (a2.isAlwaysTrue()) {
+  return rexBuilder.makeLiteral(false);
+}
+if (a2.isAlwaysFalse()) {
+  return rexBuilder.makeLiteral(true);
+}
+return rexBuilder.makeCall(SqlStdOperatorTable.NOT, a2);
   }
 
   private RexNode simplifyIs(RexCall call) {
diff --git a/core/src/test/java/org/apache/calcite/test/RexProgramTest.java 
b/core/src/test/java/org/apache/calcite/test/RexProgramTest.java
index 3ff9260..3234050 100644
--- a/core/src/test/java/org/apache/calcite/test/RexProgramTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RexProgramTest.java
@@ -1902,6 +1902,22 @@ public class RexProgramTest extends 
RexProgramBuilderBase {
 checkSimplify(caseNode, "<=(/(?0.notNullInt0, 1), 1)");
   }
 
+  @Test public void testPushNotIntoCase() {
+checkSimplify(
+not(
+case_(
+isTrue(vBool()), vBool(1),
+gt(div(vIntNotNull(), literal(2)), literal(1)), vBool(2),
+vBool(3))),
+"CASE(IS TRUE(?0.bool0), NOT(?0.bool1), >(/(?0.notNullInt0, 2), 1), 
NOT(?0.bool2), NOT(?0.bool3))");
+  }
+
+  @Test public void testNotRecursion() {
+checkSimplify(
+not(coalesce(nullBool, trueLiteral)),
+"false");
+  }
+
   @Test public void testSimplifyAnd() {
 RelDataType booleanNotNullableType =
 typeFactory.createTypeWithNullability(
diff --git 
a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml 
b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
index 5accc18..8c7e46c 100644
--- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
@@ -8562,7 +8562,7 @@ LogicalProject(DEPTNO=[$0])
 
 
 
 
 
 
 

[calcite] branch master updated: [CALCITE-2791] Add the JSON_TYPE function (xuqianjin)

2019-02-22 Thread hongze
This is an automated email from the ASF dual-hosted git repository.

hongze pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/master by this push:
 new 5101703  [CALCITE-2791] Add the JSON_TYPE function (xuqianjin)
5101703 is described below

commit 5101703c3bb7e051766f9dc932534a0a7acba0c5
Author: xuqianjin 
AuthorDate: Sat Feb 23 00:44:49 2019 +0800

[CALCITE-2791] Add the JSON_TYPE function (xuqianjin)

Close apache/calcite#1013
---
 babel/src/main/codegen/config.fmpp |  1 +
 core/src/main/codegen/config.fmpp  |  1 +
 core/src/main/codegen/templates/Parser.jj  | 19 ++
 .../calcite/adapter/enumerable/RexImpTable.java|  2 +
 .../apache/calcite/runtime/CalciteResource.java|  3 +
 .../org/apache/calcite/runtime/SqlFunctions.java   | 36 +++
 .../calcite/sql/fun/SqlJsonTypeFunction.java   | 69 ++
 .../calcite/sql/fun/SqlStdOperatorTable.java   |  2 +
 .../org/apache/calcite/util/BuiltInMethod.java |  1 +
 .../calcite/runtime/CalciteResource.properties |  1 +
 core/src/test/codegen/config.fmpp  |  1 +
 .../calcite/rel/rel2sql/RelToSqlConverterTest.java |  8 +++
 .../apache/calcite/sql/parser/SqlParserTest.java   | 10 
 .../calcite/sql/test/SqlOperatorBaseTest.java  | 24 
 .../java/org/apache/calcite/test/JdbcTest.java | 12 
 .../apache/calcite/test/SqlJsonFunctionsTest.java  | 23 +++-
 .../org/apache/calcite/test/SqlValidatorTest.java  |  8 +++
 server/src/main/codegen/config.fmpp|  1 +
 site/_docs/reference.md| 24 
 19 files changed, 245 insertions(+), 1 deletion(-)

diff --git a/babel/src/main/codegen/config.fmpp 
b/babel/src/main/codegen/config.fmpp
index 2fa6061..f8f4a0b 100644
--- a/babel/src/main/codegen/config.fmpp
+++ b/babel/src/main/codegen/config.fmpp
@@ -139,6 +139,7 @@ data: {
 "ISOLATION"
 "JAVA"
 "JSON"
+"JSON_TYPE"
 "K"
 "KEY"
 "KEY_MEMBER"
diff --git a/core/src/main/codegen/config.fmpp 
b/core/src/main/codegen/config.fmpp
index d7d5066..4021ab1 100644
--- a/core/src/main/codegen/config.fmpp
+++ b/core/src/main/codegen/config.fmpp
@@ -159,6 +159,7 @@ data: {
 "ISOLATION"
 "JAVA"
 "JSON"
+"JSON_TYPE"
 "K"
 "KEY"
 "KEY_MEMBER"
diff --git a/core/src/main/codegen/templates/Parser.jj 
b/core/src/main/codegen/templates/Parser.jj
index 6cecc56..a546495 100644
--- a/core/src/main/codegen/templates/Parser.jj
+++ b/core/src/main/codegen/templates/Parser.jj
@@ -4827,6 +4827,8 @@ SqlNode BuiltinFunctionCall() :
 |
 node = JsonObjectFunctionCall() { return node; }
 |
+node = JsonTypeFunctionCall() { return node; }
+|
 node = JsonObjectAggFunctionCall() { return node; }
 |
 node = JsonArrayFunctionCall() { return node; }
@@ -5246,6 +5248,22 @@ SqlCall JsonObjectFunctionCall() :
 }
 }
 
+SqlCall JsonTypeFunctionCall() :
+{
+final SqlNode[] args = new SqlNode[1];
+SqlNode e;
+final Span span;
+}
+{
+ { span = span(); }
+ e = JsonValueExpression(true) {
+args[0] = e;
+}
+ {
+return SqlStdOperatorTable.JSON_TYPE.createCall(span.end(this), args);
+}
+}
+
 SqlCall JsonObjectAggFunctionCall() :
 {
 final SqlNode[] args = new SqlNode[2];
@@ -6274,6 +6292,7 @@ SqlPostfixOperator PostfixRowOperator() :
 |   < JSON_EXISTS: "JSON_EXISTS" >
 |   < JSON_VALUE: "JSON_VALUE" >
 |   < JSON_OBJECT: "JSON_OBJECT">
+|   < JSON_TYPE: "JSON_TYPE">
 |   < JSON_OBJECTAGG: "JSON_OBJECTAGG">
 |   < JSON_QUERY: "JSON_QUERY" >
 |   < K: "K" >
diff --git 
a/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java 
b/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java
index 910bebe..6059d5e 100644
--- a/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java
+++ b/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java
@@ -167,6 +167,7 @@ import static 
org.apache.calcite.sql.fun.SqlStdOperatorTable.JSON_OBJECT;
 import static org.apache.calcite.sql.fun.SqlStdOperatorTable.JSON_OBJECTAGG;
 import static org.apache.calcite.sql.fun.SqlStdOperatorTable.JSON_QUERY;
 import static 
org.apache.calcite.sql.fun.SqlStdOperatorTable.JSON_STRUCTURED_VALUE_EXPRESSION;
+import static org.apache.calcite.sql.fun.SqlStdOperatorTable.JSON_TYPE;
 import static org.apache.calcite.sql.fun.SqlStdOperatorTable.JSON_VALUE_ANY;
 import static 
org.apache.calcite.sql.fun.SqlStdOperatorTable.JSON_VALUE_EXPRESSION;
 import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LAG;
@@ -450,6 +451,7 @@ public class RexImpTable {
 defineMethod(JSON_VALUE_ANY, BuiltInMethod.JSON_VALUE_ANY.method, 
NullPolicy.NONE);
 defineMethod(JSON_QUERY, BuiltInMethod.JSON_QUERY.method, 

[calcite] branch master updated: [CALCITE-2464] Allow to set nullability for columns of structured types (Ruben Quesada Lopez) - Added nullability flag to struct types (previously they were always con

2019-02-22 Thread zabetak
This is an automated email from the ASF dual-hosted git repository.

zabetak pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/master by this push:
 new 846494a  [CALCITE-2464] Allow to set nullability for columns of 
structured types (Ruben Quesada Lopez) - Added nullability flag to struct types 
(previously they were always considered as non-nullable) - Struct types will be 
built as non-nullable by default to avoid regresssions, but this can be changed 
via RelDataTypeFactory#createTypeWithNullability - SqlCreateTable will generate 
nullable struct types, unless they are defined as NOT NULL - New unit tests
846494a is described below

commit 846494a0091b00357f3ccb67c8a4a7d9d226cd7d
Author: rubenada 
AuthorDate: Thu Nov 1 17:19:40 2018 +0100

[CALCITE-2464] Allow to set nullability for columns of structured types 
(Ruben Quesada Lopez)
- Added nullability flag to struct types (previously they were always 
considered as non-nullable)
- Struct types will be built as non-nullable by default to avoid 
regresssions, but this
can be changed via RelDataTypeFactory#createTypeWithNullability
- SqlCreateTable will generate nullable struct types, unless they are 
defined as NOT NULL
- New unit tests
---
 .../apache/calcite/jdbc/JavaTypeFactoryImpl.java   | 28 +---
 .../calcite/rel/type/RelDataTypeFactoryImpl.java   | 74 +-
 .../org/apache/calcite/rel/type/RelRecordType.java | 25 +++-
 .../calcite/sql/type/SqlTypeFactoryTest.java   | 29 +
 .../java/org/apache/calcite/test/JdbcTest.java | 15 +
 .../org/apache/calcite/sql/ddl/SqlCreateTable.java |  4 ++
 .../java/org/apache/calcite/test/ServerTest.java   |  8 +++
 server/src/test/resources/sql/type.iq  | 34 +-
 8 files changed, 173 insertions(+), 44 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/jdbc/JavaTypeFactoryImpl.java 
b/core/src/main/java/org/apache/calcite/jdbc/JavaTypeFactoryImpl.java
index 08a7aa8..7b689dc 100644
--- a/core/src/main/java/org/apache/calcite/jdbc/JavaTypeFactoryImpl.java
+++ b/core/src/main/java/org/apache/calcite/jdbc/JavaTypeFactoryImpl.java
@@ -243,18 +243,26 @@ public class JavaTypeFactoryImpl
   /** Converts a type in Java format to a SQL-oriented type. */
   public static RelDataType toSql(final RelDataTypeFactory typeFactory,
   RelDataType type) {
+return toSql(typeFactory, type, true);
+  }
+
+  private static RelDataType toSql(final RelDataTypeFactory typeFactory,
+  RelDataType type, boolean mustSetNullability) {
+RelDataType sqlType = type;
 if (type instanceof RelRecordType) {
-  return typeFactory.createStructType(
-  Lists.transform(type.getFieldList(),
-  field -> toSql(typeFactory, field.getType())),
-  type.getFieldNames());
-}
-if (type instanceof JavaType) {
-  return typeFactory.createTypeWithNullability(
-  typeFactory.createSqlType(type.getSqlTypeName()),
-  type.isNullable());
+  // We do not need to change the nullability of the nested fields,
+  // since it can be overridden by the existing implementation of 
createTypeWithNullability
+  // when we treat the nullability of the root struct type.
+  sqlType = typeFactory.createStructType(
+  Lists.transform(type.getFieldList(),
+field -> toSql(typeFactory, field.getType(), false)),
+  type.getFieldNames());
+} else if (type instanceof JavaType) {
+  sqlType = typeFactory.createSqlType(type.getSqlTypeName());
 }
-return type;
+return mustSetNullability
+? typeFactory.createTypeWithNullability(sqlType, type.isNullable())
+: sqlType;
   }
 
   public Type createSyntheticType(List types) {
diff --git 
a/core/src/main/java/org/apache/calcite/rel/type/RelDataTypeFactoryImpl.java 
b/core/src/main/java/org/apache/calcite/rel/type/RelDataTypeFactoryImpl.java
index d529ff7..26d4980 100644
--- a/core/src/main/java/org/apache/calcite/rel/type/RelDataTypeFactoryImpl.java
+++ b/core/src/main/java/org/apache/calcite/rel/type/RelDataTypeFactoryImpl.java
@@ -70,7 +70,7 @@ public abstract class RelDataTypeFactoryImpl implements 
RelDataTypeFactory {
   new RelDataTypeFieldImpl(
   key.names.get(i), i, key.types.get(i)));
 }
-return new RelRecordType(key.kind, list.build());
+return new RelRecordType(key.kind, list.build(), key.nullable);
   }
 
   private static final Map CLASS_FAMILIES =
@@ -141,8 +141,16 @@ public abstract class RelDataTypeFactoryImpl implements 
RelDataTypeFactory {
   public RelDataType createStructType(StructKind kind,
   final List typeList,
   final List fieldNameList) {
+return createStructType(kind, typeList,
+fieldNameList, false);
+  }
+
+  private RelDataType createStructType(StructKind kind,
+  final List typeList,
+