Add pipes_as_concat_mode (#2252)

This commit will add a new sql mode named MODE_PIPES_AS_CONCAT:
Description:
1、If this mode is active, '||' will be handled different from the original way ('||' and 'or' are seen as the same symbols in Doris) that it can be used to concat two exps and returns a new string. For example, 'a' || 'b' = 'ab' and 1 || 0 = '10'.
2. User can active this mode by "SET sql_mode = PIPES_AS_CONCAT", and deactive it by "SET sql_mode = '' ".
This commit is contained in:
xy720
2019-11-22 15:01:53 +08:00
committed by Mingyu Chen
parent 297542bd3f
commit 79ff0ad2a4
5 changed files with 115 additions and 4 deletions

View File

@ -0,0 +1,79 @@
// 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.doris.analysis;
import org.apache.doris.qe.SqlModeHelper;
import org.junit.Assert;
import org.junit.Test;
import java.io.StringReader;
public class SqlModeTest {
@Test
public void testScannerConstructor() {
String stmt = new String("SELECT * FROM db1.tbl1 WHERE name = 'BILL GATES'");
SqlParser parser = new SqlParser(new SqlScanner(new StringReader(stmt)));
SelectStmt selectStmt = null;
try {
selectStmt = (SelectStmt) parser.parse().value;
} catch (Exception e) {
Assert.fail(e.getMessage());
}
Assert.assertEquals("SELECT FROM `db1`.`tbl1` WHERE `name` = 'BILL GATES'", selectStmt.toSql());
parser = new SqlParser(new SqlScanner(new StringReader(stmt), SqlModeHelper.MODE_DEFAULT));
try {
selectStmt = (SelectStmt) parser.parse().value;
} catch (Exception e) {
Assert.fail(e.getMessage());
}
Assert.assertEquals("SELECT FROM `db1`.`tbl1` WHERE `name` = 'BILL GATES'", selectStmt.toSql());
}
@Test
public void testPipesAsConcatMode() {
// Mode Active
String stmt = new String("SELECT 'a' || 'b' || 'c'");
SqlParser parser = new SqlParser(new SqlScanner(new StringReader(stmt), SqlModeHelper.MODE_PIPES_AS_CONCAT));
SelectStmt selectStmt = null;
try {
selectStmt = (SelectStmt) parser.parse().value;
} catch (Exception e) {
Assert.fail(e.getMessage());
}
Expr expr = selectStmt.getSelectList().getItems().get(0).getExpr();
if (!(expr instanceof FunctionCallExpr)) {
Assert.fail("Mode not working");
}
Assert.assertEquals("concat('a', 'b', 'c')", expr.toSql());
// Mode DeActive
parser = new SqlParser(new SqlScanner(new StringReader(stmt), SqlModeHelper.MODE_DEFAULT));
try {
selectStmt = (SelectStmt) parser.parse().value;
} catch (Exception e) {
Assert.fail(e.getMessage());
}
expr = selectStmt.getSelectList().getItems().get(0).getExpr();
if (!(expr instanceof CompoundPredicate)) {
Assert.fail();
}
Assert.assertEquals("(('a') OR ('b')) OR ('c')", expr.toSql());
}
}