|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.ppl.calcite; |
| 7 | + |
| 8 | +import org.apache.calcite.rel.RelNode; |
| 9 | +import org.apache.calcite.test.CalciteAssert; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +public class CalcitePPLBinTest extends CalcitePPLAbstractTest { |
| 13 | + |
| 14 | + public CalcitePPLBinTest() { |
| 15 | + super(CalciteAssert.SchemaSpec.SCOTT_WITH_TEMPORAL); |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + public void testBinWithSpan() { |
| 20 | + String ppl = "source=EMP | bin SAL span=1000"; |
| 21 | + RelNode root = getRelNode(ppl); |
| 22 | + |
| 23 | + String expectedLogical = |
| 24 | + "LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], " |
| 25 | + + "SAL=[SPAN_BUCKET($5, 1000)], COMM=[$6], DEPTNO=[$7])\n" |
| 26 | + + " LogicalTableScan(table=[[scott, EMP]])\n"; |
| 27 | + verifyLogical(root, expectedLogical); |
| 28 | + |
| 29 | + String expectedSparkSql = |
| 30 | + "SELECT `EMPNO`, `ENAME`, `JOB`, `MGR`, `HIREDATE`, `SPAN_BUCKET`(`SAL`, 1000) `SAL`," |
| 31 | + + " `COMM`, `DEPTNO`\n" |
| 32 | + + "FROM `scott`.`EMP`"; |
| 33 | + verifyPPLToSparkSQL(root, expectedSparkSql); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testBinWithBins() { |
| 38 | + String ppl = "source=EMP | bin SAL bins=10"; |
| 39 | + RelNode root = getRelNode(ppl); |
| 40 | + |
| 41 | + // Note: WIDTH_BUCKET uses window functions without ROWS UNBOUNDED PRECEDING in the actual |
| 42 | + // output |
| 43 | + verifyLogical( |
| 44 | + root, |
| 45 | + "LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], " |
| 46 | + + "SAL=[WIDTH_BUCKET($5, 10, " |
| 47 | + + "-(MAX($5) OVER (), MIN($5) OVER ()), " |
| 48 | + + "MAX($5) OVER ())], COMM=[$6], DEPTNO=[$7])\n" |
| 49 | + + " LogicalTableScan(table=[[scott, EMP]])\n"); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testBinWithMinspan() { |
| 54 | + String ppl = "source=EMP | bin SAL minspan=100"; |
| 55 | + RelNode root = getRelNode(ppl); |
| 56 | + |
| 57 | + // Note: MINSPAN_BUCKET converts the minspan to DOUBLE and uses window functions without ROWS |
| 58 | + // clause |
| 59 | + verifyLogical( |
| 60 | + root, |
| 61 | + "LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], " |
| 62 | + + "SAL=[MINSPAN_BUCKET($5, 100.0E0:DOUBLE, " |
| 63 | + + "-(MAX($5) OVER (), MIN($5) OVER ()), " |
| 64 | + + "MAX($5) OVER ())], COMM=[$6], DEPTNO=[$7])\n" |
| 65 | + + " LogicalTableScan(table=[[scott, EMP]])\n"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testBinWithStartEnd() { |
| 70 | + String ppl = "source=EMP | bin SAL start=1000 end=5000"; |
| 71 | + RelNode root = getRelNode(ppl); |
| 72 | + |
| 73 | + // Note: RANGE_BUCKET uses window functions without ROWS UNBOUNDED PRECEDING |
| 74 | + verifyLogical( |
| 75 | + root, |
| 76 | + "LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], " |
| 77 | + + "SAL=[RANGE_BUCKET($5, " |
| 78 | + + "MIN($5) OVER (), MAX($5) OVER (), " |
| 79 | + + "1000, 5000)], COMM=[$6], DEPTNO=[$7])\n" |
| 80 | + + " LogicalTableScan(table=[[scott, EMP]])\n"); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testBinWithTimeSpan() { |
| 85 | + String ppl = "source=products_temporal | bin SYS_START span=1h"; |
| 86 | + RelNode root = getRelNode(ppl); |
| 87 | + |
| 88 | + // Time span binning generates FROM_UNIXTIME expression |
| 89 | + verifyLogical( |
| 90 | + root, |
| 91 | + "LogicalProject(ID=[$0], SUPPLIER=[$1], " |
| 92 | + + "SYS_START=[FROM_UNIXTIME(*(FLOOR(/(/(UNIX_TIMESTAMP($2), 3600), 1)), 3600))], " |
| 93 | + + "SYS_END=[$3])\n" |
| 94 | + + " LogicalTableScan(table=[[scott, products_temporal]])\n"); |
| 95 | + |
| 96 | + verifyPPLToSparkSQL( |
| 97 | + root, |
| 98 | + "SELECT `ID`, `SUPPLIER`, `FROM_UNIXTIME`(FLOOR(`UNIX_TIMESTAMP`(`SYS_START`) / 3600 / 1) *" |
| 99 | + + " 3600) `SYS_START`, `SYS_END`\n" |
| 100 | + + "FROM `scott`.`products_temporal`"); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testBinWithAligntime() { |
| 105 | + String ppl = "source=products_temporal | bin SYS_START span=1h aligntime=earliest"; |
| 106 | + RelNode root = getRelNode(ppl); |
| 107 | + |
| 108 | + // Time span binning with aligntime generates the same expression as without aligntime for this |
| 109 | + // case |
| 110 | + verifyLogical( |
| 111 | + root, |
| 112 | + "LogicalProject(ID=[$0], SUPPLIER=[$1], " |
| 113 | + + "SYS_START=[FROM_UNIXTIME(*(FLOOR(/(/(UNIX_TIMESTAMP($2), 3600), 1)), 3600))], " |
| 114 | + + "SYS_END=[$3])\n" |
| 115 | + + " LogicalTableScan(table=[[scott, products_temporal]])\n"); |
| 116 | + |
| 117 | + verifyPPLToSparkSQL( |
| 118 | + root, |
| 119 | + "SELECT `ID`, `SUPPLIER`, `FROM_UNIXTIME`(FLOOR(`UNIX_TIMESTAMP`(`SYS_START`) / 3600 / 1) *" |
| 120 | + + " 3600) `SYS_START`, `SYS_END`\n" |
| 121 | + + "FROM `scott`.`products_temporal`"); |
| 122 | + } |
| 123 | +} |
0 commit comments