[jira] [Created] (CALCITE-4207) Validation fails for positional aggregate with current_date in 'case' expression

2020-09-01 Thread Igor Guzenko (Jira)
Igor Guzenko created CALCITE-4207:
-

 Summary: Validation fails for positional aggregate with 
current_date in 'case' expression
 Key: CALCITE-4207
 URL: https://issues.apache.org/jira/browse/CALCITE-4207
 Project: Calcite
  Issue Type: Bug
  Components: core
Reporter: Igor Guzenko


*Steps to reproduce: *
 Add test to SqlValidatorTest.java
{code:java}
  @Test void testPositionalAggregateWithExpandedCurrentDateFunction() {
sql("SELECT "
+ "CASE WHEN HIREDATE >= CURRENT_DATE "
+ "THEN 'Forward' "
+ "ELSE 'Actual' END AS fa,"
+ "COUNT(*) "
+ "FROM EMP "
+ "GROUP BY 1").ok();
  }
{code}

*Expected result*
Validation passed successfully.

*Actual result*

{code:none}org.apache.calcite.sql.validate.SqlValidatorException: Expression 
'HIREDATE' is not being grouped
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at 
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at 
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at 
org.apache.calcite.runtime.Resources$ExInstWithCause.ex(Resources.java:467)
at org.apache.calcite.runtime.Resources$ExInst.ex(Resources.java:560)
at org.apache.calcite.sql.SqlUtil.newContextException(SqlUtil.java:883)
at org.apache.calcite.sql.SqlUtil.newContextException(SqlUtil.java:868)
at 
org.apache.calcite.sql.validate.SqlValidatorImpl.newValidationError(SqlValidatorImpl.java:5003)
at org.apache.calcite.sql.validate.AggChecker.visit(AggChecker.java:113)
at org.apache.calcite.sql.validate.AggChecker.visit(AggChecker.java:40)
at org.apache.calcite.sql.SqlIdentifier.accept(SqlIdentifier.java:321)
at 
org.apache.calcite.sql.util.SqlBasicVisitor$ArgHandlerImpl.visitChild(SqlBasicVisitor.java:123)
at org.apache.calcite.sql.SqlOperator.acceptCall(SqlOperator.java:879)
at org.apache.calcite.sql.validate.AggChecker.visit(AggChecker.java:212)
at org.apache.calcite.sql.validate.AggChecker.visit(AggChecker.java:40)
at org.apache.calcite.sql.SqlCall.accept(SqlCall.java:139)
at 
org.apache.calcite.sql.util.SqlBasicVisitor.visit(SqlBasicVisitor.java:52)
at org.apache.calcite.sql.SqlNodeList.accept(SqlNodeList.java:145)
at 
org.apache.calcite.sql.util.SqlBasicVisitor$ArgHandlerImpl.visitChild(SqlBasicVisitor.java:123)
at org.apache.calcite.sql.SqlOperator.acceptCall(SqlOperator.java:879)
at org.apache.calcite.sql.validate.AggChecker.visit(AggChecker.java:212)
at org.apache.calcite.sql.validate.AggChecker.visit(AggChecker.java:40)
at org.apache.calcite.sql.SqlCall.accept(SqlCall.java:139)
at 
org.apache.calcite.sql.util.SqlBasicVisitor$ArgHandlerImpl.visitChild(SqlBasicVisitor.java:123)
at 
org.apache.calcite.sql.SqlAsOperator.acceptCall(SqlAsOperator.java:121)
at org.apache.calcite.sql.validate.AggChecker.visit(AggChecker.java:212)
at org.apache.calcite.sql.validate.AggChecker.visit(AggChecker.java:40)
at org.apache.calcite.sql.SqlCall.accept(SqlCall.java:139)
at 
org.apache.calcite.sql.validate.AggregatingSelectScope.checkAggregateExpr(AggregatingSelectScope.java:212)
at 
org.apache.calcite.sql.validate.AggregatingSelectScope.validateExpr(AggregatingSelectScope.java:221)
{code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Created] (CALCITE-3525) RexSimplify: eliminate redundant rex calls in OR

2019-11-20 Thread Igor Guzenko (Jira)
Igor Guzenko created CALCITE-3525:
-

 Summary: RexSimplify: eliminate redundant rex calls in OR
 Key: CALCITE-3525
 URL: https://issues.apache.org/jira/browse/CALCITE-3525
 Project: Calcite
  Issue Type: Improvement
Reporter: Igor Guzenko
Assignee: Igor Guzenko


Sample case to reproduce in {code}RexProgramTest.simplifyOrTerms{code}: 

{code:java}
// (a=1 or a=2 or (arr[1]>4 and arr[1]<3 and a=3)) => a=1 or a=2
final RelDataType intArrayType = typeFactory.createArrayType(intType, -1);
final RexInputRef ref0 = rexBuilder.makeInputRef(intType, 0);
final RexInputRef ref3 = rexBuilder.makeInputRef(intArrayType, 3);
final RexCall itm1 = (RexCall) rexBuilder.makeCall(intType, 
SqlStdOperatorTable.ITEM,
ImmutableList.of(ref3, literal1));
simplify = this.simplify.withParanoid(false);
checkSimplifyFilter(
or(
eq(ref0, literal1),
eq(ref0, literal2),
and(
gt(itm1, literal4),
lt(itm1, literal3),
eq(ref0, literal3)
)
),
"OR(=($0, 1), =($0, 2))"
);
simplify = simplify.withParanoid(true);
{code}




--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Created] (CALCITE-3424) AssertionError thrown for user-defined table function with array argument

2019-10-17 Thread Igor Guzenko (Jira)
Igor Guzenko created CALCITE-3424:
-

 Summary: AssertionError thrown for user-defined table function 
with array argument
 Key: CALCITE-3424
 URL: https://issues.apache.org/jira/browse/CALCITE-3424
 Project: Calcite
  Issue Type: Bug
  Components: core
Affects Versions: 1.21.0
Reporter: Igor Guzenko
Assignee: Igor Guzenko


*Steps to reproduce:*

*1.* Add method with list parameter to Smalls.java
{code:java}
  public static final Method GENERATE_STRINGS_2_METHOD =
  Types.lookupMethod(Smalls.class, "generateStrings2", List.class);
  public static QueryableTable generateStrings2(final List list) {
return generateStrings(list.size());
  }
{code}
*2.* Add test method which uses new user-defined table function to 
TableFunctionTest.java
{code:java}
  @Test public void testTableFunction2() throws SQLException {
try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) {
  CalciteConnection calciteConnection =
  connection.unwrap(CalciteConnection.class);
  SchemaPlus rootSchema = calciteConnection.getRootSchema();
  SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
  final TableFunction table =
  TableFunctionImpl.create(Smalls.GENERATE_STRINGS_2_METHOD);
  schema.add("GenerateStrings2", table);
  final String sql = "select *\n"
  + "from table(\"s\".\"GenerateStrings2\"(5,4,3,1,2)) as t(n, c)\n"
  + "where char_length(c) > 3";
  ResultSet resultSet = connection.createStatement().executeQuery(sql);
  assertThat(CalciteAssert.toString(resultSet),
  equalTo("N=4; C=abcd\n"));
}
  }
{code}
Execution result produced by such test method is the following stack trace:
{code:none}
java.lang.AssertionError: use createArrayType() instead

at 
org.apache.calcite.sql.type.SqlTypeFactoryImpl.assertBasic(SqlTypeFactoryImpl.java:221)
at 
org.apache.calcite.sql.type.SqlTypeFactoryImpl.createSqlType(SqlTypeFactoryImpl.java:48)
at 
org.apache.calcite.jdbc.JavaTypeFactoryImpl.toSql(JavaTypeFactoryImpl.java:255)
at 
org.apache.calcite.prepare.CalciteCatalogReader.toSql(CalciteCatalogReader.java:381)
at 
org.apache.calcite.prepare.CalciteCatalogReader.lambda$toSql$7(CalciteCatalogReader.java:370)
at 
com.google.common.collect.Lists$TransformingRandomAccessList$1.transform(Lists.java:640)
at 
com.google.common.collect.TransformedIterator.next(TransformedIterator.java:48)
at java.util.AbstractCollection.toArray(AbstractCollection.java:141)
at 
com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:239)
at org.apache.calcite.sql.SqlFunction.(SqlFunction.java:123)
at 
org.apache.calcite.sql.validate.SqlUserDefinedFunction.(SqlUserDefinedFunction.java:63)
at 
org.apache.calcite.sql.validate.SqlUserDefinedTableFunction.(SqlUserDefinedTableFunction.java:45)
at 
org.apache.calcite.prepare.CalciteCatalogReader.toOp(CalciteCatalogReader.java:338)
at 
org.apache.calcite.prepare.CalciteCatalogReader.toOp(CalciteCatalogReader.java:302)
at 
org.apache.calcite.prepare.CalciteCatalogReader.lambda$lookupOperatorOverloads$3(CalciteCatalogReader.java:271)
at 
java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at 
java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at 
java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
at 
java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at 
java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at 
java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at 
java.util.stream.ReferencePipeline.forEachOrdered(ReferencePipeline.java:490)
at 
org.apache.calcite.prepare.CalciteCatalogReader.lookupOperatorOverloads(CalciteCatalogReader.java:272)
at 
org.apache.calcite.sql.util.ChainedSqlOperatorTable.lookupOperatorOverloads(ChainedSqlOperatorTable.java:73)
at 
org.apache.calcite.sql.validate.SqlValidatorImpl.performUnconditionalRewrites(SqlValidatorImpl.java:1195)
at 
org.apache.calcite.sql.validate.SqlValidatorImpl.performUnconditionalRewrites(SqlValidatorImpl.java:1180)
at 
org.apache.calcite.sql.validate.SqlValidatorImpl.performUnconditionalRewrites(SqlValidatorImpl.java:1180)
at 
org.apache.calcite.sql.validate.SqlValidatorImpl.performUnconditionalRewrites(SqlValidatorImpl.java:1180)
at 
org.apache.calcite.sql.validate.SqlValidatorImpl.validateScopedExpression(SqlValidatorImpl.java:937)
at 

[jira] [Created] (CALCITE-3393) RelStructuredTypeFlattener: improve support for functions with struct input

2019-10-09 Thread Igor Guzenko (Jira)
Igor Guzenko created CALCITE-3393:
-

 Summary: RelStructuredTypeFlattener: improve support for functions 
with struct input
 Key: CALCITE-3393
 URL: https://issues.apache.org/jira/browse/CALCITE-3393
 Project: Calcite
  Issue Type: Improvement
  Components: core
Reporter: Igor Guzenko
Assignee: Igor Guzenko


1. 
[RelStructuredTypeFlattener|https://github.com/apache/calcite/blob/148bfd329413c0272395cc0b7c322b3c5a34b667/core/src/main/java/org/apache/calcite/sql2rel/RelStructuredTypeFlattener.java#L376]
 doesn't support aggregate functions for struct type column.
Example test case:

{code:java}
  @Test
  public void testAggregateFunctionWithStructInput() {
final String sql =
"select count(dn.skill) from sales.dept_nested dn";
sql(sql).ok();
  }
{code}

2. For other functions, except ITEM, flattener uses first nested primitive 
field from 
original struct input. Example test case:

{code:java}
  @Test
  public void testFunctionWithStructInput() {
final String sql =
"select json_type(dn.skill) from sales.dept_nested dn";
sql(sql).ok();
  }
{code}

Generated plan:
{code}
LogicalProject(EXPR$0=[JSON_TYPE($2.TYPE)])
  LogicalTableScan(table=[[CATALOG, SALES, DEPT_NESTED]])
{code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Created] (CALCITE-2639) FilterReduceExpressionsRule causes ArithmeticException at execution time

2018-10-24 Thread Igor Guzenko (JIRA)
Igor Guzenko created CALCITE-2639:
-

 Summary: FilterReduceExpressionsRule causes ArithmeticException at 
execution time
 Key: CALCITE-2639
 URL: https://issues.apache.org/jira/browse/CALCITE-2639
 Project: Calcite
  Issue Type: Bug
Affects Versions: 1.17.0
Reporter: Igor Guzenko
Assignee: Julian Hyde


Consider logical plan generated for test case(put this in {{RelOptRuleTest}}): 

 
{code:java}
@Test public void testOversimplifiedCaseStatement() {
HepProgram program = new HepProgramBuilder()
.addRuleInstance(ReduceExpressionsRule.FILTER_INSTANCE)
.build();

String sql = "select * from emp " 
  + "where MGR > 0 and "
  + "case when MGR > 0 then deptno / MGR else null end > 1";
checkPlanning(program, sql);
  }
{code}
Before applying  ReduceExpressionsRule.FILTER_INSTANCE rule, query plan is 

 
{code:java}
LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], 
SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
  LogicalFilter(condition=[AND(>($3, 0), >(CASE(>($3, 0), /($7, $3), null), 
1))])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
{code}
Plan after applying the rule:
{code:java}
LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], 
SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
  LogicalFilter(condition=[AND(>($3, 0), CASE(IS NOT NULL($3), >(/($7, $3), 1), 
false))])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
{code}
Here {{LogicalFilter}} has condition {{AND(>($3, 0), CASE(IS NOT NULL($3), 
>(/($7, $3), 1), false))}} where {{CASE}} condition was replaced by {{IS NOT 
NULL($3)}} condition. Since {{AND}} allows permutations for operands, the first 
operand may become {{CASE}}, so query may fail with {{ArithmeticException: / by 
zero}} error at execution stage.

The regression was caused by 
[CALCITE-1413|https://github.com/apache/calcite/commit/b470a0cd4572c9f6c4c0e9b51926b97c5af58d3f].

 



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)