Search in sources :

Example 1 with TruncateOperatorConversion

use of org.apache.druid.sql.calcite.expression.builtin.TruncateOperatorConversion in project druid by druid-io.

the class ExpressionsTest method testTruncate.

@Test
public void testTruncate() {
    final SqlFunction truncateFunction = new TruncateOperatorConversion().calciteOperator();
    testHelper.testExpression(truncateFunction, testHelper.makeInputRef("a"), DruidExpression.ofExpression(ColumnType.LONG, (args) -> "(cast(cast(" + args.get(0).getExpression() + " * 1,'long'),'double') / 1)", ImmutableList.of(DruidExpression.ofColumn(ColumnType.LONG, "a"))), 10.0);
    testHelper.testExpression(truncateFunction, testHelper.makeInputRef("x"), DruidExpression.ofExpression(ColumnType.FLOAT, (args) -> "(cast(cast(" + args.get(0).getExpression() + " * 1,'long'),'double') / 1)", ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "x"))), 2.0);
    testHelper.testExpression(truncateFunction, testHelper.makeInputRef("y"), DruidExpression.ofExpression(ColumnType.LONG, (args) -> "(cast(cast(" + args.get(0).getExpression() + " * 1,'long'),'double') / 1)", ImmutableList.of(DruidExpression.ofColumn(ColumnType.LONG, "y"))), 3.0);
    testHelper.testExpression(truncateFunction, testHelper.makeInputRef("z"), DruidExpression.ofExpression(ColumnType.FLOAT, (args) -> "(cast(cast(" + args.get(0).getExpression() + " * 1,'long'),'double') / 1)", ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "z"))), -2.0);
    testHelper.testExpressionString(truncateFunction, ImmutableList.of(testHelper.makeInputRef("x"), testHelper.makeLiteral(1)), DruidExpression.ofExpression(ColumnType.FLOAT, (args) -> "(cast(cast(" + args.get(0).getExpression() + " * 10.0,'long'),'double') / 10.0)", ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "x"), DruidExpression.ofLiteral(ColumnType.LONG, DruidExpression.numberLiteral(1)))), 2.2);
    testHelper.testExpressionString(truncateFunction, ImmutableList.of(testHelper.makeInputRef("z"), testHelper.makeLiteral(1)), DruidExpression.ofExpression(ColumnType.FLOAT, (args) -> "(cast(cast(" + args.get(0).getExpression() + " * 10.0,'long'),'double') / 10.0)", ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "z"), DruidExpression.ofLiteral(ColumnType.LONG, DruidExpression.numberLiteral(1)))), -2.2);
    testHelper.testExpressionString(truncateFunction, ImmutableList.of(testHelper.makeInputRef("b"), testHelper.makeLiteral(-1)), DruidExpression.ofExpression(ColumnType.LONG, (args) -> "(cast(cast(" + args.get(0).getExpression() + " * 0.1,'long'),'double') / 0.1)", ImmutableList.of(DruidExpression.ofColumn(ColumnType.LONG, "b"), DruidExpression.ofLiteral(ColumnType.LONG, DruidExpression.numberLiteral(-1)))), 20.0);
    testHelper.testExpressionString(truncateFunction, ImmutableList.of(testHelper.makeInputRef("z"), testHelper.makeLiteral(-1)), DruidExpression.ofExpression(ColumnType.FLOAT, (args) -> "(cast(cast(" + args.get(0).getExpression() + " * 0.1,'long'),'double') / 0.1)", ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "z"), DruidExpression.ofLiteral(ColumnType.LONG, DruidExpression.numberLiteral(-1)))), 0.0);
}
Also used : RPadOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.RPadOperatorConversion) SqlTrimFunction(org.apache.calcite.sql.fun.SqlTrimFunction) TimeUnit(org.apache.calcite.avatica.util.TimeUnit) ReverseOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.ReverseOperatorConversion) BigDecimal(java.math.BigDecimal) SearchQueryDimFilter(org.apache.druid.query.filter.SearchQueryDimFilter) TimeParseOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.TimeParseOperatorConversion) Map(java.util.Map) LeftOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.LeftOperatorConversion) RoundOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion) IAE(org.apache.druid.java.util.common.IAE) ContainsOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.ContainsOperatorConversion) TimeFloorOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.TimeFloorOperatorConversion) StringFormatOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.StringFormatOperatorConversion) DateTimes(org.apache.druid.java.util.common.DateTimes) RegexDimFilter(org.apache.druid.query.filter.RegexDimFilter) ImmutableMap(com.google.common.collect.ImmutableMap) RegexpExtractOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.RegexpExtractOperatorConversion) TestExprMacroTable(org.apache.druid.query.expression.TestExprMacroTable) RegexDimExtractionFn(org.apache.druid.query.extraction.RegexDimExtractionFn) TimeCeilOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.TimeCeilOperatorConversion) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) TruncateOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.TruncateOperatorConversion) RegexpLikeOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.RegexpLikeOperatorConversion) RightOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.RightOperatorConversion) DateTruncOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.DateTruncOperatorConversion) RepeatOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.RepeatOperatorConversion) TimeUnitRange(org.apache.calcite.avatica.util.TimeUnitRange) ImmutableList(com.google.common.collect.ImmutableList) TimeExtractOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.TimeExtractOperatorConversion) ExpressionVirtualColumn(org.apache.druid.segment.virtual.ExpressionVirtualColumn) HumanReadableFormatOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.HumanReadableFormatOperatorConversion) Before(org.junit.Before) TimeShiftOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.TimeShiftOperatorConversion) SqlIntervalQualifier(org.apache.calcite.sql.SqlIntervalQualifier) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) Period(org.joda.time.Period) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) TimeFormatOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.TimeFormatOperatorConversion) LPadOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.LPadOperatorConversion) Test(org.junit.Test) StrposOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.StrposOperatorConversion) SqlFunction(org.apache.calcite.sql.SqlFunction) NullHandling(org.apache.druid.common.config.NullHandling) RowSignature(org.apache.druid.segment.column.RowSignature) ParseLongOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.ParseLongOperatorConversion) ColumnType(org.apache.druid.segment.column.ColumnType) ContainsSearchQuerySpec(org.apache.druid.query.search.ContainsSearchQuerySpec) Collections(java.util.Collections) TruncateOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.TruncateOperatorConversion) SqlFunction(org.apache.calcite.sql.SqlFunction) Test(org.junit.Test)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 BigDecimal (java.math.BigDecimal)1 Collections (java.util.Collections)1 Map (java.util.Map)1 TimeUnit (org.apache.calcite.avatica.util.TimeUnit)1 TimeUnitRange (org.apache.calcite.avatica.util.TimeUnitRange)1 SqlFunction (org.apache.calcite.sql.SqlFunction)1 SqlIntervalQualifier (org.apache.calcite.sql.SqlIntervalQualifier)1 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)1 SqlTrimFunction (org.apache.calcite.sql.fun.SqlTrimFunction)1 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)1 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)1 NullHandling (org.apache.druid.common.config.NullHandling)1 DateTimes (org.apache.druid.java.util.common.DateTimes)1 IAE (org.apache.druid.java.util.common.IAE)1 TestExprMacroTable (org.apache.druid.query.expression.TestExprMacroTable)1 RegexDimExtractionFn (org.apache.druid.query.extraction.RegexDimExtractionFn)1 RegexDimFilter (org.apache.druid.query.filter.RegexDimFilter)1 SearchQueryDimFilter (org.apache.druid.query.filter.SearchQueryDimFilter)1