(calcite) branch main updated: Link to calcite notes about runnable examples and concise documents

2024-03-09 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new ad08ce5fbd Link to calcite notes about runnable examples and concise 
documents
ad08ce5fbd is described below

commit ad08ce5fbdf3ea92162c853f2dbcfeb9d09694e6
Author: Jiajun Xie 
AuthorDate: Sat Mar 9 15:32:32 2024 +0800

Link to calcite notes about runnable examples and concise documents
---
 site/community/index.md | 1 +
 1 file changed, 1 insertion(+)

diff --git a/site/community/index.md b/site/community/index.md
index 9d18bcadff..e447fc3be2 100644
--- a/site/community/index.md
+++ b/site/community/index.md
@@ -226,6 +226,7 @@ If you have something interesting to share with the 
community drop us an email o
 consider creating a pull request on GitHub. If you just finished a cool 
project using Calcite
 consider writing a short article about it for our [news section]({{ 
site.baseurl }}/news/index.html).
 
+* https://github.com/JiajunBernoulli/calcite-notes;>Calcite notes 
about runnable examples and concise documents (Jiajun Xie, 2024)
 * https://www.feldera.com/blog/calcite-irs/;>Calcite program 
representations (Mihai Budiu, October 2023)
 * https://datalore.jetbrains.com/view/notebook/JYTVfn90xYSmv6U5f2NIQR;>Building
 a new Calcite frontend (GraphQL) (Gavin Ray, 2022)
 * https://github.com/ieugen/calcite-clj;>Write Calcite adapters in 
Clojure (Ioan Eugen Stan, 2022)



(calcite) branch main updated: [CALCITE-6273] Add sqrt negative test in SqlOperatorTest

2024-02-19 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 31d66e797d [CALCITE-6273] Add sqrt negative test in SqlOperatorTest
31d66e797d is described below

commit 31d66e797d3ca66d154262b15ee35876804df963
Author: caicancai <2356672...@qq.com>
AuthorDate: Sun Feb 18 23:28:52 2024 +0800

[CALCITE-6273] Add sqrt negative test in SqlOperatorTest
---
 .../main/java/org/apache/calcite/test/SqlOperatorTest.java   | 12 
 1 file changed, 12 insertions(+)

diff --git a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java 
b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
index 2ff72a1697..12052c9a82 100644
--- a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
+++ b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
@@ -6110,6 +6110,18 @@ public class SqlOperatorTest {
 isWithin(1.4142d, 0.0001d));
 f.checkScalarApprox("sqrt(cast(2 as decimal(2, 0)))", "DOUBLE NOT NULL",
 isWithin(1.4142d, 0.0001d));
+f.checkScalarApprox("sqrt(0)", "DOUBLE NOT NULL",
+isWithin(0, 0.0001d));
+f.checkScalarApprox("sqrt(0.1)", "DOUBLE NOT NULL",
+isWithin(0.31622776601683794, 0.0001d));
+f.checkScalarApprox("sqrt(2.0/3)", "DOUBLE NOT NULL",
+isWithin(0.816496580927726, 0.0001d));
+f.checkScalarApprox("sqrt(cast(10e8 as integer))", "DOUBLE NOT NULL",
+isWithin(31622.776601683792, 0.0001d));
+f.checkScalarApprox("sqrt(cast(10e8 as double))", "DOUBLE NOT NULL",
+isWithin(31622.776601683792, 0.0001d));
+f.checkScalarApprox("sqrt(-1)", "DOUBLE NOT NULL",
+"NaN");
 f.checkNull("sqrt(cast(null as integer))");
 f.checkNull("sqrt(cast(null as double))");
   }



(calcite) branch main updated: [CALCITE-6214] Remove DISTINCT in aggregate function if field is unique

2024-02-11 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new ec0dc3c886 [CALCITE-6214] Remove DISTINCT in aggregate function if 
field is unique
ec0dc3c886 is described below

commit ec0dc3c886ef06020294dc80971c29ec3b90fa44
Author: Jiajun Xie 
AuthorDate: Sun Jan 21 15:23:19 2024 +0800

[CALCITE-6214] Remove DISTINCT in aggregate function if field is unique
---
 .../java/org/apache/calcite/tools/RelBuilder.java  |  42 ++-
 .../apache/calcite/test/SqlToRelConverterTest.java | 122 +++
 .../apache/calcite/test/SqlToRelConverterTest.xml  | 132 +
 3 files changed, 295 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java 
b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java
index 9d8d32bd22..ad58474bae 100644
--- a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java
+++ b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java
@@ -2430,7 +2430,11 @@ public class RelBuilder {
 RelNode r = frame.rel;
 final List aggregateCalls = new ArrayList<>();
 for (AggCallPlus aggCall : aggCalls) {
-  aggregateCalls.add(aggCall.aggregateCall(registrar, groupSet, r));
+  AggregateCall aggregateCall = aggCall.aggregateCall(registrar, groupSet, 
r);
+  if (groupSets.size() <= 1) {
+aggregateCall = removeRedundantAggregateDistinct(aggregateCall, 
groupSet, r);
+  }
+  aggregateCalls.add(aggregateCall);
 }
 
 assert ImmutableBitSet.ORDERING.isStrictlyOrdered(groupSets) : groupSets;
@@ -2525,6 +2529,30 @@ public class RelBuilder {
 return project(projects.transform((i, name) -> aliasMaybe(field(i), 
name)));
   }
 
+  /**
+   * Removed redundant distinct if an input is already unique.
+   */
+  private AggregateCall removeRedundantAggregateDistinct(
+  AggregateCall aggregateCall,
+  ImmutableBitSet groupSet,
+  RelNode relNode) {
+if (aggregateCall.isDistinct() && config.removeRedundantDistinct()) {
+  final RelMetadataQuery mq = relNode.getCluster().getMetadataQuery();
+  final List argList = aggregateCall.getArgList();
+  final ImmutableBitSet distinctArg = ImmutableBitSet.builder()
+  .addAll(argList)
+  .build();
+  final ImmutableBitSet columns = groupSet.union(distinctArg);
+  final Boolean alreadyUnique =
+  mq.areColumnsUnique(relNode, columns);
+  if (alreadyUnique != null && alreadyUnique) {
+// columns have been distinct or columns are primary keys
+return aggregateCall.withDistinct(false);
+  }
+}
+return aggregateCall;
+  }
+
   /** Returns whether an input is already unique, and therefore a Project
* can be created instead of an Aggregate.
*
@@ -4902,6 +4930,18 @@ public class RelBuilder {
 
 /** Sets {@link #convertCorrelateToJoin()}. */
 Config withConvertCorrelateToJoin(boolean convertCorrelateToJoin);
+
+/** Whether to remove the distinct that in aggregate if we know that the 
input is
+ * already unique; default false. */
+@Value.Default
+default boolean removeRedundantDistinct() {
+  return false;
+}
+
+/**
+ * Sets {@link #removeRedundantDistinct()}.
+ */
+Config withRemoveRedundantDistinct(boolean removeRedundantDistinct);
   }
 
 }
diff --git 
a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java 
b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
index 810a1b3db1..e62eeb78b5 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
@@ -5078,6 +5078,128 @@ class SqlToRelConverterTest extends SqlToRelTestBase {
 .ok();
   }
 
+  /** Test case for
+   * https://issues.apache.org/jira/browse/CALCITE-6214;>[CALCITE-6214]
+   * Remove DISTINCT in COUNT if field is unique. */
+  @Test void testRemoveDistinctIfUnique1() {
+final String sql = "SELECT\n"
++ "deptno,\n"
++ "COUNT(DISTINCT sal) as cds,\n"
++ "COUNT(sal) as cs,\n"
++ "SUM(DISTINCT sal) AS sds,\n"
++ "SUM(sal) AS ss\n"
++ "FROM (\n"
++ "SELECT DISTINCT deptno, sal\n"
++ "FROM emp)\n"
++ "GROUP BY deptno";
+sql(sql)
+.withConfig(c ->
+c.addRelBuilderConfigTransform(c2 ->
+c2.withRemoveRedundantDistinct(true))).ok();
+  }
+
+  /** Test case for
+   * https://issues.apache.org/jira/browse/CALCITE-6214;>[CALCITE-6214]
+   * Remove DISTINCT in COUNT if field is unique. */
+  @Test void testRemoveDistinctIfUnique2() {
+final String sql 

(calcite) branch main updated: [CALCITE-6199] Trim unused fields for SNAPSHOT and SAMPLE if table has VIRTUAL column

2024-01-27 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 75511b82ab [CALCITE-6199] Trim unused fields for SNAPSHOT and SAMPLE 
if table has VIRTUAL column
75511b82ab is described below

commit 75511b82ab13ff95bc65f6180d40cf1e234b834e
Author: Jiajun Xie 
AuthorDate: Sat Jan 13 17:15:52 2024 +0800

[CALCITE-6199] Trim unused fields for SNAPSHOT and SAMPLE if table has 
VIRTUAL column
---
 .../apache/calcite/sql2rel/RelFieldTrimmer.java| 65 ++
 .../apache/calcite/test/SqlToRelConverterTest.java | 48 
 .../apache/calcite/test/SqlToRelConverterTest.xml  | 48 
 3 files changed, 161 insertions(+)

diff --git a/core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java 
b/core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java
index c5a9fb20e5..bfa69d0a4d 100644
--- a/core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java
+++ b/core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java
@@ -34,7 +34,9 @@ import org.apache.calcite.rel.core.Join;
 import org.apache.calcite.rel.core.JoinRelType;
 import org.apache.calcite.rel.core.Project;
 import org.apache.calcite.rel.core.RelFactories;
+import org.apache.calcite.rel.core.Sample;
 import org.apache.calcite.rel.core.SetOp;
+import org.apache.calcite.rel.core.Snapshot;
 import org.apache.calcite.rel.core.Sort;
 import org.apache.calcite.rel.core.SortExchange;
 import org.apache.calcite.rel.core.TableScan;
@@ -1236,6 +1238,69 @@ public class RelFieldTrimmer implements 
ReflectiveVisitor {
 return result(newValues, mapping, values);
   }
 
+  /**
+   * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
+   * {@link org.apache.calcite.rel.core.Sample}.
+   */
+  public TrimResult trimFields(
+  final Sample sample,
+  ImmutableBitSet fieldsUsed,
+  Set extraFields) {
+final RelDataType rowType = sample.getRowType();
+final int fieldCount = rowType.getFieldCount();
+final RelNode input = sample.getInput();
+
+// Create input with trimmed columns.
+final Set inputExtraFields = Collections.emptySet();
+TrimResult trimResult =
+trimChild(sample, input, fieldsUsed, inputExtraFields);
+final RelNode newInput = trimResult.left;
+final Mapping inputMapping = trimResult.right;
+
+// If the input is unchanged, and we need to project all columns,
+// there's nothing we can do.
+if (newInput == input
+&& fieldsUsed.cardinality() == fieldCount) {
+  return result(sample, Mappings.createIdentity(fieldCount));
+}
+
+final RelNode newSample =
+sample.copy(sample.getTraitSet(), ImmutableList.of(newInput));
+return result(newSample, inputMapping, sample);
+  }
+
+  /**
+   * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
+   * {@link org.apache.calcite.rel.core.Snapshot}.
+   */
+  public TrimResult trimFields(
+  final Snapshot snapshot,
+  ImmutableBitSet fieldsUsed,
+  Set extraFields) {
+final RelDataType rowType = snapshot.getRowType();
+final int fieldCount = rowType.getFieldCount();
+final RelNode input = snapshot.getInput();
+
+// Create input with trimmed columns.
+final Set inputExtraFields = Collections.emptySet();
+TrimResult trimResult =
+trimChild(snapshot, input, fieldsUsed, inputExtraFields);
+final RelNode newInput = trimResult.left;
+final Mapping inputMapping = trimResult.right;
+
+// If the input is unchanged, and we need to project all columns,
+// there's nothing we can do.
+if (newInput == input
+&& fieldsUsed.cardinality() == fieldCount) {
+  return result(snapshot, Mappings.createIdentity(fieldCount));
+}
+
+final Snapshot newSnapshot =
+snapshot.copy(snapshot.getTraitSet(), newInput,
+snapshot.getPeriod());
+return result(newSnapshot, inputMapping, snapshot);
+  }
+
   protected Mapping createMapping(ImmutableBitSet fieldsUsed, int fieldCount) {
 final Mapping mapping =
 Mappings.create(
diff --git 
a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java 
b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
index cd95b88aef..810a1b3db1 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
@@ -4955,6 +4955,54 @@ class SqlToRelConverterTest extends SqlToRelTestBase {
 sql(sql).withTrim(true).ok();
   }
 
+  /**
+   * Test case for
+   * https://issues.apache.org/jira/browse/CALCITE-6199;>[CALCITE-6199]
+   * Trim unused fields for SNAPSHOT and SAMPLE if table has VIRTUAL 
column.
+   */
+  @Test void testTrimSnapshotOnTemporalTable1() {
+// Test temporal table w

(calcite) branch main updated: [MINOR] Add checkNull Test on SqlOperatorTest

2023-12-24 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 6e7b05d776 [MINOR] Add checkNull Test on SqlOperatorTest
6e7b05d776 is described below

commit 6e7b05d7767381c9928fea7caed6dcb08b54a63f
Author: caicancai <2356672...@qq.com>
AuthorDate: Sun Dec 24 12:44:01 2023 +0800

[MINOR] Add checkNull Test on SqlOperatorTest
---
 .../main/java/org/apache/calcite/test/SqlOperatorTest.java  | 13 +
 1 file changed, 13 insertions(+)

diff --git a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java 
b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
index f9722692ce..8385195980 100644
--- a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
+++ b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
@@ -12376,6 +12376,10 @@ public class SqlOperatorTest {
 f.checkScalar("FORMAT_DATE('The date is: %x', DATE '2008-12-25')",
 "The date is: 12/25/08",
 "VARCHAR(2000) NOT NULL");
+f.checkNull("FORMAT_DATE('%x', CAST(NULL AS DATE))");
+f.checkNull("FORMAT_DATE('%b-%d-%Y', CAST(NULL AS DATE))");
+f.checkNull("FORMAT_DATE('%b %Y', CAST(NULL AS DATE))");
+f.checkNull("FORMAT_DATE(NULL, CAST(NULL AS DATE))");
   }
 
   @Test void testFormatTimestamp() {
@@ -12439,6 +12443,10 @@ public class SqlOperatorTest {
 f.checkScalar("PARSE_DATE('%F', '2022-06-01')",
 "2022-06-01",
 "DATE NOT NULL");
+f.checkNull("PARSE_DATE('%F', CAST(NULL AS DATE))");
+f.checkNull("PARSE_DATE('%Y%m%d', CAST(NULL AS DATE))");
+f.checkNull("PARSE_DATE('%x', CAST(NULL AS DATE))");
+f.checkNull("PARSE_DATE(NULL, CAST(NULL AS DATE))");
   }
 
   @Test void testParseDatetime() {
@@ -12478,6 +12486,11 @@ public class SqlOperatorTest {
 f.checkScalar("PARSE_TIME('%I:%M:%S %p', '2:23:38 pm')",
 "14:23:38",
 "TIME(0) NOT NULL");
+f.checkNull("PARSE_TIME('%I:%M:%S', CAST(NULL AS TIME))");
+f.checkNull("PARSE_TIME('%T', CAST(NULL AS TIME))");
+f.checkNull("PARSE_TIME('%H', CAST(NULL AS TIME))");
+f.checkNull("PARSE_TIME('%I:%M:%S %p', CAST(NULL AS TIME))");
+f.checkNull("PARSE_TIME(NULL, CAST(NULL AS TIME))");
   }
 
   @Test void testParseTimestamp() {



(calcite) branch main updated: [CALCITE-6121] Invalid unparse for TIMESTAMP with SparkSqlDialect

2023-11-20 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 8556807787 [CALCITE-6121] Invalid unparse for TIMESTAMP with 
SparkSqlDialect
8556807787 is described below

commit 8556807787bcde19bbfe05d040adf7c37784e97b
Author: Jiajun Xie 
AuthorDate: Sat Nov 18 17:04:42 2023 +0800

[CALCITE-6121] Invalid unparse for TIMESTAMP with SparkSqlDialect
---
 .../main/java/org/apache/calcite/sql/SqlDialect.java | 14 ++
 .../apache/calcite/sql/dialect/ExasolSqlDialect.java | 20 
 .../apache/calcite/sql/dialect/PrestoSqlDialect.java |  4 
 .../apache/calcite/sql/dialect/SparkSqlDialect.java  |  4 
 .../calcite/rel/rel2sql/RelToSqlConverterTest.java   | 17 +
 5 files changed, 43 insertions(+), 16 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/sql/SqlDialect.java 
b/core/src/main/java/org/apache/calcite/sql/SqlDialect.java
index 5940485560..71157191e8 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlDialect.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlDialect.java
@@ -760,6 +760,13 @@ public class SqlDialect {
 return false;
   }
 
+  /**
+   * Returns whether this dialect supports TIMESTAMP with precision.
+   */
+  public boolean supportsTimestampPrecision() {
+return true;
+  }
+
   /** Returns whether this dialect supports the use of FILTER clauses for
* aggregate functions. e.g. {@code COUNT(*) FILTER (WHERE a = 2)}. */
   public boolean supportsAggregateFunctionFilter() {
@@ -839,6 +846,13 @@ public class SqlDialect {
 // if needed, adjust varchar length to max length supported by the 
system
 maxPrecision = getTypeSystem().getMaxPrecision(type.getSqlTypeName());
 break;
+  case TIMESTAMP:
+if (!supportsTimestampPrecision()) {
+  return new SqlDataTypeSpec(
+  new SqlBasicTypeNameSpec(type.getSqlTypeName(), 
SqlParserPos.ZERO),
+  SqlParserPos.ZERO);
+}
+break;
   default:
 break;
   }
diff --git 
a/core/src/main/java/org/apache/calcite/sql/dialect/ExasolSqlDialect.java 
b/core/src/main/java/org/apache/calcite/sql/dialect/ExasolSqlDialect.java
index 6e127cfc3d..e3122b41a1 100644
--- a/core/src/main/java/org/apache/calcite/sql/dialect/ExasolSqlDialect.java
+++ b/core/src/main/java/org/apache/calcite/sql/dialect/ExasolSqlDialect.java
@@ -16,15 +16,11 @@
  */
 package org.apache.calcite.sql.dialect;
 
-import org.apache.calcite.rel.type.RelDataType;
-import org.apache.calcite.sql.SqlBasicTypeNameSpec;
 import org.apache.calcite.sql.SqlCall;
-import org.apache.calcite.sql.SqlDataTypeSpec;
 import org.apache.calcite.sql.SqlDialect;
 import org.apache.calcite.sql.SqlKind;
 import org.apache.calcite.sql.SqlNode;
 import org.apache.calcite.sql.SqlWriter;
-import org.apache.calcite.sql.parser.SqlParserPos;
 
 import com.google.common.collect.ImmutableList;
 
@@ -134,6 +130,10 @@ public class ExasolSqlDialect extends SqlDialect {
 return false;
   }
 
+  @Override public boolean supportsTimestampPrecision() {
+return false;
+  }
+
   @Override public boolean supportsAggregateFunction(SqlKind kind) {
 switch (kind) {
 case AVG:
@@ -158,18 +158,6 @@ public class ExasolSqlDialect extends SqlDialect {
 || RESERVED_KEYWORDS.contains(val.toUpperCase(Locale.ROOT));
   }
 
-  @Override public @Nullable SqlNode getCastSpec(RelDataType type) {
-switch (type.getSqlTypeName()) {
-case TIMESTAMP:
-  // Exasol does not support TIMESTAMP with precision.
-  return new SqlDataTypeSpec(
-new SqlBasicTypeNameSpec(type.getSqlTypeName(), SqlParserPos.ZERO),
-SqlParserPos.ZERO);
-default:
-  return super.getCastSpec(type);
-}
-  }
-
   @Override public void unparseOffsetFetch(SqlWriter writer, @Nullable SqlNode 
offset,
 @Nullable SqlNode fetch) {
 unparseFetchUsingLimit(writer, offset, fetch);
diff --git 
a/core/src/main/java/org/apache/calcite/sql/dialect/PrestoSqlDialect.java 
b/core/src/main/java/org/apache/calcite/sql/dialect/PrestoSqlDialect.java
index 75b232e349..4209fe77bf 100644
--- a/core/src/main/java/org/apache/calcite/sql/dialect/PrestoSqlDialect.java
+++ b/core/src/main/java/org/apache/calcite/sql/dialect/PrestoSqlDialect.java
@@ -64,6 +64,10 @@ public class PrestoSqlDialect extends SqlDialect {
 return true;
   }
 
+  @Override public boolean supportsTimestampPrecision() {
+return false;
+  }
+
   @Override public void unparseOffsetFetch(SqlWriter writer, @Nullable SqlNode 
offset,
   @Nullable SqlNode fetch) {
 unparseUsingLimit(writer, offset, fetch);
diff --git 
a/core/src/main/java/org/apache/calcite/sql/dialect/SparkSqlDialect.java 
b/core/src/main/java/org/apache/calcite/sql/dialect/SparkSqlDialect.java
index e97cfcac01..f1f67fa35b 100644

(calcite) branch main updated: [CALCITE-6102] SqlWriter in SqlInsert's unparse start a list but does not end it

2023-11-15 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new db192e92a5 [CALCITE-6102] SqlWriter in SqlInsert's unparse start a 
list but does not end it
db192e92a5 is described below

commit db192e92a5ff0a8688b6f24027d5a6c2bb92f792
Author: jiefei 
AuthorDate: Fri Nov 10 17:56:52 2023 +0800

[CALCITE-6102] SqlWriter in SqlInsert's unparse start a list but does not 
end it
---
 .../java/org/apache/calcite/sql/SqlInsert.java |  3 ++-
 .../calcite/sql/test/SqlPrettyWriterTest.java  | 24 ++
 .../calcite/sql/test/SqlPrettyWriterTest.xml   |  7 +++
 3 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/calcite/sql/SqlInsert.java 
b/core/src/main/java/org/apache/calcite/sql/SqlInsert.java
index 699298b0ff..f95932bfbf 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlInsert.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlInsert.java
@@ -150,7 +150,7 @@ public class SqlInsert extends SqlCall {
   }
 
   @Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) 
{
-writer.startList(SqlWriter.FrameTypeEnum.SELECT);
+final SqlWriter.Frame frame = 
writer.startList(SqlWriter.FrameTypeEnum.SELECT);
 writer.sep(isUpsert() ? "UPSERT INTO" : "INSERT INTO");
 final int opLeft = getOperator().getLeftPrec();
 final int opRight = getOperator().getRightPrec();
@@ -160,6 +160,7 @@ public class SqlInsert extends SqlCall {
 }
 writer.newlineAndIndent();
 source.unparse(writer, 0, 0);
+writer.endList(frame);
   }
 
   @Override public void validate(SqlValidator validator, SqlValidatorScope 
scope) {
diff --git 
a/core/src/test/java/org/apache/calcite/sql/test/SqlPrettyWriterTest.java 
b/core/src/test/java/org/apache/calcite/sql/test/SqlPrettyWriterTest.java
index 37cd6bab81..03c65fce99 100644
--- a/core/src/test/java/org/apache/calcite/sql/test/SqlPrettyWriterTest.java
+++ b/core/src/test/java/org/apache/calcite/sql/test/SqlPrettyWriterTest.java
@@ -18,6 +18,7 @@ package org.apache.calcite.sql.test;
 
 import org.apache.calcite.sql.SqlNode;
 import org.apache.calcite.sql.SqlSelect;
+import org.apache.calcite.sql.SqlWith;
 import org.apache.calcite.sql.SqlWriter;
 import org.apache.calcite.sql.SqlWriterConfig;
 import org.apache.calcite.sql.parser.SqlParseException;
@@ -486,6 +487,29 @@ class SqlPrettyWriterTest {
 .check();
   }
 
+  @Test void testInsert() {
+sql("insert into t1 select * from t2")
+.check();
+  }
+
+  /** Test case for
+   * https://issues.apache.org/jira/browse/CALCITE-6102;>[CALCITE-6102]
+   * SqlWriter in SqlInsert's unparse start a list but does not end it. */
+  @Test void testSqlWithBodyIsSqlInsert() throws SqlParseException {
+final String withSql = "with tmp as (select * from t1) select 1";
+final String insertSql = "insert into t2 select * from tmp";
+final String expectedSql = "WITH `TMP` AS (SELECT *\n"
++ "FROM `T1`) INSERT INTO `T2`\n"
++ "SELECT *\n"
++ "FROM `TMP`";
+final SqlNode sqlInsert = SqlParser.create(insertSql).parseStmt();
+final SqlNode sqlNode = SqlParser.create(withSql).parseQuery();
+assertThat(sqlNode, instanceOf(SqlWith.class));
+final SqlWith sqlWith = (SqlWith) sqlNode;
+sqlWith.setOperand(1, sqlInsert);
+assertThat(sqlWith, hasToString(isLinux(expectedSql)));
+  }
+
   public static void main(String[] args) throws SqlParseException {
 final String sql = "select x as a, b as b, c as c, d,"
 + " 'mixed-Case string',"
diff --git 
a/core/src/test/resources/org/apache/calcite/sql/test/SqlPrettyWriterTest.xml 
b/core/src/test/resources/org/apache/calcite/sql/test/SqlPrettyWriterTest.xml
index 965712f049..bf4d3a76b8 100644
--- 
a/core/src/test/resources/org/apache/calcite/sql/test/SqlPrettyWriterTest.xml
+++ 
b/core/src/test/resources/org/apache/calcite/sql/test/SqlPrettyWriterTest.xml
@@ -225,6 +225,13 @@ FROM `X`
 INNER JOIN `Y` ON `X`.`K` = `Y`.`K`]]>
 
   
+  
+
+  
+
+  
   
 
   

(calcite) branch main updated: [CALCITE-5826] Add FIND_IN_SET function (enabled in Hive and Spark libraries)

2023-10-31 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 2ddc6053b5 [CALCITE-5826] Add FIND_IN_SET function (enabled in Hive 
and Spark libraries)
2ddc6053b5 is described below

commit 2ddc6053b55147622f4a4642786d36403ce7f49a
Author: Runkang He 
AuthorDate: Fri Jul 14 09:12:35 2023 +0800

[CALCITE-5826] Add FIND_IN_SET function (enabled in Hive and Spark 
libraries)
---
 .../calcite/adapter/enumerable/RexImpTable.java|  2 ++
 .../org/apache/calcite/runtime/SqlFunctions.java   | 25 ++
 .../calcite/sql/fun/SqlLibraryOperators.java   |  8 +++
 .../org/apache/calcite/util/BuiltInMethod.java |  1 +
 site/_docs/reference.md|  1 +
 .../org/apache/calcite/test/SqlOperatorTest.java   | 25 ++
 6 files changed, 62 insertions(+)

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 eaf12af158..77cd73d24f 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
@@ -178,6 +178,7 @@ import static 
org.apache.calcite.sql.fun.SqlLibraryOperators.EXISTS_NODE;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.EXTRACT_VALUE;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.EXTRACT_XML;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.FACTORIAL;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.FIND_IN_SET;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.FLOOR_BIG_QUERY;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.FORMAT_DATE;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.FORMAT_DATETIME;
@@ -598,6 +599,7 @@ public class RexImpTable {
   defineReflective(REGEXP_INSTR, BuiltInMethod.REGEXP_INSTR2.method,
   BuiltInMethod.REGEXP_INSTR3.method, 
BuiltInMethod.REGEXP_INSTR4.method,
   BuiltInMethod.REGEXP_INSTR5.method);
+  defineMethod(FIND_IN_SET, BuiltInMethod.FIND_IN_SET.method, 
NullPolicy.ANY);
 
   map.put(TRIM, new TrimImplementor());
 
diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java 
b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
index a92e1a95a9..5f3cbf9563 100644
--- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
+++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -146,6 +146,8 @@ import static java.util.Objects.requireNonNull;
 @SuppressWarnings("UnnecessaryUnboxing")
 @Deterministic
 public class SqlFunctions {
+  private static final String COMMA_DELIMITER = ",";
+
   @SuppressWarnings("unused")
   private static final DecimalFormat DOUBLE_FORMAT =
   NumberUtil.decimalFormat("0.0E0");
@@ -1142,6 +1144,29 @@ public class SqlFunctions {
 return LEVENSHTEIN_DISTANCE.apply(string1, string2);
   }
 
+  /** SQL FIND_IN_SET(matchStr, textStr) function.
+   * Returns the index (1-based) of the given matchStr
+   * in the comma-delimited list textStr. Returns 0,
+   * if the matchStr is not found or if the matchStr
+   * contains a comma. */
+  public static @Nullable Integer findInSet(
+  @Nullable String matchStr,
+  @Nullable String textStr) {
+if (matchStr == null || textStr == null) {
+  return null;
+}
+if (matchStr.contains(COMMA_DELIMITER)) {
+  return 0;
+}
+String[] splits = textStr.split(COMMA_DELIMITER);
+for (int i = 0; i < splits.length; i++) {
+  if (matchStr.equals(splits[i])) {
+return i + 1;
+  }
+}
+return 0;
+  }
+
   /** SQL ASCII(string) function. */
   public static int ascii(String s) {
 return s.isEmpty()
diff --git 
a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java 
b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
index e05c4fd1c4..a5440a211d 100644
--- a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
@@ -413,6 +413,14 @@ public abstract class SqlLibraryOperators {
   OperandTypes.STRING_STRING_OPTIONAL_STRING,
   SqlFunctionCategory.STRING);
 
+  /** The "FIND_IN_SET(matchStr, textStr)" function. */
+  @LibraryOperator(libraries = {HIVE, SPARK})
+  public static final SqlFunction FIND_IN_SET =
+  SqlBasicFunction.create("FIND_IN_SET",
+  ReturnTypes.INTEGER_NULLABLE,
+  OperandTypes.STRING_STRING,
+  SqlFunctionCategory.STRING);
+
   /** The "GREATEST(value, value)" function. */
   @LibraryOperator(libraries = {BIG_QUERY, ORACLE})
   publi

(calcite) branch main updated: [CALCITE-6037] The function category of ARRAY/EXTRACT_VALUE/XML_TRANSFORM/EXTRACT_XML/EXISTSNODE is incorrect

2023-10-29 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new edec1c3891 [CALCITE-6037] The function category of 
ARRAY/EXTRACT_VALUE/XML_TRANSFORM/EXTRACT_XML/EXISTSNODE is incorrect
edec1c3891 is described below

commit edec1c3891b587aa56c1da09a3dd810f4b7adcc1
Author: Ran Tao 
AuthorDate: Fri Oct 6 15:29:22 2023 +0800

[CALCITE-6037] The function category of 
ARRAY/EXTRACT_VALUE/XML_TRANSFORM/EXTRACT_XML/EXISTSNODE is incorrect
---
 .../org/apache/calcite/sql/fun/SqlLibraryOperators.java   | 15 ++-
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java 
b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
index 5c7a6e5a56..3c4949807b 100644
--- a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
@@ -550,26 +550,30 @@ public abstract class SqlLibraryOperators {
   public static final SqlFunction EXTRACT_VALUE =
   SqlBasicFunction.create("EXTRACTVALUE",
   ReturnTypes.VARCHAR_2000.andThen(SqlTypeTransforms.FORCE_NULLABLE),
-  OperandTypes.STRING_STRING);
+  OperandTypes.STRING_STRING,
+  SqlFunctionCategory.STRING);
 
   @LibraryOperator(libraries = {ORACLE})
   public static final SqlFunction XML_TRANSFORM =
   SqlBasicFunction.create("XMLTRANSFORM",
   ReturnTypes.VARCHAR.andThen(SqlTypeTransforms.FORCE_NULLABLE),
-  OperandTypes.STRING_STRING);
+  OperandTypes.STRING_STRING,
+  SqlFunctionCategory.STRING);
 
   @LibraryOperator(libraries = {ORACLE})
   public static final SqlFunction EXTRACT_XML =
   SqlBasicFunction.create("EXTRACT",
   ReturnTypes.VARCHAR.andThen(SqlTypeTransforms.FORCE_NULLABLE),
-  OperandTypes.STRING_STRING_OPTIONAL_STRING);
+  OperandTypes.STRING_STRING_OPTIONAL_STRING,
+  SqlFunctionCategory.STRING);
 
   @LibraryOperator(libraries = {ORACLE})
   public static final SqlFunction EXISTS_NODE =
   SqlBasicFunction.create("EXISTSNODE",
   ReturnTypes.INTEGER_NULLABLE
   .andThen(SqlTypeTransforms.FORCE_NULLABLE),
-  OperandTypes.STRING_STRING_OPTIONAL_STRING);
+  OperandTypes.STRING_STRING_OPTIONAL_STRING,
+  SqlFunctionCategory.STRING);
 
   /** The "BOOL_AND(condition)" aggregate function, PostgreSQL and Redshift's
* equivalent to {@link SqlStdOperatorTable#EVERY}. */
@@ -1098,7 +1102,8 @@ public abstract class SqlLibraryOperators {
   public static final SqlFunction ARRAY =
   SqlBasicFunction.create("ARRAY",
   SqlLibraryOperators::arrayReturnType,
-  OperandTypes.SAME_VARIADIC);
+  OperandTypes.SAME_VARIADIC,
+  SqlFunctionCategory.SYSTEM);
 
   private static RelDataType mapReturnType(SqlOperatorBinding opBinding) {
 Pair<@Nullable RelDataType, @Nullable RelDataType> type =



(calcite) branch main updated: [CALCITE-5825] Add URL_ENCODE and URL_DECODE function (enabled in Spark library)

2023-10-27 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new ad2e843c5d [CALCITE-5825] Add URL_ENCODE and URL_DECODE function 
(enabled in Spark library)
ad2e843c5d is described below

commit ad2e843c5d9b3bec001d22e680ebe6b5de4e2078
Author: Runkang He 
AuthorDate: Sat Jul 15 07:39:17 2023 +0800

[CALCITE-5825] Add URL_ENCODE and URL_DECODE function (enabled in Spark 
library)
---
 .../calcite/adapter/enumerable/RexImpTable.java|  6 +++
 .../org/apache/calcite/runtime/UrlFunctions.java   | 59 ++
 .../calcite/sql/fun/SqlLibraryOperators.java   | 16 ++
 .../org/apache/calcite/util/BuiltInMethod.java |  3 ++
 site/_docs/reference.md|  2 +
 .../org/apache/calcite/test/SqlOperatorTest.java   | 54 
 6 files changed, 140 insertions(+)

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 b3a5565eb5..3263cda837 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
@@ -276,6 +276,8 @@ import static 
org.apache.calcite.sql.fun.SqlLibraryOperators.UNIX_DATE;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.UNIX_MICROS;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.UNIX_MILLIS;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.UNIX_SECONDS;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.URL_DECODE;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.URL_ENCODE;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.XML_TRANSFORM;
 import static org.apache.calcite.sql.fun.SqlStdOperatorTable.ABS;
 import static org.apache.calcite.sql.fun.SqlStdOperatorTable.ACOS;
@@ -903,6 +905,10 @@ public class RexImpTable {
   // Compression Operators
   defineMethod(COMPRESS, BuiltInMethod.COMPRESS.method, NullPolicy.ARG0);
 
+  // Url Operators
+  defineMethod(URL_ENCODE, BuiltInMethod.URL_ENCODE.method, 
NullPolicy.ARG0);
+  defineMethod(URL_DECODE, BuiltInMethod.URL_DECODE.method, 
NullPolicy.ARG0);
+
   // Xml Operators
   defineMethod(EXTRACT_VALUE, BuiltInMethod.EXTRACT_VALUE.method, 
NullPolicy.ARG0);
   defineMethod(XML_TRANSFORM, BuiltInMethod.XML_TRANSFORM.method, 
NullPolicy.ARG0);
diff --git a/core/src/main/java/org/apache/calcite/runtime/UrlFunctions.java 
b/core/src/main/java/org/apache/calcite/runtime/UrlFunctions.java
new file mode 100644
index 00..1cfb12666e
--- /dev/null
+++ b/core/src/main/java/org/apache/calcite/runtime/UrlFunctions.java
@@ -0,0 +1,59 @@
+/*
+ * 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.calcite.runtime;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+
+import static org.apache.calcite.util.Static.RESOURCE;
+
+/**
+ * A collection of functions used in Url processing.
+ */
+public class UrlFunctions {
+
+  private UrlFunctions() {
+  }
+
+  private static final Charset UTF_8 = StandardCharsets.UTF_8;
+
+  /** The "URL_DECODE(string)" function for Hive and Spark,
+   * which returns original value when decoded error. */
+  public static String urlDecode(String value) {
+try {
+  return URLDecoder.decode(value, UTF_8.name());
+} catch (UnsupportedEncodingException e) {
+  throw RESOURCE.charsetEncoding(value, UTF_8.name()).ex();
+} catch (RuntimeException e) {
+  return value;
+}
+  }
+
+  /** The "URL_ENCODE(string)" function for Hive and Spark. */
+  public static String urlEncode(String url) {
+String value;
+try {
+  value = URLEncoder.encode(url, UTF_8.name());
+} catch (UnsupportedEncodingException e) {
+  throw RESOURCE.charsetEncoding(url, UTF_8.name()).ex();
+}
+return value;
+  }

[calcite] branch main updated: [MINOR] Add example for MAP type in reference docs

2023-10-23 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new e8b64802c6 [MINOR] Add example for MAP type in reference docs
e8b64802c6 is described below

commit e8b64802c61920976527869e6fa28e1dc7a21aa4
Author: Ran Tao 
AuthorDate: Thu Sep 28 00:31:00 2023 +0800

[MINOR] Add example for MAP type in reference docs
---
 site/_docs/reference.md | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/site/_docs/reference.md b/site/_docs/reference.md
index b2a2483f88..5135d36fe6 100644
--- a/site/_docs/reference.md
+++ b/site/_docs/reference.md
@@ -1199,8 +1199,8 @@ Note:
 |: |:---|:---
 | ANY  | The union of all types |
 | UNKNOWN  | A value of an unknown type; used as a placeholder |
-| ROW  | Row with 1 or more columns | Example: Row(f0 int null, f1 varchar)
-| MAP  | Collection of keys mapped to values |
+| ROW  | Row with 1 or more columns | Example: row(f0 int null, f1 varchar)
+| MAP  | Collection of keys mapped to values | Example: (int, varchar) map
 | MULTISET | Unordered collection that may contain duplicates | Example: int 
multiset
 | ARRAY| Ordered, contiguous collection that may contain duplicates | 
Example: varchar(10) array
 | CURSOR   | Cursor over the result of executing a query |



[calcite] branch main updated: [CALCITE-6022] Support "CREATE TABLE ... LIKE" DDL in server module

2023-10-18 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 23b7931c3e [CALCITE-6022] Support "CREATE TABLE ... LIKE" DDL in 
server module
23b7931c3e is described below

commit 23b7931c3e516bdb6cfedda956213f7fe06c6b24
Author: macroguo 
AuthorDate: Tue Sep 26 14:07:23 2023 +0800

[CALCITE-6022] Support "CREATE TABLE ... LIKE" DDL in server module
---
 .../main/java/org/apache/calcite/sql/SqlKind.java  |   6 +-
 .../apache/calcite/sql/ddl/SqlCreateTableLike.java | 121 
 .../org/apache/calcite/sql/ddl/SqlDdlNodes.java|   8 ++
 server/src/main/codegen/config.fmpp|   1 +
 server/src/main/codegen/includes/parserImpls.ftl   |  67 -
 .../apache/calcite/server/ServerDdlExecutor.java   |  99 +
 .../org/apache/calcite/test/ServerParserTest.java  |  30 
 .../java/org/apache/calcite/test/ServerTest.java   | 160 +
 server/src/test/resources/sql/table.iq | 137 ++
 site/_docs/reference.md|   8 ++
 10 files changed, 632 insertions(+), 5 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/sql/SqlKind.java 
b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
index b2b5556c5e..7688cae915 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlKind.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
@@ -1161,6 +1161,9 @@ public enum SqlKind {
   /** {@code CREATE TABLE} DDL statement. */
   CREATE_TABLE,
 
+  /** {@code CREATE TABLE LIKE} DDL statement. */
+  CREATE_TABLE_LIKE,
+
   /** {@code ALTER TABLE} DDL statement. */
   ALTER_TABLE,
 
@@ -1281,7 +1284,8 @@ public enum SqlKind {
   public static final EnumSet DDL =
   EnumSet.of(COMMIT, ROLLBACK, ALTER_SESSION,
   CREATE_SCHEMA, CREATE_FOREIGN_SCHEMA, DROP_SCHEMA,
-  CREATE_TABLE, ALTER_TABLE, DROP_TABLE, TRUNCATE_TABLE,
+  CREATE_TABLE, CREATE_TABLE_LIKE,
+  ALTER_TABLE, DROP_TABLE, TRUNCATE_TABLE,
   CREATE_FUNCTION, DROP_FUNCTION,
   CREATE_VIEW, ALTER_VIEW, DROP_VIEW,
   CREATE_MATERIALIZED_VIEW, ALTER_MATERIALIZED_VIEW,
diff --git 
a/core/src/main/java/org/apache/calcite/sql/ddl/SqlCreateTableLike.java 
b/core/src/main/java/org/apache/calcite/sql/ddl/SqlCreateTableLike.java
new file mode 100644
index 00..c291de2fe3
--- /dev/null
+++ b/core/src/main/java/org/apache/calcite/sql/ddl/SqlCreateTableLike.java
@@ -0,0 +1,121 @@
+/*
+ * 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.calcite.sql.ddl;
+
+import org.apache.calcite.sql.SqlCreate;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlLiteral;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlSpecialOperator;
+import org.apache.calcite.sql.SqlWriter;
+import org.apache.calcite.sql.Symbolizable;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.util.ImmutableNullableList;
+
+import com.google.common.base.Preconditions;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Parse tree for {@code CREATE TABLE LIKE} statement.
+ */
+public class SqlCreateTableLike extends SqlCreate {
+  private static final SqlOperator OPERATOR =
+  new SqlSpecialOperator("CREATE TABLE LIKE", SqlKind.CREATE_TABLE_LIKE);
+
+  /**
+   * The LikeOption specify which additional properties of the original table 
to copy.
+   */
+  public enum LikeOption implements Symbolizable {
+ALL,
+DEFAULTS,
+GENERATED
+  }
+
+  public final SqlIdentifier name;
+  public final SqlIdentifier sourceTable;
+  public final SqlNodeList includingOptions;
+  public final SqlNodeList excludingOptions;
+
+
+  public SqlCreateTableLike(SqlParserPos pos, boolean replace, boolean 
ifNotExists,
+  SqlIdentifier name, SqlIdentifie

[calcite] branch main updated: Following [CALCITE-5570] Support nested map type for SqlDataTypeSpec

2023-10-15 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 5d614ed9c8 Following [CALCITE-5570] Support nested map type for 
SqlDataTypeSpec
5d614ed9c8 is described below

commit 5d614ed9c872df08e165445cd4a3ab1e24836ab8
Author: xiejiajun 
AuthorDate: Sat Oct 14 16:57:24 2023 +0800

Following [CALCITE-5570] Support nested map type for SqlDataTypeSpec
---
 .../java/org/apache/calcite/sql/type/SqlTypeUtil.java | 11 ++-
 .../calcite/rel/rel2sql/RelToSqlConverterTest.java| 19 +++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java 
b/core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java
index 6a82619dd3..ff1becd2bb 100644
--- a/core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java
+++ b/core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java
@@ -30,6 +30,7 @@ import org.apache.calcite.sql.SqlCollation;
 import org.apache.calcite.sql.SqlCollectionTypeNameSpec;
 import org.apache.calcite.sql.SqlDataTypeSpec;
 import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlMapTypeNameSpec;
 import org.apache.calcite.sql.SqlNode;
 import org.apache.calcite.sql.SqlRowTypeNameSpec;
 import org.apache.calcite.sql.SqlTypeNameSpec;
@@ -1146,6 +1147,14 @@ public abstract class SqlTypeUtil {
   .map(f -> convertTypeToSpec(f.getType()))
   .collect(Collectors.toList());
   typeNameSpec = new SqlRowTypeNameSpec(SqlParserPos.ZERO, fieldNames, 
fieldTypes);
+} else if (isMap(type)) {
+  final RelDataType keyType =
+  requireNonNull(type.getKeyType(), () -> "keyType of " + type);
+  final RelDataType valueType =
+  requireNonNull(type.getValueType(), () -> "valueType of " + type);
+  final SqlDataTypeSpec keyTypeSpec = convertTypeToSpec(keyType);
+  final SqlDataTypeSpec valueTypeSpec = convertTypeToSpec(valueType);
+  typeNameSpec = new SqlMapTypeNameSpec(keyTypeSpec, valueTypeSpec, 
SqlParserPos.ZERO);
 } else {
   throw new UnsupportedOperationException(
   "Unsupported type when convertTypeToSpec: " + typeName);
@@ -1212,7 +1221,7 @@ public abstract class SqlTypeUtil {
   public static RelDataType createRecordTypeFromMap(
   RelDataTypeFactory typeFactory, RelDataType type) {
 RelDataType keyType = requireNonNull(type.getKeyType(), () -> "keyType of 
" + type);
-RelDataType valueType = requireNonNull(type.getValueType(), () -> "keyType 
of " + type);
+RelDataType valueType = requireNonNull(type.getValueType(), () -> 
"valueType of " + type);
 return typeFactory.createStructType(
 Arrays.asList(keyType, valueType), Arrays.asList("f0", "f1"));
   }
diff --git 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index 0e97580cb6..de7b08a6ab 100644
--- 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++ 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -4016,6 +4016,25 @@ class RelToSqlConverterTest {
 sql(query).withSpark().ok(expected);
   }
 
+  /**
+   * Test case for
+   * https://issues.apache.org/jira/browse/CALCITE-5570;>[CALCITE-5570]
+   * Support nested map type for SqlDataTypeSpec.
+   */
+  @Test void testCastAsMapType() {
+sql("SELECT CAST(MAP['A', 1.0] AS MAP)")
+.ok("SELECT CAST(MAP['A', 1.0] AS MAP< VARCHAR CHARACTER SET 
\"ISO-8859-1\", DOUBLE >)\n"
++ "FROM (VALUES (0)) AS \"t\" (\"ZERO\")");
+sql("SELECT CAST(MAP['A', ARRAY[1, 2, 3]] AS MAP)")
+.ok("SELECT CAST(MAP['A', ARRAY[1, 2, 3]] AS "
++ "MAP< VARCHAR CHARACTER SET \"ISO-8859-1\", INTEGER ARRAY >)\n"
++ "FROM (VALUES (0)) AS \"t\" (\"ZERO\")");
+sql("SELECT CAST(MAP[ARRAY['A'], MAP[1, 2]] AS MAP>)")
+.ok("SELECT CAST(MAP[ARRAY['A'], MAP[1, 2]] AS "
++ "MAP< VARCHAR CHARACTER SET \"ISO-8859-1\" ARRAY, MAP< INTEGER, 
INTEGER > >)\n"
++ "FROM (VALUES (0)) AS \"t\" (\"ZERO\")");
+  }
+
   /** Test case for
* https://issues.apache.org/jira/browse/CALCITE-4674;>[CALCITE-4674]
* Excess quotes in generated SQL when STAR is a column alias. */



[calcite] branch main updated: [CALCITE-6006] RelToSqlConverter loses charset information

2023-10-12 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 485a5d0ae3 [CALCITE-6006] RelToSqlConverter loses charset information
485a5d0ae3 is described below

commit 485a5d0ae3274a83a36788290b3bdc4d973387b4
Author: Mihai Budiu 
AuthorDate: Thu Oct 12 13:25:57 2023 -0700

[CALCITE-6006] RelToSqlConverter loses charset information

Signed-off-by: Mihai Budiu 
---
 .../org/apache/calcite/rel/rel2sql/SqlImplementor.java   | 16 +++-
 .../calcite/rel/rel2sql/RelToSqlConverterTest.java   | 12 
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git 
a/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java 
b/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java
index 16455ff2f6..573c497885 100644
--- a/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java
+++ b/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java
@@ -16,6 +16,7 @@
  */
 package org.apache.calcite.rel.rel2sql;
 
+import org.apache.calcite.config.CalciteSystemProperty;
 import org.apache.calcite.linq4j.Ord;
 import org.apache.calcite.linq4j.tree.Expressions;
 import org.apache.calcite.plan.RelOptUtil;
@@ -90,6 +91,7 @@ import org.apache.calcite.sql.type.SqlTypeName;
 import org.apache.calcite.sql.validate.SqlValidatorUtil;
 import org.apache.calcite.util.DateString;
 import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.calcite.util.NlsString;
 import org.apache.calcite.util.Pair;
 import org.apache.calcite.util.RangeSets;
 import org.apache.calcite.util.Sarg;
@@ -1409,8 +1411,20 @@ public abstract class SqlImplementor {
 () -> "literal " + literal
 + " has null SqlTypeFamily, and is SqlTypeName is " + 
typeName);
 switch (family) {
-case CHARACTER:
+case CHARACTER: {
+  final NlsString value = literal.getValueAs(NlsString.class);
+  if (value != null) {
+final String defaultCharset = 
CalciteSystemProperty.DEFAULT_CHARSET.value();
+final String charsetName = value.getCharsetName();
+if (!defaultCharset.equals(charsetName)) {
+  // Set the charset only if it is not the same as the default charset
+  return SqlLiteral.createCharString(
+  castNonNull(value).getValue(), charsetName, POS);
+}
+  }
+  // Create a string without specifying a charset
   return SqlLiteral.createCharString((String) 
castNonNull(literal.getValue2()), POS);
+}
 case NUMERIC:
 case EXACT_NUMERIC:
   return SqlLiteral.createExactNumeric(
diff --git 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index 661f7163cb..0e97580cb6 100644
--- 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++ 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -83,6 +83,7 @@ import org.apache.calcite.tools.Programs;
 import org.apache.calcite.tools.RelBuilder;
 import org.apache.calcite.tools.RuleSet;
 import org.apache.calcite.tools.RuleSets;
+import org.apache.calcite.util.ConversionUtil;
 import org.apache.calcite.util.ImmutableBitSet;
 import org.apache.calcite.util.TestUtil;
 import org.apache.calcite.util.Util;
@@ -282,6 +283,17 @@ class RelToSqlConverterTest {
 sql(query).ok(expected);
   }
 
+  /** Test case for https://issues.apache.org/jira/browse/CALCITE-6006;>[CALCITE-6006]
+   * RelToSqlConverter loses charset information. */
+  @Test void testCharset() {
+sql("select _UTF8'\u4F60\u597D'")
+.withMysql() // produces a simpler output query
+.ok("SELECT _UTF-8'\u4F60\u597D'");
+sql("select _UTF16'" + ConversionUtil.TEST_UNICODE_STRING + "'")
+.withMysql()
+.ok("SELECT _UTF-16LE'" + ConversionUtil.TEST_UNICODE_STRING + "'");
+  }
+
   /** Test case for
* https://issues.apache.org/jira/browse/CALCITE-4321;>[CALCITE-4321]
* JDBC adapter omits FILTER (WHERE ...) expressions when generating SQL



[calcite] branch main updated: [CALCITE-6009] Add optimization to remove redundant LIMIT that is more than input row count

2023-10-03 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 87cdfd97f0 [CALCITE-6009] Add optimization to remove redundant LIMIT 
that is more than input row count
87cdfd97f0 is described below

commit 87cdfd97f0948443f7199c9f5b694feb8808caeb
Author: shenlang 
AuthorDate: Fri Sep 29 20:41:34 2023 +0800

[CALCITE-6009] Add optimization to remove redundant LIMIT that is more than 
input row count
---
 .../org/apache/calcite/rel/rules/CoreRules.java|  5 +-
 .../calcite/rel/rules/SortRemoveRedundantRule.java | 66 ++
 .../org/apache/calcite/test/RelOptRulesTest.java   | 36 
 .../org/apache/calcite/test/RelOptRulesTest.xml| 55 ++
 4 files changed, 150 insertions(+), 12 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java 
b/core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java
index 5c5cc04c71..c02b1ab12f 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java
@@ -727,8 +727,9 @@ public class CoreRules {
   public static final SortRemoveConstantKeysRule SORT_REMOVE_CONSTANT_KEYS =
   SortRemoveConstantKeysRule.Config.DEFAULT.toRule();
 
-  /** Rule that removes redundant {@link Sort} if its input max row number
-   * is less than or equal to one. */
+  /** Rule that removes redundant {@code Order By} or {@code Limit} when its 
input RelNode's
+   * max row count is less than or equal to specified row count.All of them
+   * are represented by {@link Sort}*/
   public static final SortRemoveRedundantRule SORT_REMOVE_REDUNDANT =
   SortRemoveRedundantRule.Config.DEFAULT.toRule();
 
diff --git 
a/core/src/main/java/org/apache/calcite/rel/rules/SortRemoveRedundantRule.java 
b/core/src/main/java/org/apache/calcite/rel/rules/SortRemoveRedundantRule.java
index 68a449b43d..8dd2e722e2 100644
--- 
a/core/src/main/java/org/apache/calcite/rel/rules/SortRemoveRedundantRule.java
+++ 
b/core/src/main/java/org/apache/calcite/rel/rules/SortRemoveRedundantRule.java
@@ -17,15 +17,21 @@
 package org.apache.calcite.rel.rules;
 
 import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelOptUtil;
 import org.apache.calcite.plan.RelRule;
 import org.apache.calcite.rel.core.Sort;
+import org.apache.calcite.rex.RexLiteral;
 
 import org.immutables.value.Value;
 
-/**
- * Planner rule that removes
- * the redundant {@link org.apache.calcite.rel.core.Sort} if its input
- * max row number is less than or equal to one.
+import java.util.Optional;
+
+/** Rule that removes redundant {@code Order By} or {@code Limit} when its 
input RelNode's
+ * max row count is less than or equal to specified row count.All of them
+ * are represented by {@link Sort}
+ *
+ *  If a {@code Sort} is pure order by,and its offset is null,when its 
input RelNode's
+ * max row count is less than or equal to 1,then we could remove the redundant 
sort.
  *
  *  For example:
  * {@code
@@ -37,6 +43,23 @@ import org.immutables.value.Value;
  *  select max(totalprice) from orders}
  *  
  *
+ *  If a {@code Sort} is pure limit,and its offset is null, when its input
+ * RelNode's max row count is less than or equal to the limit's fetch,then we 
could
+ * remove the redundant sort.
+ *
+ *  For example:
+ * {@code
+ * SELECT * FROM (VALUES 1,2,3,4,5,6) AS t1 LIMIT 10}
+ * 
+ *
+ *  The above values max row count is 6 rows, and the limit's fetch is 10,
+ * so we could remove the redundant sort.
+ *
+ *  It could be converted to:
+ * {@code
+ * SELECT * FROM (VALUES 1,2,3,4,5,6) AS t1}
+ * 
+ *
  * @see CoreRules#SORT_REMOVE_REDUNDANT
  */
 @Value.Enclosing
@@ -49,20 +72,43 @@ public class SortRemoveRedundantRule
 
   @Override public void onMatch(final RelOptRuleCall call) {
 final Sort sort = call.rel(0);
-if (sort.offset != null || sort.fetch != null) {
-  // Don't remove sort if it has explicit OFFSET and LIMIT
+if (RelOptUtil.isOffset(sort)) {
+  // Don't remove sort if it has explicit OFFSET
   return;
 }
 
 // Get the max row count for sort's input RelNode.
-final Double maxRowCount = 
call.getMetadataQuery().getMaxRowCount(sort.getInput());
-// If the max row count is not null and less than or equal to 1,
-// then we could remove the sort.
-if (maxRowCount != null && maxRowCount <= 1D) {
+final Double inputMaxRowCount = 
call.getMetadataQuery().getMaxRowCount(sort.getInput());
+
+// Get the target max row count with sort's semantics.
+// If sort is pure order by, the target max row count is 1.
+// If sort is pure limit, the target max row count is the limit's fetch.
+final Optional targetMaxRowCount = 
getSortInputSpecificMaxRowCount(sort);
+
+if (!targetMaxRowCoun

[calcite] branch main updated: [CALCITE-5570] Support nested map type for SqlDataTypeSpec

2023-10-01 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 6f79436c17 [CALCITE-5570] Support nested map type for SqlDataTypeSpec
6f79436c17 is described below

commit 6f79436c178beec639e559d9152c237bbf8ec3e8
Author: xiejiajun 
AuthorDate: Sun Mar 12 14:12:18 2023 +0800

[CALCITE-5570] Support nested map type for SqlDataTypeSpec
---
 core/src/main/codegen/templates/Parser.jj  | 21 +
 .../org/apache/calcite/sql/SqlMapTypeNameSpec.java | 94 ++
 .../org/apache/calcite/test/SqlValidatorTest.java  | 20 +
 core/src/test/resources/sql/misc.iq| 19 +
 .../apache/calcite/sql/parser/SqlParserTest.java   | 14 
 .../org/apache/calcite/test/catalog/Fixture.java   |  6 ++
 .../test/catalog/MockCatalogReaderExtended.java|  5 ++
 7 files changed, 179 insertions(+)

diff --git a/core/src/main/codegen/templates/Parser.jj 
b/core/src/main/codegen/templates/Parser.jj
index 269e11d2ba..f03f36409f 100644
--- a/core/src/main/codegen/templates/Parser.jj
+++ b/core/src/main/codegen/templates/Parser.jj
@@ -76,6 +76,7 @@ import org.apache.calcite.sql.SqlKind;
 import org.apache.calcite.sql.SqlLiteral;
 import org.apache.calcite.sql.SqlMatchRecognize;
 import org.apache.calcite.sql.SqlMerge;
+import org.apache.calcite.sql.SqlMapTypeNameSpec;
 import org.apache.calcite.sql.SqlNode;
 import org.apache.calcite.sql.SqlNodeList;
 import org.apache.calcite.sql.SqlNumericLiteral;
@@ -5689,6 +5690,9 @@ SqlTypeNameSpec TypeName() :
 typeNameSpec = SqlTypeName(s)
 |
 typeNameSpec = RowTypeName()
+|
+LOOKAHEAD(2)
+typeNameSpec = MapTypeName()
 |
 typeName = CompoundIdentifier() {
 typeNameSpec = new SqlUserDefinedTypeNameSpec(typeName, 
s.end(this));
@@ -5962,6 +5966,23 @@ SqlTypeNameSpec RowTypeName() :
 }
 }
 
+SqlTypeNameSpec MapTypeName() :
+{
+SqlDataTypeSpec keyType = null;
+SqlDataTypeSpec valType = null;
+}
+{
+
+
+keyType = DataType()
+
+valType = DataType()
+
+{
+return new SqlMapTypeNameSpec(keyType, valType, getPos());
+}
+}
+
 /**
 * Parse character types: char, varchar.
 */
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlMapTypeNameSpec.java 
b/core/src/main/java/org/apache/calcite/sql/SqlMapTypeNameSpec.java
new file mode 100644
index 00..37520fc834
--- /dev/null
+++ b/core/src/main/java/org/apache/calcite/sql/SqlMapTypeNameSpec.java
@@ -0,0 +1,94 @@
+/*
+ * 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.calcite.sql;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.validate.SqlValidator;
+import org.apache.calcite.util.Litmus;
+
+/**
+ * Parse SQL MAP type, i.e. MAPINT NOT NULL, TIMESTAMP NULL, the key 
and value can specify a
+ * suffix to indicate if the type is nullable, default is not null.
+ *
+ * MAP type does not belong to standard SQL.
+ */
+public class SqlMapTypeNameSpec extends SqlTypeNameSpec {
+
+  private final SqlDataTypeSpec keyType;
+  private final SqlDataTypeSpec valType;
+
+  /**
+   * Creates a {@code SqlMapTypeNameSpec}.
+   *
+   * @param keyType key type
+   * @param valType value type
+   * @param pos the parser position
+   */
+  public SqlMapTypeNameSpec(SqlDataTypeSpec keyType, SqlDataTypeSpec valType, 
SqlParserPos pos) {
+super(new SqlIdentifier(SqlTypeName.MAP.getName(), pos), pos);
+this.keyType = keyType;
+this.valType = valType;
+  }
+
+  public SqlDataTypeSpec getKeyType() {
+return keyType;
+  }
+
+  public SqlDataTypeSpec getValType() {
+return valType;
+  }
+
+  @Override public RelDataType deriveType(SqlValidator validator) {
+return validator
+.getTypeFactory()
+.createMapType(keyType.deriveType(validator), 
valType.deriveType(validator));
+  }
+
+  @Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) 
{
+   

[calcite] branch main updated: [CALCITE-5962] Support parse Spark-style syntax LEFT ANTI JOIN in Babel parser

2023-09-28 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new be2146679e [CALCITE-5962] Support parse Spark-style syntax LEFT ANTI 
JOIN in Babel parser
be2146679e is described below

commit be2146679e3be0d4ce1a7578b93237c30b1a1709
Author: jiefei 
AuthorDate: Mon Aug 28 20:44:12 2023 +0800

[CALCITE-5962] Support parse Spark-style syntax LEFT ANTI JOIN in Babel 
parser
---
 babel/src/main/codegen/config.fmpp |  3 ++
 babel/src/main/codegen/includes/parserImpls.ftl|  7 +
 .../org/apache/calcite/test/BabelParserTest.java   | 34 ++
 .../java/org/apache/calcite/test/BabelTest.java| 15 ++
 babel/src/test/resources/sql/select.iq | 22 ++
 core/src/main/codegen/templates/Parser.jj  |  2 +-
 .../main/java/org/apache/calcite/sql/JoinType.java |  7 +
 .../main/java/org/apache/calcite/sql/SqlJoin.java  |  4 +++
 .../apache/calcite/sql/validate/JoinNamespace.java |  3 +-
 .../org/apache/calcite/sql/validate/JoinScope.java |  6 ++--
 .../calcite/sql/validate/SqlValidatorImpl.java |  3 +-
 11 files changed, 101 insertions(+), 5 deletions(-)

diff --git a/babel/src/main/codegen/config.fmpp 
b/babel/src/main/codegen/config.fmpp
index dc7b8fb695..4e1f9c36d4 100644
--- a/babel/src/main/codegen/config.fmpp
+++ b/babel/src/main/codegen/config.fmpp
@@ -42,6 +42,7 @@ data: {
 # List of new keywords. Example: "DATABASES", "TABLES". If the keyword is
 # not a reserved keyword, add it to the 'nonReservedKeywords' section.
 keywords: [
+  "ANTI"
   "DISCARD"
   "IF"
   "PLANS"
@@ -56,6 +57,7 @@ data: {
 # items in this list become non-reserved
 nonReservedKeywordsToAdd: [
   # not in core, added in babel
+  "ANTI"
   "DISCARD"
   "IF"
   "PLANS"
@@ -545,6 +547,7 @@ data: {
 # List of additional join types. Each is a method with no arguments.
 # Example: "LeftSemiJoin".
 joinTypes: [
+  "LeftAntiJoin",
   "LeftSemiJoin"
 ]
 
diff --git a/babel/src/main/codegen/includes/parserImpls.ftl 
b/babel/src/main/codegen/includes/parserImpls.ftl
index fb169fe1e8..b481a29f0f 100644
--- a/babel/src/main/codegen/includes/parserImpls.ftl
+++ b/babel/src/main/codegen/includes/parserImpls.ftl
@@ -22,6 +22,13 @@ JoinType LeftSemiJoin() :
{ return JoinType.LEFT_SEMI_JOIN; }
 }
 
+JoinType LeftAntiJoin() :
+{
+}
+{
+   { return JoinType.LEFT_ANTI_JOIN; }
+}
+
 SqlNode DateaddFunctionCall() :
 {
 final Span s;
diff --git a/babel/src/test/java/org/apache/calcite/test/BabelParserTest.java 
b/babel/src/test/java/org/apache/calcite/test/BabelParserTest.java
index 69c6465127..14df0a38a6 100644
--- a/babel/src/test/java/org/apache/calcite/test/BabelParserTest.java
+++ b/babel/src/test/java/org/apache/calcite/test/BabelParserTest.java
@@ -18,6 +18,7 @@ package org.apache.calcite.test;
 import org.apache.calcite.sql.SqlDialect;
 import org.apache.calcite.sql.dialect.MysqlSqlDialect;
 import org.apache.calcite.sql.dialect.PostgresqlSqlDialect;
+import org.apache.calcite.sql.dialect.SparkSqlDialect;
 import org.apache.calcite.sql.parser.SqlAbstractParserImpl;
 import org.apache.calcite.sql.parser.SqlParser;
 import org.apache.calcite.sql.parser.SqlParserFixture;
@@ -430,6 +431,39 @@ class BabelParserTest extends SqlParserTest {
 f.sql("DISCARD TEMP").same();
   }
 
+  @Test void testSparkLeftAntiJoin() {
+final SqlParserFixture f = fixture().withDialect(SparkSqlDialect.DEFAULT);
+final String sql = "select a.cid, a.cname, count(1) as amount\n"
++ "from geo.area1 as a\n"
++ "left anti join (select distinct cid, cname\n"
++ "from geo.area2\n"
++ "where cname = 'cityA') as b on a.cid = b.cid\n"
++ "group by a.cid, a.cname";
+final String expected = "SELECT A.CID, A.CNAME, COUNT(1) AMOUNT\n"
++ "FROM GEO.AREA1 A\n"
++ "LEFT ANTI JOIN (SELECT DISTINCT CID, CNAME\n"
++ "FROM GEO.AREA2\n"
++ "WHERE (CNAME = 'cityA')) B ON (A.CID = B.CID)\n"
++ "GROUP BY A.CID, A.CNAME";
+f.sql(sql).ok(expected);
+  }
+
+  @Test void testLeftAntiJoin() {
+final String sql = "select a.cid, a.cname, count(1) as amount\n"
++ "from geo.area1 as a\n"
++ "left anti join (select distinct cid, cname\n"
++ "from geo.area2\n"
++ "where cname = 'cityA') as b on a.cid = b.cid\n"
++ "group by a.cid, a.cname";
+final String expected = "SELECT `A`.`CID

[calcite] branch main updated: [CALCITE-5940] Add the Rule to merge Limit

2023-09-27 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new aef9bdb5c0 [CALCITE-5940] Add the Rule to merge Limit
aef9bdb5c0 is described below

commit aef9bdb5c091e4df84eeb47a9aa70770dd3d3fb8
Author: shenlang 
AuthorDate: Sun Aug 27 20:42:06 2023 +0800

[CALCITE-5940] Add the Rule to merge Limit
---
 .../java/org/apache/calcite/plan/RelOptUtil.java   |   7 +
 .../org/apache/calcite/rel/rules/CoreRules.java|   5 +
 .../apache/calcite/rel/rules/SortMergeRule.java| 141 +
 .../org/apache/calcite/test/RelOptRulesTest.java   |  91 +++
 .../org/apache/calcite/test/RelOptRulesTest.xml| 170 +
 5 files changed, 414 insertions(+)

diff --git a/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java 
b/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
index 6ec10cf951..90dedd5cfa 100644
--- a/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
+++ b/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
@@ -194,6 +194,13 @@ public abstract class RelOptUtil {
 return (rel instanceof Sort) && !((Sort) 
rel).getCollation().getFieldCollations().isEmpty();
   }
 
+  /**
+   * Whether this node contains a offset specification.
+   */
+  public static boolean isOffset(RelNode rel) {
+return (rel instanceof Sort) && ((Sort) rel).offset != null;
+  }
+
   /**
* Returns a set of tables used by this expression or its children.
*/
diff --git a/core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java 
b/core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java
index 43b1eb830a..5c5cc04c71 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java
@@ -716,6 +716,11 @@ public class CoreRules {
   public static final SortRemoveRule SORT_REMOVE =
   SortRemoveRule.Config.DEFAULT.toRule();
 
+  /** Rule that merge a {@link Sort} representing the Limit semantics and
+   * another {@link Sort} representing the Limit or TOPN semantics. */
+  public static final SortMergeRule LIMIT_MREGE =
+  SortMergeRule.Config.LIMIT_MERGE.toRule();
+
   /** Rule that removes keys from a {@link Sort}
* if those keys are known to be constant, or removes the entire Sort if all
* keys are constant. */
diff --git a/core/src/main/java/org/apache/calcite/rel/rules/SortMergeRule.java 
b/core/src/main/java/org/apache/calcite/rel/rules/SortMergeRule.java
new file mode 100644
index 00..b4a484e333
--- /dev/null
+++ b/core/src/main/java/org/apache/calcite/rel/rules/SortMergeRule.java
@@ -0,0 +1,141 @@
+/*
+ * 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.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelOptUtil;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.core.Sort;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.tools.RelBuilder;
+
+import com.google.common.collect.ImmutableList;
+
+import org.immutables.value.Value;
+
+/**
+ * This rule try to merge the double {@link Sort},one is Limit semantics,
+ * another sort is Limit or TOPN semantics.
+ *
+ *  It generally used with the {@link SortProjectTransposeRule} rule.
+ *
+ *  For example:
+ * {@code
+ * select
+ *   concat('-', N_REGIONKEY) from
+ *   (
+ * select
+ *   *
+ * from nation limit 1) limit 10}
+ *  
+ *
+ *  will convert to
+ * {@code
+ * select
+ *   concat('-', N_REGIONKEY)
+ * from
+ *   nation limit 10
+ * }
+ *
+ *  The sql :
+ * {@code
+ * select concat('-',N_REGIONKEY) from
+ * (SELECT * FROM nation order BY N_REGIONKEY DESC LIMIT 1) limit 10
+ * }
+ *
+ *  will convert to
+ * {@code
+ * SELECT concat('-',N_REGIONKEY) FROM nation order BY N_REGIONKEY DESC LIMIT 
10
+ * }
+ *
+ *  In the future,we could also extend other sort merge logic in this rule.
+ *
+ * @see CoreRules#LIMIT_MREGE
+ */
+@Value.Enclosing
+public clas

[calcite] branch main updated: [CALCITE-5997] OFFSET operator is incorrectly unparsed

2023-09-19 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 9ce7077bc8 [CALCITE-5997] OFFSET operator is incorrectly unparsed
9ce7077bc8 is described below

commit 9ce7077bc8353e21e71ddb9f4a6a3025422ea8c4
Author: Mihai Budiu 
AuthorDate: Mon Sep 18 15:09:32 2023 -0700

[CALCITE-5997] OFFSET operator is incorrectly unparsed

Signed-off-by: Mihai Budiu 
---
 .../apache/calcite/sql/fun/SqlItemOperator.java|  8 +-
 .../apache/calcite/sql/parser/SqlParserTest.java   | 31 ++
 2 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/calcite/sql/fun/SqlItemOperator.java 
b/core/src/main/java/org/apache/calcite/sql/fun/SqlItemOperator.java
index 627115f5a3..4f5371c9fc 100644
--- a/core/src/main/java/org/apache/calcite/sql/fun/SqlItemOperator.java
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlItemOperator.java
@@ -78,7 +78,13 @@ public class SqlItemOperator extends SqlSpecialOperator {
   SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
 call.operand(0).unparse(writer, leftPrec, 0);
 final SqlWriter.Frame frame = writer.startList("[", "]");
-call.operand(1).unparse(writer, 0, 0);
+if (!this.getName().equals("ITEM")) {
+  final SqlWriter.Frame offsetFrame = writer.startFunCall(this.getName());
+  call.operand(1).unparse(writer, 0, 0);
+  writer.endFunCall(offsetFrame);
+} else {
+  call.operand(1).unparse(writer, 0, 0);
+}
 writer.endList(frame);
   }
 
diff --git 
a/testkit/src/main/java/org/apache/calcite/sql/parser/SqlParserTest.java 
b/testkit/src/main/java/org/apache/calcite/sql/parser/SqlParserTest.java
index 60606f58ac..edda041414 100644
--- a/testkit/src/main/java/org/apache/calcite/sql/parser/SqlParserTest.java
+++ b/testkit/src/main/java/org/apache/calcite/sql/parser/SqlParserTest.java
@@ -725,6 +725,37 @@ public class SqlParserTest {
 + ".*");
   }
 
+  /** Test case for https://issues.apache.org/jira/browse/CALCITE-5997;>[CALCITE-5997]
+   * OFFSET operator is incorrectly unparsed. */
+  @Test void testOffset() {
+sql("SELECT ARRAY[2,4,6][2]")
+.ok("SELECT (ARRAY[2, 4, 6])[2]");
+sql("SELECT ARRAY[2,4,6][ORDINAL(2)]")
+.withDialect(BIG_QUERY)
+.ok("SELECT (ARRAY[2, 4, 6])[ORDINAL(2)]");
+sql("SELECT ARRAY[2,4,6][OFFSET(2)]")
+.withDialect(BIG_QUERY)
+.ok("SELECT (ARRAY[2, 4, 6])[OFFSET(2)]");
+sql("SELECT ARRAY[2,4,6][SAFE_OFFSET(2)]")
+.withDialect(BIG_QUERY)
+.ok("SELECT (ARRAY[2, 4, 6])[SAFE_OFFSET(2)]");
+sql("SELECT ARRAY[2,4,6][SAFE_ORDINAL(2)]")
+.withDialect(BIG_QUERY)
+.ok("SELECT (ARRAY[2, 4, 6])[SAFE_ORDINAL(2)]");
+
+// All these tests work without BIG_QUERY as well.
+// The SQL parser accepts this syntax, so we need to be
+// able to unparse it into something.
+sql("SELECT ARRAY[2,4,6][ORDINAL(2)]")
+.ok("SELECT (ARRAY[2, 4, 6])[ORDINAL(2)]");
+sql("SELECT ARRAY[2,4,6][OFFSET(2)]")
+.ok("SELECT (ARRAY[2, 4, 6])[OFFSET(2)]");
+sql("SELECT ARRAY[2,4,6][SAFE_OFFSET(2)]")
+.ok("SELECT (ARRAY[2, 4, 6])[SAFE_OFFSET(2)]");
+sql("SELECT ARRAY[2,4,6][SAFE_ORDINAL(2)]")
+.ok("SELECT (ARRAY[2, 4, 6])[SAFE_ORDINAL(2)]");
+  }
+
   @Test void testInvalidToken() {
 // Causes problems to the test infrastructure because the token mgr
 // throws a java.lang.Error. The usual case is that the parser throws



[calcite] branch main updated: [CALCITE-5999] DECIMAL literals as sometimes unparsed looking as DOUBLE literals

2023-09-18 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 008c553aef [CALCITE-5999] DECIMAL literals as sometimes unparsed 
looking as DOUBLE literals
008c553aef is described below

commit 008c553aefacbe14787f50510c660a4dd739b54e
Author: Mihai Budiu 
AuthorDate: Sat Sep 16 10:15:14 2023 -0700

[CALCITE-5999] DECIMAL literals as sometimes unparsed looking as DOUBLE 
literals

Signed-off-by: Mihai Budiu 
---
 core/src/main/java/org/apache/calcite/sql/SqlNumericLiteral.java  | 4 ++--
 .../java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java| 2 +-
 .../src/main/java/org/apache/calcite/sql/parser/SqlParserTest.java| 4 
 3 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/sql/SqlNumericLiteral.java 
b/core/src/main/java/org/apache/calcite/sql/SqlNumericLiteral.java
index 8cf6bd1ff0..25205b99ab 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlNumericLiteral.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlNumericLiteral.java
@@ -88,9 +88,9 @@ public class SqlNumericLiteral extends SqlLiteral {
   }
 
   @Override public String toValue() {
-BigDecimal bd = getValueNonNull();
+final BigDecimal bd = getValueNonNull();
 if (isExact) {
-  return getValueNonNull().toString();
+  return bd.toPlainString();
 }
 return Util.toScientificNotation(bd);
   }
diff --git 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index 212336abf6..661f7163cb 100644
--- 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++ 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -1167,7 +1167,7 @@ class RelToSqlConverterTest {
   @Test void testCastDecimal1() {
 final String query = "select -0.000123\n"
 + " from \"expense_fact\"";
-final String expected = "SELECT -1.23E-8\n"
+final String expected = "SELECT -0.000123\n"
 + "FROM \"foodmart\".\"expense_fact\"";
 sql(query).ok(expected);
   }
diff --git 
a/testkit/src/main/java/org/apache/calcite/sql/parser/SqlParserTest.java 
b/testkit/src/main/java/org/apache/calcite/sql/parser/SqlParserTest.java
index bd3ad987ee..60606f58ac 100644
--- a/testkit/src/main/java/org/apache/calcite/sql/parser/SqlParserTest.java
+++ b/testkit/src/main/java/org/apache/calcite/sql/parser/SqlParserTest.java
@@ -937,6 +937,10 @@ public class SqlParserTest {
 .ok("SELECT 211");
 sql("select DECIMAL '.11E-2'")
 .ok("SELECT 0.0011");
+// Test case for [CALCITE-5999] DECIMAL literals as sometimes unparsed
+// looking as DOUBLE literals.
+sql("select DECIMAL '0.1'")
+.ok("SELECT 0.1");
 sql("select DECIMAL ^''^")
 .fails("(?s)Literal '' can not be parsed to type 'DECIMAL'.*");
 sql("select DECIMAL ^'-'^")



[calcite] branch main updated: [CALCITE-5836] Implement Rel2Sql for MERGE

2023-09-14 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new f3f5e7eeac [CALCITE-5836] Implement Rel2Sql for MERGE
f3f5e7eeac is described below

commit f3f5e7eeacea2216123f9e9c3094a0926096b923
Author: macroguo 
AuthorDate: Thu Aug 24 21:06:23 2023 +0800

[CALCITE-5836] Implement Rel2Sql for MERGE

Co-authored-by: Greg Hart 
---
 .../java/org/apache/calcite/plan/RelOptUtil.java   |   1 +
 .../apache/calcite/prepare/CalcitePrepareImpl.java |   2 +
 .../calcite/rel/rel2sql/RelToSqlConverter.java |  58 -
 .../calcite/rel/rel2sql/RelToSqlConverterTest.java | 130 +
 .../org/apache/calcite/test/JdbcAdapterTest.java   |  41 +++
 5 files changed, 231 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java 
b/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
index b9aa94d981..6ec10cf951 100644
--- a/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
+++ b/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
@@ -2138,6 +2138,7 @@ public abstract class RelOptUtil {
 case INSERT:
 case DELETE:
 case UPDATE:
+case MERGE:
   return typeFactory.createStructType(
   PairList.of(AvaticaConnection.ROWCOUNT_COLUMN_NAME,
   typeFactory.createSqlType(SqlTypeName.BIGINT)));
diff --git 
a/core/src/main/java/org/apache/calcite/prepare/CalcitePrepareImpl.java 
b/core/src/main/java/org/apache/calcite/prepare/CalcitePrepareImpl.java
index bb0447d1a9..d78e08cded 100644
--- a/core/src/main/java/org/apache/calcite/prepare/CalcitePrepareImpl.java
+++ b/core/src/main/java/org/apache/calcite/prepare/CalcitePrepareImpl.java
@@ -591,6 +591,7 @@ public class CalcitePrepareImpl implements CalcitePrepare {
 case INSERT:
 case DELETE:
 case UPDATE:
+case MERGE:
   return Meta.StatementType.IS_DML;
 default:
   return Meta.StatementType.SELECT;
@@ -667,6 +668,7 @@ public class CalcitePrepareImpl implements CalcitePrepare {
   case INSERT:
   case DELETE:
   case UPDATE:
+  case MERGE:
   case EXPLAIN:
 // FIXME: getValidatedNodeType is wrong for DML
 x = RelOptUtil.createDmlRowType(sqlNode.getKind(), typeFactory);
diff --git 
a/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java 
b/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java
index 2462b89d55..5b59d2b9ee 100644
--- a/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java
+++ b/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java
@@ -72,6 +72,7 @@ import org.apache.calcite.sql.SqlJoin;
 import org.apache.calcite.sql.SqlKind;
 import org.apache.calcite.sql.SqlLiteral;
 import org.apache.calcite.sql.SqlMatchRecognize;
+import org.apache.calcite.sql.SqlMerge;
 import org.apache.calcite.sql.SqlNode;
 import org.apache.calcite.sql.SqlNodeList;
 import org.apache.calcite.sql.SqlSampleSpec;
@@ -1084,7 +1085,62 @@ public class RelToSqlConverter extends SqlImplementor
 
   return result(sqlDelete, input.clauses, modify, null);
 }
-case MERGE:
+case MERGE: {
+  final Result input = visitInput(modify, 0);
+  final SqlSelect select = input.asSelect();
+  // When querying with both the `WHEN MATCHED THEN UPDATE` and
+  // `WHEN NOT MATCHED THEN INSERT` clauses, the selectList consists of 
three parts:
+  // the insert expression, the target table reference, and the update 
expression.
+  // When querying with the `WHEN MATCHED THEN UPDATE` clause, the 
selectList will not
+  // include the update expression.
+  // However, when querying with the `WHEN NOT MATCHED THEN INSERT` clause,
+  // the expression list will only contain the insert expression.
+  final SqlNodeList selectList = 
SqlUtil.stripListAs(select.getSelectList());
+  final SqlJoin join =  requireNonNull((SqlJoin) select.getFrom());
+  final SqlNode condition = requireNonNull(join.getCondition());
+  final SqlNode source = join.getLeft();
+
+  SqlUpdate update = null;
+  final List updateColumnList =
+  requireNonNull(modify.getUpdateColumnList(),
+  () -> "modify.getUpdateColumnList() is null for " + modify);
+  final int nUpdateFiled = updateColumnList.size();
+  if (nUpdateFiled != 0) {
+final SqlNodeList expressionList =
+Util.last(selectList, nUpdateFiled).stream()
+.collect(SqlNodeList.toList());
+update =
+new SqlUpdate(POS, sqlTargetTable,
+identifierList(updateColumnList),
+expressionList,
+condition, null, null);
+  }
+
+  final RelDataType targetRowType = modify.getTable().getRowType();
+  final int

[calcite] branch main updated: [CALCITE-5931] Allow round decimals like 1.00 in window ranges

2023-09-09 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new d4046d0274 [CALCITE-5931] Allow round decimals like 1.00 in window 
ranges
d4046d0274 is described below

commit d4046d027450bb792481fae46af76719d562c904
Author: Claude Brisson 
AuthorDate: Tue Aug 15 15:50:32 2023 +0200

[CALCITE-5931] Allow round decimals like 1.00 in window ranges

Co-authored-by: xiejiajun 
---
 .../main/java/org/apache/calcite/sql/SqlWindow.java |  4 +++-
 .../org/apache/calcite/test/SqlValidatorTest.java   |  3 +++
 core/src/test/resources/sql/winagg.iq   | 21 +
 3 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/calcite/sql/SqlWindow.java 
b/core/src/main/java/org/apache/calcite/sql/SqlWindow.java
index 198b9961c3..28cca4fb0b 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlWindow.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlWindow.java
@@ -39,6 +39,7 @@ import 
org.checkerframework.checker.nullness.qual.EnsuresNonNullIf;
 import org.checkerframework.checker.nullness.qual.Nullable;
 import org.checkerframework.dataflow.qual.Pure;
 
+import java.math.BigDecimal;
 import java.util.List;
 
 import static org.apache.calcite.linq4j.Nullness.castNonNull;
@@ -691,7 +692,8 @@ public class SqlWindow extends SqlCall {
   final SqlNumericLiteral boundLiteral =
   (SqlNumericLiteral) boundVal;
   if (!boundLiteral.isExact()
-  || (boundLiteral.getScale() != null && boundLiteral.getScale() 
!= 0)
+  || (boundLiteral.getScale() != null
+&& 
boundLiteral.getValueAs(BigDecimal.class).stripTrailingZeros().scale() > 0)
   || (0 > boundLiteral.longValue(true))) {
 // true == throw if not exact (we just tested that - right?)
 throw validator.newValidationError(boundVal,
diff --git a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java 
b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
index 1b2149e37f..150dbdf785 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
@@ -3356,6 +3356,9 @@ public class SqlValidatorTest extends 
SqlValidatorTestCase {
 win("window w as (rows ^2.5^ preceding)")
 .fails("ROWS value must be a non-negative integral constant");
 
+// CALCITE-5931 - Allow integers like 1.00 in window frame
+win("window w as (rows 2.00 preceding)").ok();
+
 // ---
 // --   negative testings   --
 // ---
diff --git a/core/src/test/resources/sql/winagg.iq 
b/core/src/test/resources/sql/winagg.iq
index e6228a3966..7304a00382 100644
--- a/core/src/test/resources/sql/winagg.iq
+++ b/core/src/test/resources/sql/winagg.iq
@@ -98,6 +98,27 @@ order by 1;
 
 !ok
 
+# [CALCITE-5931] Allow integers like 1.00 in window frame
+select empno,
+  stddev(comm) over (order by empno rows 2 preceding) as stdev_2int,
+  stddev(comm) over (order by empno rows 2.00 preceding) as stdev_2double
+from emp
+where deptno = 30
+order by 1;
++---+-+-+
+| EMPNO | STDEV_2INT  | STDEV_2DOUBLE  
 |
++---+-+-+
+|  7499 | |
 |
+|  7521 |  141.421356237309510106570087373256683349609375 |  
141.421356237309510106570087373256683349609375 |
+|  7654 | 585.9465277082316561063635163009166717529296875 | 
585.9465277082316561063635163009166717529296875 |
+|  7698 | 636.3961030678927954795653931796550750732421875 | 
636.3961030678927954795653931796550750732421875 |
+|  7844 |  989.949493661166570745990611612796783447265625 |  
989.949493661166570745990611612796783447265625 |
+|  7900 | |
 |
++---+-+-+
+(6 rows)
+
+!ok
+
 !use post
 # [CALCITE-1540] Support multiple columns in PARTITION BY clause of window 
function
 select gender,deptno,



[calcite] branch main updated: [CALCITE-5944] Add metadata for Sample

2023-09-07 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 64268b9dd7 [CALCITE-5944] Add metadata for Sample
64268b9dd7 is described below

commit 64268b9dd70bcdc15a3421ab120b8e5ecba17339
Author: xiejiajun 
AuthorDate: Sun Aug 20 17:30:58 2023 +0800

[CALCITE-5944] Add metadata for Sample

- RelMdAllPredicates
- RelMdColumnOrigins
- RelMdExpressionLineage
- RelMdMaxRowCount
- RelMdMinRowCount
- RelMdPredicates
- RelMdRowCount
---
 .../calcite/rel/metadata/RelMdAllPredicates.java   |  9 
 .../calcite/rel/metadata/RelMdColumnOrigins.java   |  6 +++
 .../rel/metadata/RelMdExpressionLineage.java   |  9 
 .../calcite/rel/metadata/RelMdMaxRowCount.java |  5 ++
 .../calcite/rel/metadata/RelMdMinRowCount.java |  5 ++
 .../calcite/rel/metadata/RelMdPredicates.java  |  8 
 .../apache/calcite/rel/metadata/RelMdRowCount.java |  7 +++
 .../org/apache/calcite/test/RelMetadataTest.java   | 56 ++
 .../GeneratedMetadata_AllPredicatesHandler.java|  2 +
 .../GeneratedMetadata_ColumnOriginHandler.java |  2 +
 ...GeneratedMetadata_ExpressionLineageHandler.java |  2 +
 .../GeneratedMetadata_MaxRowCountHandler.java  |  2 +
 .../GeneratedMetadata_MinRowCountHandler.java  |  2 +
 .../GeneratedMetadata_PredicatesHandler.java   |  2 +
 .../janino/GeneratedMetadata_RowCountHandler.java  |  2 +
 15 files changed, 119 insertions(+)

diff --git 
a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdAllPredicates.java 
b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdAllPredicates.java
index a062ab6502..d86a61fad8 100644
--- a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdAllPredicates.java
+++ b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdAllPredicates.java
@@ -27,6 +27,7 @@ import org.apache.calcite.rel.core.Exchange;
 import org.apache.calcite.rel.core.Filter;
 import org.apache.calcite.rel.core.Join;
 import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Sample;
 import org.apache.calcite.rel.core.SetOp;
 import org.apache.calcite.rel.core.Sort;
 import org.apache.calcite.rel.core.TableModify;
@@ -345,6 +346,14 @@ public class RelMdAllPredicates
 return newPreds;
   }
 
+  /**
+   * Extracts predicates for a Sample.
+   */
+  public @Nullable RelOptPredicateList getAllPredicates(Sample sample,
+  RelMetadataQuery mq) {
+return mq.getAllPredicates(sample.getInput());
+  }
+
   /**
* Extracts predicates for a Sort.
*/
diff --git 
a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdColumnOrigins.java 
b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdColumnOrigins.java
index dc30e6a20a..a6ed11c104 100644
--- a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdColumnOrigins.java
+++ b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdColumnOrigins.java
@@ -25,6 +25,7 @@ import org.apache.calcite.rel.core.Exchange;
 import org.apache.calcite.rel.core.Filter;
 import org.apache.calcite.rel.core.Join;
 import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Sample;
 import org.apache.calcite.rel.core.SetOp;
 import org.apache.calcite.rel.core.Snapshot;
 import org.apache.calcite.rel.core.Sort;
@@ -195,6 +196,11 @@ public class RelMdColumnOrigins
 return mq.getColumnOrigins(rel.getInput(), iOutputColumn);
   }
 
+  public @Nullable Set getColumnOrigins(Sample rel,
+  RelMetadataQuery mq, int iOutputColumn) {
+return mq.getColumnOrigins(rel.getInput(), iOutputColumn);
+  }
+
   public @Nullable Set getColumnOrigins(Snapshot rel,
   RelMetadataQuery mq, int iOutputColumn) {
 return mq.getColumnOrigins(rel.getInput(), iOutputColumn);
diff --git 
a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdExpressionLineage.java
 
b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdExpressionLineage.java
index 1b3445..3025ee1d4a 100644
--- 
a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdExpressionLineage.java
+++ 
b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdExpressionLineage.java
@@ -26,6 +26,7 @@ import org.apache.calcite.rel.core.Filter;
 import org.apache.calcite.rel.core.Join;
 import org.apache.calcite.rel.core.JoinRelType;
 import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Sample;
 import org.apache.calcite.rel.core.Snapshot;
 import org.apache.calcite.rel.core.Sort;
 import org.apache.calcite.rel.core.TableModify;
@@ -394,6 +395,14 @@ public class RelMdExpressionLineage
 return mq.getExpressionLineage(rel.getInput(), outputExpression);
   }
 
+  /**
+   * Expression lineage from Sample.
+   */
+  public @Nullable Set getExpressionLineage(Sample rel,
+  RelMetadataQuery mq, RexNode outputExpression) {
+return

[calcite] branch main updated: [CALCITE-5922] The SQL generated for the POSITION function(with 3 input arguments) by the SparkSqlDialect is not recognized by Spark SQL

2023-08-25 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 730361b664 [CALCITE-5922] The SQL generated for the POSITION 
function(with 3 input arguments) by the SparkSqlDialect is not recognized by 
Spark SQL
730361b664 is described below

commit 730361b66442e5d12448e120e8ce67fc070b271a
Author: macroguo 
AuthorDate: Sun Aug 13 16:29:45 2023 +0800

[CALCITE-5922] The SQL generated for the POSITION function(with 3 input 
arguments) by the SparkSqlDialect is not recognized by Spark SQL
---
 .../org/apache/calcite/sql/dialect/SparkSqlDialect.java  |  3 +++
 .../calcite/rel/rel2sql/RelToSqlConverterTest.java   | 16 
 2 files changed, 19 insertions(+)

diff --git 
a/core/src/main/java/org/apache/calcite/sql/dialect/SparkSqlDialect.java 
b/core/src/main/java/org/apache/calcite/sql/dialect/SparkSqlDialect.java
index 4c3aa6abd5..e97cfcac01 100644
--- a/core/src/main/java/org/apache/calcite/sql/dialect/SparkSqlDialect.java
+++ b/core/src/main/java/org/apache/calcite/sql/dialect/SparkSqlDialect.java
@@ -147,6 +147,9 @@ public class SparkSqlDialect extends SqlDialect {
   case TRIM:
 unparseHiveTrim(writer, call, leftPrec, rightPrec);
 break;
+  case POSITION:
+SqlUtil.unparseFunctionSyntax(SqlStdOperatorTable.POSITION, writer, 
call, false);
+break;
   default:
 super.unparseCall(writer, call, leftPrec, rightPrec);
   }
diff --git 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index dded73c882..50e9808a84 100644
--- 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++ 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -2494,6 +2494,22 @@ class RelToSqlConverterTest {
 sql(query).withBigQuery().ok(expected);
   }
 
+  /** Test case for
+   * https://issues.apache.org/jira/browse/CALCITE-5922;>[CALCITE-5922]
+   * The SQL generated for the POSITION function(with 3 input arguments) by the
+   * SparkSqlDialect is not recognized by Spark SQL. */
+  @Test void testPositionForSpark() {
+final String query = "SELECT POSITION('a' IN 'abc')";
+final String expected = "SELECT POSITION('a', 'abc')\n"
++ "FROM (VALUES (0)) t (ZERO)";
+sql(query).withSpark().ok(expected);
+
+final String query2 = "SELECT POSITION('a' IN 'abc' FROM 1)";
+final String expected2 = "SELECT POSITION('a', 'abc', 1)\n"
++ "FROM (VALUES (0)) t (ZERO)";
+sql(query2).withSpark().ok(expected2);
+  }
+
   @Test void testInstrFunction4Operands() {
 final String query = "SELECT INSTR('ABC', 'A', 1, 1) from \"product\"";
 final String expectedBQ = "SELECT INSTR('ABC', 'A', 1, 1)\n"



[calcite] branch main updated (f901d7d44a -> dc3f11bf9b)

2023-08-20 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

jiajunxie pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


from f901d7d44a [CALCITE-5870] Allow literals like DECIMAL '12.3' 
(consistent with Postgres)
 add dc3f11bf9b [CALCITE-985] Validate MERGE

No new revisions were added by this update.

Summary of changes:
 .../calcite/sql/validate/SqlValidatorImpl.java | 23 +-
 .../apache/calcite/test/SqlToRelConverterTest.java | 16 ++--
 .../org/apache/calcite/test/SqlValidatorTest.java  | 94 ++
 .../apache/calcite/test/SqlToRelConverterTest.xml  | 22 +
 4 files changed, 144 insertions(+), 11 deletions(-)



[calcite] branch main updated: [CALCITE-5870] Allow literals like DECIMAL '12.3' (consistent with Postgres)

2023-08-18 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new f901d7d44a [CALCITE-5870] Allow literals like DECIMAL '12.3' 
(consistent with Postgres)
f901d7d44a is described below

commit f901d7d44a41e3a1db010c42dd4a422dcb31996c
Author: shenlang 
AuthorDate: Tue Jul 25 20:14:07 2023 +0800

[CALCITE-5870] Allow literals like DECIMAL '12.3' (consistent with Postgres)
---
 core/src/main/codegen/templates/Parser.jj  |  6 +++
 .../apache/calcite/sql/parser/SqlParserUtil.java   | 12 ++
 .../apache/calcite/sql/test/SqlAdvisorTest.java|  1 +
 .../org/apache/calcite/test/SqlValidatorTest.java  |  2 +
 core/src/test/resources/sql/misc.iq| 23 
 site/_docs/reference.md|  2 +-
 .../apache/calcite/sql/parser/SqlParserTest.java   | 43 ++
 7 files changed, 88 insertions(+), 1 deletion(-)

diff --git a/core/src/main/codegen/templates/Parser.jj 
b/core/src/main/codegen/templates/Parser.jj
index ee9a9c02a7..6780339003 100644
--- a/core/src/main/codegen/templates/Parser.jj
+++ b/core/src/main/codegen/templates/Parser.jj
@@ -4447,6 +4447,7 @@ SqlNode LiteralOrIntervalExpression() :
 /** Parses a unsigned numeric literal */
 SqlNumericLiteral UnsignedNumericLiteral() :
 {
+final String p;
 }
 {
  {
@@ -4456,6 +4457,11 @@ SqlNumericLiteral UnsignedNumericLiteral() :
  {
 return SqlLiteral.createExactNumeric(token.image, getPos());
 }
+|
+
+p = SimpleStringLiteral() {
+return SqlParserUtil.parseDecimalLiteral(SqlParserUtil.trim(p, " "), 
getPos());
+}
 |
  {
 return SqlLiteral.createApproxNumeric(token.image, getPos());
diff --git 
a/core/src/main/java/org/apache/calcite/sql/parser/SqlParserUtil.java 
b/core/src/main/java/org/apache/calcite/sql/parser/SqlParserUtil.java
index 5f36342e81..e4b8ef2b41 100644
--- a/core/src/main/java/org/apache/calcite/sql/parser/SqlParserUtil.java
+++ b/core/src/main/java/org/apache/calcite/sql/parser/SqlParserUtil.java
@@ -325,6 +325,18 @@ public final class SqlParserUtil {
 return SqlLiteral.createDate(d, pos);
   }
 
+  public static SqlNumericLiteral parseDecimalLiteral(String s, SqlParserPos 
pos) {
+try {
+  // The s maybe scientific notation string,e.g. 1.2E-3,
+  // we need to convert it to 0.0012
+  s = new BigDecimal(s).toPlainString();
+} catch (NumberFormatException e) {
+  throw SqlUtil.newContextException(pos,
+  RESOURCE.invalidLiteral(s, "DECIMAL"));
+}
+return SqlLiteral.createExactNumeric(s, pos);
+  }
+
   public static SqlTimeLiteral parseTimeLiteral(String s, SqlParserPos pos) {
 final DateTimeUtils.PrecisionTime pt =
 DateTimeUtils.parsePrecisionDateTimeLiteral(s,
diff --git a/core/src/test/java/org/apache/calcite/sql/test/SqlAdvisorTest.java 
b/core/src/test/java/org/apache/calcite/sql/test/SqlAdvisorTest.java
index eba04ac157..f3195b3f92 100644
--- a/core/src/test/java/org/apache/calcite/sql/test/SqlAdvisorTest.java
+++ b/core/src/test/java/org/apache/calcite/sql/test/SqlAdvisorTest.java
@@ -165,6 +165,7 @@ class SqlAdvisorTest extends SqlValidatorTestCase {
   "KEYWORD(CURSOR)",
   "KEYWORD(DATE)",
   "KEYWORD(DATETIME)",
+  "KEYWORD(DECIMAL)",
   "KEYWORD(DENSE_RANK)",
   "KEYWORD(ELEMENT)",
   "KEYWORD(EVERY)",
diff --git a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java 
b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
index cb6184d65d..d068702d59 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
@@ -243,6 +243,8 @@ public class SqlValidatorTest extends SqlValidatorTestCase {
 .columnType("BOOLEAN NOT NULL");
 expr("unknown")
 .columnType("BOOLEAN");
+expr("DECIMAL '123456.7890'")
+.columnType("DECIMAL(10, 4) NOT NULL");
   }
 
   /** Tests that date-time literals with invalid strings are considered 
invalid.
diff --git a/core/src/test/resources/sql/misc.iq 
b/core/src/test/resources/sql/misc.iq
index b868a090f1..321843c183 100644
--- a/core/src/test/resources/sql/misc.iq
+++ b/core/src/test/resources/sql/misc.iq
@@ -2520,4 +2520,27 @@ EnumerableCalc(expr#0..7=[{inputs}], 
expr#8=[CAST($t6):INTEGER], expr#9=[IS NOT
   EnumerableTableScan(table=[[scott, EMP]])
 !plan
 
+# [CALCITE-5870] Allow literals like DECIMAL '12.3' (consistent with Postgres)
+# Test a decimal value between decimal logic for range checking.
+select 12.3 between decimal '5.6' and decimal '17.8';
+++
+| EXPR$0 |
+++
+| true  

[calcite] branch main updated: [CALCITE-5906] JDBC adapter should generate TABLESAMPLE

2023-08-15 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 51da34bdf9 [CALCITE-5906] JDBC adapter should generate TABLESAMPLE
51da34bdf9 is described below

commit 51da34bdf91e602f32f965b3490bace026d51ef4
Author: shenlang 
AuthorDate: Tue Aug 8 22:44:36 2023 +0800

[CALCITE-5906] JDBC adapter should generate TABLESAMPLE
---
 .../calcite/rel/rel2sql/RelToSqlConverter.java | 25 ++
 .../apache/calcite/rel/rel2sql/SqlImplementor.java |  3 +-
 .../calcite/rel/rel2sql/RelToSqlConverterTest.java | 40 ++
 3 files changed, 67 insertions(+), 1 deletion(-)

diff --git 
a/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java 
b/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java
index 5853ddd414..2462b89d55 100644
--- a/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java
+++ b/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java
@@ -19,6 +19,7 @@ package org.apache.calcite.rel.rel2sql;
 import org.apache.calcite.adapter.jdbc.JdbcTable;
 import org.apache.calcite.linq4j.Ord;
 import org.apache.calcite.linq4j.tree.Expressions;
+import org.apache.calcite.plan.RelOptSamplingParameters;
 import org.apache.calcite.plan.RelOptTable;
 import org.apache.calcite.rel.RelCollation;
 import org.apache.calcite.rel.RelCollations;
@@ -36,6 +37,7 @@ import org.apache.calcite.rel.core.JoinRelType;
 import org.apache.calcite.rel.core.Match;
 import org.apache.calcite.rel.core.Minus;
 import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Sample;
 import org.apache.calcite.rel.core.Sort;
 import org.apache.calcite.rel.core.TableFunctionScan;
 import org.apache.calcite.rel.core.TableModify;
@@ -72,6 +74,7 @@ import org.apache.calcite.sql.SqlLiteral;
 import org.apache.calcite.sql.SqlMatchRecognize;
 import org.apache.calcite.sql.SqlNode;
 import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.SqlSampleSpec;
 import org.apache.calcite.sql.SqlSelect;
 import org.apache.calcite.sql.SqlTableRef;
 import org.apache.calcite.sql.SqlUpdate;
@@ -875,6 +878,28 @@ public class RelToSqlConverter extends SqlImplementor
 return result(query, clauses, e, null);
   }
 
+  /** Visits a Sample; called by {@link #dispatch} via reflection. */
+  public Result visit(Sample e) {
+Result x = visitInput(e, 0);
+RelOptSamplingParameters parameters = e.getSamplingParameters();
+boolean isRepeatable = parameters.isRepeatable();
+boolean isBernoulli = parameters.isBernoulli();
+
+SqlSampleSpec tableSampleSpec =
+isRepeatable
+? SqlSampleSpec.createTableSample(
+isBernoulli, parameters.sampleRate, parameters.getRepeatableSeed())
+: SqlSampleSpec.createTableSample(isBernoulli, 
parameters.sampleRate);
+
+SqlLiteral tableSampleLiteral =
+SqlLiteral.createSample(tableSampleSpec, POS);
+
+SqlNode tableRef =
+SqlStdOperatorTable.TABLESAMPLE.createCall(POS, x.node, 
tableSampleLiteral);
+
+return result(tableRef, ImmutableList.of(Clause.FROM), e, null);
+  }
+
   private @Nullable SqlIdentifier getDual() {
 final List names = dialect.getSingleRowTableName();
 if (names == null) {
diff --git 
a/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java 
b/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java
index 6517440c5b..4c535c14e3 100644
--- a/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java
+++ b/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java
@@ -541,7 +541,8 @@ public abstract class SqlImplementor {
 || node instanceof SqlCall
 && (((SqlCall) node).getOperator() instanceof SqlSetOperator
 || ((SqlCall) node).getOperator() == SqlStdOperatorTable.AS
-|| ((SqlCall) node).getOperator() == 
SqlStdOperatorTable.VALUES)
+|| ((SqlCall) node).getOperator() == SqlStdOperatorTable.VALUES
+|| ((SqlCall) node).getOperator() == 
SqlStdOperatorTable.TABLESAMPLE)
 : node;
 if (requiresAlias(node)) {
   node = as(node, "t");
diff --git 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index 6670a44349..dded73c882 100644
--- 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++ 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -360,6 +360,46 @@ class RelToSqlConverterTest {
 sql(query).ok(expected);
   }
 
+  /** Test case for
+   * https://issues.apache.org/jira/browse/CALCITE-5906;>[CALCITE-5906]
+   * JDBC adapter should generate TABLESAMPLE. */
+  @Test void tes

[calcite] branch main updated: [CALCITE-5813] Type inference for sql functions REPEAT, SPACE, XML_TRANSFORM, and XML_EXTRACT is incorrect

2023-08-12 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new ed42c35bd1 [CALCITE-5813] Type inference for sql functions REPEAT, 
SPACE, XML_TRANSFORM, and XML_EXTRACT is incorrect
ed42c35bd1 is described below

commit ed42c35bd1a357dd0b5d5dc268e93573b724a800
Author: Mihai Budiu 
AuthorDate: Fri Aug 11 22:11:33 2023 -0700

[CALCITE-5813] Type inference for sql functions REPEAT, SPACE, 
XML_TRANSFORM, and XML_EXTRACT is incorrect

Signed-off-by: Mihai Budiu 
---
 .../calcite/sql/fun/SqlLibraryOperators.java   |   8 +-
 .../calcite/sql/fun/SqlStdOperatorTable.java   |   2 +-
 .../org/apache/calcite/test/RelOptRulesTest.java   |  59 
 .../org/apache/calcite/test/RelOptRulesTest.xml|  51 ++
 .../org/apache/calcite/test/SqlOperatorTest.java   | 103 -
 5 files changed, 197 insertions(+), 26 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java 
b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
index 6b234c2165..c3bcb53607 100644
--- a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
@@ -493,13 +493,13 @@ public abstract class SqlLibraryOperators {
   @LibraryOperator(libraries = {ORACLE})
   public static final SqlFunction XML_TRANSFORM =
   SqlBasicFunction.create("XMLTRANSFORM",
-  ReturnTypes.VARCHAR_2000.andThen(SqlTypeTransforms.FORCE_NULLABLE),
+  ReturnTypes.VARCHAR.andThen(SqlTypeTransforms.FORCE_NULLABLE),
   OperandTypes.STRING_STRING);
 
   @LibraryOperator(libraries = {ORACLE})
   public static final SqlFunction EXTRACT_XML =
   SqlBasicFunction.create("EXTRACT",
-  ReturnTypes.VARCHAR_2000.andThen(SqlTypeTransforms.FORCE_NULLABLE),
+  ReturnTypes.VARCHAR.andThen(SqlTypeTransforms.FORCE_NULLABLE),
   OperandTypes.STRING_STRING_OPTIONAL_STRING);
 
   @LibraryOperator(libraries = {ORACLE})
@@ -802,7 +802,7 @@ public abstract class SqlLibraryOperators {
   @LibraryOperator(libraries = {BIG_QUERY, MYSQL, POSTGRESQL})
   public static final SqlFunction REPEAT =
   SqlBasicFunction.create("REPEAT",
-  ReturnTypes.ARG0_NULLABLE_VARYING,
+  ReturnTypes.VARCHAR_NULLABLE,
   OperandTypes.STRING_INTEGER,
   SqlFunctionCategory.STRING);
 
@@ -814,7 +814,7 @@ public abstract class SqlLibraryOperators {
   @LibraryOperator(libraries = {MYSQL})
   public static final SqlFunction SPACE =
   SqlBasicFunction.create("SPACE",
-  ReturnTypes.VARCHAR_2000_NULLABLE,
+  ReturnTypes.VARCHAR_NULLABLE,
   OperandTypes.INTEGER,
   SqlFunctionCategory.STRING);
 
diff --git 
a/core/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.java 
b/core/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.java
index 65bb44e9e7..530368d278 100644
--- a/core/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.java
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.java
@@ -1574,7 +1574,7 @@ public class SqlStdOperatorTable extends 
ReflectiveSqlOperatorTable {
   /** The {@code REPLACE(string, search, replace)} function. Not standard SQL,
* but in Oracle and Postgres. */
   public static final SqlFunction REPLACE =
-  SqlBasicFunction.create("REPLACE", ReturnTypes.ARG0_NULLABLE_VARYING,
+  SqlBasicFunction.create("REPLACE", ReturnTypes.VARCHAR_NULLABLE,
   OperandTypes.STRING_STRING_STRING, SqlFunctionCategory.STRING);
 
   /** The {@code CONVERT(charValue, srcCharsetName, destCharsetName)}
diff --git a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java 
b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
index a9b66f1076..c296ab7ca2 100644
--- a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
@@ -224,6 +224,65 @@ class RelOptRulesTest extends RelOptTestBase {
   && "item".equalsIgnoreCase(((RexCall) expr).getOperator().getName());
   }
 
+  /**
+   * Test case for https://issues.apache.org/jira/browse/CALCITE-5813;>[CALCITE-5813]
+   * Type inference for sql functions REPEAT, SPACE, XML_TRANSFORM,
+   * and XML_EXTRACT is incorrect. */
+  @Test void testRepeat() {
+HepProgramBuilder builder = new HepProgramBuilder();
+builder.addRuleClass(ReduceExpressionsRule.class);
+HepPlanner hepPlanner = new HepPlanner(builder.build());
+hepPlanner.addRule(CoreRules.PROJECT_REDUCE_EXPRESSIONS);
+
+final String sql = "select REPEAT('abc', 2)";
+fixture()
+.withFactory(
+t -> t.withOp

[calcite] branch main updated: [CALCITE-5885] SqlNode#toSqlString() does not honor dialect's supportsCharSet() flag on nested types

2023-08-10 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 697412624e [CALCITE-5885] SqlNode#toSqlString() does not honor 
dialect's supportsCharSet() flag on nested types
697412624e is described below

commit 697412624ef6baf0bf94554a51a5f7ed54d09ce7
Author: xiejiajun 
AuthorDate: Sun Aug 6 14:44:07 2023 +0800

[CALCITE-5885] SqlNode#toSqlString() does not honor dialect's 
supportsCharSet() flag on nested types
---
 .../java/org/apache/calcite/sql/SqlBasicTypeNameSpec.java |  2 +-
 .../org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java | 11 +++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git 
a/core/src/main/java/org/apache/calcite/sql/SqlBasicTypeNameSpec.java 
b/core/src/main/java/org/apache/calcite/sql/SqlBasicTypeNameSpec.java
index 2b9cf6e0e4..547bdfb22f 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlBasicTypeNameSpec.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlBasicTypeNameSpec.java
@@ -180,7 +180,7 @@ public class SqlBasicTypeNameSpec extends SqlTypeNameSpec {
   writer.keyword("WITH LOCAL TIME ZONE");
 }
 
-if (charSetName != null) {
+if (writer.getDialect().supportsCharSet() && charSetName != null) {
   writer.keyword("CHARACTER SET");
   writer.identifier(charSetName, true);
 }
diff --git 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index 51044cb250..6670a44349 100644
--- 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++ 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -2024,6 +2024,17 @@ class RelToSqlConverterTest {
 sql(query).withHive().ok(expected);
   }
 
+  /** Test case for
+   * https://issues.apache.org/jira/browse/CALCITE-5885;>[CALCITE-5885]
+   * SqlNode#toSqlString() does not honor dialect's supportsCharSet() flag on 
nested types.
+   */
+  @Test void testCastArrayCharset() {
+final String query = "select cast(array['a', 'b', 'c'] as varchar array)";
+final String expected = "SELECT CAST(ARRAY['a', 'b', 'c'] AS VARCHAR 
ARRAY)";
+sql(query)
+.withHive().ok(expected);
+  }
+
   /** Test case for
* https://issues.apache.org/jira/browse/CALCITE-3282;>[CALCITE-3282]
* HiveSqlDialect unparse Interger type as Int in order



[calcite] branch main updated: Following [CALCITE-5688] Eliminate warnings in server parser

2023-07-28 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 11126c582f Following [CALCITE-5688] Eliminate warnings in server parser
11126c582f is described below

commit 11126c582fa49e38d17a75ce412fe47a824e3c7f
Author: xiejiajun 
AuthorDate: Mon Jul 24 08:37:55 2023 +0800

Following [CALCITE-5688] Eliminate warnings in server parser
---
 core/src/main/codegen/templates/Parser.jj | 1 +
 1 file changed, 1 insertion(+)

diff --git a/core/src/main/codegen/templates/Parser.jj 
b/core/src/main/codegen/templates/Parser.jj
index 1ea1cebcf1..0a11b28426 100644
--- a/core/src/main/codegen/templates/Parser.jj
+++ b/core/src/main/codegen/templates/Parser.jj
@@ -1138,6 +1138,7 @@ SqlNode SqlStmt() :
 |
 
 <#if 
(parser.truncateStatementParserMethods!default.parser.truncateStatementParserMethods)?size
 != 0>
+LOOKAHEAD(2)
 stmt = SqlTruncate()
 |
 



[calcite] branch main updated: [CALCITE-5688] Support TRUNCATE TABLE DDL statement in server parser

2023-07-05 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new fb6f43192c [CALCITE-5688] Support TRUNCATE TABLE DDL statement in 
server parser
fb6f43192c is described below

commit fb6f43192c4253caea63c0705067a9aa12a3fa74
Author: xiejiajun 
AuthorDate: Sun Jun 18 10:10:51 2023 +0800

[CALCITE-5688] Support TRUNCATE TABLE DDL statement in server parser

Co-authored-by: sumeetgajjar 
---
 core/src/main/codegen/default_config.fmpp  |  6 ++
 core/src/main/codegen/templates/Parser.jj  | 28 +
 .../main/java/org/apache/calcite/sql/SqlKind.java  |  5 +-
 .../java/org/apache/calcite/sql/SqlTruncate.java   | 33 +++
 .../org/apache/calcite/sql/ddl/SqlDdlNodes.java|  6 ++
 .../apache/calcite/sql/ddl/SqlTruncateTable.java   | 66 ++
 server/src/main/codegen/config.fmpp|  8 +++
 server/src/main/codegen/includes/parserImpls.ftl   | 19 +++
 .../apache/calcite/server/ServerDdlExecutor.java   | 38 +
 .../org/apache/calcite/test/ServerParserTest.java  | 11 
 .../java/org/apache/calcite/test/ServerTest.java   | 15 +
 server/src/test/resources/sql/table.iq | 19 +++
 12 files changed, 253 insertions(+), 1 deletion(-)

diff --git a/core/src/main/codegen/default_config.fmpp 
b/core/src/main/codegen/default_config.fmpp
index 8c6fa48470..6a172a6ef7 100644
--- a/core/src/main/codegen/default_config.fmpp
+++ b/core/src/main/codegen/default_config.fmpp
@@ -424,6 +424,12 @@ parser: {
   dropStatementParserMethods: [
   ]
 
+  # List of methods for parsing extensions to "TRUNCATE" calls.
+  # Each must accept arguments "(SqlParserPos pos)".
+  # Example: "SqlTruncate".
+  truncateStatementParserMethods: [
+  ]
+
   # Binary operators tokens.
   # Example: "< INFIX_CAST: \"::\" >".
   binaryOperatorsTokens: [
diff --git a/core/src/main/codegen/templates/Parser.jj 
b/core/src/main/codegen/templates/Parser.jj
index 56e44c06e8..822f6a407b 100644
--- a/core/src/main/codegen/templates/Parser.jj
+++ b/core/src/main/codegen/templates/Parser.jj
@@ -1136,6 +1136,10 @@ SqlNode SqlStmt() :
 <#if 
(parser.dropStatementParserMethods!default.parser.dropStatementParserMethods)?size
 != 0>
 stmt = SqlDrop()
 |
+
+<#if 
(parser.truncateStatementParserMethods!default.parser.truncateStatementParserMethods)?size
 != 0>
+stmt = SqlTruncate()
+|
 
 stmt = OrderedQueryOrExpr(ExprContext.ACCEPT_QUERY)
 |
@@ -4354,6 +4358,30 @@ SqlDrop SqlDrop() :
 }
 
 
+<#if 
(parser.truncateStatementParserMethods!default.parser.truncateStatementParserMethods)?size
 != 0>
+/**
+ * Parses a TRUNCATE statement.
+ */
+SqlTruncate SqlTruncate() :
+{
+final Span s;
+final SqlTruncate truncate;
+}
+{
+ { s = span(); }
+(
+<#-- additional literal parser methods are included here -->
+<#list 
(parser.truncateStatementParserMethods!default.parser.truncateStatementParserMethods)
 as method>
+truncate = ${method}(s)
+<#sep>|
+
+)
+{
+return truncate;
+}
+}
+
+
 /**
  * Parses a literal expression, allowing continued string literals.
  * Usually returns an SqlLiteral, but a continued string literal
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlKind.java 
b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
index b7cbd3f65f..c98a0ccf49 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlKind.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
@@ -1158,6 +1158,9 @@ public enum SqlKind {
   /** {@code DROP TABLE} DDL statement. */
   DROP_TABLE,
 
+  /** {@code TRUNCATE TABLE} DDL statement. */
+  TRUNCATE_TABLE,
+
   /** {@code CREATE VIEW} DDL statement. */
   CREATE_VIEW,
 
@@ -1269,7 +1272,7 @@ public enum SqlKind {
   public static final EnumSet DDL =
   EnumSet.of(COMMIT, ROLLBACK, ALTER_SESSION,
   CREATE_SCHEMA, CREATE_FOREIGN_SCHEMA, DROP_SCHEMA,
-  CREATE_TABLE, ALTER_TABLE, DROP_TABLE,
+  CREATE_TABLE, ALTER_TABLE, DROP_TABLE, TRUNCATE_TABLE,
   CREATE_FUNCTION, DROP_FUNCTION,
   CREATE_VIEW, ALTER_VIEW, DROP_VIEW,
   CREATE_MATERIALIZED_VIEW, ALTER_MATERIALIZED_VIEW,
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlTruncate.java 
b/core/src/main/java/org/apache/calcite/sql/SqlTruncate.java
new file mode 100644
index 00..d8e75bb312
--- /dev/null
+++ b/core/src/main/java/org/apache/calcite/sql/SqlTruncate.java
@@ -0,0 +1,33 @@
+/*
+ * 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 Apa

[calcite] branch main updated: [MINOR] Assert can be replaced in `AbstractMaterializedViewRule#perform()`

2023-06-29 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new aff0fc981b [MINOR] Assert can be replaced in 
`AbstractMaterializedViewRule#perform()`
aff0fc981b is described below

commit aff0fc981b4bd76680aa5d92c20ef98b84cd5304
Author: xiejiajun 
AuthorDate: Sun Jun 11 10:20:15 2023 +0800

[MINOR] Assert can be replaced in `AbstractMaterializedViewRule#perform()`
---
 .../calcite/rel/rules/materialize/MaterializedViewRule.java | 13 -
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/rel/rules/materialize/MaterializedViewRule.java
 
b/core/src/main/java/org/apache/calcite/rel/rules/materialize/MaterializedViewRule.java
index 4a7f101e23..0ce1de86da 100644
--- 
a/core/src/main/java/org/apache/calcite/rel/rules/materialize/MaterializedViewRule.java
+++ 
b/core/src/main/java/org/apache/calcite/rel/rules/materialize/MaterializedViewRule.java
@@ -365,7 +365,7 @@ public abstract class MaterializedViewRule

[calcite] branch main updated: [CALCITE-5751] Add ARRAY_APPEND, ARRAY_POSITION, ARRAY_REMOVE, ARRAY_PREPEND function (enabled in Spark library)

2023-06-21 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new a2d2a31a70 [CALCITE-5751] Add ARRAY_APPEND, ARRAY_POSITION, 
ARRAY_REMOVE, ARRAY_PREPEND function (enabled in Spark library)
a2d2a31a70 is described below

commit a2d2a31a70be3b20f3f2b8f311bf580dd9ae1e24
Author: yongen.ly 
AuthorDate: Sun Jun 11 10:23:19 2023 +0800

[CALCITE-5751] Add ARRAY_APPEND, ARRAY_POSITION, ARRAY_REMOVE, 
ARRAY_PREPEND function (enabled in Spark library)
---
 .../calcite/adapter/enumerable/RexImpTable.java|   8 ++
 .../org/apache/calcite/runtime/SqlFunctions.java   |  36 ++
 .../main/java/org/apache/calcite/sql/SqlKind.java  |  12 ++
 .../calcite/sql/fun/SqlLibraryOperators.java   |  45 
 .../org/apache/calcite/sql/type/ReturnTypes.java   |   8 ++
 .../org/apache/calcite/util/BuiltInMethod.java |   4 +
 site/_docs/reference.md|   4 +
 .../org/apache/calcite/test/SqlOperatorTest.java   | 126 +
 8 files changed, 243 insertions(+)

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 1144b140fd..3a27c361b1 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
@@ -118,6 +118,7 @@ import static 
org.apache.calcite.sql.fun.SqlInternalOperators.THROW_UNLESS;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ACOSH;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_AGG;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_APPEND;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_COMPACT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_CONCAT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_CONCAT_AGG;
@@ -128,6 +129,9 @@ import static 
org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_INTERSECT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_LENGTH;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_MAX;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_MIN;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_POSITION;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_PREPEND;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_REMOVE;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_REPEAT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_REVERSE;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_SIZE;
@@ -698,6 +702,7 @@ public class RexImpTable {
   defineMethod(ELEMENT, BuiltInMethod.ELEMENT.method, NullPolicy.STRICT);
   defineMethod(STRUCT_ACCESS, BuiltInMethod.STRUCT_ACCESS.method, 
NullPolicy.ANY);
   defineMethod(MEMBER_OF, BuiltInMethod.MEMBER_OF.method, NullPolicy.NONE);
+  defineMethod(ARRAY_APPEND, BuiltInMethod.ARRAY_APPEND.method, 
NullPolicy.ARG0);
   defineMethod(ARRAY_COMPACT, BuiltInMethod.ARRAY_COMPACT.method, 
NullPolicy.STRICT);
   defineMethod(ARRAY_CONTAINS, BuiltInMethod.LIST_CONTAINS.method, 
NullPolicy.ANY);
   defineMethod(ARRAY_DISTINCT, BuiltInMethod.ARRAY_DISTINCT.method, 
NullPolicy.STRICT);
@@ -706,6 +711,9 @@ public class RexImpTable {
   defineMethod(ARRAY_LENGTH, BuiltInMethod.COLLECTION_SIZE.method, 
NullPolicy.STRICT);
   defineMethod(ARRAY_MAX, BuiltInMethod.ARRAY_MAX.method, 
NullPolicy.STRICT);
   defineMethod(ARRAY_MIN, BuiltInMethod.ARRAY_MIN.method, 
NullPolicy.STRICT);
+  defineMethod(ARRAY_PREPEND, BuiltInMethod.ARRAY_PREPEND.method, 
NullPolicy.ARG0);
+  defineMethod(ARRAY_POSITION, BuiltInMethod.ARRAY_POSITION.method, 
NullPolicy.ANY);
+  defineMethod(ARRAY_REMOVE, BuiltInMethod.ARRAY_REMOVE.method, 
NullPolicy.ANY);
   defineMethod(ARRAY_REPEAT, BuiltInMethod.ARRAY_REPEAT.method, 
NullPolicy.NONE);
   defineMethod(ARRAY_REVERSE, BuiltInMethod.ARRAY_REVERSE.method, 
NullPolicy.STRICT);
   defineMethod(ARRAY_SIZE, BuiltInMethod.COLLECTION_SIZE.method, 
NullPolicy.STRICT);
diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java 
b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
index 6157af6061..e7473b4790 100644
--- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
+++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -3904,6 +3904,14 @@ public class SqlFunctions {
 return result;
   }
 
+  /** Support the ARRAY_APPEND function. */
+  public static List arrayAppend(List list, Object element) {
+final List result = new

[calcite] branch main updated: [CALCITE-5704] Add ARRAY_EXCEPT, ARRAY_INTERSECT, ARRAY_UNION function (enabled in Spark library)

2023-06-07 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 821f03be6a [CALCITE-5704] Add ARRAY_EXCEPT, ARRAY_INTERSECT, 
ARRAY_UNION function (enabled in Spark library)
821f03be6a is described below

commit 821f03be6a99d44d2e9f9380354f28e884430602
Author: yongen.ly 
AuthorDate: Mon May 15 12:52:09 2023 +0800

[CALCITE-5704] Add ARRAY_EXCEPT, ARRAY_INTERSECT, ARRAY_UNION function 
(enabled in Spark library)
---
 .../calcite/adapter/enumerable/RexImpTable.java|  9 ++-
 .../org/apache/calcite/runtime/SqlFunctions.java   | 22 ++
 .../main/java/org/apache/calcite/sql/SqlKind.java  | 12 
 .../calcite/sql/fun/SqlLibraryOperators.java   | 51 ++
 .../org/apache/calcite/util/BuiltInMethod.java |  3 +
 site/_docs/reference.md|  5 +-
 .../org/apache/calcite/test/SqlOperatorTest.java   | 82 +++---
 7 files changed, 160 insertions(+), 24 deletions(-)

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 fb764466ab..f55ed21542 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
@@ -122,12 +122,15 @@ import static 
org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_COMPACT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_CONCAT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_CONCAT_AGG;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_DISTINCT;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_EXCEPT;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_INTERSECT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_LENGTH;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_MAX;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_MIN;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_REPEAT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_REVERSE;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_SIZE;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_UNION;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ASINH;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ATANH;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.BOOL_AND;
@@ -686,19 +689,21 @@ public class RexImpTable {
   // Multisets & arrays
   defineMethod(CARDINALITY, BuiltInMethod.COLLECTION_SIZE.method,
   NullPolicy.STRICT);
-  defineMethod(ARRAY_LENGTH, BuiltInMethod.COLLECTION_SIZE.method,
-  NullPolicy.STRICT);
   defineMethod(SLICE, BuiltInMethod.SLICE.method, NullPolicy.NONE);
   defineMethod(ELEMENT, BuiltInMethod.ELEMENT.method, NullPolicy.STRICT);
   defineMethod(STRUCT_ACCESS, BuiltInMethod.STRUCT_ACCESS.method, 
NullPolicy.ANY);
   defineMethod(MEMBER_OF, BuiltInMethod.MEMBER_OF.method, NullPolicy.NONE);
   defineMethod(ARRAY_COMPACT, BuiltInMethod.ARRAY_COMPACT.method, 
NullPolicy.STRICT);
   defineMethod(ARRAY_DISTINCT, BuiltInMethod.ARRAY_DISTINCT.method, 
NullPolicy.STRICT);
+  defineMethod(ARRAY_EXCEPT, BuiltInMethod.ARRAY_EXCEPT.method, 
NullPolicy.ANY);
+  defineMethod(ARRAY_INTERSECT, BuiltInMethod.ARRAY_INTERSECT.method, 
NullPolicy.ANY);
+  defineMethod(ARRAY_LENGTH, BuiltInMethod.COLLECTION_SIZE.method, 
NullPolicy.STRICT);
   defineMethod(ARRAY_MAX, BuiltInMethod.ARRAY_MAX.method, 
NullPolicy.STRICT);
   defineMethod(ARRAY_MIN, BuiltInMethod.ARRAY_MIN.method, 
NullPolicy.STRICT);
   defineMethod(ARRAY_REPEAT, BuiltInMethod.ARRAY_REPEAT.method, 
NullPolicy.NONE);
   defineMethod(ARRAY_REVERSE, BuiltInMethod.ARRAY_REVERSE.method, 
NullPolicy.STRICT);
   defineMethod(ARRAY_SIZE, BuiltInMethod.COLLECTION_SIZE.method, 
NullPolicy.STRICT);
+  defineMethod(ARRAY_UNION, BuiltInMethod.ARRAY_UNION.method, 
NullPolicy.ANY);
   defineMethod(MAP_ENTRIES, BuiltInMethod.MAP_ENTRIES.method, 
NullPolicy.STRICT);
   defineMethod(MAP_KEYS, BuiltInMethod.MAP_KEYS.method, NullPolicy.STRICT);
   defineMethod(MAP_VALUES, BuiltInMethod.MAP_VALUES.method, 
NullPolicy.STRICT);
diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java 
b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
index a3b92ee6fb..e1835b49d0 100644
--- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
+++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -3926,6 +3926,28 @@ public class SqlFunctions {
 return Collections.nCopies(numberOfElement, ele

[calcite] branch main updated: [CALCITE-5710] Add ARRAY_MAX, ARRAY_MIN function (enabled in Spark library)

2023-06-04 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new baa6b104f3 [CALCITE-5710] Add ARRAY_MAX, ARRAY_MIN function (enabled 
in Spark library)
baa6b104f3 is described below

commit baa6b104f33a851e419bd95e18acf3766aa19838
Author: yongen.ly 
AuthorDate: Thu May 18 16:51:44 2023 +0800

[CALCITE-5710] Add ARRAY_MAX, ARRAY_MIN function (enabled in Spark library)
---
 .../calcite/adapter/enumerable/RexImpTable.java|  4 +++
 .../org/apache/calcite/runtime/SqlFunctions.java   | 28 +++
 .../main/java/org/apache/calcite/sql/SqlKind.java  |  6 +
 .../calcite/sql/fun/SqlLibraryOperators.java   | 14 ++
 .../org/apache/calcite/sql/type/ReturnTypes.java   | 17 +++-
 .../apache/calcite/sql/type/SqlTypeTransforms.java |  5 ++--
 .../org/apache/calcite/util/BuiltInMethod.java |  2 ++
 site/_docs/reference.md|  2 ++
 .../org/apache/calcite/test/SqlOperatorTest.java   | 31 ++
 9 files changed, 106 insertions(+), 3 deletions(-)

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 925bbec92e..cad65ffa60 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
@@ -123,6 +123,8 @@ import static 
org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_CONCAT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_CONCAT_AGG;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_DISTINCT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_LENGTH;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_MAX;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_MIN;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_REPEAT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_REVERSE;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_SIZE;
@@ -691,6 +693,8 @@ public class RexImpTable {
   defineMethod(MEMBER_OF, BuiltInMethod.MEMBER_OF.method, NullPolicy.NONE);
   defineMethod(ARRAY_COMPACT, BuiltInMethod.ARRAY_COMPACT.method, 
NullPolicy.STRICT);
   defineMethod(ARRAY_DISTINCT, BuiltInMethod.ARRAY_DISTINCT.method, 
NullPolicy.STRICT);
+  defineMethod(ARRAY_MAX, BuiltInMethod.ARRAY_MAX.method, 
NullPolicy.STRICT);
+  defineMethod(ARRAY_MIN, BuiltInMethod.ARRAY_MIN.method, 
NullPolicy.STRICT);
   defineMethod(ARRAY_REPEAT, BuiltInMethod.ARRAY_REPEAT.method, 
NullPolicy.NONE);
   defineMethod(ARRAY_REVERSE, BuiltInMethod.ARRAY_REVERSE.method, 
NullPolicy.STRICT);
   defineMethod(ARRAY_SIZE, BuiltInMethod.COLLECTION_SIZE.method, 
NullPolicy.STRICT);
diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java 
b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
index 8c68768d1b..0db3921cca 100644
--- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
+++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -3886,6 +3886,34 @@ public class SqlFunctions {
 return new ArrayList<>(result);
   }
 
+  /** Support the ARRAY_MAX function. */
+  public static @Nullable > T 
arrayMax(
+  List list) {
+
+T max = null;
+for (int i = 0; i < list.size(); i++) {
+  T item = list.get(i);
+  if (item != null && (max == null || item.compareTo(max) > 0)) {
+max = item;
+  }
+}
+return max;
+  }
+
+  /** Support the ARRAY_MIN function. */
+  public static @Nullable > T 
arrayMin(
+  List list) {
+
+T min = null;
+for (int i = 0; i < list.size(); i++) {
+  T item = list.get(i);
+  if (item != null && (min == null || item.compareTo(min) < 0)) {
+min = item;
+  }
+}
+return min;
+  }
+
   /** Support the ARRAY_REPEAT function. */
   public static @Nullable List repeat(Object element, Object count) {
 if (count == null) {
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlKind.java 
b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
index b1f18b7136..7c2c698e1d 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlKind.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
@@ -686,6 +686,12 @@ public enum SqlKind {
   /** {@code ARRAY_DISTINCT} function (Spark semantics). */
   ARRAY_DISTINCT,
 
+  /** {@code ARRAY_MAX} function (Spark semantics). */
+  ARRAY_MAX,
+
+  /** {@code ARRAY_MIN} function (Spark semantics). */
+  ARRAY_MIN,
+
   /** {@code ARRAY_REPEAT} function (Spark semantics). */
   ARRAY_REPEAT,
 
diff --git 
a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibrary

[calcite] branch main updated: [CALCITE-5714] Add MAP_ENTRIES function (enabled in Spark library)

2023-06-02 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new c14e071590 [CALCITE-5714] Add MAP_ENTRIES function (enabled in Spark 
library)
c14e071590 is described below

commit c14e071590665a0ddda6c56cb95e2955fd8d5349
Author: yongen.ly 
AuthorDate: Fri May 19 17:37:09 2023 +0800

[CALCITE-5714] Add MAP_ENTRIES function (enabled in Spark library)
---
 .../calcite/adapter/enumerable/RexImpTable.java |  2 ++
 .../org/apache/calcite/runtime/SqlFunctions.java|  9 +
 .../main/java/org/apache/calcite/sql/SqlKind.java   |  3 +++
 .../apache/calcite/sql/fun/SqlLibraryOperators.java |  7 +++
 .../org/apache/calcite/sql/type/ReturnTypes.java| 21 +
 .../apache/calcite/sql/type/SqlTypeTransforms.java  | 11 +++
 .../org/apache/calcite/sql/type/SqlTypeUtil.java| 10 ++
 .../java/org/apache/calcite/util/BuiltInMethod.java |  1 +
 site/_docs/reference.md |  1 +
 .../org/apache/calcite/test/SqlOperatorTest.java| 14 ++
 10 files changed, 79 insertions(+)

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 620926cf34..925bbec92e 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
@@ -172,6 +172,7 @@ import static 
org.apache.calcite.sql.fun.SqlLibraryOperators.LOG;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.LOGICAL_AND;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.LOGICAL_OR;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.LPAD;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.MAP_ENTRIES;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.MAP_KEYS;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.MAP_VALUES;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.MAX_BY;
@@ -693,6 +694,7 @@ public class RexImpTable {
   defineMethod(ARRAY_REPEAT, BuiltInMethod.ARRAY_REPEAT.method, 
NullPolicy.NONE);
   defineMethod(ARRAY_REVERSE, BuiltInMethod.ARRAY_REVERSE.method, 
NullPolicy.STRICT);
   defineMethod(ARRAY_SIZE, BuiltInMethod.COLLECTION_SIZE.method, 
NullPolicy.STRICT);
+  defineMethod(MAP_ENTRIES, BuiltInMethod.MAP_ENTRIES.method, 
NullPolicy.STRICT);
   defineMethod(MAP_KEYS, BuiltInMethod.MAP_KEYS.method, NullPolicy.STRICT);
   defineMethod(MAP_VALUES, BuiltInMethod.MAP_VALUES.method, 
NullPolicy.STRICT);
   map.put(ARRAY_CONCAT, new ArrayConcatImplementor());
diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java 
b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
index e47a2d86f9..8c68768d1b 100644
--- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
+++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -3898,6 +3898,15 @@ public class SqlFunctions {
 return Collections.nCopies(numberOfElement, element);
   }
 
+  /** Support the MAP_ENTRIES function. */
+  public static List mapEntries(Map map) {
+final List result = new ArrayList(map.size());
+for (Map.Entry entry : map.entrySet()) {
+  result.add(Arrays.asList(entry.getKey(), entry.getValue()));
+}
+return result;
+  }
+
   /** Support the MAP_KEYS function. */
   public static List mapKeys(Map map) {
 return new ArrayList<>(map.keySet());
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlKind.java 
b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
index f8e10a41f4..b1f18b7136 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlKind.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
@@ -695,6 +695,9 @@ public enum SqlKind {
   /** {@code ARRAY_SIZE} function (Spark semantics). */
   ARRAY_SIZE,
 
+  /** {@code MAP_ENTRIES} function (Spark semantics). */
+  MAP_ENTRIES,
+
   /** {@code MAP_KEYS} function (Spark semantics). */
   MAP_KEYS,
 
diff --git 
a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java 
b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
index 476024dad5..bcc6e3fa1f 100644
--- a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
@@ -923,6 +923,13 @@ public abstract class SqlLibraryOperators {
   ReturnTypes.LEAST_RESTRICTIVE,
   OperandTypes.AT_LEAST_ONE_SAME_VARIADIC);
 
+  /** The "MAP_ENTRIES(map)" function. */
+  @LibraryOperator(libraries = {SPARK})
+  public static final SqlFunction MAP_ENTRIES =
+  SqlBasicFunction.create(SqlKind.MAP_ENTRIES,
+  ReturnTypes.TO_MAP_

[calcite] branch main updated: [CALCITE-5723] Oracle dialect generates SQL that cannot be recognized by lower version Oracle Server(<12) when unparsing OffsetFetch

2023-06-01 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new d98f96d830 [CALCITE-5723] Oracle dialect generates SQL that cannot be 
recognized by lower version Oracle Server(<12) when unparsing OffsetFetch
d98f96d830 is described below

commit d98f96d830e2114aff4166476ebedf6bd82a4c4e
Author: ILuffZhe 
AuthorDate: Thu May 25 20:41:28 2023 +0800

[CALCITE-5723] Oracle dialect generates SQL that cannot be recognized by 
lower version Oracle Server(<12) when unparsing OffsetFetch
---
 .../calcite/sql/dialect/OracleSqlDialect.java  | 12 +
 .../calcite/rel/rel2sql/RelToSqlConverterTest.java | 29 +-
 2 files changed, 40 insertions(+), 1 deletion(-)

diff --git 
a/core/src/main/java/org/apache/calcite/sql/dialect/OracleSqlDialect.java 
b/core/src/main/java/org/apache/calcite/sql/dialect/OracleSqlDialect.java
index fbd9aef93b..e99ab2a44f 100644
--- a/core/src/main/java/org/apache/calcite/sql/dialect/OracleSqlDialect.java
+++ b/core/src/main/java/org/apache/calcite/sql/dialect/OracleSqlDialect.java
@@ -69,9 +69,12 @@ public class OracleSqlDialect extends SqlDialect {
 
   public static final SqlDialect DEFAULT = new 
OracleSqlDialect(DEFAULT_CONTEXT);
 
+  private final int majorVersion;
+
   /** Creates an OracleSqlDialect. */
   public OracleSqlDialect(Context context) {
 super(context);
+this.majorVersion = context.databaseMajorVersion();
   }
 
   @Override public boolean supportsApproxCountDistinct() {
@@ -188,4 +191,13 @@ public class OracleSqlDialect extends SqlDialect {
   }
 }
   }
+
+  @Override public void unparseOffsetFetch(SqlWriter writer, @Nullable SqlNode 
offset,
+   @Nullable SqlNode fetch) {
+// majorVersion in SqlDialect.EMPTY_CONTEXT is -1 by default
+if (this.majorVersion != -1 && this.majorVersion < 12) {
+  throw new RuntimeException("Lower Oracle version(<12) doesn't support 
offset/fetch syntax!");
+}
+super.unparseOffsetFetch(writer, offset, fetch);
+  }
 }
diff --git 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index dc6658162c..412ddb4c16 100644
--- 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++ 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -6866,6 +6866,22 @@ class RelToSqlConverterTest {
 .withCalcite().ok(expectedCalciteX);
   }
 
+  /** Test case for
+   * https://issues.apache.org/jira/browse/CALCITE-5723;>[CALCITE-5723]
+   * Oracle dialect generates SQL that cannot be recognized by lower version
+   * Oracle Server(<12) when unparsing OffsetFetch. */
+  @Test void testFetchOffsetOracle() {
+String query = "SELECT \"department_id\" FROM \"employee\" LIMIT 2 OFFSET 
1";
+String expected = "SELECT \"department_id\"\n"
++ "FROM \"foodmart\".\"employee\"\n"
++ "OFFSET 1 ROWS\n"
++ "FETCH NEXT 2 ROWS ONLY";
+sql(query)
+.withOracle().ok(expected)
+.withOracle(19).ok(expected)
+.withOracle(11).throws_("Lower Oracle version(<12) doesn't support 
offset/fetch syntax!");
+  }
+
   /** Test case for
* https://issues.apache.org/jira/browse/CALCITE-5265;>[CALCITE-5265]
* JDBC adapter sometimes adds unnecessary parentheses around SELECT in 
INSERT. */
@@ -7122,7 +7138,18 @@ class RelToSqlConverterTest {
 }
 
 Sql withOracle() {
-  return dialect(DatabaseProduct.ORACLE.getDialect());
+  return withOracle(12);
+}
+
+Sql withOracle(int majorVersion) {
+  final SqlDialect oracleDialect = DatabaseProduct.ORACLE.getDialect();
+  return dialect(
+  new OracleSqlDialect(OracleSqlDialect.DEFAULT_CONTEXT
+  .withDatabaseProduct(DatabaseProduct.ORACLE)
+  .withDatabaseMajorVersion(majorVersion)
+  .withIdentifierQuoteString(oracleDialect.quoteIdentifier("")
+  .substring(0, 1))
+  .withNullCollation(oracleDialect.getNullCollation(;
 }
 
 Sql withPostgresql() {



[calcite] branch main updated: [CALCITE-5695] Add MAP_KEYS, MAP_VALUES function (enabled in Spark library)

2023-05-20 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 9c33d7aeef [CALCITE-5695] Add MAP_KEYS, MAP_VALUES function (enabled 
in Spark library)
9c33d7aeef is described below

commit 9c33d7aeefe082bf5f7be617ef231e1285418a6c
Author: yongen.ly 
AuthorDate: Wed May 17 10:35:11 2023 +0800

[CALCITE-5695] Add MAP_KEYS, MAP_VALUES function (enabled in Spark library)
---
 .../calcite/adapter/enumerable/RexImpTable.java|  4 
 .../org/apache/calcite/runtime/SqlFunctions.java   | 10 
 .../main/java/org/apache/calcite/sql/SqlKind.java  |  6 +
 .../calcite/sql/fun/SqlLibraryOperators.java   | 14 +++
 .../org/apache/calcite/sql/type/OperandTypes.java  |  3 +++
 .../org/apache/calcite/sql/type/ReturnTypes.java   | 24 +++
 .../apache/calcite/sql/type/SqlTypeTransforms.java | 28 ++
 .../org/apache/calcite/util/BuiltInMethod.java |  2 ++
 site/_docs/reference.md|  2 ++
 .../org/apache/calcite/test/SqlOperatorTest.java   | 28 ++
 10 files changed, 121 insertions(+)

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 1f833ad0a4..0d5090a93a 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
@@ -165,6 +165,8 @@ import static 
org.apache.calcite.sql.fun.SqlLibraryOperators.LOG;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.LOGICAL_AND;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.LOGICAL_OR;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.LPAD;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.MAP_KEYS;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.MAP_VALUES;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.MAX_BY;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.MD5;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.MIN_BY;
@@ -680,6 +682,8 @@ public class RexImpTable {
   defineMethod(ARRAY_REPEAT, BuiltInMethod.ARRAY_REPEAT.method, 
NullPolicy.NONE);
   defineMethod(ARRAY_REVERSE, BuiltInMethod.ARRAY_REVERSE.method, 
NullPolicy.STRICT);
   defineMethod(ARRAY_SIZE, BuiltInMethod.COLLECTION_SIZE.method, 
NullPolicy.STRICT);
+  defineMethod(MAP_KEYS, BuiltInMethod.MAP_KEYS.method, NullPolicy.STRICT);
+  defineMethod(MAP_VALUES, BuiltInMethod.MAP_VALUES.method, 
NullPolicy.STRICT);
   map.put(ARRAY_CONCAT, new ArrayConcatImplementor());
   final MethodImplementor isEmptyImplementor =
   new MethodImplementor(BuiltInMethod.IS_EMPTY.method, NullPolicy.NONE,
diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java 
b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
index 29ba098dda..1fc339fc07 100644
--- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
+++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -3831,6 +3831,16 @@ public class SqlFunctions {
 return Collections.nCopies(numberOfElement, element);
   }
 
+  /** Support the MAP_KEYS function. */
+  public static List mapKeys(Map map) {
+return new ArrayList<>(map.keySet());
+  }
+
+  /** Support the MAP_VALUES function. */
+  public static List mapValues(Map map) {
+return new ArrayList<>(map.values());
+  }
+
   /** Support the SLICE function. */
   public static List slice(List list) {
 List result = new ArrayList(list.size());
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlKind.java 
b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
index 9104af350c..d52322bd2f 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlKind.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
@@ -692,6 +692,12 @@ public enum SqlKind {
   /** {@code ARRAY_SIZE} function (Spark semantics). */
   ARRAY_SIZE,
 
+  /** {@code MAP_KEYS} function (Spark semantics). */
+  MAP_KEYS,
+
+  /** {@code MAP_VALUES} function (Spark semantics). */
+  MAP_VALUES,
+
   /** {@code REVERSE} function (SQL Server, MySQL). */
   REVERSE,
 
diff --git 
a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java 
b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
index fe3269d18a..a1952900eb 100644
--- a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
@@ -888,6 +888,20 @@ public abstract class SqlLibraryOperators {
   ReturnTypes.LEAST_RESTRICTIVE,
   OperandTypes.AT_LEAST_ONE_SAME_VARIADIC);
 
+  /** The "MAP_KEYS(map)" function. 

[calcite] branch main updated: [CALCITE-5700] Add ARRAY_SIZE, ARRAY_REPEAT function (enabled in Spark library).

2023-05-16 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 48f51ea5d9 [CALCITE-5700] Add ARRAY_SIZE, ARRAY_REPEAT function 
(enabled in Spark library).
48f51ea5d9 is described below

commit 48f51ea5d9443fb11e070eea094bf551c8ff22fc
Author: yongen.ly 
AuthorDate: Fri May 12 15:06:00 2023 +0800

[CALCITE-5700] Add ARRAY_SIZE, ARRAY_REPEAT function (enabled in Spark 
library).
---
 .../calcite/adapter/enumerable/RexImpTable.java|  4 +++
 .../org/apache/calcite/runtime/SqlFunctions.java   | 12 
 .../main/java/org/apache/calcite/sql/SqlKind.java  |  6 
 .../calcite/sql/fun/SqlLibraryOperators.java   | 16 ++
 .../org/apache/calcite/util/BuiltInMethod.java |  1 +
 site/_docs/reference.md|  2 ++
 .../org/apache/calcite/test/SqlOperatorTest.java   | 36 ++
 7 files changed, 77 insertions(+)

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 bf835c0d18..16a9df8632 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
@@ -118,7 +118,9 @@ import static 
org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_CONCAT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_CONCAT_AGG;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_DISTINCT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_LENGTH;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_REPEAT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_REVERSE;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_SIZE;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.BOOL_AND;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.BOOL_OR;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.CHAR;
@@ -671,7 +673,9 @@ public class RexImpTable {
   defineMethod(STRUCT_ACCESS, BuiltInMethod.STRUCT_ACCESS.method, 
NullPolicy.ANY);
   defineMethod(MEMBER_OF, BuiltInMethod.MEMBER_OF.method, NullPolicy.NONE);
   defineMethod(ARRAY_DISTINCT, BuiltInMethod.ARRAY_DISTINCT.method, 
NullPolicy.STRICT);
+  defineMethod(ARRAY_REPEAT, BuiltInMethod.ARRAY_REPEAT.method, 
NullPolicy.NONE);
   defineMethod(ARRAY_REVERSE, BuiltInMethod.ARRAY_REVERSE.method, 
NullPolicy.STRICT);
+  defineMethod(ARRAY_SIZE, BuiltInMethod.COLLECTION_SIZE.method, 
NullPolicy.STRICT);
   map.put(ARRAY_CONCAT, new ArrayConcatImplementor());
   final MethodImplementor isEmptyImplementor =
   new MethodImplementor(BuiltInMethod.IS_EMPTY.method, NullPolicy.NONE,
diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java 
b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
index 75ced53c9a..52c2da6878 100644
--- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
+++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -3797,6 +3797,18 @@ public class SqlFunctions {
 return new ArrayList<>(result);
   }
 
+  /** Support the ARRAY_REPEAT function. */
+  public static @Nullable List repeat(Object element, Object count) {
+if (count == null) {
+  return null;
+}
+int numberOfElement = (int) count;
+if (numberOfElement < 0) {
+  numberOfElement = 0;
+}
+return Collections.nCopies(numberOfElement, element);
+  }
+
   /** Support the SLICE function. */
   public static List slice(List list) {
 List result = new ArrayList(list.size());
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlKind.java 
b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
index 44cd4cd02d..9104af350c 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlKind.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
@@ -683,9 +683,15 @@ public enum SqlKind {
   /** {@code ARRAY_DISTINCT} function (Spark semantics). */
   ARRAY_DISTINCT,
 
+  /** {@code ARRAY_REPEAT} function (Spark semantics). */
+  ARRAY_REPEAT,
+
   /** {@code ARRAY_REVERSE} function (BigQuery semantics). */
   ARRAY_REVERSE,
 
+  /** {@code ARRAY_SIZE} function (Spark semantics). */
+  ARRAY_SIZE,
+
   /** {@code REVERSE} function (SQL Server, MySQL). */
   REVERSE,
 
diff --git 
a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java 
b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
index 7eedae2918..4ae48490e5 100644
--- a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
@@ -858,6 +858,15 @@ public abstract class SqlLibrary

[calcite] branch main updated: [CALCITE-5657] Add ARRAY_DISTINCT function (enabled in Spark library).

2023-05-09 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new d68656a251 [CALCITE-5657] Add ARRAY_DISTINCT function (enabled in 
Spark library).
d68656a251 is described below

commit d68656a251b08c923101d881ebeea37601887229
Author: yongen.ly 
AuthorDate: Wed Apr 19 00:26:04 2023 +0800

[CALCITE-5657] Add ARRAY_DISTINCT function (enabled in Spark library).
---
 .../org/apache/calcite/adapter/enumerable/RexImpTable.java   |  2 ++
 .../main/java/org/apache/calcite/runtime/SqlFunctions.java   |  7 +++
 core/src/main/java/org/apache/calcite/sql/SqlKind.java   |  3 +++
 .../java/org/apache/calcite/sql/fun/SqlLibraryOperators.java |  7 +++
 .../src/main/java/org/apache/calcite/util/BuiltInMethod.java |  1 +
 site/_docs/reference.md  |  1 +
 .../main/java/org/apache/calcite/test/SqlOperatorTest.java   | 12 
 7 files changed, 33 insertions(+)

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 9ad452277c..3c2ecf8edc 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
@@ -115,6 +115,7 @@ import static 
org.apache.calcite.sql.fun.SqlInternalOperators.THROW_UNLESS;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_AGG;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_CONCAT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_CONCAT_AGG;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_DISTINCT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_LENGTH;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_REVERSE;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.BOOL_AND;
@@ -668,6 +669,7 @@ public class RexImpTable {
   defineMethod(ELEMENT, BuiltInMethod.ELEMENT.method, NullPolicy.STRICT);
   defineMethod(STRUCT_ACCESS, BuiltInMethod.STRUCT_ACCESS.method, 
NullPolicy.ANY);
   defineMethod(MEMBER_OF, BuiltInMethod.MEMBER_OF.method, NullPolicy.NONE);
+  defineMethod(ARRAY_DISTINCT, BuiltInMethod.ARRAY_DISTINCT.method, 
NullPolicy.STRICT);
   defineMethod(ARRAY_REVERSE, BuiltInMethod.ARRAY_REVERSE.method, 
NullPolicy.STRICT);
   map.put(ARRAY_CONCAT, new ArrayConcatImplementor());
   final MethodImplementor isEmptyImplementor =
diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java 
b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
index 83a511ff12..75ced53c9a 100644
--- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
+++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -92,6 +92,7 @@ import java.util.Comparator;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Locale;
@@ -3790,6 +3791,12 @@ public class SqlFunctions {
 return atomic;
   }
 
+  /** Support the ARRAY_DISTINCT function. */
+  public static List distinct(List list) {
+Set result = new LinkedHashSet<>(list);
+return new ArrayList<>(result);
+  }
+
   /** Support the SLICE function. */
   public static List slice(List list) {
 List result = new ArrayList(list.size());
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlKind.java 
b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
index 287b7d1a47..44cd4cd02d 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlKind.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
@@ -680,6 +680,9 @@ public enum SqlKind {
   /** {@code ARRAY_CONCAT} function (BigQuery semantics). */
   ARRAY_CONCAT,
 
+  /** {@code ARRAY_DISTINCT} function (Spark semantics). */
+  ARRAY_DISTINCT,
+
   /** {@code ARRAY_REVERSE} function (BigQuery semantics). */
   ARRAY_REVERSE,
 
diff --git 
a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java 
b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
index b75dcab0fb..a2f1254147 100644
--- a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
@@ -825,6 +825,13 @@ public abstract class SqlLibraryOperators {
   .withOperandTypeInference(InferTypes.RETURN_TYPE)
   .withKind(SqlKind.CONCAT2);
 
+  /** The "ARRAY_DISTINCT(array)" function. */
+  @LibraryOperator(libraries = {SPARK})
+  public static final SqlFunction ARRAY_DISTINCT =
+  SqlBasicFunction.create(SqlKind.ARRAY_DISTINCT,
+  ReturnTypes.ARG0_NULLABLE,
+  

[calcite] branch main updated: [CALCITE-5605] Add BigQuery as supported library for CHR

2023-03-30 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new a10b858664 [CALCITE-5605] Add BigQuery as supported library for CHR
a10b858664 is described below

commit a10b858664250f9005e32857e57a18b8c48ec931
Author: Tanner Clary 
AuthorDate: Tue Mar 21 11:52:55 2023 -0700

[CALCITE-5605] Add BigQuery as supported library for CHR
---
 .../java/org/apache/calcite/sql/fun/SqlLibraryOperators.java |  2 +-
 site/_docs/reference.md  |  2 +-
 .../main/java/org/apache/calcite/test/SqlOperatorTest.java   | 12 
 3 files changed, 10 insertions(+), 6 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java 
b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
index f00c4c5f9d..af3aa62f27 100644
--- a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
@@ -1133,7 +1133,7 @@ public abstract class SqlLibraryOperators {
 
   /** The "CHR(n)" function; returns the character whose UTF-8 code is
* {@code n}. */
-  @LibraryOperator(libraries = {ORACLE, POSTGRESQL})
+  @LibraryOperator(libraries = {BIG_QUERY, ORACLE, POSTGRESQL})
   public static final SqlFunction CHR =
   SqlBasicFunction.create("CHR",
   ReturnTypes.CHAR,
diff --git a/site/_docs/reference.md b/site/_docs/reference.md
index b784755401..c1a441db80 100644
--- a/site/_docs/reference.md
+++ b/site/_docs/reference.md
@@ -2647,7 +2647,7 @@ BigQuery's type system uses confusingly different names 
for types and functions:
 | b | ARRAY_LENGTH(array)| Synonym for 
`CARDINALITY`
 | b | ARRAY_REVERSE(array)   | Reverses elements of 
*array*
 | m s | CHAR(integer)| Returns the character 
whose ASCII code is *integer* % 256, or null if *integer*  0
-| o p | CHR(integer) | Returns the character 
whose UTF-8 code is *integer*
+| b o p | CHR(integer)   | Returns the character 
whose UTF-8 code is *integer*
 | b o | COSH(numeric)| Returns the hyperbolic 
cosine of *numeric*
 | o | CONCAT(string, string) | Concatenates two strings
 | b m p | CONCAT(string [, string ]*)| Concatenates two or 
more strings
diff --git a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java 
b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
index 50524b8de1..8cd4bff390 100644
--- a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
+++ b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
@@ -1747,12 +1747,16 @@ public class SqlOperatorTest {
   @Test void testChr() {
 final SqlOperatorFixture f0 = fixture()
 .setFor(SqlLibraryOperators.CHR, VM_FENNEL, VM_JAVA);
-final SqlOperatorFixture f = f0.withLibrary(SqlLibrary.ORACLE);
-f.checkScalar("chr(97)", "a", "CHAR(1) NOT NULL");
-f.checkScalar("chr(48)", "0", "CHAR(1) NOT NULL");
-f.checkScalar("chr(0)", String.valueOf('\u'), "CHAR(1) NOT NULL");
 f0.checkFails("^chr(97.1)^",
 "No match found for function signature CHR\\(\\)", false);
+final Consumer consumer = f -> {
+  f.checkScalar("chr(97)", "a", "CHAR(1) NOT NULL");
+  f.checkScalar("chr(48)", "0", "CHAR(1) NOT NULL");
+  f.checkScalar("chr(0)", String.valueOf('\u'), "CHAR(1) NOT NULL");
+  f.checkNull("chr(null)");
+};
+f0.forEachLibrary(list(SqlLibrary.BIG_QUERY, SqlLibrary.ORACLE, 
SqlLibrary.POSTGRESQL),
+consumer);
   }
 
   @Test void testSelect() {



[calcite] branch main updated: [MINOR][JAVADOC] Updated javadoc instructions for RelColumnOrigin#isDerived method

2023-03-19 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 34b96765fc [MINOR][JAVADOC] Updated javadoc instructions for 
RelColumnOrigin#isDerived method
34b96765fc is described below

commit 34b96765fc2447bb3028942bbef2c056522580f7
Author: zhujiang.97 <374879...@qq.com>
AuthorDate: Fri Mar 17 20:11:17 2023 +0800

[MINOR][JAVADOC] Updated javadoc instructions for RelColumnOrigin#isDerived 
method
---
 core/src/main/java/org/apache/calcite/rel/metadata/RelColumnOrigin.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git 
a/core/src/main/java/org/apache/calcite/rel/metadata/RelColumnOrigin.java 
b/core/src/main/java/org/apache/calcite/rel/metadata/RelColumnOrigin.java
index 28aa144d5a..974501b50b 100644
--- a/core/src/main/java/org/apache/calcite/rel/metadata/RelColumnOrigin.java
+++ b/core/src/main/java/org/apache/calcite/rel/metadata/RelColumnOrigin.java
@@ -62,7 +62,7 @@ public class RelColumnOrigin {
   /**
* Consider the query select a+b as c, d as e from t. The
* output column c has two origins (a and b), both of them derived. The
-   * output column d as one origin (c), which is not derived.
+   * output column e has one origin (d), which is not derived.
*
* @return false if value taken directly from column in origin table; true
* otherwise



[calcite] branch main updated: [CALCITE-5518] RelToSql converter generates invalid order of ROLLUP fields

2023-02-25 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 90599a6e9b [CALCITE-5518] RelToSql converter generates invalid order 
of ROLLUP fields
90599a6e9b is described below

commit 90599a6e9b0143eb3d9175af7b5ff374e1b95252
Author: xiejiajun 
AuthorDate: Sun Feb 12 15:34:14 2023 +0800

[CALCITE-5518] RelToSql converter generates invalid order of ROLLUP fields
---
 .../calcite/rel/rel2sql/RelToSqlConverter.java |  7 +-
 .../calcite/rel/rel2sql/RelToSqlConverterTest.java | 28 ++
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git 
a/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java 
b/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java
index b8747c1a35..efcdc0c93f 100644
--- a/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java
+++ b/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java
@@ -620,8 +620,13 @@ public class RelToSqlConverter extends SqlImplementor
   // a singleton CUBE and ROLLUP are the same but we prefer ROLLUP;
   // fall through
 case ROLLUP:
+  final List rollupBits = 
Aggregate.Group.getRollup(aggregate.groupSets);
+  final List rollupKeys = rollupBits
+  .stream()
+  .map(bit -> builder.context.field(bit))
+  .collect(Collectors.toList());
   return ImmutableList.of(
-  SqlStdOperatorTable.ROLLUP.createCall(SqlParserPos.ZERO, groupKeys));
+  SqlStdOperatorTable.ROLLUP.createCall(SqlParserPos.ZERO, 
rollupKeys));
 default:
 case OTHER:
   // Make sure that the group sets contains all bits.
diff --git 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index 83f1fd13d1..7016a74b84 100644
--- 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++ 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -707,6 +707,34 @@ class RelToSqlConverterTest {
 .withMysql().ok(expectedMysql);
   }
 
+  /** Test case for
+   * https://issues.apache.org/jira/browse/CALCITE-5518;>[CALCITE-5518]
+   * RelToSql converter generates invalid order of ROLLUP fields.
+   */
+  @Test void testGroupingSetsRollupNonNaturalOrder() {
+final String query1 = "select \"product_class_id\", \"brand_name\"\n"
++ "from \"product\"\n"
++ "group by GROUPING SETS ((\"product_class_id\", \"brand_name\"),"
++ " (\"brand_name\"), ())\n";
+final String expected1 = "SELECT \"product_class_id\", \"brand_name\"\n"
++ "FROM \"foodmart\".\"product\"\n"
++ "GROUP BY ROLLUP(\"brand_name\", \"product_class_id\")";
+sql(query1)
+.withPostgresql().ok(expected1);
+
+final String query2 = "select \"product_class_id\", \"brand_name\", 
\"product_id\"\n"
++ "from \"product\"\n"
++ "group by GROUPING SETS ("
++ " (\"product_class_id\", \"brand_name\", \"product_id\"),"
++ " (\"product_class_id\", \"brand_name\"),"
++ " (\"brand_name\"), ())\n";
+final String expected2 = "SELECT \"product_class_id\", \"brand_name\", 
\"product_id\"\n"
++ "FROM \"foodmart\".\"product\"\n"
++ "GROUP BY ROLLUP(\"brand_name\", \"product_class_id\", 
\"product_id\")";
+sql(query2)
+.withPostgresql().ok(expected2);
+  }
+
   /** Tests a query with GROUP BY and a sub-query which is also with GROUP BY.
* If we flatten sub-queries, the number of rows going into AVG becomes
* incorrect. */



[calcite] branch main updated: Site: Add Jiajun Xie as committer

2023-02-12 Thread jiajunxie
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/main by this push:
 new 82fb4e96f0 Site: Add Jiajun Xie as committer
82fb4e96f0 is described below

commit 82fb4e96f06e7a24473e527a2551f3bb5de343ff
Author: xiejiajun 
AuthorDate: Sat Feb 11 15:43:07 2023 +0800

Site: Add Jiajun Xie as committer
---
 site/_data/contributors.yml | 5 +
 1 file changed, 5 insertions(+)

diff --git a/site/_data/contributors.yml b/site/_data/contributors.yml
index 01e2e7ab0c..7feb12789c 100644
--- a/site/_data/contributors.yml
+++ b/site/_data/contributors.yml
@@ -142,6 +142,11 @@
   githubId: jcamachor
   org: Hortonworks
   role: PMC
+- name: Jiajun Xie
+  apacheId: jiajunxie
+  githubId: jiajunbernoulli
+  org: ByteDance
+  role: Committer
 - name: Jinfeng Ni
   apacheId: jni
   githubId: jinfengni